• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Order of processing amongst strategy and indicators

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

    Order of processing amongst strategy and indicators

    Hello,

    Please consider the following scenario:

    Custom indicator 1,2,3 all calculating OnBarUpdate.
    Strategy 1 also calculating OnBarUpdate.

    Custom indicators 1,2,3 are exposing some variables to Strategy 1. Custom Indicator 1,2,3 take 20 ms to run and they take more calculations than the strategy. If the Strategy is calling the exposed variables from the indicators (plots and no plots). If both the indicators and the strategy are calculating on the same OnBarUpdate. How does it work?

    The strategy is getting whatever value is exposed from the indicator or waits for the indicators to finish their calculation and update their value and then the strategy continues with its calculation?

    In case the strategy just gets the value without waiting for the Indicator to finish updating. Is it possible to tell the Strategy to wait for the Indicator/s to finish their calculations and get the updated value. If so, how can this be done?

    #2
    Hello roblogic,
    Thanks for your post.

    There would not be any 'waiting' for the indicators to update. The strategy is going execute its logic on each call to OnBarUpdate() and the indicators would be up to date based on the data series they are running on.

    What are you seeing that makes you believe your strategy needs to wait on an indicator for its calculations. Please be specific.
    Josh G.NinjaTrader Customer Service

    Comment


      #3
      The case I am thinking of is when an indicator and a strategy both runs whenever a specific price pattern happens. So if the indicator takes longer than the strategy to calculate, the strategy will take the "oldest" non updated values from the exposed variables. Correct?

      Then, if this is really the case. Is there a proven system for the strategy to "wait" till the updated value from the indicator to perform the calculations?

      Should I delay some ms the execution of the Strategy to wait for the indicator to update the exposed variables? Should I delay the strategy 1 bar?...

      Comment


        #4
        roblogic,

        No, the strategy would take the newest values supplied by the indicator(unless you actually wanted the oldest values). What exposed values are you referring to? Are these values you are using to calculate your strategies logic? I am assuming the indicator is running on a different time frame than the strategy?

        If you need your indicator to update more frequently you have some options. You could add a more granular series to your strategy to calculate your indicator on. You could also use Calculate.OnEachTick to get the fastest updates possible to your scripts.

        https://ninjatrader.com/support/help...nstruments.htm

        If you want your strategy to "wait" for your indicator to update, that's not really going to be possible.

        Josh G.NinjaTrader Customer Service

        Comment


          #5
          Why not just call Update() for the indicator before using its values?

          See here:
          https://ninjatrader.com/support/help...us/?update.htm
          Last edited by bltdavid; 02-06-2019, 11:27 PM. Reason: add link

          Comment


            #6
            Thanks all for your answers. I will try to reword the question as I think the potential problem I see here is not addressed with these solutions.

            Let's assume that both the indicator and the strategy calculates OnBarClose and runs whenever the same price pattern happens. For instance after 3 consecutive Bull or Bear bars. When this happens the indicator updates a exposed variable to the strategy. double exposedVariable = Close[0];

            If the indicator takes 20 ms to calculate and update the exposed variable and the strategy takes < 1ms, what will happen is that whenever the price pattern occurs, since the strategy runs much faster, the strategy will take the exposed variable from the indicator which has not been updated yet, as they both run in parallel and the indicator takes much longer to update than the strategy. In this case if the last 4 Bars where 4 consecutive Bull Bars, the strategy will take the value from the exposed variable from the indicator being Close[4] (not updated yet), instead of Close[0]. Once the strategy has performed its calculations with the non updated yet exposed variable, then the exposed variable will be updated (as it takes 20 ms more than the strategy to calculate).

            So calculating OnEachTick or calling Update() should not make the indicator update the exposed variable prior the calculations of the strategy, as they both run whenever the same price pattern occurs and the strategy runs much faster than the indicator. So the only possible solution here is to delay the execution of the strategy until the indicator has updated its exposed variable.

            Can you confirm that this scenario is going to happen? If so, I will have to find a solution to delay the execution of the strategy so that it takes the updated exposed variable.


            Comment


              #7
              Originally posted by roblogic View Post
              Thanks all for your answers. I will try to reword the question as I think the potential problem I see here is not addressed with these solutions.

              Let's assume that both the indicator and the strategy calculates OnBarClose and runs whenever the same price pattern happens. For instance after 3 consecutive Bull or Bear bars. When this happens the indicator updates a exposed variable to the strategy. double exposedVariable = Close[0];

              If the indicator takes 20 ms to calculate and update the exposed variable and the strategy takes < 1ms, what will happen is that whenever the price pattern occurs, since the strategy runs much faster, the strategy will take the exposed variable from the indicator which has not been updated yet, as they both run in parallel and the indicator takes much longer to update than the strategy. In this case if the last 4 Bars where 4 consecutive Bull Bars, the strategy will take the value from the exposed variable from the indicator being Close[4] (not updated yet), instead of Close[0]. Once the strategy has performed its calculations with the non updated yet exposed variable, then the exposed variable will be updated (as it takes 20 ms more than the strategy to calculate).

              So calculating OnEachTick or calling Update() should not make the indicator update the exposed variable prior the calculations of the strategy, as they both run whenever the same price pattern occurs and the strategy runs much faster than the indicator. So the only possible solution here is to delay the execution of the strategy until the indicator has updated its exposed variable.

              Can you confirm that this scenario is going to happen? If so, I will have to find a solution to delay the execution of the strategy so that it takes the updated exposed variable.

              I am a little confused. Are you calling the exposed indicator values inside the strategy code?

              Indicators placed into and called from a strategy always update before the strategy does, due to the strategy's dependency on the indicator output. I can't imagine a situation where this could happen in a different manner. The only thing I can think of is you are using a method of referencing indicators nobody has ever seen before, or you are confused about what is really happening. If you are calculating on each tick, or if you are running a multi-time frame/instrument strategy, there is a high likelihood your code is referencing a fictional value due to how the software manages time frames and instruments. For instance, this will happen if you are calculating on each tick but are not using OnFirstTickOfBar and referencing the bar zero index [0] instead of [1].

              My experience has been all indicators added to a strategy (the strategy's inputs) always calculate before the strategy does. How long they take should not matter as the software knows to wait for its inputs.

              How are you adding indicators to your strategy?

              Comment


                #8
                Hello roblogic,

                Something sounds fishy with what you are attempting, or we do not have enough information. Indicator plots should be up to date with the strategy as the indicator and the strategy are running on the same data series.

                Exposed variables that are not plots should have Update() called in the getter so the value is guaranteed to be up to date when the variable is referenced.

                Below is a link to our sample on using exposed variables that are not plots. Are you able to observe this issue following this example code? If so, could you create a demonstration script following this example so we can see what you are doing exactly and provide further comment?

                SampleBoolSeries - https://ninjatrader.com/support/help...alues_that.htm

                I look forward to being of further assistance.
                JimNinjaTrader Customer Service

                Comment


                  #9
                  I run a little test and effectively as per Liquid150 comments, the Strategy does "wait" for the indicator to finish the calculations and update, in order for the Strategy to "get" the value from the exposed variable.

                  The test I done runs both the indicator and Strategy OnBarUpdate:

                  i) The indicator adds 3000 ms with a while loop and a StopWatch before it updates the exposed variable.
                  ii) At the same bar, the Strategy calls the exposed variable and when it "gets" the value from the exposed variable, the measurement from the StopWatch shows that the Strategy has taken also 3000 ms, which is the time the Indicator has taken to update the exposed variable.

                  So the strategy really "waits", in this case 3000 ms, which is the time the indicator takes to update. And the Strategy will use the updated exposed variable. That's great and that's the behaviour I was expecting when I opened the thread, although I got confused with the initial comments, probably due to some misunderstanding! All solved now!

                  PS: Attached the 2 test files, in case anybody is interested.

                  Attached Files

                  Comment


                    #10
                    If you ever have an issue (and you may someday) this comment from Jim is critical to getting it resolved:

                    "(Indicator) exposed variables that are not plots should have Update() called in the getter so the value is guaranteed to be up to date when the variable is referenced (by the strategy)."

                    I, too, found this comment from JoshG extremely confusing:

                    "If you want your strategy to "wait" for your indicator to update, that's not really going to be possible."

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by HiddenPhilosopher, Today, 07:24 PM
                    0 responses
                    7 views
                    0 likes
                    Last Post HiddenPhilosopher  
                    Started by dkbyond, Today, 06:43 PM
                    0 responses
                    6 views
                    0 likes
                    Last Post dkbyond
                    by dkbyond
                     
                    Started by i2w8am9ii2, Today, 06:05 PM
                    0 responses
                    2 views
                    0 likes
                    Last Post i2w8am9ii2  
                    Started by YevhenShynkarenko, Today, 05:46 PM
                    0 responses
                    5 views
                    0 likes
                    Last Post YevhenShynkarenko  
                    Started by Bionian, Today, 04:14 PM
                    0 responses
                    5 views
                    0 likes
                    Last Post Bionian
                    by Bionian
                     
                    Working...
                    X