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

What is the current bar number when FirstTickOfBar=True?

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

    What is the current bar number when FirstTickOfBar=True?

    Greetings,

    When FirstTickOfBar=True it appears that OHLC for Bar 0 (Open[0]... Close[0]) may have different values --> for the bar #0 there were at least several ticks with different values --> the current bar (for which the first tick has just arrived) is not bar #0 --> what bar is it then? What is its price?

    Also please confirm that
    1) Bar #0 refers to the last closed bar rather than to the current one when FirstTickOfBar=True
    2) Bar #0 refers to the current bar when FirstTickOfBar=False

    Thank you.

    #2
    Hello alex.nt,

    Thank you for your post.

    The current bar is in fact bar # 0, no matter if you use FirstTickOfBar or not.

    If you call Close and Open with the barsAgo value of 0 (zero), you are returning the first tick of the current bar's values. If you want the current bar's OHLC values you can just call these without the use of the FirstTickOfBar.

    If you are looking for the previous bar's values right when the previous bar closes then FirstTickOfBar will be helpful. However, you will need to use the barsAgo value of 1 (Close[1], Open[1], etc.).

    For information on FirstTickOfBar please visit the following link: http://www.ninjatrader.com/support/h...ttickofbar.htm

    For an example of using FirstTickOfBar to seperate your calculation logic when a bar closes when you are using CalculateOnBarClose = False, please visit the following link: http://www.ninjatrader.com/support/f...ad.php?t=19387

    Please let me know if I may be of further assistance.

    Comment


      #3
      Sorry, I think I poorly explained the problem

      Consider the code:
      protected override void OnBarUpdate()
      {
      if (!FirstTickOfBar)
      {
      return;
      }

      Out("OHLC[1]: " + Open[1] + ", " + High[1] + ", " + Low[1] + ", " + Close[1]
      + " OHLC[0]: " + Open[0] + ", " + High[0] + ", " + Low[0] + ", " + Close[0]);

      return;
      }

      and CalculateOnBarClose = false;

      Now, this is the output for me:
      OHLC[1]: 1452.75, 1453, 1452.25, 1452.25 OHLC[0]: 1452.25, 1452.5, 1452.25, 1452.25

      As you may see OHLC[0] contains 2 values, which is impossible with a single tick.

      Comment


        #4
        Hello alex.nt,

        Thank you for your response.

        I am unable to replicate the same output on my end.
        Please advise the following information:
        • What instrument are you running this code on?
        • What interval is the instrument set to (Minute, Tick, etc.)?
        • What Session Template is applied to the instrument?
        • Who do you connect to for data?
        • What version of NinjaTrader are you using? You can check this by going to Help > About (Example: 7.0.1000.X)

        In addition, please attach a working sample of you code to your response so I may test this matter on my end with your code.

        I look forward to assisting you further.
        Last edited by NinjaTrader_PatrickH; 10-03-2012, 06:28 AM.

        Comment


          #5
          Originally posted by NinjaTrader_PatrickH View Post
          [*]What instrument are you running this code on?
          FGBL 12-12

          Originally posted by NinjaTrader_PatrickH View Post
          [*]What interval is the instrument set to (Minute, Tick, etc.)?
          5 Min

          Originally posted by NinjaTrader_PatrickH View Post
          [*]What Session Template is applied to the instrument?
          None/Default (Never set)

          Originally posted by NinjaTrader_PatrickH View Post
          [*]Who do you connect to for data?
          AMP-CQG Demo Account

          Originally posted by NinjaTrader_PatrickH View Post
          [*]What version of NinjaTrader are you using? You can check this by going to Help > About (Example: 7.0.1000.X)
          7.0.1000.10

          Originally posted by NinjaTrader_PatrickH View Post
          In addition, please attach a working sample of you code to your response so I may test this matter on my end with you code.
          I will prepare the code, re-check the things and post again.

          Comment


            #6
            Hello alex.nt,

            Thank you for your response.

            I look forward to your update on this matter.

            Comment


              #7
              Hello! I'm a newbie to programming and I'm very interested in that question too.

              ES-09-15 (8 Min), 24/7, Continuum (NinjaBrokerage), 7.0.1000.29

              Here is my code:

              Code:
                      if(FirstTickOfBar)
                      {
                                      if (!CalculateOnBarClose)
                                      {
                                          Debug.Write("O[0]:");
                                          Debug.Write(Open[0]);
                                          Debug.Write(", H[0]:");
                                          Debug.Write(High[0]);
                                          Debug.Write(", L[0]:");
                                          Debug.Write(Low[0]);
                                          Debug.Write(", C[0]:");
                                          Debug.WriteLine(Close[0]);
              
                                          Debug.Write("O[1]:");
                                          Debug.Write(Open[1]);
                                          Debug.Write(", H[1]:");
                                          Debug.Write(High[1]);
                                          Debug.Write(", L[1]:");
                                          Debug.Write(Low[1]);
                                          Debug.Write(", C[1]:");
                                          Debug.WriteLine(Close[1]);
                                          Debug.WriteLine("");
                                      }
              
                              }
              Here is the output (I'm at CET +3:00):
              O[0]:2084,25, H[0]:2085,25, L[0]:2083,5, C[0]:2015-07-08 19:54:42:838 2085,25
              O[1]:2085,5, H[1]:2086, L[1]:2084,25, C[1]:2015-07-08 19:54:42:838 2084,25
              2015-07-08 19:55:21:046
              O[0]:2085,25, H[0]:2085,25, L[0]:2084,75, C[0]:2015-07-08 19:55:21:066 2084,75
              O[1]:2084,25, H[1]:2085,25, L[1]:2083,5, C[1]:2015-07-08 19:55:21:096 2085,25
              2015-07-08 19:55:40:846
              O[0]:2084,75, H[0]:2084,75, L[0]:2082,75, C[0]:2015-07-08 19:55:40:876 2082,75
              O[1]:2085,25, H[1]:2085,25, L[1]:2084,75, C[1]:2015-07-08 19:55:40:876 2084,75
              2015-07-08 19:55:45:446
              O[0]:2082,75, H[0]:2083,25, L[0]:2082,25, C[0]:2015-07-08 19:55:45:456 2083,25
              O[1]:2084,75, H[1]:2084,75, L[1]:2082,75, C[1]:2015-07-08 19:55:45:456 2082,75
              2015-07-08 19:55:47:006
              O[0]:2083,25, H[0]:2083,75, L[0]:2083,25, C[0]:2015-07-08 19:55:47:006 2083,75
              O[1]:2082,75, H[1]:2083,25, L[1]:2082,25, C[1]:2015-07-08 19:55:47:016 2083,25

              What do OHLC[0] really show?
              Last edited by quicktrick; 07-08-2015, 11:25 AM.

              Comment


                #8
                Hello quicktrick,

                Thank you for your inquiry.

                What Open[0], High[0], Low[0], and Close[0] are showing is the current bar's open, high, low, and close prices. What that 0 signifies is a barsAgo integer variable. barsAgo denotes how many bars from the current bar to check. A 0 means current bar, 1 means previous bar, 2 means second previous bar, etc.

                Please take a look at the NinjaTrader help guide at these links to understand the functionality of each of these:
                Open: http://ninjatrader.com/support/helpGuides/nt7/?open.htm
                High: http://ninjatrader.com/support/helpGuides/nt7/?high.htm
                Low: http://ninjatrader.com/support/helpGuides/nt7/?low.htm
                Close: http://ninjatrader.com/support/helpG...nt7/?close.htm

                Please, let us know if we may be of further assistance.
                Zachary G.NinjaTrader Customer Service

                Comment


                  #9
                  Hi, ZacharyG!

                  I hope you had read the beginning of the thread before you posted your answer. The question really was not about what OHLC and barsAgo mean. The question was about what exactly mean OHLC[0] when CalculateOnBarClose = false && FirstTickOfBar = true. And why they are different as shown in the printout I posted.

                  Thank you for your answer.

                  Comment


                    #10
                    Hello quicktrick,

                    Please read more about FirstTickOfBar here: http://ninjatrader.com/support/helpG...ttickofbar.htm
                    Please read more about CalculateOnBarClose here: http://ninjatrader.com/support/helpG...onbarclose.htm

                    You are getting different values because the strategy is checking every bar. Here's an example from your output:
                    Code:
                    O[0]:2082,75, H[0]:2083,25, L[0]:2082,25, C[0]:2015-07-08 19:55:45:456 2083,25
                    O[1]:2084,75, H[1]:2084,75, L[1]:2082,75, C[1]:2015-07-08 19:55:45:456 2082,75
                    
                    O[0]:2083,25, H[0]:2083,75, L[0]:2083,25, C[0]:2015-07-08 19:55:47:006 2083,75
                    O[1]:2082,75, H[1]:2083,25, L[1]:2082,25, C[1]:2015-07-08 19:55:47:016 2083,25
                    You will notice that the second section of OHLC[1] will match the first section of OHLC[0]. A strategy will be checking every historical bar before checking the current real-time bar. For each bar that the strategy runs through, your output code executes. This is why you have multiple OHLC values.

                    So, for the output that runs on the current, real-time bar (the last one), OHLC will be at a barsAgo of 0. You will see that the OHLC at a barsAgo of 1 matches the previous OHLC at a barsAgo of 0. When the strategy ran on that previous bar, the barsAgo value was at 0 since it was the current bar.

                    If you would rather the strategy not go through historical bars, you could wrap the entire code block you have given me with an "if (!Historical)" conditional.

                    Please read the "How Bar Data is Referenced" section at this link in the NinjaTrader help guide for more information and clarification about this: http://ninjatrader.com/support/helpG...nstruments.htm
                    Zachary G.NinjaTrader Customer Service

                    Comment


                      #11
                      ZacharyG,

                      Thank you very much for the detailed explanation! Now I can see where I was wrong.

                      Comment


                        #12
                        One more question, please.

                        Am I correct in the following?

                        Code:
                        protected override void Initialize()
                        {
                          CalculateOnBarClose = false;
                        }
                        
                        protected override void OnBarUpdate()
                        {
                          int lastClosedBarNumber;  //The number of the very last closed bar
                        
                          if (FirstTickOfBar)
                          {
                             lastClosedBarNumber = Historical ? CurrentBar : CurrentBar - 1;
                          }
                        }

                        Comment


                          #13
                          Hello quicktrick,

                          Yes, this syntax would be correct if you want to assign your lastClosedBarNumber integer to the CurrentBar value of the very last closed bar.
                          Zachary G.NinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by futtrader, Today, 01:16 PM
                          0 responses
                          4 views
                          0 likes
                          Last Post futtrader  
                          Started by Segwin, 05-07-2018, 02:15 PM
                          14 responses
                          1,789 views
                          0 likes
                          Last Post aligator  
                          Started by Jimmyk, 01-26-2018, 05:19 AM
                          6 responses
                          838 views
                          0 likes
                          Last Post emuns
                          by emuns
                           
                          Started by jxs_xrj, 01-12-2020, 09:49 AM
                          6 responses
                          3,294 views
                          1 like
                          Last Post jgualdronc  
                          Started by Touch-Ups, Today, 10:36 AM
                          0 responses
                          13 views
                          0 likes
                          Last Post Touch-Ups  
                          Working...
                          X