MACD crossover

Created at 12 Oct 2022, 22:08
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!
EV

evan.werry

Joined 12.10.2022

MACD crossover
12 Oct 2022, 22:08


Hello,

 

Wrote this code and build it in Ctrader, Cbot.

No errors arose when building the Cbot, but when back testing it only keeps loading and will not process anything.

Does anybody have a solution to this problem?

See code below:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class MACD : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }
        
        // create indicator variables
        private AverageTrueRange atr;
        private MacdCrossOver macd;
        
        protected override void OnStart()
        {
            // Load indicators on start up
            atr = Indicators.AverageTrueRange(14, MovingAverageType.Exponential);
            macd = Indicators.MacdCrossOver(6, 12, 9);                 
           
        }

        protected override void OnBar()
        {
            //Calculate Trade Amount based on ATR
            var PrevATR = Math.Round(atr.Result.Last(1) / Symbol.PipSize);
            var TradeAmount = (Account.Equity * 0.02) / (1.5 * PrevATR * Symbol.PipValue);
            TradeAmount = Symbol.NormalizeVolumeInUnits(TradeAmount, RoundingMode.Down);
            
            //Zero Line Cross Example
            var Histogram = macd.Histogram.Last(1);
            var PrevHistogram = macd.Histogram.Last(2);
            
            //Check for trade signal
            if (Histogram > 0 & PrevHistogram < 0)
            {
               ExecuteMarketOrder(TradeType.Buy, SymbolName, TradeAmount, "MACD", 1.5 * PrevATR, PrevATR); 
            }
            else if (Histogram < 0 & PrevHistogram > 0)
            {
               ExecuteMarketOrder(TradeType.Sell, SymbolName, TradeAmount, "MACD", 1.5 * PrevATR, PrevATR);  
            }
            
            
        }

        protected override void OnStop()
        {
            // Handle cBot stop here
        }
    }
}

 

Thank you in advance for the feedback.

 


@evan.werry
Replies

PanagiotisChar
13 Oct 2022, 08:54

Hi there,

Do you run this on any virtualization technology? Try setting AccessRights to FullAccess.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

 


@PanagiotisChar