Topics
21 Aug 2021, 10:56
 1215
 3
Replies

bernisurf
23 Aug 2021, 20:36

RE:

Thanks a lot 

Hi,

You can develop a multi time frame MACD indicator based on built-in MACD indicator easily, try this:

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class MACDMTF : Indicator
    {
        private MacdCrossOver _macd;

        private Bars _baseTimeFrameBars;

        [Parameter("Base TimeFrame", DefaultValue = "Daily")]
        public TimeFrame BaseTimeFrame { get; set; }

        [Parameter("Source", DefaultValue = DataSource.Close)]
        public DataSource DataSource { get; set; }

        [Parameter("Long Cycle", DefaultValue = 26)]
        public int LongCycle { get; set; }

        [Parameter("Long Cycle", DefaultValue = 12)]
        public int ShortCycle { get; set; }

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

        [Output("MACD", LineColor = "Blue", LineStyle = LineStyle.Solid, PlotType = PlotType.Line)]
        public IndicatorDataSeries Macd { get; set; }

        [Output("Signal", LineColor = "Red", LineStyle = LineStyle.Lines, PlotType = PlotType.Line)]
        public IndicatorDataSeries Signal { get; set; }

        [Output("Histogram", LineColor = "Brown", LineStyle = LineStyle.Solid, PlotType = PlotType.Histogram)]
        public IndicatorDataSeries Histogram { get; set; }

        protected override void Initialize()
        {
            _baseTimeFrameBars = MarketData.GetBars(BaseTimeFrame);

            var dataSeries = GetDataSource();

            _macd = Indicators.MacdCrossOver(dataSeries, LongCycle, ShortCycle, SignalPeriods);
        }

        public override void Calculate(int index)
        {
            var baseIndex = _baseTimeFrameBars.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);

            Macd[index] = _macd.MACD[baseIndex];
            Signal[index] = _macd.Signal[baseIndex];
            Histogram[index] = _macd.Histogram[baseIndex];
        }

        private DataSeries GetDataSource()
        {
            switch (DataSource)
            {
                case DataSource.Open:
                    return _baseTimeFrameBars.OpenPrices;

                case DataSource.High:
                    return _baseTimeFrameBars.HighPrices;

                case DataSource.Low:
                    return _baseTimeFrameBars.LowPrices;

                case DataSource.Close:
                    return _baseTimeFrameBars.ClosePrices;

                default:
                    throw new InvalidOperationException("Not supported data source type");
            }
        }
    }

    public enum DataSource
    {
        Open,
        High,
        Low,
        Close
    }
}

 

 


@bernisurf