Simple MACD

Created at 20 Sep 2014, 15:00
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!
YM

Ymer

Joined 03.05.2014

Simple MACD
20 Sep 2014, 15:00


Hi There, i trying to build a simple cbot based on MACD. Basically, the cbot should open a buy position when the macd goes up through the signal line and vice versa. Proble is looks like the values of the MACD.Histogram i get are not the correct ones.

I have the following but it does open and close positions each time the macd goes through the signal line.

Thanks for ur help.

using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.API.Requests;
using cAlgo.Indicators;
using System.Collections.Generic;
using System.Text;
using System.IO;

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

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

        [Parameter("Period", DefaultValue = 7)]
        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;
            Print(_macd.Histogram.LastValue); 
            
            if (_macd.Histogram.LastValue > 0 && !isLongPositionOpen)
            {
                ClosePosition();
                Buy();
            }

            if (_macd.Histogram.LastValue < 0 && !isShortPositionOpen)
            {
                ClosePosition();
                Sell();
            }
        }

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

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


@Ymer
Replies

Ymer
23 Sep 2014, 22:13

Hi there, is Spotware actually checking this thread? Hope i can get an answer to the above! Thanks


@Ymer

Spotware
24 Sep 2014, 09:52

We can recommend you to contact one of our Partners or post a job in Jobs section.


@Spotware