Profile Picture

LookBack error when accessing RList in Custom Indicator.

Posted By johan 2 Years Ago

LookBack error when accessing RList in Custom Indicator.

Message
Posted Thursday June 11 2015
I have the following code:

for (int i = index; i <= index + length - 1; i++)
                {
                    j = Filt.LookBack(i);

                    if (j > highestC)
                    {
                        highestC = j;
                    }
                    else if (j<lowestC)
                    {
                        lowestC = j;
                    }
                }

When I run the indicator, it gives an error on the LookBack.  If I change index to i=0; i<=length-1, i++, the For loop works.  Above code works for other arrays, such as when I do not use a RList, eg the following:

for (int i = index; i <= index + length-1; i++)
                {
                    if(inputs[0].LookBack(i)>HHN3)
                    {
                        HHN3 = inputs[0].LookBack(i);
                    }
                    if(inputs[0].LookBack(i)<LLN3)
                    {
                        LLN3 = inputs[0].LookBack(i);
                    }
                }

Is there a specific issue with Rlist?

Thanks

Johan
Posted Thursday June 11 2015
Where are the index and length values coming from?  Can you post the whole code for this indicator or whatever the context of this code is?

Thanks,
Daniel
Posted Thursday June 11 2015
Hi Daniel

I include the code, but the indicator works with setting i to zero, in stead of index.  Just interested to know why index is not working.  It could be that I am adding to the RList in the CalcNewValue method, and that I am out of range as a result of this.  Hereby the full code - I am a self taught programmer, so the code is not very elegant.

using System;
using System.Drawing;
using System.Collections.Generic;
using RightEdge.Common;


#pragma warning disable 1591
namespace RightEdge.Indicators
{

    /// <indicator_doc var_name="Modified Stochastic Indicator"/>
    [YYEIndicatorAttribute(System.Drawing.KnownColor.IndianRed,
        YYEIndicatorAttribute.EIndicatorGroup.Other,
        Name = "Ehlers Modified Stochastic Indicator",
        Description = "Filtered and Smoothed Stochastic",
        Id = "STUMPF-010",
        DefaultDrawingPane = "Modified Stochastic Indicator",
        HelpText = "Created by John Ehlers, the Modified Stochastic Indicator" +
            " contains a Stochastic Indicator that is preceded" +
            " by a roofing filter and smoothed afterwards by." +
            " a Super Smoother")]
    [Serializable]
    [SeriesInputAttribute("Input", 1, Value = BarElement.Close)]

    public class ModifiedStochastic : SeriesCalculatorBaseWithValues
    {
        private int length = 20;
        private int LPPeriod = 10;
        private int HPPeriod = 48;
        private int validCount = 0;
        private double alpha1 = 0;
        private double a1 = 0;
        private double b1 = 0;
        private double c1 = 0;
        private double c2 = 0;
        private double c3 = 0;
       
       
        public RList<double> Filt;
        private RList <double> Stoc;
        private RList<double> HP;

        /// <summary>
        /// Constructs a Modified Stochastic indicator instance.
        /// </summary>
        /// <param name="periods">The number of periods.</param>
        [ConstructorArgument("Stochastic Length", ConstructorArgumentType.Integer, "20", 1)]
        [ConstructorArgument("Short Critical Period", ConstructorArgumentType.Integer, "10", 2)]
        [ConstructorArgument("Long Critical Period", ConstructorArgumentType.Integer, "48", 3)]
        public ModifiedStochastic(int length, int LPPeriod, int HPPeriod)
            : base(1)
        {
            this.length = length;
            this.LPPeriod = LPPeriod;
            this.HPPeriod = HPPeriod;
            HP = new RList<double>();
            Filt = new RList<double>();
            Stoc = new RList<double>();
            alpha1 = (Math.Cos(1.414 * Math.PI / HPPeriod) + (Math.Sin(1.414 * Math.PI / HPPeriod)) - 1) / Math.Cos(1.414 * Math.PI / HPPeriod);
            a1 = Math.Exp(-1.414 * Math.PI / LPPeriod);
            b1 = 2 * a1 * Math.Cos(1.414 * Math.PI / LPPeriod);
            c2 = b1;
            c3 = -a1 * a1;
            c1 = 1 - c2 - c3;
        }

        /// <summary>
        /// Constructs a Modified Stochastic Indicator instance with the specified input series.
        /// </summary>
        /// <param name="periods">The number of periods.</param>
        /// <param name="input">The series perform the calculation against.</param>
        public ModifiedStochastic(int length, int HPPeriod, int LPPeriod, ISeries input)
            : this(length, HPPeriod, LPPeriod)
        {
            SetInputs(input);
        }

        /// <summary>
        /// Calculates a new value for the series.
        /// </summary>
        /// <param name="index">The index of the value to calculate.</param>
        /// <returns>The calculated value of the series at <paramref name="index"/>.</returns>
        protected override double CalcNewValue(int index)
        {
            validCount++;
            double filt = 0.0;
            double stoc = 0.0;
            double HPVar = 0.0;
            double modstoc = 0.0;
            double highestC = 0;
            double lowestC = 0;
            double j=0;

            if (validCount < 3)
            {
                HP.Add(0.0);
                Filt.Add(0.0);
                Stoc.Add(0.0);
                modstoc=Double.NaN;
            }
            if (validCount>=3 && validCount<=length)
            {
                double price1 = inputs[0].LookBack(index);
                double price2 = inputs[0].LookBack(index + 1);
                double price3 = inputs[0].LookBack(index + 2);
                HPVar = (1 - alpha1 / 2) * (1 - alpha1 / 2) * (price1 - 2 * price2 + price3) + 2 * (1 - alpha1) * HP.LookBack(0) - (1 - alpha1) * (1 - alpha1) * HP.LookBack(1);
                HP.Add(HPVar);
                filt = c1 * (HP.LookBack(0) + HP.LookBack(1)) / 2 + c2 * Filt.LookBack(0) + c3 * Filt.LookBack(1);
                Filt.Add(filt);
                Stoc.Add(0.0);
                modstoc = Double.NaN;
            }

            if (validCount>length)
            {
                double price1 = inputs[0].LookBack(index);
                double price2 = inputs[0].LookBack(index + 1);
                double price3 = inputs[0].LookBack(index + 2);
                HPVar = (1 - alpha1 / 2) * (1 - alpha1 / 2) * (price1 - 2 * price2 + price3) + 2 * (1 - alpha1) * HP.LookBack(0) - (1 - alpha1) * (1 - alpha1) * HP.LookBack(1);
                HP.Add(HPVar);
                filt = c1 * (HP.LookBack(0) + HP.LookBack(1)) / 2 + c2 * Filt.LookBack(0) + c3 * Filt.LookBack(1);
                Filt.Add(filt);
                highestC = filt;
                lowestC = filt;
                for (int i = 0; i <= length - 1; i++)
                {
                    j = Filt.LookBack(i);

                    if (j > highestC)
                    {
                        highestC = j;
                    }
                    else if (j<lowestC)
                    {
                        lowestC = j;
                    }
                }

                stoc = (filt - lowestC) / (highestC - lowestC);
                Stoc.Add(stoc);
                if (validCount == length + 1)
                {
                    modstoc = Stoc.LookBack(0);
                }
                else if (validCount == length + 2)
                {
                    modstoc = (Stoc.LookBack(0) + Stoc.LookBack(1)) / 2;
                }
                else
                {
                    modstoc = c1 * (Stoc.LookBack(0) + Stoc.LookBack(1)) / 2 + c2 * this.LookBack(0) + c3 * this.LookBack(1);
                }
               
            }
            return modstoc;
        }


        /// <summary>
        /// Resets the series.
        /// </summary>
        /// <remarks>
        /// This function will be called when the series should be recalculated completely.
        /// </remarks>
        protected override void Reset()
        {
            validCount = 0;
        }
    }
}
#pragma warning restore 1591
Posted Friday September 08 2017
Hi johan,
i'm also coding Ehler's stuff and i faced similar issues. Have you found an elegant way to default the value of a RList to zero to make code cleaner ?

Cheers
Bertrand


Similar Topics


Reading This Topic


2005-2017 © RightEdge Systems