Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Problem with indicator

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

    Problem with indicator

    Hello,

    First time posting here.

    I'm trying to make an indicator to calculate some levels from previous bars.

    With the code :

    Code:
     else if (State == State.Configure)
                {
                    BarsPeriodType bpt;
                    if (Enum.TryParse(timeFrame1.ToString(), out bpt))
                    {
                        AddDataSeries(bpt, tfValue);
                        if (showOutput) Print("Added time series: " + bpt + " value: " + tfValue);
                    }
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (CurrentBars[0] < BarsRequiredToPlot || CurrentBars[1] < BarsRequiredToPlot)
                    return;
    
                if (BarsInProgress == 0)
                {
    
                    int b = (State == State.Realtime) ? 1 : 0;
    
    
                    MID[0] = (Highs[1][b] + Lows[1][b])/ 2;​
    I got this indicator to plot on chart.

    But when i try to add more levels to plot, code compiles but doesn't plot anything on the chart:

    Code:
    else if (State == State.Configure)
                {
                    BarsPeriodType bpt;
                    if (Enum.TryParse(timeFrame1.ToString(), out bpt))
                    {
                        AddDataSeries(bpt, tfValue);
                        if (showOutput) Print("Added time series: " + bpt + " value: " + tfValue);
                    }
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (CurrentBars[0] < BarsRequiredToPlot || CurrentBars[1] < BarsRequiredToPlot || CurrentBars[2] < BarsRequiredToPlot)
                    return;
    
                if (BarsInProgress == 0)
                {
    
                    int b = (State == State.Realtime) ? 1 : 0;
    
    
                    MID[0] = (Highs[1][b] + Lows[1][b])/ 2;
                    MID2[0] = (Highs[2][b] + Lows[2][b])/2;
                    MID3[0] = (MID[0] + MID2[0])/2;​
    I would appreciate if somebody would help/navigate where i made mistake.

    Thank you.

    #2
    Hello badbess,

    Welcome to the NinjaTrader forums!

    We'd want to confirm a few things first.

    Are these plots being set that are added with AddPlot() in State.SetDefaults?

    Are MID, MID2, MID3 public series that are returning Values[0], Values[1], Values[2]?

    Below is a link to a forum post with details.
    https://ninjatrader.com/support/foru...er#post1182264

    If you add a print one line above those assignments of the bar time, are you seeing the prints appearing in the output window?

    Below is a link to a forum post on using prints to debug and understand behavior.
    https://ninjatrader.com/support/foru...121#post791121

    Are there errors appearing on the Log tab of the Control Center?

    If you print those calculated values, do you see the correct values appearing in the output window?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello,

      Thank you for tips. I had plots and public series, i have added Print function.

      I got current error on output;

      With code:

      Code:
      if (CurrentBars[0] < BarsRequiredToPlot || CurrentBars[2] < BarsRequiredToPlot)
                      return;​
      Error : Error on calling 'OnBarUpdate' method on bar 20: Index was outside the bounds of the array.

      or

      Code:
      if (CurrentBars[0] < BarsRequiredToPlot || CurrentBars[1] < BarsRequiredToPlot || CurrentBars[2] < BarsRequiredToPlot)
                      return;​
      Error : Error on calling 'OnBarUpdate' method on bar 2077: Index was outside the bounds of the array

      Still error persists.

      The indicator only works with MID[0] = (Highs[1][b] + Lows[1][b])/ 2; and the following
      Code:
      if (CurrentBars[0] < BarsRequiredToPlot || CurrentBars[1] < BarsRequiredToPlot)
                      return;​
      If im referring to [2] bars ago it gets above error mentioned.​

      Thank you.

      Comment


        #4
        Hello badbess,

        CurrentBars[2] would be the 3rd data series.

        The primary chart bars is CurrentBars[0]. The first added series with AddDataSeries() would be CurrentBars[1]. An additional added series would be CurrentBars[2].

        May I confirm there are 2 series added with AddDataSeries()?

        As a heads up, adding series dynamically is not supported.

        Where you have:
        Code:
        if (Enum.TryParse(timeFrame1.ToString(), out bpt))
        {
        AddDataSeries(bpt, tfValue);​
        This would be dynamically adding the series with a condition, which NinjaTrader does not support.


        What is MID?
        Is this an array? Is this a List? Is this a Series? Is this Values[0]?

        If this is a plot, is this being set in BarsInProgress 0?
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

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