Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Making Plots work properly

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

    Making Plots work properly

    I am struggling to get plots to stop giving me this error:
    Strategy 'MovingAvgTest': Error on calling 'OnBarUpdate' method on bar 0: 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.

    I can get the plot lines to output properly over historical data, but once I enable the strategy, the plot lines quickly converge and overlap each other even if the settings are a 21 MA and a 200 MA. I am not sure what I'm missing. I've done a ton of reading on the "Values" command and the AddPlot command, but can't seem to make any of them talk to each other and output properly.

    This is the condensed code I have
    Code:
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class MovingAvgTest: Strategy
    {
    private double smma1;
    private double smma2;
    private double smma3;
    private double smma4;
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"N/A";
    Name = "MovingAveragesTest";
    Calculate = Calculate.OnEachTick;
    AddPlot(Brushes.White, "21 SMMA");
    AddPlot(Brushes.Lime, "50 SMMA");
    AddPlot(Brushes.Yellow, "100 SMMA");
    AddPlot(Brushes.Red, "200 SMMA");
    }
    else if (State == State.Configure)
    {
    //AddDataSeries(Data.BarsPeriodType.Minute, 1);
    }
    else if (State == State.DataLoaded)
    {
    //PlaceHolder
    }
    }
    
    protected override void OnBarUpdate()
    {
    double len1 = 21;
    double len2 = 50;
    double len3 = 100;
    double len4 = 200;
    
    smma1 = CalculateSMMA(Closes[0], len1, smma1);
    smma2 = CalculateSMMA(Closes[0], len2, smma2);
    smma3 = CalculateSMMA(Closes[0], len3, smma3);
    smma4 = CalculateSMMA(Closes[0], len4, smma4);
    
    Values[0][0] = smma1;
    Values[1][0] = smma2;
    Values[2][0] = smma3;
    Values[3][0] = smma4;
    
    }
    
    private double CalculateSMMA(PriceSeries source, double length, double previousValue)
    {
    double smma = (previousValue * (length - 1) + source[0]) / length;
    return smma;
    }
    
    [HASHTAG="t3322"]region[/HASHTAG] Properties
    [NinjaScriptProperty]
    public bool TrendFill { get; set; } = true;
    #endregion
    }
    }​
    What am I missing? It doesn't make sense to me that "Values" guesses that it's referring to smma1 and "AddPlot(brushes.white..."

    As I understand: Values[1][0] refers to the Values of Data set 1, 0 bars ago. However if I try to put in something like Highs[1][0], it gives me the error I stated up at the beginning. I'm stumped as to how to get this program to understand what it's doing.

    #2
    Hello alphatango,

    I'm seeing you are using source[0] in the calculations of CalculateSMMA, but I am not seeing that this collection variable has been defined and I am not seeing this has been assigned a value.
    Is this meant to be a custom Series<double> object defined in the scope of the class, instantiated in State.DataLoaded, and assigned a value for the current bar in OnBarUpdate()?


    Are you trying to call the SMA indicator and plot the value?

    In the scope of the class:
    private SMA sma1;

    In State.DataLoaded:
    sma1 = SMA(len1);

    In OnBarUpdate():
    Values[0][0] = sma1[0];


    Chelsea B.NinjaTrader Customer Service

    Comment


      #3

      I'm not using strictly the SMA, the "smma" is the Smoothed Moving Average, so it requires a little bit of math.

      Originally posted by NinjaTrader_ChelseaB View Post
      Hello alphatango,

      I'm seeing you are using source[0] in the calculations of CalculateSMMA, but I am not seeing that this collection variable has been defined and I am not seeing this has been assigned a value.
      Is this meant to be a custom Series<double> object defined in the scope of the class, instantiated in State.DataLoaded, and assigned a value for the current bar in OnBarUpdate()?
      source[0] is supposed to be referring to the prior value of smma1

      relevant coding
      -------
      smma1 = CalculateSMMA(Closes[0], len1, smma1);

      [...]


      private double CalculateSMMA(PriceSeries source, double length, double previousValue)
      {
      double smma = (previousValue * (length - 1) + source[0]) / length;
      return smma;
      }
      ​---------

      ​This might be where my coding error is. Source[0] is not collecting values like it should be. If I did my coding right, source[0] should be Closes[0] passed down from the function call.

      Appending thoughts:
      I imagine Closes[0] could simply be Close[0].
      Last edited by alphatango; 12-13-2023, 10:38 AM.

      Comment


        #4
        Hello alphatango,

        I see, the source variable is from the CalculateSMMA parameters. Apologies, I overlooked this.

        This is currently being supplied Closes[0], which would be the primary data series and chart bars close (and not the previous value of the indicator).


        However, I've tested your code and I am not seeing any error.

        Attached is a screenshot.
        Click image for larger version

Name:	2023-12-13_10-02-30.png
Views:	158
Size:	139.1 KB
ID:	1282085

        Is there a specific way I need to run this to get the error?

        I would advise to add CurrentBar checks in the code, which might solve the behavior.​



        If you want the previous value of smma1, smma2, etc, you could use a Series<double> object instead of a double.
        In a series, you can access the smma value as it was on each bar, using a barsAgo index.
        https://ninjatrader.com/support/help...t8/seriest.htm

        private Series<double> smma1;

        In State.DataLoaded:
        smma1 = new Series<double>(this);

        In OnBarUpdate():
        smma1[0] = CalculateSMMA(Closes[0], len1, smma1[0]);


        Or you could just make smma1 a public Series<double> that returns the plot series Values[plotSeriesIndex].

        [XmlIgnore()]
        [Browsable(false)]
        public Series<double> Smma1
        { get { return Values[0]; } }​

        In OnBarUpdate() (or any method called from OnBarUpdate()):

        if (CurrentBar < 1)
        return;

        smma1 = CalculateSMMA(Closes[0], len1, smma1[1]);



        I attached a version of the script that also makes the plots publicly accessible to the Strategy Builder and Market Analzyer.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          so "source[0]" is likely the wrong (for lack of better words) "container" for capturing the Closes[0]. What syntax am I looking for to pass "closes[0]" into the function?

          Closes[0] should be PriceSeries source. I may have misunderstood that though.
          In the previous posts, the call is here:
          relevant coding
          -------
          smma1 = CalculateSMMA(Closes[0], len1, smma1);

          [...]


          private double CalculateSMMA(PriceSeries source, double length, double previousValue)
          {
          double smma = (previousValue * (length - 1) + source[0]) / length;
          return smma;
          }
          ​---------
          Last edited by alphatango; 12-13-2023, 11:22 AM.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Yesterday, 05:17 AM
          0 responses
          66 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          141 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          76 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          47 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          51 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X