Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Stochastic Indicator only updates OnBarClose

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

    Stochastic Indicator only updates OnBarClose

    Hi,

    I've done some digging in the forums and found that candle indicators purposely update OnBarClose to avoid false flags. In my case, I want the Stochastic indicator to update as prices change on a 5 minute candle. I actually implemented this in Python before I moved over to NinjaTrader but I'm having a lot of trouble getting things squared away over here.

    Thanks for any help!

    #2
    Hello met17,

    Thanks for your post and welcome to the NinjaTrader forums!

    By default, most platform provided indicators will operate once per bar when the bar closes. There are a couple of reasons for this. Waiting until the bar closes assures that the indicator's current value will not change as it would during the forming of the bar and processing once per bar is less of a burden to your CPU/GPU.

    With almost any indicator, you can change how the indicator processes based on these three modes:

    1) Calculate.OnBarClose (The default), indicator updates when the currently forming bar completes.
    2) Calculate.OnPriceChange. the indicator will update any time the price changes (this is the recommended mode to use when you want to have intrabar updates)
    3) Calculate.OnEachTick. This is the heaviest use of the 3 modes as it will update on each incoming tick regardless if it changes price or not. Some indicators need to use this mode, examples are OrdeFlow indicators.

    Reference: https://ninjatrader.com/support/help...?calculate.htm

    When you add an indicator to a chart, you can change the "Calculate" setting as you wish in the "Properties" of the indicator.


    Comment


      #3
      Hi Paul,

      Thanks for the reply. That's what I hoped for the stochastic indicator but unless I'm doing something wrong, it doesn't get calculated until we get a candle close even with Calculate.OnPriceChange set.

      Here are some logs which appear to verify my issue:

      Code:
      on 5 minute with value 6.40090090090041 at time 7/2/2021 3:54:55 PM with tick price of 4338.25
      on 5 minute with value 6.40090090090041 at time 7/2/2021 3:54:56 PM with tick price of 4338
      on 5 minute with value 6.40090090090041 at time 7/2/2021 3:54:56 PM with tick price of 4338
      on 5 minute with value 6.40090090090041 at time 7/2/2021 3:54:56 PM with tick price of 4338
      on 5 minute with value 6.40090090090041 at time 7/2/2021 3:54:56 PM with tick price of 4338
      on 5 minute with value 6.40090090090041 at time 7/2/2021 3:54:56 PM with tick price of 4338
      on 5 minute with value 6.40090090090041 at time 7/2/2021 3:54:56 PM with tick price of 4338
      on 5 minute with value 6.40090090090041 at time 7/2/2021 3:54:57 PM with tick price of 4338.25
      on 5 minute with value 10.2619047619043 at time 7/2/2021 3:55:00 PM with tick price of 4338.25
      on 5 minute with value 10.2619047619043 at time 7/2/2021 3:55:00 PM with tick price of 4338.25
      on 5 minute with value 10.2619047619043 at time 7/2/2021 3:55:00 PM with tick price of 4338.25
      on 5 minute with value 10.2619047619043 at time 7/2/2021 3:55:01 PM with tick price of 4338
      You can see the stochastic's K value stays the same as the price changes until we get to a 5 minute close.

      Comment


        #4
        Hello met17,

        Thanks for your reply.

        I'm not sure how you coded it or how you are running it.

        Here is an example I tested with that you can recreate:

        Code:
        public class PrintStochastics : Indicator
        {
        private Stochastics mySto;
        
        protected override void OnStateChange()
        {
        if (State == State.SetDefaults)
        {
        Description = @"Enter the description for your new custom Indicator here.";
        Name = "PrintStochastics";
        Calculate = Calculate.OnPriceChange;
        IsOverlay = true;
        DisplayInDataBox = true;
        DrawOnPricePanel = true;
        DrawHorizontalGridLines = true;
        DrawVerticalGridLines = true;
        PaintPriceMarkers = true;
        ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
        //Disable this property if your indicator requires custom values that cumulate with each new market data event.
        //See Help Guide for additional information.
        IsSuspendedWhileInactive = true;
        }
        else if (State == State.DataLoaded)
        {
        mySto = Stochastics(7, 14, 3);
        }
        }
        
        protected override void OnBarUpdate()
        {
        if (State != State.Realtime)
        return;
        {
        Print ("StocasticsK = "+mySto.K[0]);
        }
        }
        }
        Here are a few results from NQ 09-21, 5 minute bars, real time data:

        StocasticsK = 19.7059584534631
        StocasticsK = 19.8602794411175
        StocasticsK = 20.0146004287718
        StocasticsK = 19.7059584534631
        StocasticsK = 19.8602794411175
        StocasticsK = 20.0146004287718
        StocasticsK = 19.3973164781545
        StocasticsK = 19.5516374658088
        StocasticsK = 20.0146004287718
        StocasticsK = 20.3232424040804
        StocasticsK = 20.1689214164261
        StocasticsK = 20.0146004287718
        StocasticsK = 20.1689214164261
        StocasticsK = 20.4775633917347
        StocasticsK = 20.1689214164261
        StocasticsK = 20.3232424040804
        StocasticsK = 20.1689214164261
        StocasticsK = 20.3232424040804
        StocasticsK = 20.4775633917347
        StocasticsK = 20.1689214164261
        StocasticsK = 20.0146004287718
        StocasticsK = 20.1689214164261
        StocasticsK = 19.8602794411175
        StocasticsK = 19.7059584534631
        StocasticsK = 19.8602794411175
        StocasticsK = 19.7059584534631
        StocasticsK = 19.8602794411175
        StocasticsK = 20.0146004287718
        StocasticsK = 19.8602794411175
        StocasticsK = 20.0146004287718
        StocasticsK = 20.1689214164261
        StocasticsK = 19.7059584534631
        StocasticsK = 19.5516374658088
        StocasticsK = 19.2429954905002
        StocasticsK = 19.3973164781545

        Comment


          #5
          That's pretty close to what I have in terms of setup. But I'm using multi-timeframe data series so maybe I'm doing something wrong there.

          My State.Configure setup:

          Code:
          else if (State == State.Configure)
          {[INDENT]AddDataSeries("ES 09-21", Data.BarsPeriodType.Minute, 5, Data.MarketDataType.Last);
          AddDataSeries("ES 09-21", Data.BarsPeriodType.Minute, 30, Data.MarketDataType.Last);
          
          // only purpose of this data series is to see the ticks
          AddDataSeries("ES 09-21", Data.BarsPeriodType.Tick, 1, Data.MarketDataType.Last);
          _buyKStochastic = Stochastics(BarsArray[1], PeriodD, PeriodK, Smooth).K;
          _buyDStochastic = Stochastics(BarsArray[1], PeriodD, PeriodK, Smooth).D;
          _anchorKStochastic = Stochastics(BarsArray[2], PeriodD, PeriodK, Smooth).K;
          _buyAtr = ATR(BarsArray[1], 5);[/INDENT]
           }
          and my OnBarUpdate() for these purposes:

          Code:
          protected override void OnBarUpdate()
          {[INDENT]Print("on 5 minute with value " + _buyKStochastic[0] + " at time " + Time[0] + " with tick price of " + Closes[3][0]);[/INDENT]
           }

          Comment


            #6
            Originally posted by met17 View Post
            and my OnBarUpdate() for these purposes:

            Code:
            protected override void OnBarUpdate()
            {[INDENT]Print("on 5 minute with value " + _buyKStochastic[0] + " at time " + Time[0] + " with tick price of " + Closes[3][0]);[/INDENT]
            }
            Which is probably wrong.

            Your OnBarUpdate runs for each BarsInProgress, producing lots of extra print statements
            that may be confusing you.

            Comment


              #7
              Fair point. But I want the 5 minute stochastic indicator to update when the price changes. If I wrap that print around an if with BarsInProgress == 1, I only get the 5 minute closes instead of each new price.

              I am certain that I don't understand something or I'm doing something wrong, but I think it should be possible for my 5 minute stochastic indicator to get updated during the candle. Only reason I want to do this is because if I'm waiting for the candle close, I'm potentially late on a move. I understand why this isn't always desirable but I have some safeguards in mind to avoid issues with this approach.

              Comment


                #8
                Hello met17,

                Thanks for your reply.

                I modified my earlier example and used ES 09-21, 1-minute bars as the base. I added the two data series and then assigned the 5 minute bars as the input series to the stochastics as you did but using Closes[1] which is the same as BarsArray[1].

                Here is a short video that shows this and its results on live data: https://Paul-ninjaTrader.tinytake.co...OF8xNzM4NTYwMw

                Comment


                  #9
                  Hey Paul,

                  Thanks so much for the video. You're definitely getting the behavior I'm wanting and I think I have the same set up as you and I'm still not seeing my cached stochastic update until the 5 minute candle closes.

                  The only difference, I think, is that I'm doing this in backtesting whereas it looks like you were using a real time chart. Could that have anything to do with what I'm seeing? I can setup a real time chart to see if this is my issue but to be honest I've mostly only worked in the backtester so I'm not sure how to do that

                  Comment


                    #10
                    Hello met17,

                    Thanks for your reply.

                    Yes, using historical data would be the issue. Historical data is made up only of the OHLC values of the bars and not of the ticks within the bar.

                    You can enable tick replay for the platform and that should give you tick by tick performance, however, you would need to have historical tick data over the period of the backtest. It will also significantly slow the backtesting down.

                    To enable Tick replay: https://ninjatrader.com/support/help...ick_replay.htm

                    Alternately you might try testing with the Playback connection using market replay data to better simulate real-time conditions.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                    0 responses
                    636 views
                    0 likes
                    Last Post Geovanny Suaza  
                    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                    0 responses
                    366 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by Mindset, 02-09-2026, 11:44 AM
                    0 responses
                    107 views
                    0 likes
                    Last Post Mindset
                    by Mindset
                     
                    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                    0 responses
                    568 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by RFrosty, 01-28-2026, 06:49 PM
                    0 responses
                    571 views
                    1 like
                    Last Post RFrosty
                    by RFrosty
                     
                    Working...
                    X