Announcement

Collapse
No announcement yet.

Strategy Analyzer question

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

  • Strategy Analyzer question

    Hi all,

    The attached file displays 2 charts. The one on the left is a realtime chart with real time trades recorded in Formula Output. The one on the right is the same chart as it has been reloaded.
    As you can see , the Data cursor is at the 11:20 am time frame. A realtime "buy" was recorded at 776.7 at 11:20 and a realtime "sell " was recorded at 11:30am at a price at 775.6, however in backtesting ,these trades are ommited from the data in the analyzer and "no trades" are shown being taken.
    As you can see from the chart on the right, after the reload on the chart, the same buy and the same sell are now ignored.
    There are a couple of other trades ( at 10:05 and 10:10 )the analyzer does not record ; however, I believe the coding is correct in the program to produce these valid sells and buys.

    Can anyone help with the discrepancies?? Code and snapshot of Backtest will follow.

    angelo
    Attached Files
    ang.

  • #2
    To follow up , here is the code .
    Attached Files
    ang.

    Comment


    • #3
      This is the backtest of reloaded chart with analyzer results.
      Attached Files
      ang.

      Comment


      • #4
        Hi angatgg,

        I tried running the startegy but am missing the efs that is in your in "newdownload" directory. If you could post it the problem you are seeing can be recreated.

        One definite change you may want to consider for starters that will make the backtesting results more realistic is to change the order from Market Thisbar to Market Nextbar.

        Market Thisbar is actually recorded as the open price in the backtester and this will cause the mismatch you are seeing.

        Try that and let me know how you make out, if you could post the missing code I can run it also.

        Good Luck,

        Glen
        Glen Demarco
        [email protected]

        Comment


        • #5
          Hi Glen,

          Here is the cleaned-up code for the strategy. Didn't need the other external call(s) to run program, so I took them out.

          If you run the program in playback, you can see some trades are
          actual trades, but in analyzer, they are skipped. (Reference 11:20 trade in realtime vs 11:20 trade in Backtest).

          Of course this plays havoc on actual profits if the losing trades are not recorded correctly in Analyzer.

          The Strategy of "Thisbar" is correct because it calls for a Buy or Sell of the "first tick" as the signal cross, which happens to be the open bar , in BARSTATE_NEWBAR. Not withstanding any slippage, the opening bar is where the trade is taken in "live" trading. And yes, this does lead to some unwanted reversals
          when an early trade is taken. But this is the purpose of Backtesting this strategy.

          But that is not the issue. The issue is , when a trade is in realtime, and the conditions are met , they become a valid trade as called for. Like the 11:20 trade....In realtime, it is a bonifide trade. All the conditions are met. The signals are met at the open of newbar... DSStoch= 26.52 >= DSSig = 25.62 = "BUY" and the recorded price is 776.70. However, in backtest, it is ignored for reasons I can't find.

          I understand that the analyzer "looks forward" and doesn't know the price of entry, unless you tell it so. However , all the "other" trades are being correctly recorded, with the same strategy. I just need to filter out the "bad trades"... if only Strategy would correctly record them.

          I will post the playback of the exact time of the 11:20 bar to show the trade at BarState_ NewBar. Then look at the result in a
          reloaded chart. The trade at 11:20 is not recorded as a Buy.

          I swear... this feels like "The Matrix" with such strange anamolies!

          Thanks for looking at this again.... any help is appreciated....

          angelo
          Attached Files
          ang.

          Comment


          • #6
            Here is the actual realtime trade as Barstate_Newbar occurs.
            You can see the result is a "good trade", meaning , the program is doing what it is supposed to do, as written. A Buy order fires off, and recorded correctly at bar 11:20.

            Now look at previous post below(right chart of same referenced time frame in backtesting). As you can see, the trade is not being recorded as a buy, but ignored.

            angelo
            Attached Files
            ang.

            Comment


            • #7
              Hi Angelo,

              I would be very interested in seeing how the realtime or tick replay results differ from the backtester results. I know for certain chart types like Renko, PB, P&F, the backtester results aren't even close when compared to tick replay or realtime. I've never actually run it for "regular" candle charts as I always assumed the backtester results were accurate, but that's not what you are seeing.

              I was under the impression that anytime you evaluate your entry and exit based on bar index 0, that to avoid the forward looking aspects of the backtester you must use STRATEGY-NEXTBAR or THISBAR-CLOSE.

              Now if you run this strategy on a 5 minute chart of AB #F (which is also my preferred stock index to trade), you will see an equity graph that looks promising. If you change it to NEXTBAR the equity curve drops straight down.

              I have a bunch of strategies that I gave up on using STRATEGY.THISBAR when evaluating on bar 0 as using NEXTBAR rendered them worse then useless.

              I'm not familiar with these Blau studies or this strategy so can't really say one way or the other if using THISBAR has the same profit inflating result I see and I have no idea why the trades are not matching the backtesters.

              One other cool and probably most accurate way to test a system that I like is to use is the generic broker functions calls instead of the backtester in tick replay sending trades to the eSignal Paper Broker.


              There are some good examples of strategies in the knowledge base inder Technical Analysis of Stocks and Commodities TASC that test for BARSTATE_NEWBAR and BARINDEX == 0 which determines if a strategy is running realtime and if so a flag is set to use the generic broker calls instead of the Strategy object.

              Here is an simple example from a strategy that I run:
              PHP Code:
                if ( getCurrentBarIndex() == && getBarState() == BARSTATE_NEWBAR)   bRealTime true
                          
                  
                      
              //we are long realtime sell long and go short
                      
              if (vPSAR.getValue(ParabolicStudy.STOP,-1) >= high(-1) & bRealTime == true  ) { 
                           
                            
                            
              sellShortMarketgetSymbol(), Size );
                           
                            
              vPosition = -1;// because there are no Strategy.IsShort() must keep track of postiion status 
                            
                      
              }  
                      
              // added following backtesting logic, nice to be able to rerun backtest to validate results 
                      // of changes made to realtime program
                      
              if (vPSAR.getValue(ParabolicStudy.STOP,-1) >= high(-1)  bRealTime == false ){  //long in backtester
                            // using THISBAR OK evaluating on barindex -1
                            
              Strategy.doShort("Short" Strategy.MARKETStrategy.THISBAR); 
                           
                            
                            
                      } 
              Its actually really handy to have one strategy that you can back test, run in tick replay, or actually trade realtime electronically using the generic broker functions, all in the same efs.

              Then you run it in tick replay and it will detect that it is running in "realtime" and execute the generic broker functions instead of the Strategy.xxx methods.

              These trades are recorded in the eSignal Brokerage Paper trading account (which takes about 10 seconds to setup) realtime as the tick replay is running.

              You don't get all the nice backtesting reports, but you do get all the execution reports, trade P&L, total P&l etc. So you don't have to use the DebugPrint to see the trades and calculate it all by hand, just watch it "trade" in the chart and in the paper trade account.

              I'm attaching a program that is essentially a simple "button" tester that display all the possible generic broker functions possibilities, and more importantly has examples for each generic broker functions in case you are interested that Jason posted some time ago.


              Keep me posted as I'm really interested in seeing what the resolution is on this. Good luck, hope it proves to be worth trading.


              Glen
              Attached Files
              Last edited by demarcog; 03-08-2007, 12:16 AM.
              Glen Demarco
              [email protected]

              Comment


              • #8
                Angelo
                There are several problems in your code.
                The first one is in the section of code in lines 105-116 which I enclose here with some comments (note that all line numbers reference the latest version of the script)
                PHP Code:
                if(nState== BARSTATE_NEWBAR){//if on the first tick of a new bar
                    
                if (bPosition == false) { //if not in a position   
                           
                if(DStoch >= DSig){ //if current value of DStoch is greater or equal to current value of DSig
                            
                nNewTrade 1
                            
                nsignal 1;
                          } else 
                           if(
                DStoch <= DSig){//same as above
                            
                nNewTrade 1
                            
                nsignal = -1;
                            }  
                        }
                    } 
                When running the script in real time you are evaluating the current values of DStoch and DSig on the first tick of each bar. However when you reload the efs or run a backtest the efs is evaluating completed bars only hence the differences in signals.
                In order to generate the same signals in real time and on a reload (or in backtesting) you need to modify lines 86 and 87 of the script and assign to the variables DStoch and DSig the prior bar's values of those indicators and not the current bar's values.
                PHP Code:
                DStoch DS_Stoch.getValue(-1);
                DSig     DS_Sig.getValue(-1); 
                The next problem is in the overall logic that determines the status of the strategy and sets the values of the flags bPosition and nNewTrade. To resolve this you need to remove or comment out all the following lines of code
                96-101, 106, 108, 112, 115, 123, 153
                If for any reason you need to have the logic contained in those lines then you will need to review and rewrite it as it is not working correctly.
                There is also a problem in how you draw the shapes. The location of the shapes is above/below the High/Low of the current bar plus or minus a fixed amount. However when the script is run in real time the commands to draw the shapes are executed only on the first tick of a new bar at which time the bar is not completed. This will cause the shapes to be redrawn in a different location when the efs is reloaded because at that point the script will be evaluating completed bars. To resolve this you need to either locate the shapes at the prior bar or draw them in real time (ie outside of a BARSTATE_NEWBAR condition) so that they adjust the location as the bar is forming.
                Once you implement all these changes you should see no differences between the signals generated while the script is running in real time and those generated on a reload of the script (or in backtesting)
                Having said all this you should consider that eSignal strongly suggests that the Strategy Object be used only for back testing and not for real time. For real time you should create your own variables and logic which you then use to keep track of the strategy's positions.
                Alex

                Comment

                Working...
                X