Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trouble accessing open candle on chart BarsInProgress

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

    Trouble accessing open candle on chart BarsInProgress

    I have a strategy in which I want it to perform certain actions on the last tick of a bar, and on the first tick of a bar. I set it to Calculate.OnBarClose, and added a 1-tick data series. But I was not having much luck with how BarsInProgress==0 was being called, so I decided to run everything from BarsInProgress==1 (the tick series).

    First off,​ in case it matters, I am using a 21-tick range bar on my chart. Volumetric range bar to be precise, but it acts the same on a plain 21-range.

    So I decided to write a couple of functions (again, called in BarsInProgress==1 section) to tell me when its the last tick of a candle and when its the first.

    I can tell if it's the last tick of the open range bar by checking that:
    1. the (high - low) == 21 ticks
    2. the (close == high || close == low)

    So in this case, close refers to the 1-tick series close, which is the last price (that's what my chart is set to display).
    Of course this will give false positives, because those two conditions can be satisfied any number of times if the price retraces before making the 22nd tick. But I only need the first one to trigger my action, so that is okay with me.

    To tell if it's the first tick of a new bar, I merely need to check that the (high == low).

    In those functions, I have to explicitly refer to the chart prices, so I use the second array index, like this:
    Code:
    private bool IsFirstCandleTick()
    {
            /// Must wait for one bar to close to establish range bar height - historical will do
            if (!oneBarClosed) return false;
    
            /// Called from tick series, so specify to use chart bars
            return (Highs[0][0] == Lows[0][0]);
    }
    ​
    (The IsLastCandleTick() function can be found in the attached code)

    ​However, it does not seem to work. When I add logging, it seems that references to High, Low, Close are returning values for the first closed candle, not the open candle, which should be [0][0].

    Can someone please explain what is going on and how I can reference the open chart candle?

    I wrote this dumbed-down sample so you can run the attached code and check the log to see what it does.
    Attached Files

    #2
    Hello DerkWehler,

    "Can someone please explain what is going on and how I can reference the open chart candle?"

    If OnBarUpdate() has not updated the primary series, then that bar cannot be referenced.

    Perhaps you want to use Calculate.OnEachTick (with TickReplay in historical) so that the primary bar series updates.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post

      If OnBarUpdate() has not updated the primary series, then that bar cannot be referenced.

      Perhaps you want to use Calculate.OnEachTick (with TickReplay in historical) so that the primary bar series updates.
      Thank you for the reply!

      My goal is to get Historical to match forward testing, for which I have had many difficulties. A much more experienced NinjaScript programmer told me if I ever wanted that to happen, that I should use OnBarClose (and forget tick replay). Do you disagree?

      Comment


        #4
        Actually, thinking a bit more on it...If I want the historical to run each tick, when I have a 1-tick series loaded in the Strategy, don't I always need to use tick replay?

        Comment


          #5
          Hello DerkWehler,

          With OnBarClose the OnBarUpdate() method runs after the bar has closed. This does not run for the new bar that has opened.
          With OnPriceChange or OnEachTick the OnBarUpdate() methods runs for every price change and tick including the first price update and tick of a new bar.

          If you want information about about the new bar that was opened, before that bar closes, you would need to use OnPriceChange or OnEachTick.


          If you are using all market orders with OnBarClose and there are no stop or limit orders, I would expect the real-time and historical performance to be the same or very similar.

          If you are using limit orders, or submitting orders intra-bar, then 1-tick intra-bar granularity and TickReplay would be needed.

          Below is a link to the support article on intra-bar granularity.


          And a link to the support article on comparing real-time to backtest.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Reading over the links you sent, in this article,



            Does "Backtest" refer to the back tester, or to historical fill, or both.

            Or more generally, to avoid ambiguity, do the docs always refer to historical fill as such, rather than "backtest"?

            Comment


              #7
              Hello DerkWehler,

              These are the same.

              A backtest in the Strategy analyzer is running through the historical data with State.Historical.
              A script enabled live on a chart will first run through historical data with State.Historical and then transition to State.Realtime.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Please forgive me posting again, out of turn, but perhaps it would be more direct to ask this:

                If in a long trade, what I want to accomplish is to close the trade on the last tick of the candle, and then immediately open a new long trade on the first tick of the new candle. I don't really care if it actually closes on the last tick; it can do the close and the open on the first tick on the new bar. (I will likely have a trailing stop that needs to be updated every tick too, so one way or the other I will need to check some things each tick)

                What is the best way to do this?

                Is it this:

                1. Enable tick replay

                2. Run "Calculate.OnEachTick" (or price change, for whatever needs tick granularity)

                3. Using managed approach, if I close a trade and then open another, NT doesn't just queue these up and do them asap, correct? Therefore...in the (BiP == 0) section, I would need to set a flag "openNewLong", then issue the ExitLong() order, and then in OnExecutionUpdate(), when the close is verified, open a new long trade if the flag is set?


                Comment


                  #9
                  Hello DerkWehler,

                  Are you trying to exit a position and enter the same position or are you trying to reverse the position?

                  If you are trying to exit a position and enter the same position, you would need to exit the position, detect the exit has filled in OnExecutionUpdate(), then send the new order to re-enter the same position.

                  If you are trying to reverse a position using a market order with the managed approach just call EnterLong() or EnterShort() and this will automatically reverse the position.

                  Predicting the last tick of a bar would be challenging if not possible at all.

                  But you could exit the position and enter a position after a bar has closed with OnBarClose or on the first tick of a new bar with OnEachTick which are basically the same.

                  In historical you would need intra-bar granularity.
                  Chelsea B.NinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by NullPointStrategies, Yesterday, 05:17 AM
                  0 responses
                  56 views
                  0 likes
                  Last Post NullPointStrategies  
                  Started by argusthome, 03-08-2026, 10:06 AM
                  0 responses
                  132 views
                  0 likes
                  Last Post argusthome  
                  Started by NabilKhattabi, 03-06-2026, 11:18 AM
                  0 responses
                  73 views
                  0 likes
                  Last Post NabilKhattabi  
                  Started by Deep42, 03-06-2026, 12:28 AM
                  0 responses
                  45 views
                  0 likes
                  Last Post Deep42
                  by Deep42
                   
                  Started by TheRealMorford, 03-05-2026, 06:15 PM
                  0 responses
                  49 views
                  0 likes
                  Last Post TheRealMorford  
                  Working...
                  X