Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error on calling 'OnBarUpdate' method on bar 1

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

    Error on calling 'OnBarUpdate' method on bar 1

    Hello everyone, i have a problem whith my code.

    I want to keep an eye on the candle, the number of which I indicate in the input "CountDirCandle". When I choose to trade in real time, I don't have any errors. But when I use the Strategy Analyzer and the parameter CountDirCandle > 1 I get an error "Strategy 'NewCDC': Error on calling 'OnBarUpdate' method on bar 1: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart."

    What check did I miss or what am I doing wrong? Here is my code below:

    Code:
                int CountDirCandle = 2;
    
                if (CurrentBars[0] < 1)
                    return;
                
                
                if((ToTime(Time[0]) >= TradingStart && ToTime(Time[0]) < TradingEnd ))
                {
                     // Set 1
                    if ((Buy == true)
                         && (Position.MarketPosition == MarketPosition.Flat)
                         && (Open[CountDirCandle] < Close[CountDirCandle]))
                    {
                        EnterLong(Convert.ToInt32(Lots), "Long");
                    }
                
                     // Set 2
                    if ((Sell == true)
                        && (Position.MarketPosition == MarketPosition.Flat)
                         && (Open[CountDirCandle] > Close[CountDirCandle]))
                    {
                        EnterShort(Convert.ToInt32(Lots), "Short");
                    }
                }​

    #2
    Try this,

    Code:
    if (CurrentBars[0] < Math.Max(1, CountDirCandle))
        return;​

    Comment


      #3
      You need to change it to
      if (CurrentBars[0] < 1 || CurrentBars[0] < CountDirCandle)
      return;

      What is happening is, on the very first bar, it is trying to Check for Open[2] which is the 2 candles prior. That does not exist yet causing the error. You need to wait, not only one bar, but the appropriate amount of bars depending on what CountDirCandle is set to. You can just use the variable to check for this, so you can get away without having to change it when hard coding it. So if you decided to change it to 10, you don't need to do anything else. It will return until you get to the appropriate bar number.

      Comment


        #4
        Good reading here.

        Comment


          #5

          Thank you all very much, it helped​

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Today, 05:17 AM
          0 responses
          48 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          126 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          66 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          42 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          46 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X