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

Is Close[0] the current bar, or the last bar?

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

    Is Close[0] the current bar, or the last bar?



    Hi there,

    Within OnBarUpdate, is Close[0] the current bar that's being formed, or the last completed bar? If it's the bar being formed, then the value of Close[0] would vary with each tick change, correct?

    Thanks


    #2
    Hello timmbbo,

    Thanks for your post.

    The answer depends on the Calculate setting AND if you are looking at historical data OR real time (or market replay) data.

    For historical data (not including market replay), when your script loads, it will process the bars in sequence from the beginning "days to load" specified in the data series of the chart. The script will be run once per bar and will perform based on that the bar index of [0] is the just closed bar. So as each bar is processed it becomes the next [0]. This is true regardless of the Calculate setting sos please keep this in mind going forward.

    When the historical data has been processed and assuming you are connected to live data (or playback>MarketReplay data), the calculate setting of the script can change the bar references once it is processing the real time data as follows:

    Calculate.OnBarClose, in live/replay data, [0] is the latest just closed bar, your script will run once at the end of the bar, the script does not know of the currently forming bar until it closes and the index [0] shifts to the just closed bar, the previously closed bar would now be [1]. Your code would execute exactly the same as it does historically.

    Calculate.OnPriceChange, in live/replay data, Close[0] is the current price of the currently forming bar, the Close, High and Low values change as the price changes in the currently forming bar until it closes. [1] will be the just closed bar in this mode. Your script will execute on every change in price while the currently forming bar changes price which means again that Close[0], High[0] and Low[0] can change (Open[0] will not change as it is set once on the first tick).

    Calculate.OnEachTick, in live/replay data, Close[0] is the current price of the currently forming bar, the Close, High and Low values change as the price changes in the currently forming bar until it closes. Note: In this mode your code will execute on each incoming tick which may be exactly the same price as the previous tick(s). Like OnPriceChange, the currently forming bar prices of Close, High, Low will change until the bar closes. The bar index of [1] is the just closed bar.

    Reference: https://ninjatrader.com/support/help...?calculate.htm
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hi,
      I am using Calculate.OnEachTick for my script, Close [0] is giving the current price which is completely correct, but Close[0] is not returning the close value of the previous bar, it's giving me the price from the previous tick!! What I am missing here? is this the desired behavior? How can I get the close of precious bar then?

      here you can see my code and the script outputs:
      Code:
       private bool HasVeryLongTail(int barsAgo)
      {
      Print("HasVeryLongTail =>"
      + " barsAgo = " + barsAgo
      + " Low[barsAgo] = " + Low[barsAgo].ToString()
      + " Close[barsAgo] = " + Close[barsAgo]
      + " GetLowCloseSpreadTicks(barsAgo)= " + GetLowCloseSpreadTicks(barsAgo).ToString()
      + " VeryLongSpreadTicks = " + VeryLongSpreadTicks.ToString());
      
      return Low[barsAgo] != Close[barsAgo] && Math.Abs(GetLowCloseSpreadTicks(barsAgo)) >= VeryLongSpreadTicks;
      }
      Click image for larger version

Name:	Capture.PNG
Views:	2052
Size:	234.0 KB
ID:	1145264

      Comment


        #4
        Originally posted by mahdighorbanpour View Post
        Hi,
        I am using Calculate.OnEachTick for my script, Close [0] is giving the current price which is completely correct, but Close[0] is not returning the close value of the previous bar, it's giving me the price from the previous tick!! What I am missing here? is this the desired behavior? How can I get the close of precious bar then?
        When using Calculate.OnEachTick, and processing RealTime data, your OnBarUpdate is called
        for each incoming tick, and Close[0] contains the current price of the actively developing bar.

        Close[1] gives you close price of the most recently closed bar.
        Close[1] does not give you the close of the previous tick.

        The code in your post isn't complete enough to understand your problem.
        Please post a small working script if you need additional help.

        Last edited by bltdavid; 03-07-2021, 12:59 PM. Reason: fix typo

        Comment


          #5
          Originally posted by bltdavid View Post

          When using Calculate.OnEachTick, and processing RealTime data, your OnBarUpdate is called
          for each incoming tick, and Close[0] contains the current price of the actively developing bar.

          Close[1] gives you close price of the most recently closed bar.
          Close[1] does not give you the close previous of the previous tick.

          The code in your post isn't complete enough to understand your problem.
          Please post a small working script if you need additional help.

          The code is clear enough, I am calling this method twice, one-time HasVeryLongTail(0) and right after it HasVeryLongTail(1). and If we put aside the logic I used there inside the method, I have a simple print Low[barsAgo] and Close[barsAgo].

          I was using the playback connection and after restarting the NinjaTrader application seems the problem is gone! I feel sometimes when I move the playback time slider, the application doesn't work as it should! Is this a known issue?
          Thank you

          Comment


            #6
            There is no NinjaTrader specific issue here.

            I took the time to look more carefully at your NinjaScript output.
            My opinion is that you may not be understanding your charts.
            Your output looks correct.
            Let me explain.

            You circled output when 'BarsAgo = 0' and the Close[0] value in the output
            window is printed as 13055.5. This looks viable, the chart shows the Close
            of the current bar at 13055.75, so being off by 1 tick in the screenshot is most
            likely a timing issue, or the additional output for this tick is obscured in the
            output window.

            You next circled output when 'BarsAgo = 1' and the Close[1] is 13050.00.
            On the chart, I see your hand drawn blue '1' pointing to the previous bar.
            But, since this is a green candlestick bar, it's Close price is at the top
            of the green body, so the Close price of this candle is 13050.00.

            That is correct.

            At the bottom of your screenshot you hand wrote "should be 13044.75".
            I believe this comment shows your confusion.

            13044.75 is the price at the bottom of the green candlestick body, so that
            must be the open price, not the close price.

            Close[1] is 13050.00
            Open[1] is 13044.75 <-- this is not the close, this is your confusion

            Make sense?

            Comment


              #7
              Originally posted by bltdavid View Post
              There is no NinjaTrader specific issue here.

              I took the time to look more carefully at your NinjaScript output.
              My opinion is that you may not be understanding your charts.
              Your output looks correct.
              Let me explain.

              You circled output when 'BarsAgo = 0' and the Close[0] value in the output
              window is printed as 13055.5. This looks viable, the chart shows the Close
              of the current bar at 13055.75, so being off by 1 tick in the screenshot is most
              likely a timing issue, or the additional output for this tick is obscured in the
              output window.

              You next circled output when 'BarsAgo = 1' and the Close[1] is 13050.00.
              On the chart, I see your hand drawn blue '1' pointing to the previous bar.
              But, since this is a green candlestick bar, it's Close price is at the top
              of the green body, so the Close price of this candle is 13050.00.

              That is correct.

              At the bottom of your screenshot you hand wrote "should be 13044.75".
              I believe this comment shows your confusion.

              13044.75 is the price at the bottom of the green candlestick body, so that
              must be the open price, not the close price.

              Close[1] is 13050.00
              Open[1] is 13044.75 <-- this is not the close, this is your confusion

              Make sense?

              Yep, now I see my mistake, it's a green candle Thank you for spending time to look at it

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by TraderBCL, Today, 04:38 AM
              3 responses
              23 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Started by WeyldFalcon, 08-07-2020, 06:13 AM
              11 responses
              1,423 views
              0 likes
              Last Post jculp
              by jculp
               
              Started by RubenCazorla, Today, 09:07 AM
              0 responses
              4 views
              0 likes
              Last Post RubenCazorla  
              Started by BarzTrading, Today, 07:25 AM
              2 responses
              29 views
              1 like
              Last Post BarzTrading  
              Started by devatechnologies, 04-14-2024, 02:58 PM
              3 responses
              21 views
              0 likes
              Last Post NinjaTrader_BrandonH  
              Working...
              X