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

Current Bar and OnBarUpdate

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

    Current Bar and OnBarUpdate

    Hi

    I am a newbie and have searched for the answer to my question and found a few relevant but ultimatly unhelpful posts for NT7.

    Nothing for NT8.

    To simplify my scenario to something completely basic I would like to detect on the open tick of a new bar whether the close of the old bar is less than the open of the current bar.

    So something like
    if(CurrentBar.Oper>Close[0]){
    }

    From what I could see in NT 7 this was not possible - which seems strange to me as I would have thought that this logic would be pretty common. Maybe I have missed something.

    Any suggestions on a way to achieve this?

    Many thanks,

    Jon

    #2
    Hello JonE1974,

    If you were to use the following

    if(Open[0]>Close[1])
    {

    }
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Hello JonE1974,

      If you were to use the following
      Code:
      if(Open[0]>Close[1])
      {
      //DoSomething
      }
      And run the strategy with Calculate set to OnEachTick, do you get the results you desire?

      I would encourage you to use the strategy builder as you learn NinjaScript, which allows you to view the code to see how the builder writes it.

      Strategy Builder NT8:
      Understand the fundamentals of basic strategy creation by developing an actual strategy using the NinjaTrader Strategy Builder.2:45 Opening a Strategy Builde...


      Strategy Builder Section of our Helpguide:


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

      Comment


        #4
        Hi Alan

        Yes I've been using the builder to create the code then modifying by hand - familiar with normal c#.

        Wouldn't using the Open[0] > Close[1] be the same as looking at the prior 2 candlesticks?

        Or does on each tick make Open[0] reference the active candle?

        I'll have a try with the code in the morning.

        Regards

        Jon

        Comment


          #5
          Hi Jon,

          When Calculate is set to .OnEachTick or .OnPriceChange, the [0] index for a series will reference the currently building and open bar.

          When Calculate is set to .OnBarClose, the [0] index will reference the most recently completed bar.



          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Thanks for the help, that clarifies things.

            Regard

            Jon

            Comment


              #7
              Actually this doesnt seem to work as expected.

              It still references the wrong bar. When I have it set to calc on each tick (using a strategy wiard to ensure it is setup properly) I use the following at the start of the OnBarUpdate function to check and see if there are multiple events being raised - I would expect to see loads for the same time and only the first should have the true state - but I only see one per time and that is set to true
              Print(Time[0].ToString("dd/MM/yyyy") + "," + Time[0].ToString("HH:mm:ss")+ IsFirstTickOfBar);

              Could this be because I am using a backtest and not live data?

              If so how would you do this kind of thing with backtesting?

              Regards

              Jon

              Comment


                #8
                Hello Jon,

                The close time of the bar is not affected. The bar will still close at the same time.

                The Calculate option only causes OnBarUpdate() to trigger each time a tick is received or the price is changed.

                If you are printing the close time of the bar (Time[0]) instead of the current time (DateTime.Now) I would expect that it would print the same time stamp repeatedly until the bar closes.


                When processing historical data and backtesting, TickReplay is required for Calculate to function for .OnEachTick or .OnPriceChange.

                From the help guide:
                "On a historical data set, only the OHLCVT of the bar is known and not each tick that made up the bar. As a result, State.Historical data processes OnBarUpdate() only on the close of each historical bar even if this property is set to OnEachTick or OnPriceChange. You can use TickReplay or a Multi-time frame script to obtain intrabar data."
                Last edited by NinjaTrader_ChelseaB; 08-28-2017, 11:24 AM.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Hi Chelsea,

                  Well spotted thanks for that.

                  The TickReplay looks like it will help me with the backtesting.

                  Thanks for the help,

                  Jon

                  Comment


                    #10
                    Originally posted by NinjaTrader_ChelseaB View Post
                    Hi Jon,

                    When Calculate is set to .OnEachTick or .OnPriceChange, the [0] index for a series will reference the currently building and open bar.

                    When Calculate is set to .OnBarClose, the [0] index will reference the most recently completed bar.

                    http://ninjatrader.com/support/helpG...taIsReferenced

                    http://ninjatrader.com/support/helpG.../calculate.htm
                    I just want to jump in here and say this post was extremely helpful for me!!! I've spent the past week trying to figure out why my multi timeframe strategy was always delayed by one bar. I'm using both 15min and 5 range bars, Calculate set to OnBarClose, with the strategy applied to the 5 range chart. Although a fairly complicated strategy, the fundamental premise is any entry would be initiated when the 5 range bar crossed below the open of a prior bullish candle, however, when backtesting or running in Playback mode it would always be delayed by a single 15min bar. I couldn't figure out why until I saw this post.

                    Click image for larger version  Name:	Screenshot 2022-06-18 142345.png Views:	0 Size:	21.0 KB ID:	1205641

                    Apparently the issue was that I assumed that [1] would always refer to the most recently completed bar, however, it appears with OnBarClose [0] actually refers to the most recently completed bar?!?! Thus, it appears that I simply need to replace the [1] to [0] to reference the most recently completed 15min bar?!

                    Click image for larger version  Name:	Screenshot 2022-06-18 142420.png Views:	0 Size:	21.2 KB ID:	1205642

                    Correct me if I'm wrong, but this change along with calculate set to OnBarClose will allow me to more accurately backtest since it is based on OHLC info?
                    Last edited by gravityflyer; 06-18-2022, 12:33 PM.

                    Comment


                      #11
                      Hello gravityflyer,

                      For accurate backtesting you would need 1-tick intra-bar granularity, which would need to be implemented unlocked in the NinjaScript Editor.


                      If the Calculate setting is not OnBarClose, then TickReplay is also needed.
                      Chelsea B.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by MarianApalaghiei, Today, 10:49 PM
                      1 response
                      5 views
                      0 likes
                      Last Post NinjaTrader_Manfred  
                      Started by love2code2trade, Yesterday, 01:45 PM
                      4 responses
                      28 views
                      0 likes
                      Last Post love2code2trade  
                      Started by funk10101, Today, 09:43 PM
                      0 responses
                      7 views
                      0 likes
                      Last Post funk10101  
                      Started by pkefal, 04-11-2024, 07:39 AM
                      11 responses
                      37 views
                      0 likes
                      Last Post jeronymite  
                      Started by bill2023, Yesterday, 08:51 AM
                      8 responses
                      45 views
                      0 likes
                      Last Post bill2023  
                      Working...
                      X