Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Error on calling 'OnBarUpdate' method for indicator 'T2' on bar 100: Индекс находился

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

    Error on calling 'OnBarUpdate' method for indicator 'T2' on bar 100: Индекс находился

    Hello, I just wrote my first indicator and when you try to run in a window of the chart nothing appears. In the log appears the following error Error on calling 'OnBarUpdate' method for indicator 'T2' on bar 100:

    sorry for my English

    here is the script

    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Gui.Chart;
    #endregion

    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
    /// <summary>
    /// Enter the description of your new custom indicator here
    /// </summary>
    [Description("Enter the description of your new custom indicator here")]
    public class T2 : Indicator
    {
    #region Variables
    // Wizard generated variables
    private string second_instr = @"6E 09-15"; // Default setting for Second_instr
    private int periodSMA = 20; // Default setting for PeriodSMA
    private int BP = 100;
    // User defined variables (add any user defined variables below)
    #endregion

    /// <summary>
    /// This method is used to configure the indicator and is called once before any bar data is loaded.
    /// </summary>
    protected override void Initialize()
    {
    Add(Second_instr,BarsPeriod.Id, BarsPeriod.Value);
    Add(new Plot(Color.FromKnownColor(KnownColor.Red), PlotStyle.Line, "Posa"));
    Add(new Plot(Color.FromKnownColor(KnownColor.Blue), PlotStyle.Line, "Posb"));


    Overlay = false;
    CalculateOnBarClose = false;
    BarsRequired = periodSMA;

    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Use this method for calculating your indicator values. Assign a value to each
    if (BarsInProgress != 0) return;
    if(Closes[0][0]<=0 || Closes[1][0]<=0) return;
    if(CurrentBar < 100) return;


    Vola.Set(Math.Sign((Highs[0][0]-Lows[0][0])-1)*100);
    Volb.Set(Math.Sign((Highs[1][0]-Lows[1][0])-1)*100);
    Moving_Average.Set(SMA(Vola,periodSMA)[0]);
    Moving_Average.Set(SMA(Volb,periodSMA)[0]);
    Posa.Set(BP/SMA(Vola,periodSMA)[0]/Closes[0][0]);
    Posb.Set(BP/SMA(Volb,periodSMA)[0]/Closes[1][0]);
    // plot below by replacing 'Close[0]' with your own formula.
    }

    #region Properties
    [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
    [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
    public DataSeries Vola
    {
    get { return Values[0]; }
    }

    [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
    [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
    public DataSeries Volb
    {
    get { return Values[1]; }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public string Second_instr
    {
    get { return second_instr; }
    set { second_instr = value; }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public int PeriodSMA
    {
    get { return periodSMA; }
    set { periodSMA = Math.Max(1, value); }
    }

    [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
    [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
    public DataSeries Moving_Average
    {
    get { return Values[2]; }
    }

    [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
    [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
    public DataSeries Posa
    {
    get { return Values[3]; }
    }

    [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
    [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
    public DataSeries Posb
    {
    get { return Values[4]; }
    }


    #endregion
    }
    }

    #2
    Hello Papercut110,

    Thanks for your post and welcome to the forums!

    Can you clarify what you are trying to do as it looks like you are loading an obsolete contract @"6E 09-15"
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hello NinjaTrader_Paul, this is the default value, there I enter ES 03-16

      Comment


        #4
        Hello Papercut110,

        Thanks for your reply.

        To debug your code you will want to use a prolific amount of "Print" statements so that you can determine the line that is generating the run time error and then investigate from there. http://ninjatrader.com/support/helpG...nt7/?print.htm

        I would recommend using a print statement before and after each of the .Set lines to find where the error is and then add more info to each print statement where you actually output the values.

        I suspect the issue may be that you are returning via If (Current<100) return without "setting" the plots so when you try to get the SMA of those plots the full dataseries is not there. If you see that is the issue then you might change your code to something like:

        if (CurrentBar<100)
        {
        Vola.Set(0); // set to zero for now
        Volb.Set(0); // set to zero for now
        //etc
        //etc
        return;
        }

        Here is a guide to debugging: http://ninjatrader.com/support/forum...ead.php?t=3418 Please be sure to read the NinjaTrader 7 section.
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          NinjaTrader_Paul, thank you very much, I am self-taught, so I will understand

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Brevo, Today, 01:45 AM
          0 responses
          3 views
          0 likes
          Last Post Brevo
          by Brevo
           
          Started by aussugardefender, Today, 01:07 AM
          0 responses
          3 views
          0 likes
          Last Post aussugardefender  
          Started by pvincent, 06-23-2022, 12:53 PM
          14 responses
          239 views
          0 likes
          Last Post Nyman
          by Nyman
           
          Started by TraderG23, 12-08-2023, 07:56 AM
          9 responses
          384 views
          1 like
          Last Post Gavini
          by Gavini
           
          Started by oviejo, Today, 12:28 AM
          0 responses
          6 views
          0 likes
          Last Post oviejo
          by oviejo
           
          Working...
          X