Announcement

Collapse
No announcement yet.

Backtesting usin reloadEFS()

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

  • Backtesting usin reloadEFS()

    Hi,

    I am trying to add the ability to automatically Backtest a Strategy with one or more variable parameters. I have been able to do this in realtime by using reloadEFS(). (See the attached code sample). Unfortunately this approach requires 1 or more new bars to test each new parameter setting. While this is usable on a short interval (e.g. 1 minute), it is impractical on a 15 minute or longer chart. Does anyone have any ideas on how to achieve the same thing without waiting for one or more new bars to form.

    I also find it curious that the two debugPrintln() statements after reloadEFS() are executing. Not really important as the code works, but not what I expected based on what I have read in other posts on reloadEFS().

    Thanks,
    Greg Schroeder

    PHP Code:
    debugClear();
    // Global Vars
    var gbInit          0;
    var 
    giBacktestParam 1;
    var 
    giBarCounter    0;

    //**************************
    // preMain Function Executes Once When Study is Loaded
    //**************************

    function preMain()
    {
        
    setPriceStudy(true);
        
    setShowCursorLabel(false);
        
    setShowTitleParameters(false);
        
    setComputeOnClose();//Delete to have study execute on every tick
    }

    //***************************
    // main Function Executes on Each New Tick (or New Bar if setComputeOnClose() is in preMain)
    //***************************
    function main()
    {
        
    // One Time Initialization
        
    if (!gbInit)
        {
            
    // Code to be run once per reload
            
    debugPrintln("In Initialization..... giBacktestParam="+giBacktestParam);
            
            
    gbInit true;
        }   
    // end Initialization
        
        
    if (isLastBarOnChart()) 
        {
            
    debugPrintln("Entered if       ..... giBacktestParam="+giBacktestParam+", giBarCounter="+giBarCounter);
            if (
    getBarState() == BARSTATE_NEWBAR)
            {
                if (
    giBacktestParam >= 10)
                {
                    
    debugPrintln("Backtest run finished. giBacktestParam="+giBacktestParam+", giBarCounter="+giBarCounter);
                    return;
                }
                else if (
    giBarCounter == 2)
                {
                    
    // Increment Backtest Parameter
                    
    giBacktestParam giBacktestParam 2;
                    
    //Restart Study with new Parameter
                    
    debugPrintln("In reloadEFS     ..... giBacktestParam="+giBacktestParam+", giBarCounter="+giBarCounter);
                    
    debugPrintln();
                    
    debugPrintln("Backtest Parameter="+giBacktestParam);
                    
    debugPrintln();
                    
    giBarCounter    0;
                    
    gbInit          0;
                    
    reloadEFS();
                    
    debugPrintln("After reloadEFS()");
                    
    debugPrintln();
                }
                
    giBarCounter++;
                
    debugPrintln("Leaving if       ..... giBacktestParam="+giBacktestParam+", giBarCounter="+giBarCounter);
            }
        }
        
        
    //-----------------------------------
        // Strategy code for Backtesting here
        //-----------------------------------
        
        // Write trade results to file
        
        
    return;


  • #2
    I'd try putting the test for isLastBarOnChart() at the very end of main(), after your Strategy code and results handling. If it's the last bar, call reloadEFS().

    I'm not sure setComputeOnClose() works with backtesting.

    FYI, I did some testing with isLastBarOnChart() and there were some anomalies compared with (getCurrentBarIndex() == 0) and/or (getCurrentBarIndex() == -1) if setComputeOnClose() was called. Can't remember what they were though - it might have only been with daily data.

    Comment


    • #3
      Thanks shortandlong,

      I'll give that a try. I know what the anomaly is you are referring to concerning setComputeOnClose(). When that is enabled, getCurrentBarIndex() is never equal to 0, so you can only test for the prior bar using -1.

      I think they should rename setComputeOnClose() to setComputeOfCurrentBarOnOpenOfNextBar()

      Seriously, I have not had any issues with using setComputeOnClose() with backtesting, but then any strategies I have don't depend on intrabar ticks.

      Greg

      Comment


      • #4
        The "anomaly" (aka bug) with isLastBarOnChart() is:

        If you've called setComputeOnClose()

        then...

        isLastBarOnChart() NEVER returns true for intervals of "D", "W", and "M".
        Last edited by shortandlong; 02-24-2010, 04:59 PM.

        Comment

        Working...
        X