How to use MACD Crossover to calgo

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

Head of Community Management at cTrader
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

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