Category Trend  Published on 17/08/2020

MACD cross over Free

Description

This bot use MACD Cross Over

1. Signal-line crossover

2. Zero crossover

 


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

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class NdnghiaMACD : Robot
    {
        private MacdCrossOver _MACD;

        [Parameter("Initial Volume Percent", DefaultValue = 1, MinValue = 0)]
        public double InitialVolumePercent { get; set; }

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

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

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

        [Parameter("Stop Loss", DefaultValue = 100)]
        public int StopLoss { get; set; }
        [Parameter("Take Profit", DefaultValue = 100)]
        public int TakeProfit { get; set; }

        [Parameter("Signal-line crossover true:if Signal-line crossover false: Zero crossover", DefaultValue = true)]
        public bool IsSignalLineCrossover { get; set; }

        protected override void OnStart()
        {
            _MACD = Indicators.MacdCrossOver(LongCycle, ShortCycle, Period);
        }

        protected override void OnBar()
        {
            var volumne = Math.Floor(Account.Balance * 10 * InitialVolumePercent / 1000) * 1000;

            if (IsSignalLineCrossover)
            {


                if (_MACD.MACD.Last(2) < _MACD.Signal.Last(2) && _MACD.MACD.Last(1) > _MACD.Signal.Last(1))
                {
                    var position = Positions.Find("NdnghiaMACD");
                    if (position != null && position.TradeType == TradeType.Sell)
                    {
                        position.Close();
                    }
                    ExecuteMarketOrder(TradeType.Buy, Symbol.Name, volumne, "NdnghiaMACD", 0, 0);

                    // ExecuteMarketOrder(TradeType.Buy, Symbol.Name, volumne, "NdnghiaMACD", StopLoss, TakeProfit);
                }

                if (_MACD.MACD.Last(2) > _MACD.Signal.Last(2) && _MACD.MACD.Last(1) < _MACD.Signal.Last(1))
                {
                    var position = Positions.Find("NdnghiaMACD");
                    if (position != null && position.TradeType == TradeType.Buy)
                    {
                        position.Close();
                    }
                    ExecuteMarketOrder(TradeType.Sell, Symbol.Name, volumne, "NdnghiaMACD", 0, 0);
                    // ExecuteMarketOrder(TradeType.Sell, Symbol.Name, volumne, "NdnghiaMACD", StopLoss, TakeProfit);
                }
            }
            //Zero cross over
            else
            {
                if (_MACD.MACD.Last(1) > 0 && _MACD.MACD.Last(2) < 0)
                {
                    //up
                    ExecuteMarketOrder(TradeType.Buy, Symbol.Name, volumne, "NdnghiaMACD", StopLoss, TakeProfit);
                }
                else if (_MACD.MACD.Last(1) < 0 && _MACD.MACD.Last(2) > 0)
                {
                    //Down
                    ExecuteMarketOrder(TradeType.Sell, Symbol.Name, volumne, "NdnghiaMACD", StopLoss, TakeProfit);
                }
            }
        }
    }



}


nghiand.amz's avatar
nghiand.amz

Joined on 06.07.2020

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: NdnghiaMACD.algo
  • Rating: 0
  • Installs: 3114
  • Modified: 13/10/2021 09:54
Comments
Log in to add a comment.
DA
daedalus09 · 1 year ago

nevermind….. i fixed it.

DA
daedalus09 · 1 year ago

Nguyen, for some reason the bot does not set TP and SL, at least not for small values below 10. Could you look into this? I think your bot has a lot of potential and i would love to use but it's useless without TP and SL being set by the bot.

VI
vilberto.barbati · 2 years ago

It works only on Forex and not in CFD

Somebody can tell me how can I make this EA just with Long or short. What I hould add to the code?

CA
caglar_G · 4 years ago

Doesnt do anything when trying to backtest it