Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy test in real time and backtest – inner working question

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

    Strategy test in real time and backtest – inner working question

    Hello,

    I’ve been reading and studying some examples and have some questions, especially about IntrabarBacktest.

    I write down my assumptions and hope to hear if they are correct.

    I want to test a simple MA cross Strategy on a daily chart for this example. But I don’t want to see how it performs when the day is over but during the day.

    ** Real Time datafeed**
    - I set CalculateOnBarClose = false
    - I have a daily chart with a 2 MA’s cross Strategy
    - On every incoming tick OnBarUpdate is called, right?
    - If after some tick my MA’s cross on the Daily the Strategy triggers during the day because CalculateOnBarClose=false right?
    - So when looking at my Strategy in real time I can look at intrabar behavior this way?

    ** Backtesting Strategy**
    - If I have only Daily data, the OnBarupdate is triggered on every Daily bar in the data, right?
    - If I want to backtest how watching only a daily chart my strategy works during the day I have to add a smaller dataseries, right?
    - So if I read the SampleIntraBarBacktest I have to look at the smaller time frame with BarsInProgress == 1, right? The sample uses ==0 but I want to see on the smaller time frame what happens on the lager.
    - If the secundairy series is 60 min data then every 60 min OnBarUpdate is called, right?
    - I then can check is the MA’s on the daily timeframe/bars have crosses and execute an order on the secundairy timeframe. Do you have to execute on the secundairy?

    I hope i explained my situation and questions good enough.

    #2

    ** Real Time datafeed**
    - I set CalculateOnBarClose = false
    - I have a daily chart with a 2 MA’s cross Strategy
    - On every incoming tick OnBarUpdate is called, right?
    - If after some tick my MA’s cross on the Daily the Strategy triggers during the day because CalculateOnBarClose=false right?
    - So when looking at my Strategy in real time I can look at intrabar behavior this way?
    Correct.


    ** Backtesting Strategy**
    - If I have only Daily data, the OnBarUpdate is triggered on every Daily bar in the data, right?
    - If I want to backtest how watching only a daily chart my strategy works during the day I have to add a smaller dataseries, right?
    - So if I read the SampleIntraBarBacktest I have to look at the smaller time frame with BarsInProgress == 1, right? The sample uses ==0 but I want to see on the smaller time frame what happens on the lager.
    - If the secundairy series is 60 min data then every 60 min OnBarUpdate is called, right?
    - I then can check is the MA’s on the daily timeframe/bars have crosses and execute an order on the secundairy timeframe. Do you have to execute on the secundairy?
    Correct. You would use the information available here to do this.



    and also here:

    You can submit orders to different Bars objects. This allows you the flexibility of submitting orders to different timeframes. Like in live trading, taking entry conditions from a 5min chart means executing your order as soon as possible instead of waiting until the next 5min bar starts building. You can achieve this by


    Let me know if I can be of further assistance.

    Comment


      #3
      Hi,

      I also have same question but couldn't find a solution based on the example and info provided in those two links. Since MA cross strategy is applied to daily data series except current day close, which is not happened yet. I want to use current intrabar (60 min) data as current daily data (Close[0]). Can you give an example how this can be done in coding? Hope my question is clear enough.

      Comment


        #4
        Hello,

        Thanks for the note.

        This would require intrabar granularity to achieve and means running on the daily series chart and also the 60 min chart in the background.

        You can submit orders to different Bars objects. This allows you the flexibility of submitting orders to different timeframes. Like in live trading, taking entry conditions from a 5min chart means executing your order as soon as possible instead of waiting until the next 5min bar starts building. You can achieve this by


        Please let me know if any questions.

        -Brett

        Comment


          #5
          Brett,

          If I understand correctly, In the following reference example, the trigger signal is still only generated from primary data bar(daily) and an order is placed at the open of new primary bar. I am not sure how this "EnterLong(1,1,"..") could help to solve the problem here.


          protectedoverridevoid OnBarUpdate()
          {
          if (BarsInProgress == 0)
          {
          if (CrossAbove(EMA(Fast), EMA(Slow), 1))
          {
          EnterLong(1, 1, "Long: 1min");
          }

          elseif (CrossBelow(EMA(Fast), EMA(Slow), 1))
          {
          EnterShort(1, 1, "Short: 1min");
          }
          }

          else
          {
          return;
          }
          }

          Comment


            #6
            Hello,

            You can use the secondary bar series to submit orders too if that is what you are looking for.

            Simply EnterLong() inside of BarsInProgress 1.

            -Brett

            Comment


              #7
              Brett,

              If I put CrossAbove() and EnterLong() inside of BarsInProgress 1, The logic only applies to secondary bar series, which is not I want. Again, what I need is to be able to apply CrossAbove() logic to primary bar series except the current bar close (Close[0][0]) is coming from secondary bar series current bar(Close[1][0]).

              This will solve the problem of waiting to the end of primary bar to trigger an order. I think backtesting this way is more close to real-time strategy running when setting CalculateOnBarClose=false.

              Comment


                #8
                Hello,

                Here is how you would do it. Please see psuedo code below.

                if (BarsInProgress == 1)

                {

                if(CrossAbove(SMA(BarsArray[0], 14) , Close[0])
                {

                EnterLong();

                }

                }

                I'm using BarsArray[0] to reference the indicator value from the primary bars. This is shown how to do this in the MTF page I linked below.

                -Brett

                Comment


                  #9
                  Brett,

                  In this code, the indicator SMA's value only updates with primary bar series at its closing price, although CrossAbove is reference to closing price of secondary bar series. What I want is both indicator (SMA) and logic (CrossAbove) will be calculated(updated) with each secondary bar series.

                  Comment


                    #10
                    I understand. In this case you need to run Calculate On Bar Close = false and have code that is separated to run COBC = true and COBC = false.

                    You can do this with the following sample.



                    -Brett

                    Comment


                      #11
                      The sample code is good for real-time tick-by-tick data. How do I use it in backtesting on historical data?

                      Comment


                        #12
                        This would be using the sample in post 4 for intrabar granularity and adding in a tick secondary series. This would be the closest you could get to live in historical. This would take large amounts of tick data however and make backtesting take longer.

                        -Brett

                        Comment


                          #13
                          I don't think my question has been understood and I'll try one more time.

                          The probelm with the sample code of intrabar granularity is that in "CrossAbove(SMA(BarsArray[0], 14) , Close[0])", SMA still calculates using all primary bar series and it won't update with secondary bar series. This can be clearly seen in the SMA formula:
                          SMA(BarsArray[0], 14)=(Close[0][0]+Close[0][1]+......+Close[0][13])/14

                          and during BarInProgress=1, Close[0][0] doesn't change and only Close[1][0] with be updated.

                          What I really want is to use Close[1][0] to replace Close[0][0] in the above SMA calculation and I think this is more close to how it works in real-time when CalculateOnBarClose=false, in which each coming new tick acts as current bar closing price Close[0][0] until it runs to end of bar.

                          Comment


                            #14
                            Hello,

                            Yes you will do this in the sample.

                            To clarify once more.

                            if(BarsInProgress == 1)
                            {
                            if (CrossAbove(SMA(BarsArray[0],14), Close[0])
                            {

                            Enter or Exit

                            }

                            This does what you need. The above code runs on the secondary series which is the tick series. Close[0] in this BarsInprogress will be the close price of the 1 tick input series. SMA(BarsArray[0]) is the value of the SMA from the primary series. I believe this is what you want. You would need to tweek this to get exactly what your looking for however.

                            -Brett

                            Comment


                              #15
                              Finally this is what I originally posted. Therefor if you still dont understand how the sample code works exactly or I'm still missing your question it might be best if you send me your phone number to support at ninjatrader dot com. I will give you a quick call and we can discuss it.

                              -Brett

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Skifree, Today, 03:41 AM
                              1 response
                              4 views
                              0 likes
                              Last Post Skifree
                              by Skifree
                               
                              Started by usazencort, Today, 01:16 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post usazencort  
                              Started by kaywai, 09-01-2023, 08:44 PM
                              5 responses
                              603 views
                              0 likes
                              Last Post NinjaTrader_Jason  
                              Started by xiinteractive, 04-09-2024, 08:08 AM
                              6 responses
                              23 views
                              0 likes
                              Last Post xiinteractive  
                              Started by Pattontje, Yesterday, 02:10 PM
                              2 responses
                              23 views
                              0 likes
                              Last Post Pattontje  
                              Working...
                              X