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

How to use OnBarUpdate() nad Highs[][] and Lows[][]

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

    How to use OnBarUpdate() nad Highs[][] and Lows[][]

    ok i tried using OnBarUpdate() To get historical Highs and Lows.
    Unfortunately i am getting junk values, furthermore, the OnBarUpdate() is getting called multiple times at the beginning of strategy . whereas it should be called only at 12:00, 12:15 and so on.

    Code:
     protected override void OnBarUpdate()
            {
    			if(GetOHLC == true && BarsInProgress == 1)
    			{
    				Print("Low of some 15 min bar ="+ Lows[1][3]);
    				Print("High of some 15 min bar ="+ Highs[1][3]);
    			}
            }
    Code:
      protected override void Initialize()
            {
                CalculateOnBarClose = true;
    			GetOHLC = true;
    			Add(PeriodType.Minute,15);
            }
    Code:
    output
    Low of some 15 min bar =103.24
    High of some 15 min bar =103.28
    Low of some 15 min bar =103.15
    High of some 15 min bar =103.27
    Low of some 15 min bar =103.15
    High of some 15 min bar =103.22
    Low of some 15 min bar =103.16
    High of some 15 min bar =103.22
    Low of some 15 min bar =103.16
    High of some 15 min bar =103.21
    Low of some 15 min bar =103.07
    High of some 15 min bar =103.22
    etc
    etc
    Any reason why this should be happening?

    So,
    a) why is OnBarUpdate() getting called at startup instead of at 1:15 pm or 1:30 pm?
    b). why are the Highs[][] and Lows[][] returning junk values.
    c) why are they printing different values. and why is checking for BarsInProgress == 0 returning the same output as BarsInProgress ==1

    Shouldnt, Add(PeriodType.Minute,15); be my primary Bars object with index of 0.
    why is it giving an output at all if i check for BarsInProgress ==1

    Edit: I read somewhere that Initialise() for all the scripts gets called, when you run one script. Is that being the issue here? so i need to delete all my scripts before running a previous one?
    I tried deleting all my previous scripts but that doesnt seem to make a difference
    Last edited by nemesis45; 04-23-2014, 03:43 AM.

    #2
    nemesis45, correct Initialize is run for all scripts, but that's not the issue here.

    The added type will be BIP == 1, everything is 0 based, so primary series you run from actually will always be BIP = 0.

    I see you changed the Highs / Lows index reference to 1, this would be correct and your 15 min series.

    Try adding in the Times as well to see what bars you actually print out here. Remember as you have it, it will be run for all historical values as well.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Ok i did that and output is
      Code:
      Low of some 15 min bar =103.24
      High of some 15 min bar =103.28
      Time is4/17/2014 8:00:00 AM
      Low of some 15 min bar =103.15
      High of some 15 min bar =103.27
      Time is4/17/2014 8:15:00 AM
      Low of some 15 min bar =103.15
      High of some 15 min bar =103.22
      Time is4/17/2014 8:30:00 AM
      Low of some 15 min bar =103.16
      High of some 15 min bar =103.22
      Time is4/17/2014 8:45:00 AM
      Low of some 15 min bar =103.16
      High of some 15 min bar =103.21
      Time is4/17/2014 9:00:00 AM
      The current time here is 3:15 Pm
      Thanks for the quick reply, but then what does BIP ==0 refer to?
      It seems to be producing the same output, so i am guessing same 15 min bars?

      Comment


        #4
        Ok, now correlate that to the chart you actually print from, your current time would be not relevant in this request.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Bertrand View Post
          Ok, now correlate that to the chart you actually print from, your current time would be not relevant in this request.
          Really?
          per se the data is correct so its not junk at least phew!

          shouldnt Highs[1][0] give me the high of the current bar?

          if i am using Highs[1][3] why is it going back to 5 days ago and printing the high of the bar at 8:00 AM.

          and then where is the loop coming from which is printing me all the values from there till today April 23,2014 current time.

          Also what does BIP == 0 refer to and why is it giving same o/p as BIP ==1,
          so any series I add , the first one will have BIP =0 AND BIP =1?

          Comment


            #6
            Yes, current time does not matter you process bar updates here. Which is done from leftmost bar (CurrentBar 0) to current most right bar. So the index you apply is relative to the currently processed bar in your OnBarUpdate call, it's all done sequentially. That's why I suggested printing the timestamp as well so you knew which bar you would need to actually compare to, you can include BarsInProgress in your print as well to understand better the exact sequence your code would run through. Historical and then going forward realtime.
            BertrandNinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_Bertrand View Post
              Yes, current time does not matter you process bar updates here. Which is done from leftmost bar (CurrentBar 0) to current most right bar. So the index you apply is relative to the currently processed bar in your OnBarUpdate call, it's all done sequentially. That's why I suggested printing the timestamp as well so you knew which bar you would need to actually compare to, you can include BarsInProgress in your print as well to understand better the exact sequence your code would run through. Historical and then going forward realtime.
              ok. They way i thought it worked was Highs[1][0] would simply give me the current bar high,and then i could go back 1 bar by using Highs[1][1]
              (This is what i actually want done btw, since in current case if my dataset is large it would uselessly process bars 100 days ago)
              so any way to do this simple? Currently it looks like i would have to compare the timestamp is the only way?

              What does BIP == 0 refer to in this case. please do clarify

              Thanks for clearing the confusion.

              Comment


                #8
                nemesis45, BIP 0 is always your primary series. You can just return out of processing for example anything for historical and just work with the first update you get realtime.



                Or you request index 0 data if the CurrentBar == Count -2 - this would be the last processed bar with CalculateOnBarClose = true.
                BertrandNinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_Bertrand View Post
                  nemesis45, BIP 0 is always your primary series. You can just return out of processing for example anything for historical and just work with the first update you get realtime.



                  Or you request index 0 data if the CurrentBar == Count -2 - this would be the last processed bar with CalculateOnBarClose = true.
                  Thanks both of these methods will work!

                  Could you please point out what is the primary series in this code? I am having trouble understanding this.
                  For eg.
                  If calculateonbarclose = false, then primary series is tick series and 1st secondary would be 15 min bars.
                  but if calculateonbarclose = true, then what is it assigning primary series title to?

                  Comment


                    #10
                    Sure, the CalculateOnBarClose would not change the primary in any way - it will just designate if your series will see updates for each tick or at their respective bar close.

                    Primary is what ever you use to start your script from. i.e. if you work from a 3 min chart and add your script in for testing > that 3 min is primary with the other series added in as per your script. Primary will always have the 0 bar index in your scripts bars array.
                    BertrandNinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_Bertrand View Post
                      Sure, the CalculateOnBarClose would not change the primary in any way - it will just designate if your series will see updates for each tick or at their respective bar close.

                      Primary is what ever you use to start your script from. i.e. if you work from a 3 min chart and add your script in for testing > that 3 min is primary with the other series added in as per your script. Primary will always have the 0 bar index in your scripts bars array.
                      oh ok. No confusion now!.
                      One last thing In your help guide, it mentions,

                      CurrentBar value is guaranteed to be <= Count - 1. This is because of the NinjaTrader multi-threaded architecture, the Count value can have additional bars as inflight ticks come in to the system.

                      It seems that CurrentBar is not always = Count -2? is Count robust? always returns No of bars? and Count -2 gives the current bar always? or it keeps on varying from Count-1 and Count -2?

                      Edit: I can understand -1, since count will also include the current live bar, why -2 :S
                      Last edited by nemesis45; 04-23-2014, 04:30 AM.

                      Comment


                        #12
                        nemesis45, CurrentBar will be 0 based, while Count is not. As you also have seen you will have to keep in mind CalculateOnBarClose when making the comparison for the last bar check.
                        BertrandNinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by NinjaTrader_Bertrand View Post
                          nemesis45, CurrentBar will be 0 based, while Count is not. As you also have seen you will have to keep in mind CalculateOnBarClose when making the comparison for the last bar check.
                          So,
                          CurrentBar == Count - 2 , -1 for the live bar, and another -1 for it being not 0 based, but starting from 1. ok, makes sense!

                          so,
                          if CalculateOnBarClose= true,
                          CurrentBar== Count - 2, check

                          and if it's false
                          CurrentBar == Count -1 check, since we are getting called for each incoming tick anyways, it doesnt wait for live bar to end.
                          Correct?

                          Comment


                            #14
                            nemesis45, you are correct in understanding.
                            BertrandNinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by kaywai, 09-01-2023, 08:44 PM
                            5 responses
                            601 views
                            0 likes
                            Last Post NinjaTrader_Jason  
                            Started by xiinteractive, 04-09-2024, 08:08 AM
                            6 responses
                            22 views
                            0 likes
                            Last Post xiinteractive  
                            Started by Pattontje, Yesterday, 02:10 PM
                            2 responses
                            16 views
                            0 likes
                            Last Post Pattontje  
                            Started by flybuzz, 04-21-2024, 04:07 PM
                            17 responses
                            230 views
                            0 likes
                            Last Post TradingLoss  
                            Started by agclub, 04-21-2024, 08:57 PM
                            3 responses
                            17 views
                            0 likes
                            Last Post TradingLoss  
                            Working...
                            X