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 kevinenergy, 02-17-2023, 12:42 PM
                117 responses
                2,766 views
                1 like
                Last Post jculp
                by jculp
                 
                Started by Mongo, Today, 11:05 AM
                5 responses
                15 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by SightCareAubetter, Today, 12:55 PM
                0 responses
                3 views
                0 likes
                Last Post SightCareAubetter  
                Started by traderqz, Today, 12:06 AM
                8 responses
                16 views
                0 likes
                Last Post traderqz  
                Started by SightCareAubetter, Today, 12:50 PM
                0 responses
                2 views
                0 likes
                Last Post SightCareAubetter  
                Working...
                X