cAlgo coding for MACD bot

Created at 30 Mar 2020, 16:18
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!
PR

Prasanna

Joined 19.03.2020

cAlgo coding for MACD bot
30 Mar 2020, 16:18


Hi,

Can someone tell me how to start learning the cAlgo coding? I want to try and build a basic MACD crossover algo that buys when MACD crosses above signal line and sells when MACD crosses below the signal line.

I tried this one below but it has a problem that it only buys when the MACD crosses above zero line and sells when it crosses below zero line and by that time it is too late to open the position.

using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class MacdBot : Robot
    {
        private MacdHistogram _macd;
        private Position _position;

        [Parameter(DefaultValue = 10000, MinValue = 0)]
        public int Volume { 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; }

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

        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.Histogram.LastValue > 0.0 && _macd.Signal.IsRising() && !isLongPositionOpen)
            {
                ClosePosition();
                Buy();
            }

            if (_macd.Histogram.LastValue < 0.0 && _macd.Signal.IsFalling() && !isShortPositionOpen)
            {
                ClosePosition();
                Sell();
            }
        }
        private void ClosePosition()
        {
            if (_position != null)
            {
                Trade.Close(_position);
                _position = null;
            }
        }

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

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

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

    }
}


@Prasanna
Replies

PanagiotisCharalampous
31 Mar 2020, 08:13

Hi kittu.ce10,

To start learning how to program using the API, you can start by looking at our cTrader Automate guide and the cTrader Automate Reference. Let me know if you have any specific questions.

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

Prasanna
31 Mar 2020, 14:30

Hi,

Thanks for the reply. I have been going through some sample cBots in the cTrader and have two questions.

1) what is the purpose of the time zone function?

[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]

what happens if I change it to my timezone? for example UTC+11

2)     [Parameter(DefaultValue = 10000, MinValue = 0)]
        public int Volume { get; set; }

How to change the volume to lots?

 

These might seem very basic questions but I'm just starting learn this programming. So, I wanted to begin with thorough understanding.

I very much appreciate if someone could answer my questions

 

Thanks in advance


@Prasanna

PanagiotisCharalampous
31 Mar 2020, 15:12

Hi kittu.ce10,

1) If you change the timezone then the Server.Time will give you time in that timezone.

2) Volume is always set in units. If you want to use Lots as a parameter you will then need to convert it to volume using Symbol.QuantityToVolumeInUnits()

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

Prasanna
31 Mar 2020, 17:16

Thanks for the reply.

Now I understood. I was actually confused with Volume to Quantity and Quantity to Volume functions but I have come across another thread where you explained this issue previously.

 

Thanks for the help.


@Prasanna