Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Issues getting indicator values in multi timeframe strategy

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

    Issues getting indicator values in multi timeframe strategy

    Hello! I built a custom Ichimoku indicator and it was working fine in a single timeframe strategy.
    Then, I added a 5min dataseries to my strategy using
    AddDataSeries(BarsPeriodType.Minute, 5);
    and I got an error :
    Error on calling 'OnBarUpdate' method on bar 425: Index was outside the bounds of the array.

    for more context, I'll show you the Ichimoku code :
    Code:
            protected override void OnBarUpdate()
            {
                if (CurrentBar < senkouSpanBPeriod) return;
    
                double tenkanSen   = (Highs[0][HighestBar(High, tenkanPeriod)] + Lows[0][LowestBar(Low, tenkanPeriod)]) / 2;
                double kijunSen    = (Highs[0][HighestBar(High, kijunPeriod)] + Lows[0][LowestBar(Low, kijunPeriod)]) / 2;
                double senkouSpanA = (tenkanSen + kijunSen) / 2;
                double senkouSpanB = (Highs[0][HighestBar(High, senkouSpanBPeriod)] + Lows[0][LowestBar(Low, senkouSpanBPeriod)]) / 2;
              
                TenkanSen[0] = tenkanSen;
                KijunSen[0] = kijunSen;
                SenkouSpanA[0] = senkouSpanA;
                SenkouSpanB[0] = senkouSpanB;
            
                // Use DrawRegion to fill the area between Senkou Span A and Senkou Span B
                // The region will be filled between SSA and SSB on the chart
                string regionID = "KumoCloud" + CurrentBar;
                Brush outlineColor = new SolidColorBrush(Color.FromArgb(70, 78, 45, 13));
                Brush areaColor = new SolidColorBrush(Color.FromArgb(30, 54, 137, 164));
                Draw.Region(this, regionID, CurrentBar, 0, SenkouSpanA, SenkouSpanB, outlineColor, areaColor, 100, 26);
            }
    
            #region Properties
            [Browsable(false)]
            [XmlIgnore()]
            public Series<double> TenkanSen
            {
                get { return Values[0]; }
            }
            [Browsable(false)]
            [XmlIgnore()]
            public Series<double> KijunSen
            {
                get { return Values[1]; }
            }
            [Browsable(false)]
            [XmlIgnore()]
            public Series<double> SenkouSpanA
            {
                get { return Values[2]; }
            }
            [Browsable(false)]
            [XmlIgnore()]
            public Series<double> SenkouSpanB
            {
                get { return Values[3]; }
            }​

    ​​
    Last edited by Tessan; 03-05-2025, 07:00 AM.

    #2
    Hello Tessan,

    Have you tried commenting out lines to see which specific line is having a problem? It could potentially be the lines with LowestBar. You have multiple period variables so you will need multiple current bar checks like this:


    if (CurrentBar < senkouSpanBPeriod) return;
    if (CurrentBar < tenkanPeriod) return;
    ​if (CurrentBar < kijunPeriod) return;

    Comment


      #3
      senkouSpanBPeriod is the greatest period no matter what so I don't think adding the two other lines you propose would make a difference unfortunatly.
      I made a weird discovery trying to debug the code. Here is a part of my strategy that use my ichimoku indicator :
      Code:
      protected override void OnBarUpdate()         {        
                  if (CurrentBars[0] < BarsRequiredToTrade || CurrentBars[1] < BarsRequiredToTrade )
                  {
                      Print("Not enough bars in " + instruments[0] + " 1min or " + instruments[0] + " 5 min to run Strategy");
                  }
                  else
                  {
                      if (BarsInProgress != 0) return;
      
                      if (Positions[0].MarketPosition == MarketPosition.Flat)
                      {
      
                          //Print("Trading allowed: position flat + correct hours");
                          if (allowedTradingHoursStart <= DateTime.Now.Hour)
                          {
                              if (CheckIfCanEnterLongPosition())
                              {
                                  SendOrderAndDrawLines(MarketPosition.Long);
                              }
                              else if (CheckIfCanEnterShortPosition())
                              {
                                  SendOrderAndDrawLines(MarketPosition.Short);
                              }
                              if (DateTime.Now.Minute != lastMinute && DateTime.Now.Second > 2)
                              {
                                  LogFunction();
                                  lastMinute = DateTime.Now.Minute;
                              }
                          }
                      }​
       }
      
      private bool CheckIfCanEnterLongPosition()
      {
      bool1L = price_crossed_above(ichimoku1m.KijunSen[0]);
      bool2L = Closes[0][0] > ichimoku1m.TenkanSen[0];
      bool3L = ichimoku1m.TenkanSen[1] <= ichimoku1m.TenkanSen[0];
      bool4L = VerifyDistanceFromPivotLevel(MarketPosition.Long);
      bool5L = Closes[0][0] > ichimoku1m.SenkouSpanA[26];
      bool6L = Closes[0][0] > ichimoku1m.SenkouSpanB[26];
      bool7L = Closes[1][0] > ichimoku5m.SenkouSpanA[26];
      bool8L = Closes[1][0] > ichimoku5m.SenkouSpanB[26];
      
      if (bool1L && bool2L && bool3L && bool4L && bool5L && bool6L && bool7L && bool8L)
      {
      return true;
      }
      return false;


      When I comment following lines which use the indicator on 5min timeframe :
      bool7L = Closes[1][0] > ichimoku5m.SenkouSpanA[26];
      bool8L = Closes[1][0] > ichimoku5m.SenkouSpanB[26];
      I'm able to run the strategy without errors. However, this is strange because in my "LogFunction();" I'm also using the value of ichimoku5m like this :

      string res2 = string.Format("close[1][0]: {0}, SSA 5min: {1}, SSB 5min: {2}",
      Closes[1][0], ichimoku5m.SenkouSpanA[26], ichimoku5m.SenkouSpanB[26]);

      Print(res1);

      Comment


        #4
        Hello Tessan,

        The only data from that information would be 26 bars are required on the series being used ,if that exceeds the period you need to use that instead. otherwise you have to make sure BarsInProgress 1 has 26 bars or that the series for ichimoku5m has 26 bars.

        Comment


          #5
          Yes but I'm already checking (CurrentBars[0] < BarsRequiredToTrade || CurrentBars[1] < BarsRequiredToTrade) at the beging of onBarUpdate() where BarsRequiredToTrade is set to 52+26.

          Comment


            #6
            Hello again, I think I solved the problem by myself, It was actually a array that I created and never populated but used in a function. Nothing related to the indicator.
            Thank you anyway for your assistance Jesse!

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            547 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            323 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            99 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            543 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            545 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X