MacdHistogram at different timeframe

Created at 13 Jul 2020, 23:58
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!
MA

MATRIXTRADER

Joined 22.06.2019

MacdHistogram at different timeframe
13 Jul 2020, 23:58


Hello
I am having difficulty converting this "Macd Histogram" indicator in a time different from the table.
can you light my lantern ..
cordially

 

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {


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

        [Parameter("MACD LongCycle", DefaultValue = 26, MinValue = 1)]
        public int LongCycle { get; set; }

        [Parameter("MACD ShortCycle", DefaultValue = 12, MinValue = 1)]
        public int ShortCycle { get; set; }

        [Parameter("MACD Period", DefaultValue = 9, MinValue = 1)]
        public int MACDPeriod { get; set; }

        [Parameter("-------------------------", DefaultValue = "")]
        public string Separator1 { get; set; }

        [Parameter("Another Timeframe MovingAverage", DefaultValue = "Hour")]
        public TimeFrame AnotherTimeFrame2 { get; set; }

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

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

        [Parameter("Moving Average Type", DefaultValue = MovingAverageType.TimeSeries)]
        public MovingAverageType MovingAverageType { get; set; }


        private MacdHistogram MACD;

        private MovingAverage MA;

        protected override void OnStart()
        {

            MACD = Indicators.MacdHistogram(LongCycle, ShortCycle, MACDPeriod);

            MA = Indicators.MovingAverage(MarketData.GetBars(AnotherTimeFrame2).ClosePrices, Period, MovingAverageType);
        }
    }
}


 


@MATRIXTRADER
Replies

PanagiotisCharalampous
14 Jul 2020, 08:35

Hi MATRIXTRADER,

Try

MACD = Indicators.MacdHistogram(MarketData.GetBars(AnotherTimeFrame2), LongCycle, ShortCycle, MACDPeriod);

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

MATRIXTRADER
14 Jul 2020, 09:55

RE:

PanagiotisCharalampous said:

Hi MATRIXTRADER,

Try

MACD = Indicators.MacdHistogram(MarketData.GetBars(AnotherTimeFrame2), LongCycle, ShortCycle, MACDPeriod);

Best Regards,

Panagiotis 

Join us on Telegram

Thanks for your help,
I had already tried this method that I had apply to MA in the example above and which works ... unfortunately for MacdHistogram the method does not want to build.

Error CS1502: The overloaded method best matches 'cAlgo.API.Internals.IIndicatorsAccessor.MacdHistogram (cAlgo.API.DataSeries, int, int, int)' has invalid arguments
Error CS1503: Argument 1: unable to convert from 'cAlgo.API.Bars' to 'cAlgo.API.DataSeries'

 

 


@MATRIXTRADER

PanagiotisCharalampous
14 Jul 2020, 09:58

Hi MATRIXTRADER,

Please post the complete cBot code that is generating these errors.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

MATRIXTRADER
14 Jul 2020, 13:40

RE:

PanagiotisCharalampous said:

Hi MATRIXTRADER,

Please post the complete cBot code that is generating these errors.

Best Regards,

Panagiotis 

Join us on Telegram


Thanks for your help
here is a code snippet

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {


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

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

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


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

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

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

        [Parameter("Moving Average Type", DefaultValue = MovingAverageType.TimeSeries)]
        public MovingAverageType MovingAverageType { get; set; }

        [Parameter("----------MACD------------", DefaultValue = "MACD")]
        public string Separator1 { get; set; }

        [Parameter("Another Timeframe MacdHistogram", DefaultValue = "Hour")]
        public TimeFrame AnotherTimeFrame2 { get; set; }

        [Parameter("MACD LongCycle", DefaultValue = 26, MinValue = 1)]
        public int LongCycle { get; set; }

        [Parameter("MACD ShortCycle", DefaultValue = 12, MinValue = 1)]
        public int ShortCycle { get; set; }

        [Parameter("MACD Period", DefaultValue = 9, MinValue = 1)]
        public int MACDPeriod { get; set; }


        private MacdHistogram MACD;

        private MovingAverage MA;

        protected override void OnStart()
        {

            MA = Indicators.MovingAverage(MarketData.GetBars(AnotherTimeFrame1).ClosePrices, Period, MovingAverageType);

            //     MACD = Indicators.MacdHistogram(LongCycle, ShortCycle, MACDPeriod);

            MACD = Indicators.MacdHistogram(MarketData.GetBars(AnotherTimeFrame2), LongCycle, ShortCycle, MACDPeriod);


        }

        protected override void OnTick()
        {



            double MA2 = MA.Result[MA.Result.Count - 2];
            int bars = MarketSeries.Close.Count - 1;
            double cl = MarketSeries.Close[bars - 1];




            if (MA2 < cl && MACD.Signal.IsRising())
            {

                Close(TradeType.Sell);
                Open(TradeType.Buy);
            }
            else if (MA2 > cl && MACD.Signal.IsFalling())
            {
                Close(TradeType.Buy);
                Open(TradeType.Sell);
            }
        }


        private void Close(TradeType tradeType)
        {

            foreach (var position in Positions.FindAll("MAMACD", Symbol, tradeType))
                //"SampleRSI"
                ClosePosition(position);

        }

        private void Open(TradeType tradeType)
        {
            var position = Positions.Find("MAMACD", Symbol, tradeType);

            if (position == null)
                ExecuteMarketOrder(tradeType, Symbol, Volume, "MAMACD", SL, TP);
        }
    }
}

 


@MATRIXTRADER

MATRIXTRADER
14 Jul 2020, 15:17

RE: RE:

I found this solution and after several tests and comparison it works well.

Add  .ClosePrices    !!
   MACD = Indicators.MacdHistogram(MarketData.GetBars(AnotherTimeFrame2).ClosePrices, LongCycle, ShortCycle, MACDPeriod);

Thank you for your support.


@MATRIXTRADER