Ma d Multitimeframe

Created at 21 Aug 2021, 10:56
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
BE

bernisurf

Joined 21.08.2021

Ma d Multitimeframe
21 Aug 2021, 10:56


Goodmorning to ever everybody 

I use the tf 15m and macd on 60m tf on TradingView but I want to backtest more candle so I want to use the ctrader platform but on the mace indicator there is no possibilities to change the timeframe. Somebody have the indicator or know the code for change the parameter on the indicator? 
thank you 


@bernisurf
Replies

amusleh
23 Aug 2021, 15:00

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
    }
}

 


@amusleh

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