Multi-Price Point Force Acceleration

by shlbnd.ms in category Trend at 13/02/2018
Description

Minor Mod to https://ctdn.com/algos/indicators/show/1708. 

Added Parameter to allow multiple price points to be used in the moving average as well as the calculation of force.

1 = OHLC ( Close )
2 = Median
3 = WeightedClose
4 = Typical 

Download
473 downloads
How to install
Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgocTrader
using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = false, AccessRights = AccessRights.None)]

    public class MultiPricePointForceAcceleration : Indicator
    {


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

        [Parameter("Price Point ( 1  = Close Price, 2 = Median Price, 3 = Weighted Close, 4 Typical Price", DefaultValue = 1)]
        public int PricePoint { get; set; }

        [Output("Positive Force", Color = Colors.White, PlotType = PlotType.Histogram, Thickness = 5)]
        public IndicatorDataSeries ForceUp { get; set; }

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

        [Output("ResistanceUp", Color = Colors.White, PlotType = PlotType.Histogram)]
        public IndicatorDataSeries ResistanceUp { get; set; }

        [Output("ResistanceDown", Color = Colors.Purple, PlotType = PlotType.Histogram)]
        public IndicatorDataSeries ResistanceDown { get; set; }

        private IndicatorDataSeries Mart;
        private HullMovingAverage iMA;

        double Force_Sum_Plus;
        double Force_Sum_Minus;
        double Force_Weight;
        double Force_Sum;
        TypicalPrice tp;
        WeightedClose wc;
        MedianPrice mp;
        protected override void Initialize()
        {
            tp = Indicators.TypicalPrice();
            wc = Indicators.WeightedClose();
            mp = Indicators.MedianPrice();


            Mart = CreateDataSeries();

            switch (PricePoint)
            {
                case 1:
                    iMA = Indicators.GetIndicator<HullMovingAverage>(MarketSeries.Close, Periods);
                    break;
                case 2:
                    iMA = Indicators.GetIndicator<HullMovingAverage>(mp, Periods);
                    break;
                case 3:
                    iMA = Indicators.GetIndicator<HullMovingAverage>(wc, Periods);
                    break;
                case 4:
                    iMA = Indicators.GetIndicator<HullMovingAverage>(tp, Periods);
                    break;
            }

        }

        public override void Calculate(int index)
        {
            int i = 0;
            int FirstPos = 0;

            if (index <= 50)
                return;

            if (Periods < 21)
                return;

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

                for (i = FirstPos; i < FirstPos - Periods - 1; i++)
                    ForceNextCalc(i);
            }
            Force(FirstPos, index);
        }

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

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

        double ForcePrice(int Shift)
        {
            switch (PricePoint)
            {
                case 1:
                    return MarketSeries.Close[Shift];
                case 2:
                    return MarketSeries.Median[Shift];
                case 3:
                    return MarketSeries.WeightedClose[Shift];
                case 4:
                    return MarketSeries.Typical[Shift];
            }

            return MarketSeries.Close[Shift];
        }

        double ForceFirstCalc(int Shift, int index)
        {
            int w = 0;
            int j = 0;
            int i = 0;

            Force_Sum = 0.0;
            if (Shift > (index - 5))
            {
                Force_Weight = 0.0;
                i = 0;
                w = Shift - 5;
                while (w <= Shift)
                {
                    i++;
                    Force_Sum = Force_Sum + i * ForcePrice(w);
                    Force_Weight = Force_Weight + i;
                    w++;
                }
                while (w <= index)
                {
                    i--;
                    Force_Sum = Force_Sum + i * ForcePrice(w);
                    Force_Weight = Force_Weight + i;
                    w++;
                }
            }
            else
            {
                Force_Sum_Minus = 0.0;
                Force_Sum_Plus = 0.0;
                for (j = Shift + 5,i = Shift - 5,w = 1; w <= 5; j--,i++,w++)
                {
                    Force_Sum = Force_Sum + w * (ForcePrice(i) + ForcePrice(j));
                    Force_Sum_Minus = Force_Sum_Minus + ForcePrice(i);
                    Force_Sum_Plus = Force_Sum_Plus + ForcePrice(j);
                }
                Force_Sum = Force_Sum + 6 * ForcePrice(Shift);
                Force_Sum_Minus = Force_Sum_Minus + ForcePrice(Shift);
                Force_Weight = 36;
            }
            return (Force_Sum / Force_Weight);
        }

        double ForceNextCalc(int Shift)
        {
            Force_Sum_Plus = Force_Sum_Plus + ForcePrice(Shift + 5);
            Force_Sum = Force_Sum - Force_Sum_Minus + Force_Sum_Plus;
            Force_Sum_Minus = Force_Sum_Minus - ForcePrice(Shift - 6) + ForcePrice(Shift);
            Force_Sum_Plus = Force_Sum_Plus - ForcePrice(Shift);
            return Force_Sum / Force_Weight;
        }

        void Drawing(int Shift)
        {
            double Dval = 0;
            double val = 0;

            val = 5 * (Mart[Shift] - Mart[ArrayMinimum(Mart, Periods, Shift)]) / 9;
            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;
        }

    }
}
Comments
0