Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Calculate.OnPriceChange vs Calculate.OnBarClose

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

    Calculate.OnPriceChange vs Calculate.OnBarClose

    Hi,

    I need to be able to make calculations at different times, some only at bar close, and some at every price change, how can that be accomplished if I'm overriding OnBarUpdate() and my global strategy calculations are set to Calculate.OnBarClose. Is there another function that gives me updates for each tick or each price change while OnBarUpdate() is updated at bar close?

    Or is there a way to detect when a Bar Closes if my OnBarUpdate() is set to update on Calculate.OnPriceChange? This would actually be a preferred method so all indicators update on each price change, but if I know when a bar opens and closes, I can make custom logic for that.

    V/R,
    --Maxim
    Last edited by DieSlower; 08-05-2020, 11:07 PM.

    #2
    Hello DieSlower,

    Thanks for your post.

    You can run your code with Calculate.OnPriceChange and set parts of the code to run only once per bar or on every price change. The bool IsFirstTickOfBar can be used to segment the code that you want to run only once per bar. Reference: https://ninjatrader.com/support/help...ttickofbar.htm

    here is a link to an educational example that demonstrates the concept: https://ninjatrader.com/support/help...either_cal.htm


    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hi Paul,

      Right, I knew about the IsFirstTickOfBar, is there a way to find the last tick? Or closing time of the bar? I guess is there a way to get the time of when one bar starts, when it ends, when the middle is, even if there are no ticks?
      If my code is set to Calculate.OnPriceChange, and an order is put in in the middle of a bar, will it execute on the same bar, or the opening of the next?

      V/R,
      --Maxim
      Last edited by DieSlower; 08-06-2020, 08:50 AM.

      Comment


        #4
        Hello Maxim,

        Thanks for your reply.

        The closing time of the bar would be available in the time series, for Time based bar types, Time[0] would be the close time of the bar Time[1] would be the close time of the bar before that. If using multiple data series then you would need to use Times[][] where the first index would be the BarsArray number and the second index would e the bars ago of that barsarray.

        References:



        For non-time based bar types, Time[0] (or Times[0][0]) would be the time of the lastest tick and not necessarily the end of the bar.

        For all bar types, the last tick of a bar (when using Calculate.OnEachTick) would be the Close[1] value and would be at the Time[1].

        "If my code is set to Calculate.OnPriceChange, and an order is put in in the middle of a bar, will it execute on the same bar, or the opening of the next?" The order would be sent on the next tick which is all you can do as, its execution would depend on the variables of internet time, exchange time, order type and market dynamics.
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Aha, I see...

          One more question related to this... If I set my strategy to use OnPriceChange, all the loaded indicators also start using OnPriceChange. How can I in my strategy, tell some indicators to use OnPriceChange and others to use OnBarClose?

          Comment


            #6
            Hi Maxim.

            OnPriceChange, each time the price changes, it will execute OnBarUpdate and all its statements contained.
            if you want to exclude some calculations and execute them only when the bar closes, you should use an if statement with the condition IsFirstTickOfBar.
            All the statements included between the curly braces will be executed only immediately after the bar closing plus one tick.

            protected override void OnBarUpdate()
            {

            // Section of Codes to be executed after each price change

            if (IsFirstTickOfBar)
            {

            // Section of Codes to be executed only and only on the first tick following the bar closure

            }

            }

            Do not forget that any reference to information about the last closed bar should be addressed with [1], not [0] which is the bar under construction.

            I hope Paul will agree.

            Good luck

            Dan

            Comment


              #7
              Hi Dan,

              Thats understandable, but when an indicator is loaded, there are usually no updates to it in OnBarUpdate()
              So it gets its information by itself from NT shared or global objects. Most indicators are created and loaded in (State.DataLoaded) and thats it. So, again if I set my strategy to use OnPriceChange, all the loaded indicators also start using OnPriceChange. How can I in my strategy, tell some indicators to use OnPriceChange and others to use OnBarClose?

              Comment


                #8
                if() {] else if() {}.

                Comment


                  #9
                  Originally posted by Emma1 View Post
                  if() {] else if() {}.
                  What is that answering or saying?

                  Comment


                    #10
                    if(condition1) (do Something) else if (Condition2) {do Somethin}

                    Comment


                      #11
                      Originally posted by Emma1 View Post
                      if(condition1) (do Something) else if (Condition2) {do Somethin}
                      I'm still not sure which question you are answering, thank you for trying though!
                      Last edited by DieSlower; 08-09-2020, 04:04 PM.

                      Comment


                        #12
                        which part of NT8 do you want code?

                        Comment


                          #13
                          Originally posted by Emma1 View Post
                          which part of NT8 do you want code?
                          I'm a Software Architect, I can write code...
                          My problem is that a lot of indicators in NT8 that can be used in a strategy are programmed to only work when your program runs on OnBarClose. If my strategy is switched to use OnPriceChange or OnEachTick, then the indicators don't work properly. A good example of this is the Vortex indicator, since it needs a full closed bar to get its info from. It tries to use the current running bar, by ticks, and if the bar only has one tick so far and it's getting more ticks through out time the Vortex pattern vanishes, braking everything.
                          If you switch the strategy to OnEachTick, all attached indicators also start using OnEachTick, which messes up all the results. This happens with RVI, MACD, and many others.

                          My question is, how can I have my Strategy run with OnPriceChange or OnEachTick, and keep the indicators that are started with the strategy still using OnBarClose.

                          Indicators get loaded/created during State == State.DataLoaded
                          mRVI = ARRVI(10);
                          AddChartIndicator(mRVI);

                          That's all the code that they use. There are no other controls in other parts of the software. Setting the Calculate property here has no effect.
                          Without having the code for NT8 itself, it's hard to see how that can be accomplished. So I'm hoping the tech support can help out.
                          Last edited by DieSlower; 08-09-2020, 04:20 PM.

                          Comment


                            #14
                            Hello Maxim,

                            Thanks for your reply.

                            Correct, the host script would drive the OnBarUpdate() processing for all scripts it uses.

                            When an indicator is processing OnEachTick (Or OnPriceChange) the current value of the indicator is subject to the changes of the forming bar data (its data is changing so any indicator calculated from it will likewise be subject to change during bar formation), however, this only occurs while referencing the bar ago of [0]. The values of the indicator(s) at a bar ago of [1] are fixed and do not change. So you may want to use IsFirstTickOFBar and reference the bars ago of [1] to obtain the last static unchanging value of the indicator(s) in your script.

                            Paul H.NinjaTrader Customer Service

                            Comment


                              #15
                              Hi Paul,

                              So correct me if I'm wrong then,
                              but from what I gather, all Indicators in NT8 to still work correct and use the Bar Close values, if your strategy is set to OnEachTick, need to be recoded and modified to use [1] in all Close, Low, High, etc etc types where it used a [0]? And obviously [2] where it was [1], and so on... Then using IsFirstTickOfBar in OnBarUpdate() will give the effect of the indicator running at OnBarClose?

                              V/R,
                              --Maxim

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Lumbeezl, 01-11-2022, 06:50 PM
                              31 responses
                              817 views
                              1 like
                              Last Post NinjaTrader_Adrian  
                              Started by xiinteractive, 04-09-2024, 08:08 AM
                              5 responses
                              14 views
                              0 likes
                              Last Post NinjaTrader_Erick  
                              Started by swestendorf, Today, 11:14 AM
                              2 responses
                              6 views
                              0 likes
                              Last Post NinjaTrader_Kimberly  
                              Started by Mupulen, Today, 11:26 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post Mupulen
                              by Mupulen
                               
                              Started by Sparkyboy, Today, 10:57 AM
                              1 response
                              6 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Working...
                              X