Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

OnBarUpdate not Being Called in an Indicator

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

    OnBarUpdate not Being Called in an Indicator

    What could prevent an indicator calling OnBarUpdate() when a bar updates?

    In this instance, Calculate == Calculate.OnBarClose.

    SOLVED:
    IsDataSeriesRequired needs to be true.
    Last edited by FatCanary; 03-24-2023, 06:58 AM. Reason: Solved

    #2
    Hello FatCanary,

    Thank you for your post.

    More information is needed to understand why OnBarUpdate() might not be called. First, check if there are any error messages on the Log tab of the Control Center. You will also need to see if you are connected to data and receiving updates for the instrument selected; if it is a futures instrument, you may need to rollover to the latest expiry to receive data. Another item is the Trading Hours selected for the data series the indicator is using - if it is outside of the selected Trading Hours session then data will not flow from the data series into the indicator.

    OnBarClose will call OnBarUpdate() once at the close of each bar. A bar is considered closed when the first tick of a new bar is formed.

    For more information, please see the links below:
    Please let me know if I may be of further assistance.

    Comment


      #3
      Hi Emily

      None of your suggestions are causing the problem.

      I do not have this issue in any of my other custom indicators. I suspect there is something in my script that could be the cause.
      Aside from what you have mentioned above, is there anything code wise that could be prevent an OnBarUpdate call?

      Comment


        #4
        Hello FatCanary,

        Thank you for your reply.

        I initially read your question as something more general and not a specific case. I suggest adding Print() statements throughout your script, even every couple of lines, to evaluate if (or when) OnBarUpdate() is being called and which portions of the logic are being hit vs. which ones are not. You could even add a Print() into each state of State.SetDefaults to let you know when the script gets through to each state. You could also test out by commenting/uncommenting different portions of code to see if the behavior of the indicator changes.

        Do you have any checks in OnBarUpdate() that cause the script the return? For example:
        if (CurrentBar < 5)
        return;

        This would start OnBarUpdate(), then check the CurrentBar index. If the value is less than 5, then the script will not process the rest of the logic in OnBarUpdate(). Once the value for CurrentBar is greater than 5, the script will continue on to the next line of code. This is another potential cause that would stop a script from processing all of its logic in OnBarUpdate(). For more tips regarding debugging a script:



        Please let me know if I may be of further assistance.

        Comment


          #5
          Yeah, sorry Emily, I should have been clearer in my initial post.

          OnBarUpdate() is not being called. I have a debug breakpoint and a Print() statement in the method, and there is no call to it.

          I'm using OnRender() extensively in this indicator. Is there anything that could, or should, be in OnRender() in order for OnBarUpdate() to function correctly? I'm just trying to think of areas where this particular indicator differs massively from my other indicators.
          Last edited by FatCanary; 03-23-2023, 11:57 AM.

          Comment


            #6
            Hello FatCanary,

            Thank you for your reply.

            OnRender() and OnBarUpdate() are called independently from each other, so there is nothing that should be added or removed from OnRender() to affect OnBarUpdate(). So I may accurately assist you, please answer all of the following questions:
            • What version of NinjaTrader are you using? Please provide the entire version number. This can be found under Help -> About (Example: 8.?.?.?)
            • Who are you connected to? This is displayed in green on the lower-left corner of the Control Center window.
            • Who is your broker?
            • What instrument symbol (and expiry if applicable) have you selected? For example, ES 03-22, AAPL, EURUSD, etc.
            • (For a chart) what interval is selected? For example, 5 minute, 1 day, 2000 volume, 4 Renko, etc.
            • Do you receive an error on the screen? Are there errors on the Log tab of the Control Center? If so, what do these errors report?
            • Do you see the same results when the indicator is added to a chart while connected to the Playback Connection? You may speed up the playback controller to show what happens as more and more time has passed.
            ​I appreciate your patience and look forward to your reply.

            Comment


              #7
              Answers as follows:
              • 8.0.27.1 64-bit
              • NinjaTrader Continuum or Playback
              • NinjaTrader brokerage
              • MES 06-23 for Continuum, MES 12-22 for Playback
              • 377t
              • No errors, anywhere
              • Yes
              Just to emphasize, all the above answers hold true for all my other indicators, which have no problem calling OnBarUpdate().

              Comment


                #8
                Originally posted by FatCanary View Post
                Just to emphasize, all the above answers hold true for all my other indicators, which have no problem calling OnBarUpdate().
                Does your indicator have any plots and are you accessing them? OnBarUpdate is only called if necessary to update the nested indicator's plots. If you don't have any plots, try calling .Update on the nested indicator first before you access its properties. Or, in the property you are accessing, in the getter, call Update(); there before you return the answer.

                Or, another approach to trick it into running OnBarUpdate even though you don't need any plot values is to add a plot and access it from the outside indicator/strategy, even though you don't need it. If you like you can set it to zero and transparent and set transparent plots to not show in the data box. Read in the zero from the outside strategy or indicator and boom, NinjaTrader understands that it needs to OnBarUpdate the nested indicator so it can give you that zero accurately. You may do anything with the zero that you wish.
                Last edited by QuantKey_Bruce; 03-23-2023, 01:26 PM.
                Bruce DeVault
                QuantKey Trading Vendor Services
                NinjaTrader Ecosystem Vendor - QuantKey

                Comment


                  #9
                  Hi Bruce
                  Thank you for the information.

                  I do have a plot, but only so that I can get a price marker to show on the vertical scale.
                  I'll need to study you suggestions a little further, since the indicator does not need to update the plot in OnBarUpdate(). The plot is set through TriggerCustomEvent().

                  Comment


                    #10
                    QuantKey_Bruce just to check, are you suggesting I access the indicator's 'plot' from another indicator so that OnBarUpdate() is called?

                    Comment


                      #11

                      Thank you for the information FatCanary.

                      Bruce, your input is appreciated, although I would like to clarify that OnBarUpdate() is called whenever a bar is updated based on the Calculate property. It is called regardless of whether an indicator has any plots or not. Calling Update() is only necessary in specific use cases; it is a method that will force OnBarUpdate() to be called for every data series in order to update indicator values to the current bar. For more details, please see the following links:With that in mind - FatCanary, if your indicator is trying to access a value such as a public property that is set in OnBarUpdate() from another indicator, then you will need to call Update() in the property getter to run OnBarUpdate() in the hosted script and get an updated value before it is returned. You could also call Update() on the indicator instance within your script, though if you call it in the property getter that will save you the trouble of having to remember to use Update() in any other script that hosts that indicator. Another way the hosted indicator can process OnBarUpdate() is if you are accessing a plot from that indicator inside of your host script.

                      Are you using any plots or values from another indicator inside of this current indicator that doesn't seem to call OnBarUpdate()? If you have not already, please make sure you test out a Print statement at the very beginning of OnBarUpdate() outside of any conditions. Here is an example:
                      Code:
                      protected override void OnBarUpdate()
                      {
                      Print("OnBarUpdate.  BIP: " + BarsInProgress + "CurrentBar: " + CurrentBar);
                      // the rest of your OnBarUpdate() logic here
                      }
                      Please test this out and let me know the result. If you'd like to share the output, you may right-click in the NinjaScript Output window and then select Save As. Give the file a name and then attach that with your response here.

                      I sincerely appreciate your patience and look forward to hearing the results.

                      Comment


                        #12
                        Hi Emily

                        Thank you for the continued help.

                        My indicator is not trying to access any public property. It is a standalone indicator and is not hosted nor is it hosting anything.

                        I have a Print() statement at the very beginning of OnBarUpdate(), but because that method is not called, there is no print output.

                        Comment


                          #13
                          Hello FatCanary,

                          Thank you for your reply.

                          I would like to see if I can reproduce this on my end. Please provide a reduced sample of your script that still reproduces this behavior. To export the script, go to Control Center > Tools > Export. Select your reduced sample and save the file. Attach that file to your forum reply so I may download and import it.

                          I appreciate your patience and look forward to assisting you further.

                          Comment


                            #14
                            Hi Emily

                            That may not be possible. I'm not entirely sure what to remove and still have the indicator function.
                            That is why I've not already tried that approach. I was hoping the there was something obvious that could prevent OnBarUpdate() being called.

                            The indicator does not rely on OnBarUpdate() but I wanted to use the method for a particular situation. That's when I noticed that it wasn't being called.

                            Comment


                              #15
                              Hello FatCanary,

                              Thank you for your reply.

                              I understand that your indicator relies on OnRender() extensively; are you also computing values inside of OnRender()? As a performance best practice, you should be cautious when it comes to calculating inside of OnRender() and instead perform calculations inside of OnBarUpdate(). If you are calculating inside of OnRender, what happens if you move the calculations to OnBarUpdate() instead? This tip is listed in the Performance section of the best practices page here:
                              "Precomputing values instead of calculating in OnRender()" - https://ninjatrader.com/support/help...tm#Performance

                              I also see your note about using TriggerCustomEvent() to set a plot; if you add a print statement once the plot is set, do the expected values show up in the output? Is there a difference if you set the plot in OnBarUpdate() and then update the value using TriggerCustomEvent() as demonstrated in the snippet on this page?


                              Unfortunately, there is not an obvious cause at this point. Something else you could try is creating a new indicator with a working print in OnBarUpdate(). You could then slowly re-introduce parts of your script into the new indicator and reload the indicator to ensure that the OnBarUpdate print is still showing. If you reach a point where the print stops appearing, then it could be likely that the last portion of logic that you copied over from your script is the cause of the unexpected behavior relating to OnBarUpdate().

                              I look forward to your reply.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              574 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              332 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              101 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              553 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              551 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X