Announcement

Collapse
No announcement yet.

Help converting backtesting to automated

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help converting backtesting to automated

    Hi

    I have created a backtesting file that I wish to change so that I can automate it on my sim trading pad (Ninja Trader if possible).

    I just have no idea where to start.

    Can somone please point me in the right direction, so give me a link to a working version so that I have something to go off. (I'm working off moving average crossovers)

    Thanks in advance

    Min

  • #2
    Sample Code for Historical, Realtime and Tick Replay.

    Min,

    This isn't exactly a great piece of code to say the least. I wrote this for debugging purposes only and never planned to show it to anyone.

    I needed a sample strategy that I new fired trades to the generic broker.


    But it does work on historical, realtime and tick replay.
    If you have any questions feel free to ask as it's not easy on the eyes.

    Hope it helps,

    Glen

    PHP Code:
    /* Example of basic strategy to illustrate how to code an strategy EFS that is  Back Test, Real Time/Bars, Real Time/Ticks & tick replay capable*/ 
     
    // variables used to hold flags which control print/display options
    var bDebugPrintEnable true;
    var 
    bTraceEnable true;
    var 
    bTextEnable true;
    var 
    bSoundEnable true;
    var 
    bTradeLogging false;
    var 
    sFilename

    // file variable to log trade information to
    var fTrades = new File("TradeLog.txt");

    // Global variables
    var vPos          =   0;                          // indicates position status, 0, -1, +1                    
    var bRealtime     =   false;                      // true if running realtime - flag determines if strategy object or generic broker functions are used
    var vOSC1         =   osc(520false);          // builtin study
    var vFixedStop    =   .0050;                      // to use for fixed stop logic....note 4 decimals for currencies
    var vFixedProfit  =   .0100;                      // to use for profit target, also 4 decimals
    var bBarTrade     =   false;                      // flag to prevent "false signals" when multiple trades occor on same bar
    var bAllowRevs    =   false;                      // flag to indicate whether to allow reversals based on indicator 
    var vRTTradeCnt     =   0;                        // variable to to track # of RT executions

    function preMain() {
      
        
    setPriceStudy(true);
        
    setStudyTitle("##s0wizard");

    }
    /* Strategy - long when OSC value is above/below zero and high/low current realtime price is above/below high/low of prior indicator or "setup bar"
       currently main is called when all bars are loading, when a bar is complete, and tick by tick in realtime.                                       */
    function main() {      
     
      if (
    getBarState() === BARSTATE_NEWBAR && getCurrentBarIndex() === 0bRealtime true;

      if (
    vOSC1.getValue(-2) == null) return;                                    // make sure indicator initializes
      
      
    if (vOSC1.getValue(-1) > && high(0) > high(-1)) LongCond();
      
        else if ( 
    vOSC1.getValue(-1) <= && low(0) < low(-1)) ShortCond();
      
      if ( 
    vPos === 1)  setBarBgColor(Color.RGB(0,128,128));
      if ( 
    vPos === -1setBarBgColor(Color.RGB(255,0,0));
      
        return 
    null;
    }

    function 
    postMain() {
      
    }
    //* note strategy assumes no price gaps as stop/entry price will always be recorded by strategy object as valid.
    function LongCond() {
        if (
    vPos != )       // If we are not already Long
        
    {
             
    debugPrintln("LongCond @: Bar#=" +getCurrentBarCount() + " SYM=" +getSymbol() +  ", " getMonth() + "/" getDay() + "/" getYear() + " "getHour() + ":" +getMinute() + ":" getSecond() +
            
    " OSC="+vOSC1.getValue(-1).toFixed(4) + " vPos=" +vPos " high(0)=" high(0)+ 
             
    " high(-1)=" high(-1) + " RT="+bRealtime " IX=" +getCurrentBarIndex() + " BarState=" +getBarState() + " TradesDone = " +vRTTradeCnt);
             
             if (
    vPos != && !bRealtime){ Strategy.doLong(""Strategy.STOPStrategy.THISBARStrategy.DEFAULT, high(-1));vRTTradeCnt++; debugPrintln("BT Long @" +high(-1) + " +1 Tick" " Pos= " +vPos +" SYM=" +getSymbol() +  ", " getMonth() + "/" getDay() + "/" getYear() + " "getHour() + ":" +getMinute() + ":" getSecond()  +" Bar#=" +getCurrentBarCount()+ " TradesDone=" +vRTTradeCnt);}
             if (
    vPos == && bRealtime && getCurrentBarIndex() === ){ buyMarket(getSymbol(), 100); vRTTradeCnt++; debugPrintln(" Exec GB buyMarket X 1 - Long @" +high(0) +  " Pos=" +vPos +" SYM=" +getSymbol() +  ", " getMonth() + "/" getDay() + "/" getYear()+ " " getHour() + ":" +getMinute() + ":" getSecond()  + " Bar#=" +getCurrentBarCount()+ " TradesDone=" +vRTTradeCnt);}
             if (
    vPos == -&& bRealtime && getCurrentBarIndex() === ){ buyMarket(getSymbol(), 100);vRTTradeCnt++;  buyMarket(getSymbol(), 100);vRTTradeCnt++; debugPrintln(" Exec GB buyMarket X 2 - Long @" +high(0) + " Pos=" +vPos +  " SYM=" +getSymbol() +  ", " getMonth() + "/" getDay() + "/" getYear() + " "getHour() + ":" +getMinute() + ":" getSecond()  +" Bar#= "+getCurrentBarCount()+ " TradesDone=" +vRTTradeCnt);}
               
             
    vPos 1;
        }  
    // end if not already long - if ( vPos != 1) 
        
    // LongCond Function End
       
    function ShortCond() {
       if (
    vPos != -)
       {
             
    PrintDebugInfo(); //test debug print
             
    debugPrintln("ShortCond @: Bar#= " +getCurrentBarCount() + " SYM=" +getSymbol() +  ", " getMonth() + "/" getDay() + "/" getYear() + " " getHour() + ":" +getMinute() + ":" getSecond()  +" OSC= "+vOSC1.getValue(-1).toFixed(4) + " vPos=" +vPos " low(0)=" low(0)+ 
             
    " low(-1)=" low(-1) + " RT= "+bRealtime " IX=" +getCurrentBarIndex()+ " BarState=" +getBarState() + " TradesDone=" +vRTTradeCnt);
              
             if (
    vPos != -&& !bRealtime) {Strategy.doShort(""Strategy.STOPStrategy.THISBARStrategy.DEFAULT, low(-1));vRTTradeCnt++;debugPrintln("BT Short @" +low(-1)  + " -1 Tick"" Pos= " +vPos " SYM=" +getSymbol() +  ", " getMonth() + "/" getDay() + "/" getYear() + " "getHour() + ":" +getMinute() + ":" getSecond() +" Bar#=" +getCurrentBarCount()+ " TradesDone=" +vRTTradeCnt);}
             if (
    vPos == 0  && bRealtime && getCurrentBarIndex() === ){ sellShortMarket(getSymbol(), 100);vRTTradeCnt++;debugPrintln(" Exec GB SellShortMarket X 1 - Short @" +low(0) +" Pos=" +vPos " SYM=" +getSymbol() +  ", " getMonth() + "/" getDay() + "/" getYear()+ " " getHour() + ":" +getMinute() + ":" getSecond()  + " Bar#= "+getCurrentBarCount()+ " TradesDone=" +vRTTradeCnt);}
             if (
    vPos == +&& bRealtime && getCurrentBarIndex() === ){ sellMarket(getSymbol(), 100);vRTTradeCnt++; sellShortMarket(getSymbol(), 100);vRTTradeCnt++;debugPrintln(" Exec GB SellMarket X 1 & SellShortMarket X 1 - Short @" +low(0) +" Pos=" +vPos " SYM=" +getSymbol() +  ", " getMonth() + "/" getDay() + "/" getYear() + " "getHour() + ":" +getMinute() + ":" getSecond()  +" Bar#= "+getCurrentBarCount()+ " TradesDone=" +vRTTradeCnt);}
             
             
    vPos = -1;
                     
        }  
    // end if not already short - if ( vPos != -1) 
       
      
    // ShortCond Function End
       
    function PrintDebugInfo() {

      
    debugPrintln" SYM=" +getSymbol() + " Int= " getInterval() + " Bar#=" +getCurrentBarCount() +  " RT=" +bRealtime " IX=" +getCurrentBarIndex()+
                   
    " BarState=" +getBarState() +  ", " getMonth() + "/" getDay() + "/" getYear() + 
                   + 
    " "getHour() + ":" +getMinute() + ":" getSecond() +
                  
    " OSC="+vOSC1.getValue(-1).toFixed(4) + " vPos=" +vPos " high(0)=" high(0)+ 
                   
    " high(-1)=" high(-1) + " low(0)=" low(0)+ 
                   
    " low(-1)=" low(-1) +  " TradesDone = " +vRTTradeCnt);
    // end of PrintDebugInfo function 
    Last edited by demarcog; 10-14-2008, 07:05 PM.
    Glen Demarco
    [email protected]

    Comment

    Working...
    X