Category Trend  Published on 10/11/2022

KF_Mtf_MA

Description

Gives you the ability to supper-impose a moving average line of a higher timeframe onto the current chart.

The screen shot below shows the moving average line of the H4 chart on a H1 chart - enjoy!!!


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

namespace cAlgo
{
    public enum DSTypeE
    {
        OpenPrices,
        ClosePrices,
        HighPrices,
        LowPrices,
        MedianPrices,
        TypicalPrices,
        WeightedPrices
    }

    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    public class KF_Mtf_MA : Indicator
    {
        #region Settings

        [Parameter("MaType", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType MaType { get; set; }

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

        [Parameter("DsType", DefaultValue = DSTypeE.ClosePrices)]
        public DSTypeE DsType { get; set; }

        [Parameter("Timeframe", DefaultValue = "Hour4")]
        public TimeFrame Timeframe { get; set; }

        #endregion


        [Output("Result", LineColor = "Chartreuse", Thickness = 4, LineStyle = LineStyle.Lines)]
        public IndicatorDataSeries Result { get; set; }


        private MovingAverage _c2M;

        private Bars _c2;
        private bool extraBarsFetched = false;


        protected override void Initialize()
        {
            _c2 = Timeframe == Chart.TimeFrame ? Bars : MarketData.GetBars(Timeframe);

            if(_c2.TimeFrame != Chart.TimeFrame) while (_c2.LoadMoreHistory() > 0) { };

            switch (DsType)
            {
                case DSTypeE.OpenPrices:
                    _c2M = Indicators.MovingAverage(_c2.OpenPrices, Period, MaType);
                    break;
                case DSTypeE.HighPrices:
                    _c2M = Indicators.MovingAverage(_c2.HighPrices, Period, MaType);
                    break;
                case DSTypeE.LowPrices:
                    _c2M = Indicators.MovingAverage(_c2.LowPrices, Period, MaType);
                    break;
                case DSTypeE.TypicalPrices:
                    _c2M = Indicators.MovingAverage(_c2.TypicalPrices, Period, MaType);
                    break;
                case DSTypeE.WeightedPrices:
                    _c2M = Indicators.MovingAverage(_c2.WeightedPrices, Period, MaType);
                    break;
                default:
                    _c2M = Indicators.MovingAverage(_c2.ClosePrices, Period, MaType);
                    break;
            }
        }

        public override void Calculate(int index)
        {
            var index2 = _c2.TimeFrame == Chart.TimeFrame ? index : _c2.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
            if (index2 < 0)
            {
                return;
            }

            _c2M.Calculate(0);

            Result[index] = _c2M.Result[index2];

            var tmp2 = _c2.TimeFrame == Chart.TimeFrame ? index - 1 : _c2.OpenTimes.GetIndexByTime(Bars.OpenTimes[index - 1]);

            int lastIdx1 = Bars.OpenTimes.GetIndexByTime(_c2.OpenTimes[index2]);
            int lastIdx2 = Bars.OpenTimes.GetIndexByTime(_c2.OpenTimes[tmp2]);
            if (lastIdx1 == lastIdx2 && _c2.TimeFrame != Chart.TimeFrame)
            {
                Result[index - 1] = double.NaN;
            }
        }
    }
}


GM
gmkenneyy

Joined on 20.03.2020 Blocked

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: KF_Mtf_MA.algo
  • Rating: 0
  • Installs: 653
  • Modified: 09/11/2022 22:22
Comments
Log in to add a comment.
No comments found.