Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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.
    Emily C.NinjaTrader Customer Service

    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.
        Emily C.NinjaTrader Customer Service

        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:


            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.
            Emily C.NinjaTrader Customer Service

            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 Jonafare, 12-06-2012, 03:48 PM
              5 responses
              3,986 views
              0 likes
              Last Post rene69851  
              Started by Fitspressorest, Today, 01:38 PM
              0 responses
              2 views
              0 likes
              Last Post Fitspressorest  
              Started by Jonker, Today, 01:19 PM
              0 responses
              2 views
              0 likes
              Last Post Jonker
              by Jonker
               
              Started by futtrader, Today, 01:16 PM
              0 responses
              8 views
              0 likes
              Last Post futtrader  
              Started by Segwin, 05-07-2018, 02:15 PM
              14 responses
              1,792 views
              0 likes
              Last Post aligator  
              Working...
              X