Adaptive Stoc

Created at 02 Jan 2016, 16:25
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
metalsing's avatar

metalsing

Joined 06.03.2015

Adaptive Stoc
02 Jan 2016, 16:25


Need Help To Correct this Indicator it From EasyLanguage,I translated  this
 But  doesnot  work.

using System;
using cAlgo.API;

namespace cAlgo.Indicators
{

    [Indicator(IsOverlay = false, AccessRights = AccessRights.None)]
    public class AdStc : Indicator
    {
        [Parameter(DefaultValue = 3)]
        public int AvgLength { get; set; }

        [Parameter()]
        public DataSeries Source { get; set; }

        [Output("AdStochastic", Color = Colors.Yellow)]
        public IndicatorDataSeries AdStochastic { get; set; }

        [Output("Levels", Color = Colors.Red)]
        public Double[] Levels { get; set; }
        [Levels(0.7, 0.3)]

        private double alpha1 = 0;
        private double a1 = 0;
        private double b1 = 0;
        private double c1 = 0;
        private double c2 = 0;
        private double c3 = 0;

        private double Sx = 0;
        private double Sy = 0;
        private double Sxx = 0;
        private double Syy = 0;
        private double Sxy = 0;
        private double X = 0;
        private double Y = 0;
        private double HighestC = 0;
        private double LowestC = 0;

        private int M = 0;
        private int N = 0;
        private int Lag = 0;

        private double MaxPwr = 0;
        private double Spx = 0;
        private double Sp = 0;
        private double DominantCycle = 0;

        private double[,] R = new double[48, 3];

        private IndicatorDataSeries HP;
        private IndicatorDataSeries Filt;
        private IndicatorDataSeries Corr;
        private IndicatorDataSeries Pwr;

        private IndicatorDataSeries CosinePart;
        private IndicatorDataSeries SinePart;
        private IndicatorDataSeries SqSum;
        private IndicatorDataSeries Stoc;

        protected override void Initialize()
        {
            //Highpass filter cyclic components whose periods are shorter than 48 bars
            alpha1 = (Math.Cos(Math.Sqrt(2) * Math.PI / 48) + Math.Sin(Math.Sqrt(2) * Math.PI / 48) - 1) / Math.Cos(Math.Sqrt(2) * Math.PI / 48);

            a1 = Math.Exp(-Math.Sqrt(2) * Math.PI / 10);
            b1 = 2 * a1 * Math.Cos(Math.Sqrt(2) * Math.PI / 10);
            c2 = b1;
            c3 = -a1 * a1;
            c1 = 1 - c2 - c3;

            HP = CreateDataSeries();
            Filt = CreateDataSeries();
            Corr = CreateDataSeries();
            Pwr = CreateDataSeries();
            CosinePart = CreateDataSeries();
            SinePart = CreateDataSeries();
            SqSum = CreateDataSeries();
            Stoc = CreateDataSeries();

            HP[0] = 0;
            HP[1] = 0;
            Filt[0] = 0;
            Filt[1] = 0;
            AdStochastic[0] = 0;
            AdStochastic[1] = 0;
        }

        public override void Calculate(int index)
        {

            if (index < 50)
                return;

            HP[index] = (1 - alpha1 / 2) * (1 - alpha1 / 2) * (Source[index] - 2 * Source[index - 1] + Source[index - 2]) + 2 * (1 - alpha1) * HP[index - 1] - (1 - alpha1) * (1 - alpha1) * HP[index - 2];

            Filt[index] = c1 * (HP[index] + HP[index - 1]) / 2 + c2 * Filt[index - 1] + c3 * Filt[index - 2];
            for (Lag = 0; Lag <= 48; Lag++)
            {
                //Set the averaging length as M
                M = AvgLength;
                if (AvgLength == 0)
                    M = Lag;
                Sx = 0;
                Sy = 0;
                Sxx = 0;
                Syy = 0;
                Sxy = 0;
                for (int count = 0; count <= M - 1; count++)
                {
                    X = Filt[index - count];

                    // how to solov it
                    Y = Filt[index - Lag - count];
                    Sx = Sx + X;
                    Sy = Sy + Y;
                    Sxx = Sxx + X * X;
                    Sxy = Sxy + X * Y;
                    Syy = Syy + Y * Y;
                }
                if ((M * Sxx - Sx * Sx) * (M * Syy - Sy * Sy) > 0)
                    Corr[index - Lag] = (M * Sxy - Sx * Sy) / Math.Sqrt((M * Sxx - Sx * Sx) * (M * Syy - Sy * Sy));
            }
            for (int Period1 = 10; Period1 <= 48; Period1++)
            {
                CosinePart[index - Period1] = 0;
                SinePart[index - Period1] = 0;
                for (N = 3; N <= 48; N++)
                {
                    CosinePart[index - Period1] = CosinePart[index - Period1] + Corr[index - N] * Math.Cos(2 * Math.PI * N / Period1);
                    SinePart[index - Period1] = SinePart[index - Period1] + Corr[index - N] * Math.Sin(2 * Math.PI * N / Period1);
                }
                SqSum[index - Period1] = CosinePart[index - Period1] * CosinePart[index - Period1] + SinePart[index - Period1] * SinePart[index - Period1];
            }
            for (int Period2 = 10; Period2 <= 48; Period2++)
            {
                R[Period2, 2] = R[Period2, 1];
                R[Period2, 1] = 0.2 * SqSum[index - Period2] * SqSum[index - Period2] + 0.8 * R[Period2, 2];
            }
            //Find Maximum Power Level for Normalization            
            MaxPwr = 0.995 * MaxPwr;
            for (int Period3 = 10; Period3 <= 48; Period3++)
            {
                if (R[Period3, 1] > MaxPwr)
                    MaxPwr = R[Period3, 1];
            }
            for (int Period4 = 3; Period4 <= 48; Period4++)
            {
                Pwr[index - Period4] = R[Period4, 1] / MaxPwr;
            }
            //Compute the dominant cycle using the CG of the spectrum
            Spx = 0;
            Sp = 0;
            for (int Period5 = 10; Period5 <= 48; Period5++)
            {
                if (Pwr[index - Period5] >= 0.5)
                {
                    Spx = Spx + Period5 * Pwr[index - Period5];
                    Sp = Sp + Pwr[index - Period5];
                }
            }
            if (Sp != 0)
                DominantCycle = Spx / Sp;
            if (DominantCycle < 10)
                DominantCycle = 10;
            if (DominantCycle > 48)
                DominantCycle = 48;
            //Stochastic Computation starts here   
            HighestC = Filt[index];
            LowestC = Filt[index];
            for (int count = 0; count <= DominantCycle - 1; count++)
            {
                if (Filt[index - count] > HighestC)
                    HighestC = Filt[index - count];
                if (Filt[index - count] < LowestC)
                    LowestC = Filt[index - count];
            }
            Stoc[index] = (Filt[index] - LowestC) / (HighestC - LowestC);
            AdStochastic[index] = c1 * (Stoc[index] + Stoc[index - 1]) / 2 + c2 * AdStochastic[index - 1] + c3 * AdStochastic[index - 2];

        }
    }
}

The Origin

{
    Adaptive Stochastic
    (c) 2013 John F. Ehlers
}
Vars:
    AvgLength(3),
    M(0),
    N(0),
    X(0),
    Y(0),
    alpha1(0),
    HP(0),
    a1(0),
    b1(0),
    c1(0),
    c2(0),
    c3(0),
    Filt(0),
    Lag(0),
    count(0),
    Sx(0),
    Sy(0),
    Sxx(0),
    Syy(0),
    Sxy(0),
    Period(0),
    Sp(0),
    Spx(0),
    MaxPwr(0),
    DominantCycle(0);
Arrays:
    Corr[48](0),
    CosinePart[48](0),
    SinePart[48](0),
    SqSum[48](0),
    R[48, 2](0),
    Pwr[48](0);
//Highpass filter cyclic components whose periods are shorter than 48 bars
alpha1 = (Cosine(.707*360 / 48) + Sine (.707*360 / 48) - 1) / Cosine(.707*360 / 48);
HP = (1 - alpha1 / 2)*(1 - alpha1 / 2)*(Close - 2*Close[1] + Close[2]) + 2*(1 - alpha1)*HP[1] - (1 - alpha1)*
(1 - alpha1)*HP[2];
//Smooth with a Super Smoother Filter from equation 3-3
a1 = expvalue(-1.414*3.14159 / 10);
b1 = 2*a1*Cosine(1.414*180 / 10);
c2 = b1;
c3 = -a1*a1;
c1 = 1 - c2 - c3;
Filt = c1*(HP + HP[1]) / 2 + c2*Filt[1] + c3*Filt[2];
//Pearson correlation for each value of lag
For Lag = 0 to 48 Begin
    //Set the averaging length as M
    M = AvgLength;
    If AvgLength = 0 Then M = Lag;
    Sx = 0;
    Sy = 0;
    Sxx = 0;
    Syy = 0;
    Sxy = 0;
    For count = 0 to M - 1 Begin
X = Filt[count];
Y = Filt[Lag + count];
Sx = Sx + X;
Sy = Sy + Y;
Sxx = Sxx + X*X;
Sxy = Sxy + X*Y;
    Syy = Syy + Y*Y;
    End;
    If (M*Sxx - Sx*Sx)*(M*Syy - Sy*Sy) > 0 Then Corr[Lag] = (M*Sxy - Sx*Sy)/SquareRoot((M*Sxx - Sx*Sx)*(M*Syy - Sy*Sy));
End;
For Period = 10 to 48 Begin
    CosinePart[Period] = 0;
    SinePart[Period] = 0;
    For N = 3 to 48 Begin
    CosinePart[Period] = CosinePart[Period] + Corr[N]*Cosine(360*N / Period);
SinePart[Period] = SinePart[Period] + Corr[N]*Sine(360*N / Period);
    End;
    SqSum[Period] = CosinePart[Period]*CosinePart[Period] + SinePart[Period]*SinePart[Period];
End;
For Period = 10 to 48 Begin
    R[Period, 2] = R[Period, 1];
    R[Period, 1] = .2*SqSum[Period]*SqSum[Period] + .8*R[Period, 2]; 
End;
//Find Maximum Power Level for Normalization
MaxPwr = .995*MaxPwr;
For Period = 10 to 48 Begin
    If R[Period, 1] > MaxPwr Then MaxPwr = R[Period, 1];
End;
For Period = 3 to 48 Begin
    Pwr[Period] = R[Period, 1] / MaxPwr;
End;
//Compute the dominant cycle using the CG of the spectrum
Spx = 0;
Sp = 0;
For Period = 10 to 48 Begin
    If Pwr[Period] >= .5 Then Begin
    Spx = Spx + Period*Pwr[Period];
    Sp = Sp + Pwr[Period];
    End;
End;
If Sp <> 0 Then DominantCycle = Spx / Sp;
If DominantCycle < 10 Then DominantCycle = 10;
If DominantCycle > 48 Then DominantCycle = 48;
//Stochastic Computation starts here
Vars:
    HighestC(0),
    LowestC(0),
    Stoc(0),
    SmoothNum(0),
    SmoothDenom(0),
    AdaptiveStochastic(0);
HighestC = Filt;
LowestC = Filt;
For count = 0 to DominantCycle - 1 Begin
    If Filt[count] > HighestC then HighestC = Filt[count];
    If Filt[count] < LowestC then LowestC = Filt[count];
End;
Stoc = (Filt - LowestC) / (HighestC - LowestC);
AdaptiveStochastic = c1*(Stoc + Stoc[1]) / 2 + c2*AdaptiveStochastic[1] + c3*AdaptiveStochastic[2];
Plot1(AdaptiveStochastic);
Plot2(.7);
Plot6(.3);

 


@metalsing
Replies

Spotware
04 Jan 2016, 02:40

Dear Trader,

We would like to inform you that we do not provide coding assistance services. We more than glad to assist you with specific questions about cAlgo.API. You can contact one of our Partners or post a job in Development Jobs section for further coding assistance.


@Spotware