Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

OnBarClose and OnPriceChange in the same indicator

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

    OnBarClose and OnPriceChange in the same indicator

    Is that possible? Any code sample?

    #2
    Hello cay7man,

    Thank you for your post.

    Yes, this would be possible by setting Calculate to Calculate.OnPriceChange in OnStateChange() when State == State.SetDefaults. Then, inside of OnBarUpdate(), you could separate out logic that you would like to be calculated on bar close within a check for when IsFirstTickOfBar is true. Here is the help guide page for IsFirstTickOfBar:


    We have a strategy reference sample that demonstrates separating logic to calculate either on bar close or on every tick that uses IsFirstTickOfBar:


    Please let us know if we may be of further assistance.

    Comment


      #3
      TY! How often OnPriceChange is expected to be called? I didn't see its getting called as price moved intrabar.

      Comment


        #4
        Hello cay7man,

        Thank you for your reply.

        On Price Change will call OnBarUpdate() for every price change during real-time data. In historical processing, OnBarUpdate() is only processed on bar close unless Tick Replay is enabled:


        Please feel free to reach out with any additional questions or concerns.

        Comment


          #5
          Ok. I'm not sure what I'm doing wrong. I've the following in the code

          Calculate = Calculate.OnEachTick;

          protected override void OnBarUpdate()
          {
          Draw.TextFixed(this, "NinjaScriptInfo",string.Format("{0}",Close[0]), TextPosition.BottomRight);​
          }

          I don't see price changing during intra bar. Its printing previous bar close. Close[0] is incorrect? What should I use to get the current price?

          Comment


            #6
            Hello cay7man,

            Thank you for your reply.

            Do you get any errors on the Log tab of the Control Center? I was able to get both of the following TextFixed objects to update the Close[0] price on each tick:
            Code:
                    protected override void OnBarUpdate()
                    {
                        Draw.TextFixed(this, "tag", Close[0].ToString(), TextPosition.BottomRight);
                        Draw.TextFixed(this, "tag1", string.Format("{0}", Close[0]), TextPosition.BottomLeft);
                    }​
            Be sure to double-check in the indicator settings when you are configuring it that Calculate is set to OnEachTick:
            World's leading screen capture + recorder from Snagit + Screencast by Techsmith. Capture, edit and share professional-quality content seamlessly.


            You could also consider adding prints in OnBarUpdate() to verify the value of calculate and see how frequently it is printing. For example:
            Print(string.Format("{0} state: {1} calculate: {2}", Time[0], State, Calculate));

            This is also a good way to see that when the state is Historical, OnBarUpdate() is only processed once per bar regardless of what Calculate is set to. Once the state is Realtime, you should see prints in OnBarUpdate() that correspond with what Calculate is set to.

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

            Comment


              #7
              TY! On indicator setting, it was set to BarClose. I thought what Calculate was set to in the code was sufficient.

              Comment

              Latest Posts

              Collapse

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