Category Oscilators  Published on 15/11/2021

Snake Force

Description

Description:

Snake Force is great indicator for determine Support and Resistance.

This indicator based on MetaTrader 4 and it has been rewrote for cTrader.

 

Note:

This is my first indicator.

This indicator reprints itself.

 

Screenshots:


using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo
{
    [Indicator(IsOverlay = false, AccessRights = AccessRights.None)]
    public class SnakeForce : Indicator
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter("Periods", DefaultValue = 24)]
        public int Periods { get; set; }

        [Output("ForceUp", LineColor = "Lime", PlotType = PlotType.Histogram, Thickness = 5)]
        public IndicatorDataSeries ForceUp { get; set; }

        [Output("ForceDown", LineColor = "Red", PlotType = PlotType.Histogram, Thickness = 5)]
        public IndicatorDataSeries ForceDown { get; set; }

        [Output("ResistanceUp", LineColor = "Lime", PlotType = PlotType.Histogram)]
        public IndicatorDataSeries ResistanceUp { get; set; }

        [Output("ResistanceDown", LineColor = "Red", PlotType = PlotType.Histogram)]
        public IndicatorDataSeries ResistanceDown { get; set; }

        private IndicatorDataSeries Mart;
        private MovingAverage iMA;

        double Snake_Sum_Plus;
        double Snake_Sum_Minus;
        double Snake_Weight;
        double Snake_Sum;
        int FirstPos = 1;

        protected override void Initialize()
        {
            Mart = CreateDataSeries();
            iMA = Indicators.MovingAverage(Source, 6, MovingAverageType.Weighted);
        }

        public override void Calculate(int index)
        {
            if (index <= 50)
                return;

            if (Periods < 21)
                return;

            if (index - Periods - 7 < 1)
            {
                FirstPos = index - Periods - 7;
                Mart[FirstPos + Periods] = SnakeFirstCalc(FirstPos + Periods, index);

                int i;
                for (i = FirstPos; i < FirstPos - Periods - 1; i++)
                    SnakeNextCalc(i);
            }

            Snake(FirstPos, index);
        }

        void Snake(int Pos, int index)
        {
            if (Pos < 6)
                Pos = index - 6;

            Mart[Pos] = SnakeFirstCalc(Pos, index);
            Drawing(Pos);
            Pos++;
            while (Pos <= index - 5)
            {
                Mart[Pos] = SnakeNextCalc(Pos);
                Drawing(Pos);
                Pos++;
            }
            while (Pos < index)
            {
                Mart[Pos] = SnakeFirstCalc(Pos, index);
                Drawing(Pos);
                Pos++;
            }
            if (Pos == index)
            {
                Mart[Pos] = iMA.Result.LastValue;
                Drawing(Pos);
            }
            return;
        }

        double SnakeFirstCalc(int Shift, int index)
        {
            Snake_Sum = 0.0;
            int w;
            int i;
            if (Shift > (index - 5))
            {
                Snake_Weight = 0.0;
                i = 0;
                w = Shift - 5;
                while (w <= Shift)
                {
                    i++;
                    Snake_Sum += i * Source[w];
                    Snake_Weight += i;
                    w++;
                }
                while (w <= index)
                {
                    i--;
                    Snake_Sum += i * Source[w];
                    Snake_Weight += i;
                    w++;
                }
            }
            else
            {
                Snake_Sum_Minus = 0.0;
                Snake_Sum_Plus = 0.0;
                int j;
                for (j = Shift + 5,i = Shift - 5,w = 1; w <= 5; j--,i++,w++)
                {
                    Snake_Sum += w * (Source[i] + Source[j]);
                    Snake_Sum_Minus += Source[i];
                    Snake_Sum_Plus += Source[j];
                }
                Snake_Sum += 6 * Source[Shift];
                Snake_Sum_Minus += Source[Shift];
                Snake_Weight = 36;
            }
            return (Snake_Sum / Snake_Weight);
        }

        double SnakeNextCalc(int Shift)
        {
            Snake_Sum_Plus += Source[Shift + 5];
            Snake_Sum = Snake_Sum - Snake_Sum_Minus + Snake_Sum_Plus;
            Snake_Sum_Minus = Snake_Sum_Minus - Source[Shift - 6] + Source[Shift];
            Snake_Sum_Plus -= Source[Shift];
            return Snake_Sum / Snake_Weight;
        }

        void Drawing(int Shift)
        {
            double val = 5 * (Mart[Shift] - Mart[ArrayMinimum(Mart, Periods, Shift)]) / 9;
            double Dval = 5 * (Mart[Shift] - Mart[Shift - 1] + Mart[ArrayMinimum(Mart, Periods, Shift - 1)] - Mart[ArrayMinimum(Mart, Periods, Shift)]) / 9;
            if (Dval > 0)
            {
                ForceUp[Shift] = val;
                ResistanceUp[Shift] = 0;
            }
            else
            {
                ForceUp[Shift] = 0;
                ResistanceUp[Shift] = val;
            }
            val = 5 * (Mart[Shift] - Mart[ArrayMaximum(Mart, Periods, Shift)]) / 9;
            Dval = 5 * (Mart[Shift] - Mart[Shift - 1] + Mart[ArrayMaximum(Mart, Periods, Shift - 1)] - Mart[ArrayMaximum(Mart, Periods, Shift)]) / 9;
            if (Dval < 0)
            {
                ForceDown[Shift] = val;
                ResistanceDown[Shift] = 0;
            }
            else
            {
                ForceDown[Shift] = 0;
                ResistanceDown[Shift] = val;
            }
            return;
        }

        int ArrayMaximum(IndicatorDataSeries array, int count = 0, int start = 0)
        {
            var result = start;
            for (var i = start - count + 1; i <= start - 1; i++)
            {
                if (array[i] > array[result])
                    result = i;
            }
            return result;
        }

        int ArrayMinimum(IndicatorDataSeries array, int count = 0, int start = 0)
        {
            var result = start;
            for (var i = start - count + 1; i <= start - 1; i++)
            {
                if (array[i] < array[result])
                    result = i;
            }
            return result;
        }

    }
}


Doustzadeh's avatar
Doustzadeh

Joined on 20.03.2016

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Snake Force.algo
  • Rating: 0
  • Installs: 4572
  • Modified: 15/11/2021 19:37
Comments
Log in to add a comment.
TR
traderforyou · 8 years ago

Not a useful indicator, it reprints itself