Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Can't access yesterday close price

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

    Can't access yesterday close price

    Hi,

    I'm running a strategy on 5 min data.
    I'm trying to compare yesterday close price to the current price.

    I've added Add(PeriodType.Day, 1); to the Initialize method.
    But when I'm trying to access Closes[1][1], I get the following error:

    Error on calling 'OnBarUpdate' method for strategy 'BelowAbpveBB/e7d3f0df74b048b885437abed2ab16fb': You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

    #2
    Hello,

    Make sure the bars you are checking from exist in order to make the calculation in OnBarUpdate:

    Code:
    protected override void OnBarUpdate()
    {
    if(CurrentBars[0] < 1 && CurrentBars[1] < 1)
    return;
    
        // Script logic calculation code...
    }
    MatthewNinjaTrader Product Management

    Comment


      #3
      I've added this check and I don't get any error.
      The problem is that I always enter the if statement - what may be the reason that I don't have enough bars ?

      Comment


        #4
        OnBarUpdate starts on the first bar on the chart, which is current bar 0.

        If you ask it to look at Closes[1][1], the first bar it runs on will not have bar before it to access, and it will throw the error you reported.

        By adding this statement, it will prevent the script from running until there a bar object to check and then do your calculations from there.
        MatthewNinjaTrader Product Management

        Comment


          #5
          Ok...
          So, can you explain me how can I know yesterday high (on daily chart) when I'm running the strategy on 5 min data ?

          10x,

          Comment


            #6
            You can use Highs[1][1] to get the prior day high

            Code:
            			protected override void Initialize()
            			{
            				Add(PeriodType.Day, 1);
            	
            			}
            
            			protected override void OnBarUpdate()
            			{
            						
            
             			  if (CurrentBars[0] < 1 || CurrentBars[1] < 1)
                			    return;
            				
            				double yesterdayHigh = Highs[1][1];
            	
            				Print(yesterdayHigh);
            
            			}
            MatthewNinjaTrader Product Management

            Comment


              #7
              I did:
              if ((CurrentBar < 2) || (CurrentBars[1] < 2))
              {
              Log("not enough bars", LogLevel.Information);
              return;
              }

              The problem is that I never passed this check in order to get Highs[1][1];
              why ?

              10x !

              Comment


                #8
                How many "Days to load" do you have set on the strategy? You can print these values at the top of OnBarUpdate to see what is loaded:

                Code:
                	Print("CurrentBars[0]: " + CurrentBars[0]);
                	Print("CurrentBars[1]: " + CurrentBars[1]);
                I suspect you'll find that the CurrentBars[1] is 0.

                If this is a strategy, try lowering the min. bars required, while increase the Days to Load.
                MatthewNinjaTrader Product Management

                Comment


                  #9
                  You are right...the CurrentBars[1] is 0, but I don't understand why.
                  The dayToLoad is 5, isn't it enough while I'm trying to get only yesterday price ?

                  Comment


                    #10
                    5 days may not be enough due to the weekend, missing bars for holiday, etc.

                    It'll also depend on the number of bars that are required. Are you able to load by increasing the days to load?
                    MatthewNinjaTrader Product Management

                    Comment


                      #11
                      I increased the days to load to 20 or 30 and I still get 0 for CurrentBars[1].
                      why ?

                      Comment


                        #12
                        Is this a strategy? If so, set the min. bars required to 1 and test again.
                        MatthewNinjaTrader Product Management

                        Comment


                          #13
                          Thanks !!
                          It works ! (god knows why )

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by geddyisodin, Today, 05:20 AM
                          1 response
                          11 views
                          0 likes
                          Last Post NinjaTrader_Gaby  
                          Started by Max238, Today, 01:28 AM
                          3 responses
                          32 views
                          0 likes
                          Last Post Max238
                          by Max238
                           
                          Started by timko, Today, 06:45 AM
                          2 responses
                          13 views
                          0 likes
                          Last Post NinjaTrader_ChristopherJ  
                          Started by habeebft, Today, 07:27 AM
                          0 responses
                          6 views
                          0 likes
                          Last Post habeebft  
                          Started by Tim-c, Today, 03:54 AM
                          1 response
                          8 views
                          0 likes
                          Last Post NinjaTrader_BrandonH  
                          Working...
                          X