MACD Crossover Bot (Help Required)

frederickjjayres since: 03 Dec 2019; 
 03 Dec 2019, 04:44
MACD Crossover Bot (Help Required)

So to provide some context, I'm not a developer/coder by any stretch of the imagination.

What I have below is a compilation of a few scripts I've seen. The ultimate goal I'm after is a bot that trades MACD Crossovers (26, 12, 9), that enters long and short positions on those crossovers, but also has a stop-loss built in to make sure any losses in this strategy are limited.

This code currently doesn't work, so I'm hoping there might be someone out there that might be able to "fix" a couple of the errors in it. At the current stage of writing, the errors are in lines 82 and 87.

Below is the code I have so far:

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 (Trade.IsExecuting)
                return;

		bool isLongPositionOpen = _position != null && _position.TradeType == TradeType.Buy;
           	bool isShortPositionOpen = _position != null && _position.TradeType == TradeType.Sell;


            if (_MACD.MACD.Last(1) < _MACD.Signal.Last(1) && _MACD.MACD.Last(0) > _MACD.Signal.Last(0) && _MACD.Signal.Last(0) < 0)
            {
                ClosePosition();
		Buy();
            }

            if (_MACD.MACD.Last(1) > _MACD.Signal.Last(1) && _MACD.MACD.Last(0) < _MACD.Signal.Last(0) && _MACD.Signal.Last(0) > 0)
            {

		ClosePosition();                
		Sell();

            }
		private void ClosePosition()
        {
            if (_position != null)
            {
                Trade.Close(_position);
                _position = null;
            }
        }

        private void Buy()
        {
            Trade.CreateBuyMarketOrder(Symbol, Volume, StopLoss);
        }

        private void Sell()
        {
            Trade.CreateSellMarketOrder(Symbol, Volume, StopLoss);
        }

        protected override void OnPositionOpened(Position openedPosition)
        {
            _position = openedPosition;
        }

    }
}
Panagiotis Charalampous's avatar
Panagiotis Charalampous since: 13 Jan 2017; 
 03 Dec 2019, 08:39

Hi frederickjjayres,

I would suggest you get the help of a professional to write the code for you. Even if we fix the compilation issues, I can see some logical bugs, so I don't believe this will work as you expect.

Best Regards,

Panagiotis

Head of Community Management at cTrader