Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

BarsArray

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

    BarsArray

    The following code causes an exception when trying to access the first bar on the chart:

    Print("Number of bars is : " + BarsArray[0].Count.ToString());
    Print("Number of time elements is : " + Times[0].Count.ToString());

    try{

    Print("The first bar on the chart is: " + Times[0][Times[0].Count-1].ToString());

    }
    catch(Exception ex)
    {
    Print("Error: " + ex.Message);
    }

    Sample output is:

    Number of bars is : 14479
    Number of time elements is : 14479
    Error: Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index



    #2
    If you want the first bar of the chart just do Times[0][CurrentBar].
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      loop

      I would actually also like to loop through all the bars as in the following example where I want to do a calculation based on bars in a certain date range:

      //calculate highest high in period Start Time to End Time
      for(int i = 0;i<Times[0].Count;i++)
      {
      if(Time[i] >= dtStart && Time[i] <= dtEnd)
      {
      Print(
      "The current bar time is : " + Time[i].ToString());
      }

      }

      But this causes an exception as before:

      Error: Index was out of range. Must be non-negative and less than the size of the collection.

      Comment


        #4
        Don't loop with .Count. Loop with CurrentBar.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          DateTime

          Thanks CurrentBar has worked.

          In the for loop below we are comparing each bar to see if it falls within a certain time range but it doesnt seem to be working properly as
          bstart is always true and bend is always false even though this is clearly not the case from looking at each bar time.

          for(int i = 0;i<CurrentBar;i++)
          {
          Print(
          "The current bar time is : " + Times[0][i].ToString());
          DateTime dtCurrent = DateTime.Parse(Times[
          0][i].ToString());
          bool bstart = dtCurrent >= dtStart;
          Print(
          "The current bar time > dtStart : " + bstart.ToString());
          bool bend = dtCurrent <= dtEnd;
          Print(
          "The current bar time < dtEnd : " + bend.ToString());

          // if(Times[0][i] >= dtStart && Times[0][i] <= dtEnd)
          // {
          // Print("The current bar time is : " + Times[0][i].ToString());
          // }
          }

          Comment


            #6
            You'll need to debug your variables. Print out what everything is at every step of the way.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              type conversion

              Is there some type conversion that has to be done if comparing
              Times[
              0][i] to a DateTime variable?

              Comment


                #8
                Shouldn't be needed. They should be DateTimes.
                Josh P.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                106 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                54 views
                0 likes
                Last Post NabilKhattabi  
                Started by Deep42, 03-06-2026, 12:28 AM
                0 responses
                36 views
                0 likes
                Last Post Deep42
                by Deep42
                 
                Started by TheRealMorford, 03-05-2026, 06:15 PM
                0 responses
                38 views
                0 likes
                Last Post TheRealMorford  
                Started by Mindset, 02-28-2026, 06:16 AM
                0 responses
                74 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Working...
                X