ma free

by Trhuelinh107 in category Trend at 11/01/2020
Description

ma

Warning! Executing the following cBot may result in loss of funds. Use it at your own risk.
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: cAlgo
using System;
using System.Linq;
using System.Text;
using System.Threading;
using System.Collections.Generic;
using System.Reflection;
using System.Globalization;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

using System.IO;
namespace cAlgo.Indicators
{

    /*

Version Update  2019.12.11

Corrected small error in average slope calculation  ///  "for (var i = index - end; i < index - start; i++)"  insted of  "for (var i = index - end; i =< index - start; i++)"

*/


    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]

    public class T3MA : Indicator
    {

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

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

        [Parameter("Volume Factor ", DefaultValue = 0.7)]
        public double Volume_Factor { get; set; }

        [Parameter("Min Threshold ", DefaultValue = 0.0, Step = 0.001, MinValue = 0)]
        public double minthr { get; set; }

        [Parameter("Start Bar ", DefaultValue = 1, MinValue = 0)]
        public int startbar { get; set; }

        [Parameter("Stop Bar ", DefaultValue = 2, MinValue = 0)]
        public int stopbar { get; set; }


        [Output("T3MA steady", LineColor = "Gray", PlotType = PlotType.DiscontinuousLine, Thickness = 3)]
        public IndicatorDataSeries steady { get; set; }

        [Output("T3MA rise", LineColor = "Green", PlotType = PlotType.DiscontinuousLine, Thickness = 3)]
        public IndicatorDataSeries rise { get; set; }

        [Output("T3MA fall", LineColor = "Red", PlotType = PlotType.DiscontinuousLine, Thickness = 3)]
        public IndicatorDataSeries fall { get; set; }

        private ExponentialMovingAverage ema1;
        private ExponentialMovingAverage ema2;
        private ExponentialMovingAverage ema3;
        private ExponentialMovingAverage ema4;
        private ExponentialMovingAverage ema5;
        private ExponentialMovingAverage ema6;

        int lastindex = 0;

        protected override void Initialize()
        {
            ema1 = Indicators.ExponentialMovingAverage(Source, Period);
            ema2 = Indicators.ExponentialMovingAverage(ema1.Result, Period);
            ema3 = Indicators.ExponentialMovingAverage(ema2.Result, Period);
            ema4 = Indicators.ExponentialMovingAverage(ema3.Result, Period);
            ema5 = Indicators.ExponentialMovingAverage(ema4.Result, Period);
            ema6 = Indicators.ExponentialMovingAverage(ema5.Result, Period);
        }


        public override void Calculate(int index)
        {


            if (IsLastBar)
            {
                if (index != lastindex)
                    lastindex = index;
                else
                    return;
            }

            var avg = averageslope(startbar, stopbar, index);

            int mode = (avg > minthr) ? 1 : (avg < -minthr) ? -1 : 0;

            var val = maketema(index);

            rise[index] = (mode == 1) ? val : double.NaN;
            fall[index] = (mode == -1) ? val : double.NaN;
            steady[index] = 1.000001 * val;
        }


        double averageslope(int start, int end, int index)
        {

            var sum = 0.0;
            var count = 0;

            for (var i = index - end; i < index - start; i++)
            {
                var p0 = maketema(i + 1);
                var p1 = maketema(i);
                var per = (p0 - p1) / p0 * 100;

                sum += per;
                count++;
            }
            return sum / count;
        }


        double maketema(int index)
        {
            double b, b2, b3, c1, c2, c3, c4;

            b = Volume_Factor;

            b2 = Math.Pow(b, 2);

            // Volume Factor Squared

            b3 = Math.Pow(b, 3);

            // Volume Factor Cubed

            c1 = -b3;

            c2 = 3 * b2 + 3 * b3;

            c3 = -6 * b2 - 3 * b - 3 * b3;

            c4 = 1 + 3 * b + b3 + 3 * b2;

            var result = c1 * ema6.Result[index] + c2 * ema5.Result[index] + c3 * ema4.Result[index] + c4 * ema3.Result[index];

            return result;

        }

    }

}
Comments
0