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 DannyP96, 05-18-2026, 02:38 PM
                1 response
                27 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by CarlTrading, 05-11-2026, 05:56 AM
                0 responses
                117 views
                0 likes
                Last Post CarlTrading  
                Started by CarlTrading, 05-10-2026, 08:12 PM
                0 responses
                69 views
                0 likes
                Last Post CarlTrading  
                Started by Hwop38, 05-04-2026, 07:02 PM
                0 responses
                226 views
                0 likes
                Last Post Hwop38
                by Hwop38
                 
                Started by CaptainJack, 04-24-2026, 11:07 PM
                0 responses
                415 views
                0 likes
                Last Post CaptainJack  
                Working...
                X