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

List Problem

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

    List Problem

    Hi,

    I have a problem with a script. After couple hours trying identifying the problem, I finaly found it come from my List-type variable.

    Each time I run the code, I get the following error message :
    "Error on calling 'OnBarUpdate' method for indicator 'KeoPivots' on bar 56: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart."

    Here is the code :

    #region Variables
    // Wizard generated variables
    private int period = 20; // Default setting for Period
    private int rcoeff = 5; // Default setting for Rcoeff
    private double rerror = 0.7; // Default setting for Rerror

    // User defined variables (add any user defined variables below)
    private DataSeries Cvalue;
    private DataSeries Hvalue;
    private DataSeries Lvalue;
    private DataSeries Range;
    private double Suplimit = 0.0 ;
    private double Inflimit = 0.0 ;
    private double Delta =0.0;
    private double Delta2 =0.0;
    private double lastCvalue =0.0;
    private double diff = 0.0;
    private int Index;
    public struct Paliers
    {
    public int barNumber;
    public double Lprice;
    public double Cprice;
    public double Hprice;
    public int Compteur;

    public Paliers(int PbarNumber,double PLprice,double PCprice,double PHprice,int PCompteur)
    {
    barNumber=PbarNumber;
    Lprice=PLprice;
    Cprice=PCprice;
    Hprice=PHprice;
    Compteur=PCompteur;
    }
    }
    private List<Paliers> xx = new List<Paliers>();

    #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(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
    CalculateOnBarClose = true;
    Overlay = true;
    Cvalue = new DataSeries (this);
    Lvalue = new DataSeries (this);
    Hvalue = new DataSeries (this);
    Range = new DataSeries (this);

    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {

    if (CurrentBar==0)
    {
    if (xx.Count==1)
    xx.RemoveAt(0);
    }

    if (CurrentBar < period)
    return;
    else
    {

    Cvalue.Set(SMA(Close,period)[0]);

    Hvalue.Set(SMA(Close,period)[0]);
    Lvalue.Set(SMA(Close,period)[0]);
    Range.Set(Hvalue[0]-Lvalue[0]);
    Delta = Range[0]*0.3*rcoeff;

    if (CrossAbove(High,Cvalue,1) || CrossBelow(Low,Cvalue,1) || (High[0]>=Cvalue[0] && Low[0]<=Cvalue[0]))
    {
    Paliers Pivot = new Paliers(CurrentBar,Lvalue[0],Cvalue[0],Hvalue[0],0);
    lastCvalue=Cvalue[0];
    xx.Add(Pivot);
    Index=xx.Count;
    // Print(Index);
    // Print(xx[Index-1].Compteur);
    DrawDot("Pivot"+CurrentBar,false,0,Cvalue[0],Color.Gold);
    }

    Suplimit = lastCvalue + Delta ;
    Inflimit = lastCvalue - Delta ;
    Delta2 = rerror*Range[0] ;



    if ((CrossAbove(Close,Suplimit,1) || CrossBelow(Close,Inflimit,1)) && Index>2)
    {
    for (int i=0 ; i<Index ;i++)
    {
    diff = lastCvalue-xx[i].Cprice ;
    if(Math.Abs(diff) <= Delta2)
    {
    xx.RemoveAt(i);
    DrawDot("PivotRemove"+CurrentBar,false,0,xx[i].Cprice,Color.Magenta);
    }
    }
    }
    }
    }


    May someone help ?

    #2
    Hello baba123,

    Thank you for your post.

    Unfortunately this is more general C# so outside of our scope of support. You'll have to use print statements and debug the values of your list. Using Try - Catch blocks may be useful in catching the line that's causing the exception. See here for help on using try - catch.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Thank you for your reply,

      I already did what you said and the line that cause the code to not work properly is in red :

      xx.RemoveAt(i).
      The error message suggest that it comes from the index i but
      i varies from 0 to Index which is equal to xx.Count so I do not understant why it does not work.
      I precise that when I remove this line the script works properly.

      Maybe other contributor can help (mr logic for example ??!!!) ?

      Comment


        #4
        baba,

        You are probably removing an index that doesn't exist in the array list.

        You may want to check this prior to calling the RemoveAt method. Something like

        if(xx.Count > 0 && i < xx.Count)
        xx.RemoveAt(i);

        That should protect against going out of bounds.

        hope this helps.
        mrlogik
        NinjaTrader Ecosystem Vendor - Purelogik Trading

        Comment


          #5
          Thank you Mrlogik !!!!

          you are my savior :-) Thanks to you it works now.

          Comment


            #6
            Glad to help

            enjoy
            mrlogik
            NinjaTrader Ecosystem Vendor - Purelogik Trading

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by kujista, Today, 06:23 AM
            0 responses
            1 view
            0 likes
            Last Post kujista
            by kujista
             
            Started by traderqz, Yesterday, 04:32 PM
            1 response
            10 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Started by f.saeidi, Today, 05:56 AM
            1 response
            4 views
            0 likes
            Last Post Jltarrau  
            Started by Jltarrau, Today, 05:57 AM
            0 responses
            4 views
            0 likes
            Last Post Jltarrau  
            Started by Stanfillirenfro, Yesterday, 09:19 AM
            7 responses
            52 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Working...
            X