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

CurrentBar of current Price Vs previous Price

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

    CurrentBar of current Price Vs previous Price

    Hi NT,

    I am building an indicator based on Calculate.OnPriceChange.
    I'd like to retrieve the 'CurrentBar' and compare it to the CurrentBar if the last price change.
    How can i do this ? Here is the Pseudo of what I am trying to achieve :

    Code:
    if Tick(CurrentBar) is not equal to Tick(CurrentBar)-1
    
    .... do this....
    Thanks
    AK

    #2
    Hello akvevo,

    Thank you for your note.

    Would you please provide more information on what you are trying to do?

    Is your if statement asking if the current bar is not the same as the previous bar?

    I look forward to your reply.
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Hi Alan,

      I am building a simple logic that counts and numbers bars from 1 to 5 for instance.
      The bars from be consecutive..

      if (Close[0] < Close[4])
      {
      counter ++;
      ... number the bar Draw.Text (... "counter"...)
      }
      This all works great on a chart historically, because the bars are closed.
      But... I am using Calculate.OnPriceChange...
      so whenever the OnBarUpdate run, the logic above runs again and increments the counter even though I am on the same bar.

      If i use Calculate.OnBarClose, then i need to wait for the bar's close.
      If like to know i the condition is satisfied during the bar's life.

      If i use Calculate.OnPrice change, then my counter increments every time the price change.

      I am trying to find something in the middle.. where the counter increments once per bar only....and am open to ideas

      The initial post was aimed at trying to find an if statement that would say:

      Code:
      if CurrentBar for current price is the same as CurrentBar of previous price change
      {
      dont increment counter
      }

      Comment


        #4
        Hello akvevo,

        What you could use to prevent a calculation running more than once on the current bar when running calculate != OnBarClose, is isFirstTickOfBar.

        Please see the following section of our helpguide,


        Please let us know if you need further assistance.
        Alan P.NinjaTrader Customer Service

        Comment


          #5
          Thanks Alan,

          This seems useful indeed, but i'snt exactly what I am looking for.
          Let me have another go at explaining ..

          I use Calculate.OnPriceChange... because i want to know in real-time if a bar meets a criteria without having to wait for the close... If the condition is true, I want to increment a counter...by 1.

          The problem is that everytime the price changes, the counter increments..(1,2,3) even though we are on the same bar.. I want the condition logic to run throughout the life of the bar but stay on counter =1, while we are on the same bar. If we move to the next bar, the counter should run throughout the life of that bar... but stay on counter =2 .
          ((the problem with IsFirstTickOfBar.. is that it runs once on the first tick but doesnt continue evaluating the bar in real-time to see if it still meets my condition))

          I suppose what I want to do it to return the CurrentBar of an event (where an event is defined as every time OnBarUpdate() runs)

          I can use [0] to access the current bar and I can use [1] to access the previous by, but how can i access the CurrentBar and last price of the 'previous event'?
          If the CurrentBar is 100 and the price is 15.00... and the price changes to 15.01 , but the bar is still 100, I dont want my conunter to increment, because the bar is the same.

          I hope this all makes sense ..I am added a screenshot which may help add visual assistance to understand my challenge
          Attached Files

          Comment


            #6
            Hello akvevo,

            I have attached a sample which will add 1 to a counter a max of 1 time per bar, by setting a bool to false after the counter has been added. This bool is set back to true on the next bar..

            This sample assumes that once your condition becomes true, it can not become false on that bar.,

            Please let us know if you have any questions or need further assistance.
            Attached Files
            Last edited by NinjaTrader_AlanP; 12-07-2017, 11:15 AM.
            Alan P.NinjaTrader Customer Service

            Comment


              #7
              Thank you Alan,
              I am taking a look, first thing I noticed is that the sample is set to Calculate.OnBarClose.. which defeats the purpose of the test, but i'll test on Calculate.OnPriceChnage to verify

              Comment


                #8
                Thanks Alan,

                This example is indeed very useful, can I just ask what the purpose of "xBar" variable is ?
                Its used a condition to set "doOnce" back to false
                Last edited by akvevo; 12-11-2017, 06:02 PM.

                Comment


                  #9
                  Hello akvevo,

                  To add one to the counter per bar, set xBar to the CurrentBar, then set a bool to false so that block does not run again on this bar. You need to reset this bool on the next bar so the counter again can have +1 added.
                  Code:
                  if(doOnce != true)
                  {
                  counter++;
                  doOnce =true;
                  xBar =CurrentBar;
                  }
                  Thus, when current bar becomes greater than xBar, which will happen on the next bar, this bool doOnce will be reset back to false so the counter can have another 1 added to it.

                  Code:
                  			
                  if(CurrentBar>xBar)			
                  doOnce =false;
                  Please let us know if you need further clarification.
                  Alan P.NinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Waxavi, Today, 02:00 AM
                  1 response
                  7 views
                  0 likes
                  Last Post NinjaTrader_LuisH  
                  Started by alifarahani, Today, 09:40 AM
                  5 responses
                  23 views
                  0 likes
                  Last Post NinjaTrader_Jesse  
                  Started by Kaledus, Today, 01:29 PM
                  4 responses
                  12 views
                  0 likes
                  Last Post Kaledus
                  by Kaledus
                   
                  Started by gentlebenthebear, Today, 01:30 AM
                  3 responses
                  16 views
                  0 likes
                  Last Post NinjaTrader_Jesse  
                  Started by PhillT, Today, 02:16 PM
                  2 responses
                  7 views
                  0 likes
                  Last Post PhillT
                  by PhillT
                   
                  Working...
                  X