Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy start behavior

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

    Strategy start behavior

    I have several bots that are stuck in a position due to a large gap in price when the globex opened on Sunday. This strategy places a bracket order on both sides of the position and since the current market price is still within these brackets the strategy will not take any new positions until the price hits one of the bracket orders. I have tested all 4 of the options for the start behavior and none of them allow the user to just start the strategy from the current bar and since the minimum days to load is 1 and that gap is within the most recent day I don't see any way around this other than changing the time frame trading hours to something that doesn't include that gap. Is there a way to just run a strategy starting from the current bar without using any historical data on start-up?

    #2
    Hello gordongekko,
    Thanks for your post.

    You could force your strategy to ignore all historical data and process its logic only once you are on real-time date by adding this line to the top of your strategies OnBarUpdate() logic.
    Code:
    if(State==State.Historical)return;
    Josh G.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the quick reply. Generally I would prefer to leave have it start live when it the last historical trade finishes (when flat) but in situations like this I need an override. I'll add that as bool property option for these situations.

      Comment


        #4
        Will this affect any indicators or variables used by the strategy that require historical data? For example if the strategy uses the 12 26 9 macd will it have to wait at least 26 bars after being enabled to generate a trigger assuming nothing else requires more data or will it be able to fire on bar 1?

        Comment


          #5
          Yes, this would effect anything that requires historical data.
          Josh G.NinjaTrader Customer Service

          Comment


            #6
            Thanks for clarifying this. So for this particular bot I'm going to run it with no historical data but use a time filter so the indicators will have their data but it wont be allowed to trigger until the time window is open.

            Comment


              #7
              Originally posted by NinjaTrader_JoshG View Post
              Yes, this would effect anything that requires historical data.
              Really?

              That sounds different from my NT7 experience.

              Just because the strategy ignores historical data doesn't mean the indicators used by the strategy also ignore historical data -- how could they?

              When "used by the strategy" I mean indicator references that are created in Initialize() or OnStartUp() -- before OnBarUpdate() is ever called.

              Comment


                #8
                Right. The indicators themselves would not ignore historical data. However, any calls to those indicators inside OnBarUpdate() would be ignored until you were in State.Realtime
                Josh G.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by gordongekko View Post
                  Will this affect any indicators or variables used by the strategy that require historical data? For example if the strategy uses the 12 26 9 macd will it have to wait at least 26 bars after being enabled to generate a trigger assuming nothing else requires more data or will it be able to fire on bar 1?
                  I think the better answer you're looking for is 'No, it will not affect any indicators, since they will still see the historical data in their own processing inside their own OnBarUpdate'.

                  That is, this line of code,

                  Code:
                  if(State==State.Historical)return;
                  absolutely affects what data your strategy will operate on inside its OnBarUpdate, but this line of code does not affect the data that the indicators are processing in their own OnBarUpdate.

                  Comment


                    #10
                    Some confusion may exist around how you use the indicators.

                    For example, say you access MACD(12,26,9) directly, with full arguments, and you don't access the indicator any other way ... for example,

                    Code:
                    if(State==State.Historical)return;
                    if(MACD(12,26,9) <test against condition ABC>)
                          EnterLong();
                    else if (MACD(12,26,9) <test against condition XYZ>)
                          EnterShort();
                    then in this case the MACD indicator will not "see" any of the historical data in its own OnBarUpdate, but the value of CurrentBar in your strategy and in the MACD will be the same -- meaning, the historical data is there for MACD to refer to, as necessary -- but the point is: MACD literally did not process any of the historical data in its own OnBarUpdate because (in this particular case) your code prevented MACD from being instantiated in time to be called with historical data.

                    EDIT:
                    I'm not strictly sure if what I said above is true, but it's the working model I have in my head. NinjaTrader support, could you please confirm everything in this posting?

                    But you can get around this 'limitation' by making the instantiation happen sooner.

                    How? Create a reference to MACD(12,26,9) before calling OnBarUpdate.
                    How? Easy, by adding MACD(12,26,9) to the chart.

                    If you use AddChartIndicator(MACD(12,26,9)), the MACD indicator will process all historical data in its own OnBarUpdate, despite the 5 lines of code above.

                    Make sense?
                    It all depends where in your strategy it first creates the reference to MACD(12,26,9).

                    The use of AddChartIndicator provides this very subtle and rarely talked about feature -- it allows the indicator to be instantiated in time such that it is guaranteed to process all historical bars in its own OnBarUpdate.

                    If you don't use AddChartIndicator, or create a reference to the indicator via some other means, then yes, the 5 lines of code above mean that on the first call to MACD(12,26,9) inside of OnBarUpdate, MACD will start processing with realtime data only (but all historical data is available to it, it just didn't process any of it inside its own OnBarUpdate).

                    But, the story gets more complicated:
                    Now we bump up against NinjaTrader's indicator caching code in the auto generated NinjaScript code at the bottom of every indicator. If some where else in NinjaTrader, such as in a chart or in another strategy, the MACD(12,26,9) has already been referenced -- then your strategy gets a copy of that previous created reference from the indicator cache -- and that copy will have most likely already processed all the historical data that your strategy is trying to skip over -- which is fine and expected and a good thing for you.

                    Bottom line:
                    Just use AddChartIndicator() and you'll have no issues.

                    Comment


                      #11
                      bltdavid,
                      Couple of things here.

                      If you use the line if(State==State.Historical)return; in your strategies OnBarUpdate() than it will not process its logic until you reach State.Realtime. If your indicator does not have that same line in its code, it will still process historical data. Bottom line is no entry logic will be processed on historical data.

                      Also, AddChartIndicator() is simply used for displaying an indicator on a chart from within a strategy. It would not be used to load more historical data to process your logic on.
                      Help Guide - AddChartIndicator()

                      Please let me know if this doesn't clear everything up.
                      Josh G.NinjaTrader Customer Service

                      Comment


                        #12
                        Originally posted by NinjaTrader_JoshG View Post
                        bltdavid,
                        Couple of things here.

                        If you use the line if(State==State.Historical)return; in your strategies OnBarUpdate() than it will not process its logic until you reach State.Realtime. If your indicator does not have that same line in its code, it will still process historical data. Bottom line is no entry logic will be processed on historical data.

                        Also, AddChartIndicator() is simply used for displaying an indicator on a chart from within a strategy. It would not be used to load more historical data to process your logic on.
                        Help Guide - AddChartIndicator()

                        Please let me know if this doesn't clear everything up.
                        Thanks Josh, but I think you failed to address my main point.

                        If your strategy has this in its OnBarUpdate(),

                        Code:
                        if(State==State.Historical)return;
                        if(MACD(12,26,9) <test against condition ABC>)
                           EnterLong();
                        else if (MACD(12,26,9) <test against condition XYZ>)
                           EnterShort();
                        and has no other MACD(12,26,9) references anywhere else in the strategy (eg, AddChartIndicator is not used), you've failed to explain how the MACD(12,26,9) is able to process any historical data in its own OnBarUpdate.

                        I mean, clearly, the MACD(12,26,9) is not being called with historical data, so that brings into question your statement in red below,

                        Originally posted by NinjaTrader_JoshG View Post
                        If you use the line if(State==State.Historical)return; in your strategies OnBarUpdate() than it will not process its logic until you reach State.Realtime. If your indicator does not have that same line in its code, it will still process historical data.
                        Obviously, the MACD does not have that same line of code, so you're saying it will process historical data. Well, sure it is capable of processing historical data, is that what you mean?

                        So, yes, we all know MACD is capable of processing historical data, but does it actually process historical data in this case above or not? If yes, then how?

                        Comment


                          #13
                          Originally posted by NinjaTrader_JoshG View Post
                          Also, AddChartIndicator() is simply used for displaying an indicator on a chart from within a strategy. It would not be used to load more historical data to process your logic on.
                          I don't think that's what I said.

                          My point is not more historical data, it's whether the indicator will see any historical data in its own OnBarUpdate.

                          I know what AddChartIndicator() does.

                          But you failed to respond directly to my comment that the reference passed to AddChartIndicator() is of any importance.

                          I'm saying that that indicator reference is important.

                          That act of creating that reference to pass to AddChartIndicator() happens to provide a very subtle service to the strategy, which is (see my previous post): "it allows the indicator to be instantiated in time such that it is guaranteed to process all historical bars in its own OnBarUpdate."

                          Is that statement true or false?

                          Comment


                            #14
                            In the situation you are describing the indicator can see its own historical data. The strategy can see that historical data as well, but will not process its logic until it's in State.Realtime

                            But you failed to respond directly to my comment that the reference passed to AddChartIndicator() is of any importance.

                            I'm saying that that indicator reference is important.

                            That act of creating that reference to pass to AddChartIndicator() happens to provide a very subtle service to the strategy, which is (see my previous post): "it allows the indicator to be instantiated in time such that it is guaranteed to process all historical bars in its own OnBarUpdate."

                            Is that statement true or false?
                            I don't believe that its as important as you are making it out to be. That would not help the indicator to process more or less historical data in its own OnBarUpdate(). I believe it is more efficient than defining that indicator each time, but it will not boost your historical data loading times

                            Are you implying that if you used the indicator elsewhere and do not pass a reference into AddChartIndicator that it would not be guaranteed to process all historical bars in its own OnBarUpdate?
                            Josh G.NinjaTrader Customer Service

                            Comment


                              #15
                              Originally posted by NinjaTrader_JoshG View Post
                              In the situation you are describing the indicator can see its own historical data. The strategy can see that historical data as well, but will not process its logic until it's in State.Realtime
                              Huh? Here again, you lack specificity. You said 'can see'. Ok, do you really mean capable of seeing or actually seeing and processing the same historical data as seen by the strategy? The capability of MACD "seeing" historical data is not at issue.

                              Originally posted by NinjaTrader_JoshG View Post
                              That would not help the indicator to process more or less historical data in its own OnBarUpdate(). I believe it is more efficient than defining that indicator each time, but it will not boost your historical data loading times.
                              I never mentioned 'boosting' of load times, that is not at issue.

                              Originally posted by NinjaTrader_JoshG View Post
                              Are you implying that if you used the indicator elsewhere and do not pass a reference into AddChartIndicator that it would not be guaranteed to process all historical bars in its own OnBarUpdate?
                              Exactly!

                              At least, that's what I keep asking you for confirmation.

                              If this is the only code in the strategy where MACD(12,26,9) is called,

                              Code:
                              protected override void OnBarUpdate()
                              {
                                   if(State==State.Historical)return;
                                   if(MACD(12,26,9) <test against condition ABC>)
                                       EnterLong();
                                   else if (MACD(12,26,9) <test against condition XYZ>)
                                       EnterShort();
                              }
                              Then please explain how the MACD is able to see and process any historical data in its own OnBarUpdate?

                              We all know MACD is capable of processing historical data, but in the above case, what happens? Does the MACD(12,26,9) process any historical data or not?

                              If you assert that the MACD(12,26,9) will process the same amount of historical data that the strategy processes (the strategy effectively ignores all its historical data, but it still sees all of it and processes all of it by returning early), then please explain how MACD(12,26,9) is able to see those same historical bars?

                              For example, using the OnBarUpdate above, if the first realtime bar is CurrentBar=5000, does the MACD(12,26,9) process historical bars 0 - 4999 or not?
                              Last edited by bltdavid; 05-25-2018, 09:28 AM. Reason: OnBarUpdate returns early, not false

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by cre8able, Today, 03:20 PM
                              1 response
                              9 views
                              0 likes
                              Last Post cre8able  
                              Started by fiddich, Today, 05:25 PM
                              0 responses
                              3 views
                              0 likes
                              Last Post fiddich
                              by fiddich
                               
                              Started by gemify, 11-11-2022, 11:52 AM
                              6 responses
                              804 views
                              2 likes
                              Last Post ultls
                              by ultls
                               
                              Started by ScottWalsh, Today, 04:52 PM
                              0 responses
                              4 views
                              0 likes
                              Last Post ScottWalsh  
                              Started by ScottWalsh, Today, 04:29 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post ScottWalsh  
                              Working...
                              X