Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

why close[1] doesn't work?

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

    why close[1] doesn't work?

    hi guys.

    i have downloaded historical data of an instrument, i'd like to create an indicator that stores into an array all Close values and then make some calculations. While testing it i realized that only Closes[0] works, other values than zero don't, i.e. plotting zeroth gives me the timeseries, plotting [1] gives me nothing.

    needless to say that i cannot access other values than the current one, as desired.

    moreover how do i find the number of bars, in other words maximum value that i can put in Close[]?

    #2
    Open up the OUTPUT WINDOW.... Do you see an error message?

    Control Center-> Tools->Output Window

    Is it this message?
    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.


    Try here:





    Originally posted by jim_feyn View Post
    hi guys.

    i have downloaded historical data of an instrument, i'd like to create an indicator that stores into an array all Close values and then make some calculations. While testing it i realized that only Closes[0] works, other values than zero don't, i.e. plotting zeroth gives me the timeseries, plotting [1] gives me nothing.

    needless to say that i cannot access other values than the current one, as desired.

    moreover how do i find the number of bars, in other words maximum value that i can put in Close[]?

    Comment


      #3
      You were right. Thank you.

      I need to fill an array with all price data until the current one. By writing in OnBarUpdate:

      data[CurrentBar]=Close[0]; I didn't have much luck.

      OnBarUpdate is like a loop that is constantly executing as new price data comes in? If so, I thought that the above code executes like:
      data[0]=Close[0];
      data[1]=Close[0];
      data[2]=Close[0];
      ........................

      hence it progressively populates my array as current values come. Or it initializes data[] each time a new value comes and past data[] is deleted?

      Comment


        #4
        Originally posted by jim_feyn View Post
        You were right. Thank you.

        I need to fill an array with all price data until the current one. By writing in OnBarUpdate:

        data[CurrentBar]=Close[0]; I didn't have much luck.

        OnBarUpdate is like a loop that is constantly executing as new price data comes in? If so, I thought that the above code executes like:
        data[0]=Close[0];
        data[1]=Close[0];
        data[2]=Close[0];
        ........................

        hence it progressively populates my array as current values come. Or it initializes data[] each time a new value comes and past data[] is deleted?
        Why do you want to fill an array? This is not necessary, as all the closes are already available through a DataSeries object. Using a structure such as data[] would only create a redundancy and increase both CPU and RAM load.

        When you apply the indicator to a chart, it can access all closes within the specified lookback period of the chart. Let us assume that your chart has 52360 bars. To find the number of bars on your chart you can use Bars.Count. With CalculateOnBarClose (COBC) = false, your indicator will access all bars until the last bar on your chart, which is CurrentBar = Bars.Count - 1 = 52359. With COBC = true your indicator will not access the last bar but stop with the second but the last bar on your chart, which is CurrentBar = Bars.Count -2 = 52358

        To access the last close (bar 52358 with COBC = true, or bar 52359 with COBC = false), you have two options

        - either Close [0]
        - or Bars.GetClose(CurrentBar)

        Accordingly

        - Close[CurrentBar]
        - or Bars.GetClose(0)

        allows you to access the close of the first bar on your chart.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        601 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        347 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        103 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        559 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        558 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X