Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Check if OnBarUpdate is called because of price change

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

    Check if OnBarUpdate is called because of price change

    I have set Calculate.OnPriceChange because my indicator has to calculate the last bar too.
    However, OnBarUpdate only needs to be called once per bar.
    Is there a proper way to detect if OnBarUpdate is called for a new bar or because of OnPriceChange?
    Or do I have to store the CurrentBar in a private field and check if the CurrentBar has changed?

    #2
    Hello Bobin,

    Thanks for your post.

    Yes, logic can be separated between Calculate.OnPriceChange and Calculate.OnBarClose using IsFirstTickOfBar. Please note that a hosted script will inherit the Calculate mode of the script that hosts it. You can take the following approach to differentiate logic between OnBarClose and OnPriceChange processing.

    Please see this reference sample which demonstrates a technique used for those who need to separate their logic to calculate some values on each tick and others only on the close of a bar. The sample uses Calculate.OnEachTick but this could be changed to OnPriceChange in your script. You will set your host script to Calculate.OnPriceChange and use if(IsFirstTickOfBar) and place all code that needs to calculate once every bar within that condition check. Then place all code that you want to calculate OnPriceChange outside of the IsFirstTickOfBar condition check.

    SampleEnterOnceExitEveryTick -https://ninjatrader.com/support/help...either_cal.htm

    Please let us know if we may assist further.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Thank you Brandon

      Comment


        #4
        The link is broken. Could you please repost the link?

        Comment


          #5
          Hello Graci117,

          Here is the link:

          HTML Code:
          https://ninjatrader.com/support/helpGuides/nt8/?separating_logic_to_either_cal.htm

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by cmoran13, 04-16-2026, 01:02 PM
          0 responses
          43 views
          0 likes
          Last Post cmoran13  
          Started by PaulMohn, 04-10-2026, 11:11 AM
          0 responses
          25 views
          0 likes
          Last Post PaulMohn  
          Started by CarlTrading, 03-31-2026, 09:41 PM
          1 response
          162 views
          1 like
          Last Post NinjaTrader_ChelseaB  
          Started by CarlTrading, 04-01-2026, 02:41 AM
          0 responses
          98 views
          1 like
          Last Post CarlTrading  
          Started by CaptainJack, 03-31-2026, 11:44 PM
          0 responses
          158 views
          2 likes
          Last Post CaptainJack  
          Working...
          X