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 Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          563 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          329 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
          547 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          548 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X