How to use MACD Crossover to calgo

Created at 17 Jul 2016, 19:34
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!
KO

korakodmy

Joined 17.07.2016

How to use MACD Crossover to calgo
17 Jul 2016, 19:34


Hello ,  How to code MACD Crossover to calgo

When MACD Line Cross Signal Line I want to robot open position buy or sell.

I try to edit from referece /algos/cbots/show/155 

 

If you know to code it , please tell me 

Thank you very much 


@korakodmy
Replies

... Deleted by UFO ...

korakodmy
18 Jul 2016, 05:48

RE:

lucian said:

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FileSystem)]
    public class MACD : Robot
    {



        [Parameter("Volume", DefaultValue = 1000)]
        public int volume { get; set; }

        [Parameter(DefaultValue = 50, MinValue = 1)]
        public int StopLoss { get; set; }

        [Parameter(DefaultValue = 50, MinValue = 1)]
        public int TakeProfit { 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 MacdCrossOver _MACD;

        protected override void OnStart()
        {

            _MACD = Indicators.MacdCrossOver(LongCycle, ShortCycle, MACDPeriod);
        }

        protected override void OnBar()
        {

            if (_MACD.MACD.Last(1) < _MACD.Signal.Last(1) && _MACD.MACD.Last(0) > _MACD.Signal.Last(0) && _MACD.Signal.Last(0) < 0)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, volume, "korakodmy", StopLoss, TakeProfit, 5);
            }

            if (_MACD.MACD.Last(1) > _MACD.Signal.Last(1) && _MACD.MACD.Last(0) < _MACD.Signal.Last(0) && _MACD.Signal.Last(0) > 0)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, volume, "korakodmy", StopLoss, TakeProfit, 5);

            }


        }

    }

}

 

Thanks very much , I will try it 


@korakodmy

korakodmy
24 Jul 2016, 17:12

Thank very much lucian 

How way to contact you I want to talk with you.


@korakodmy

cfuorvito@gmail.com
22 Sep 2017, 14:48

RE:

lucian said:

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FileSystem)]
    public class MACD : Robot
    {



        [Parameter("Volume", DefaultValue = 1000)]
        public int volume { get; set; }

        [Parameter(DefaultValue = 50, MinValue = 1)]
        public int StopLoss { get; set; }

        [Parameter(DefaultValue = 50, MinValue = 1)]
        public int TakeProfit { 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 MacdCrossOver _MACD;

        protected override void OnStart()
        {

            _MACD = Indicators.MacdCrossOver(LongCycle, ShortCycle, MACDPeriod);

            var anotherSeries = MarketData.GetSeries(AnotherTimeFrame);

            Print("My TimeFrame: {0}, Last high: {1}, Last Low: {2}", TimeFrame, MarketSeries.High.LastValue, MarketSeries.Low.LastValue);
            Print("Another TimeFrame: {0}, Last high: {1}, Last Low: {2}", AnotherTimeFrame, anotherSeries.High.LastValue, anotherSeries.Low.LastValue);
        }

        protected override void OnBar()
        {

            if (_MACD.MACD.Last(1) < _MACD.Signal.Last(1) && _MACD.MACD.Last(0) > _MACD.Signal.Last(0) && _MACD.Signal.Last(0) < 0)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, volume, "korakodmy", StopLoss, TakeProfit, 5);
            }

            if (_MACD.MACD.Last(1) > _MACD.Signal.Last(1) && _MACD.MACD.Last(0) < _MACD.Signal.Last(0) && _MACD.Signal.Last(0) > 0)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, volume, "korakodmy", StopLoss, TakeProfit, 5);

            }

        }

    }

}

 

I have attepmted to add a higher time frame to korakodmy's MACD crossover algo, for confirmation before a trade is initiated, however the robot is still opening the trade based on the current chart time frame. Can anyone provide some pointers as to how this would be possible?  Many thanks.


@cfuorvito@gmail.com

PanagiotisCharalampous
22 Sep 2017, 15:00

Hi cfuorvito@gmail.com

You need to pass the series returned into anotherSeries variable into the MacdCrossOver() function. See example below

protected override void OnBar()
        {
            var anotherSeries = MarketData.GetSeries(AnotherTimeFrame);
            _MACD = Indicators.MacdCrossOver(LongCycle, ShortCycle, MACDPeriod);
            if (_MACD.MACD.Last(1) < _MACD.Signal.Last(1) && _MACD.MACD.Last(0) > _MACD.Signal.Last(0) && _MACD.Signal.Last(0) < 0)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, volume, "korakodmy", StopLoss, TakeProfit, 5);
            }

            if (_MACD.MACD.Last(1) > _MACD.Signal.Last(1) && _MACD.MACD.Last(0) < _MACD.Signal.Last(0) && _MACD.Signal.Last(0) > 0)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, volume, "korakodmy", StopLoss, TakeProfit, 5);
            }
        }

Let me know if the above helps,

Best Regards,

Panagiotis


@PanagiotisCharalampous

cfuorvito@gmail.com
22 Sep 2017, 15:25

Hi Panagiotis, thank you for your speedy reply.  I have addapted the algo per your sugestion, however it is still taking the trade based on current time-frame even though the longer time-frame MACD signal is pointing the other direction.  I would be most greatful if you could help me solve this cunundrum.  Many thanks.


@cfuorvito@gmail.com

PanagiotisCharalampous
22 Sep 2017, 15:32

Hi,

Apologies the code had a small mistake. The anotherSeries was not passed into the MacdCrossOver() function. Can you try the below and let me know?

        protected override void OnBar()
        {
            var anotherSeries = MarketData.GetSeries(AnotherTimeFrame);
            _MACD = Indicators.MacdCrossOver(anotherSeries, LongCycle, ShortCycle, MACDPeriod);
            if (_MACD.MACD.Last(1) < _MACD.Signal.Last(1) && _MACD.MACD.Last(0) > _MACD.Signal.Last(0) && _MACD.Signal.Last(0) < 0)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, volume, "korakodmy", StopLoss, TakeProfit, 5);
            }

            if (_MACD.MACD.Last(1) > _MACD.Signal.Last(1) && _MACD.MACD.Last(0) < _MACD.Signal.Last(0) && _MACD.Signal.Last(0) > 0)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, volume, "korakodmy", StopLoss, TakeProfit, 5);
            }

Best Regards,

Panagiotis


@PanagiotisCharalampous

cfuorvito@gmail.com
22 Sep 2017, 15:46

Hi again Panagiotis, thank you for taking the time to help me.  Now when I enter "anotherSeries" intp the MacdCrossOver() function I get 2 build errors.

Error CS1502: The best overloaded method match for 'cAlgo.API.Internals.IIndicatorsAccessor.MacdCrossOver(cAlgo.API.DataSeries, int, int, int)' has some invalid arguments

Error CS1503: Argument 1: cannot convert from 'cAlgo.API.Internals.MarketSeries' to 'cAlgo.API.DataSeries'

Any ideas what I'm doing wrong?

Many thanks

Claud

 

 


@cfuorvito@gmail.com

PanagiotisCharalampous
22 Sep 2017, 15:52

Hi cfuorvito@gmail.com,

It is because anotherSeries and AnotherTimeFrame are "dummy" variable names. Since you posted them in your code, I was with the impression that you will replace them yourself with whatever suits you. See below an example that builds using a daily timeframe and the Close values

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FileSystem)]
    public class MACD : Robot
    {

        [Parameter("Volume", DefaultValue = 1000)]
        public int volume { get; set; }

        [Parameter(DefaultValue = 50, MinValue = 1)]
        public int StopLoss { get; set; }

        [Parameter(DefaultValue = 50, MinValue = 1)]
        public int TakeProfit { 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 MacdCrossOver _MACD;

        protected override void OnBar()
        {
            var dailySeries = MarketData.GetSeries(TimeFrame.Daily);
            _MACD = Indicators.MacdCrossOver(anotherSeries.Close, LongCycle, ShortCycle, MACDPeriod);
            if (_MACD.MACD.Last(1) < _MACD.Signal.Last(1) && _MACD.MACD.Last(0) > _MACD.Signal.Last(0) && _MACD.Signal.Last(0) < 0)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, volume, "korakodmy", StopLoss, TakeProfit, 5);
            }

            if (_MACD.MACD.Last(1) > _MACD.Signal.Last(1) && _MACD.MACD.Last(0) < _MACD.Signal.Last(0) && _MACD.Signal.Last(0) > 0)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, volume, "korakodmy", StopLoss, TakeProfit, 5);
            }
        }

    }

}

Best Regards,

Panagiotis


@PanagiotisCharalampous

cfuorvito@gmail.com
22 Sep 2017, 17:01

Hi Panagiotis, thank you for your help.  I will work on it and keep you updated.

Kind regards,

Claud


@cfuorvito@gmail.com

diiptrade
05 Dec 2018, 16:50

It doesn't work. MACD require DataSeries istead of MarketSeries as you in your example.


@diiptrade

PanagiotisCharalampous
06 Dec 2018, 09:42

Hi D D,

Can you please explain what do you mean? I cannot see any problem in the example.

Best Regards,

Panagiotis


@PanagiotisCharalampous

leonardohurtado
11 Dec 2019, 23:00

RE:

PanagiotisCharalampous said:

Hi D D,

Can you please explain what do you mean? I cannot see any problem in the example.

Best Regards,

Panagiotis

Hello Mr. Panagiotis.

I know this is an old threat, but I was wondering if you could help me. I would like for this indicator to only handle 1 trade at a time, which would imply the indicator closing the current trade when the MACD crosses in the opposite direction, and opening the corresponding trade for that crossing.

I would appreciate your help so much, warm regards.

Leonardo Hurtado


@leonardohurtado

PanagiotisCharalampous
12 Dec 2019, 08:44

Hi Leonardo,

See below

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FileSystem)]
    public class MACD : Robot
    {

        [Parameter("Volume", DefaultValue = 1000)]
        public int volume { get; set; }

        [Parameter(DefaultValue = 50, MinValue = 1)]
        public int StopLoss { get; set; }

        [Parameter(DefaultValue = 50, MinValue = 1)]
        public int TakeProfit { 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 MacdCrossOver _MACD;

        protected override void OnBar()
        {
            var dailySeries = MarketData.GetSeries(TimeFrame.Daily);
            _MACD = Indicators.MacdCrossOver(dailySeries.Close, LongCycle, ShortCycle, MACDPeriod);
            if (Positions.Count(x => x.TradeType == TradeType.Buy) == 0 && _MACD.MACD.Last(1) < _MACD.Signal.Last(1) && _MACD.MACD.Last(0) > _MACD.Signal.Last(0) && _MACD.Signal.Last(0) < 0)
            {
                foreach (var position in Positions.Where(x => x.TradeType == TradeType.Sell))
                    position.Close();
                ExecuteMarketOrder(TradeType.Buy, Symbol, volume, "korakodmy", StopLoss, TakeProfit, 5);
            }

            if (Positions.Count(x => x.TradeType == TradeType.Sell) == 0 && _MACD.MACD.Last(1) > _MACD.Signal.Last(1) && _MACD.MACD.Last(0) < _MACD.Signal.Last(0) && _MACD.Signal.Last(0) > 0)
            {
                foreach (var position in Positions.Where(x => x.TradeType == TradeType.Buy))
                    position.Close();
                ExecuteMarketOrder(TradeType.Sell, Symbol, volume, "korakodmy", StopLoss, TakeProfit, 5);
            }
        }

    }

}

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

leonardohurtado
12 Dec 2019, 23:10

RE:

PanagiotisCharalampous said:

Hi Leonardo,

See below

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FileSystem)]
    public class MACD : Robot
    {

        [Parameter("Volume", DefaultValue = 1000)]
        public int volume { get; set; }

        [Parameter(DefaultValue = 50, MinValue = 1)]
        public int StopLoss { get; set; }

        [Parameter(DefaultValue = 50, MinValue = 1)]
        public int TakeProfit { 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 MacdCrossOver _MACD;

        protected override void OnBar()
        {
            var dailySeries = MarketData.GetSeries(TimeFrame.Daily);
            _MACD = Indicators.MacdCrossOver(dailySeries.Close, LongCycle, ShortCycle, MACDPeriod);
            if (Positions.Count(x => x.TradeType == TradeType.Buy) == 0 && _MACD.MACD.Last(1) < _MACD.Signal.Last(1) && _MACD.MACD.Last(0) > _MACD.Signal.Last(0) && _MACD.Signal.Last(0) < 0)
            {
                foreach (var position in Positions.Where(x => x.TradeType == TradeType.Sell))
                    position.Close();
                ExecuteMarketOrder(TradeType.Buy, Symbol, volume, "korakodmy", StopLoss, TakeProfit, 5);
            }

            if (Positions.Count(x => x.TradeType == TradeType.Sell) == 0 && _MACD.MACD.Last(1) > _MACD.Signal.Last(1) && _MACD.MACD.Last(0) < _MACD.Signal.Last(0) && _MACD.Signal.Last(0) > 0)
            {
                foreach (var position in Positions.Where(x => x.TradeType == TradeType.Buy))
                    position.Close();
                ExecuteMarketOrder(TradeType.Sell, Symbol, volume, "korakodmy", StopLoss, TakeProfit, 5);
            }
        }

    }

}

Best Regards,

Panagiotis 

Join us on Telegram

 

Thank you for the fast reply Mr. Panagiotis. I was wondering how to use this code, should I just add the entirety of it to Lucian's code. Or just replace some parts? I am pretty new with all of this. I truly appreciate your help. Best regards sir!

Leonardo Hurtado


@leonardohurtado

leonardohurtado
12 Dec 2019, 23:22

RE:

PanagiotisCharalampous said:

Hi Leonardo,

See below

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FileSystem)]
    public class MACD : Robot
    {

        [Parameter("Volume", DefaultValue = 1000)]
        public int volume { get; set; }

        [Parameter(DefaultValue = 50, MinValue = 1)]
        public int StopLoss { get; set; }

        [Parameter(DefaultValue = 50, MinValue = 1)]
        public int TakeProfit { 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 MacdCrossOver _MACD;

        protected override void OnBar()
        {
            var dailySeries = MarketData.GetSeries(TimeFrame.Daily);
            _MACD = Indicators.MacdCrossOver(dailySeries.Close, LongCycle, ShortCycle, MACDPeriod);
            if (Positions.Count(x => x.TradeType == TradeType.Buy) == 0 && _MACD.MACD.Last(1) < _MACD.Signal.Last(1) && _MACD.MACD.Last(0) > _MACD.Signal.Last(0) && _MACD.Signal.Last(0) < 0)
            {
                foreach (var position in Positions.Where(x => x.TradeType == TradeType.Sell))
                    position.Close();
                ExecuteMarketOrder(TradeType.Buy, Symbol, volume, "korakodmy", StopLoss, TakeProfit, 5);
            }

            if (Positions.Count(x => x.TradeType == TradeType.Sell) == 0 && _MACD.MACD.Last(1) > _MACD.Signal.Last(1) && _MACD.MACD.Last(0) < _MACD.Signal.Last(0) && _MACD.Signal.Last(0) > 0)
            {
                foreach (var position in Positions.Where(x => x.TradeType == TradeType.Buy))
                    position.Close();
                ExecuteMarketOrder(TradeType.Sell, Symbol, volume, "korakodmy", StopLoss, TakeProfit, 5);
            }
        }

    }

}

Best Regards,

Panagiotis 

Join us on Telegram

 

I figured it out sir! Thank you again. Very much.

Best regards from Canada, 

Leonardo Hurtado


@leonardohurtado

Prasanna
24 Mar 2020, 04:05

MACD bot

Hi,

I recently come across this MACD bot. I have a question. Is there a way to remove stop loss and take profit. Instead of SL or TP when the MACD generates opposite signal the previous position is closed and a new position opens until the MACD generates another signal to the opposite side. Any ideas?


@Prasanna

NyanHtet
12 May 2020, 17:14

wonder on number 5 at execute order?

what is the last 5 means?

 

ExecuteMarketOrder(TradeType.Buy, Symbol, volume, "korakodmy", StopLoss, TakeProfit, 5);

 

ExecuteMarketOrder(TradeType.Sell, Symbol, volume, "korakodmy", StopLoss, TakeProfit, 5);

 

regards,

Nyan


@NyanHtet

rtdias
23 May 2020, 13:03

RE: wonder on number 5 at execute order?

NyanHtet said:

what is the last 5 means?

 

ExecuteMarketOrder(TradeType.Buy, Symbol, volume, "korakodmy", StopLoss, TakeProfit, 5);

 

ExecuteMarketOrder(TradeType.Sell, Symbol, volume, "korakodmy", StopLoss, TakeProfit, 5);

 

regards,

Nyan

ExecuteMarketOrder SYNTAX:

ExecuteMarketOrder(TradeType tradeType, string symbolName, double volume, string label, double? stopLossPips, double? takeProfitPips, string comment, bool hasTrailingStop, StopTriggerMethod? stopLossTriggerMethod)

@rtdias

ctid2080802
31 Jul 2020, 01:19

RE: RE: wonder on number 5 at execute order?

rtdias said:

NyanHtet said:

what is the last 5 means?

 

ExecuteMarketOrder(TradeType.Buy, Symbol, volume, "korakodmy", StopLoss, TakeProfit, 5);

 

ExecuteMarketOrder(TradeType.Sell, Symbol, volume, "korakodmy", StopLoss, TakeProfit, 5);

 

regards,

Nyan

ExecuteMarketOrder SYNTAX:

ExecuteMarketOrder(TradeType tradeType, string symbolName, double volume, string label, double? stopLossPips, double? takeProfitPips, string comment, bool hasTrailingStop, StopTriggerMethod? stopLossTriggerMethod)

Hello all.

Please where can I search for a documentation in more details about apis of calgo. I didnt find until now, for example, docs about Indicators.MacdCrossOver and its subcollections objects.

Thanks.


@ctid2080802

PanagiotisCharalampous
31 Jul 2020, 08:58

Hi ctid2080802,

You can check the reference here. If you have any questions, just ask!

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous