Category Trend  Published on 21/05/2024

Moving Average TimeFrame

Description

Moving Average TimeFrame

I looked for it on cTrader to save time, and it was nowhere to be found, so I did it, and now it will be there.
 

Enjoy for Free =) 

Previous account here : https://ctrader.com/users/profile/70920
Contact telegram :  https://t.me/nimi012 



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    public class STOCHTF : Indicator
    {
        [Parameter("TimeFrame", DefaultValue = "Daily", Group = "Calculation Type")]
        public TimeFrame TF1 { get; set; }
        [Parameter("DrawMode (slope)", DefaultValue = EnumDrawMode.DrawModeSlope, Group = "Calculation Type")]
        public EnumDrawMode DrawMode { get; set; }
        public enum EnumDrawMode
        {
            DrawModeSteps,
            DrawModeSlope
        }
        [Parameter("Period", DefaultValue = 9, Group = "Calculation Ma")]
        public int Period { get; set; }
        [Parameter("Ma Type", DefaultValue = MovingAverageType.Weighted, Group = "Calculation Ma")]
        public MovingAverageType MaTypePeriod { get; set; }
        [Parameter("Period Signal", DefaultValue = 9, Group = "Calculation Ma")]
        public int PeriodSignal { get; set; }
        [Parameter("Ma Type", DefaultValue = MovingAverageType.Weighted, Group = "Calculation Ma")]
        public MovingAverageType MaTypePeriodSignal { get; set; }

        [Output("Result", PlotType = PlotType.Line, LineColor = "Lime", Thickness = 1)]
        public IndicatorDataSeries Result { get; set; }
        [Output("Signal", PlotType = PlotType.Line, LineColor = "Red", Thickness = 1)]
        public IndicatorDataSeries Signal { get; set; }

        private MovingAverage ma, signal;
        private Bars bars1;
        private IndicatorDataSeries res;

        protected override void Initialize()
        {
            bars1 = MarketData.GetBars(TF1);
            res = CreateDataSeries();

            if (!IsBacktesting)
            {
                while (bars1.OpenTimes[0] > Bars.OpenTimes[0])
                    bars1.LoadMoreHistory();
            }
            ma = Indicators.MovingAverage(bars1.ClosePrices, Period, MaTypePeriod);
            signal = Indicators.MovingAverage(res, PeriodSignal, MaTypePeriodSignal);
        }

        public override void Calculate(int index)
        {
            int idx1 = TF1 == Chart.TimeFrame ? index : bars1.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
            int idx11 = TF1 == Chart.TimeFrame ? index - 1 : bars1.OpenTimes.GetIndexByTime(Bars.OpenTimes[index - 1]);

            Result[index] = idx1 >= 0 ? ma.Result[idx1] : 50.0; ;

            res[idx1] = ma.Result[idx1];
            Signal[index] = idx1 >= 0 ? signal.Result[idx1] : 50.0;

            if (idx1 >= 0 && DrawMode == EnumDrawMode.DrawModeSlope && TF1 != Chart.TimeFrame && Bars.OpenTimes.GetIndexByTime(bars1.OpenTimes[idx1]) == Bars.OpenTimes.GetIndexByTime(bars1.OpenTimes[idx11]))
            {
                Result[index - 1] = double.NaN;
                Signal[index - 1] = double.NaN;
            }
        }
    }
}

YE
YesOrNot2

Joined on 17.05.2024

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Ma TF_withSourceCode.algo
  • Rating: 5
  • Installs: 509
  • Modified: 21/05/2024 22:00
Comments
Log in to add a comment.
No comments found.