My cbot work on backtest but not in the Market

Created at 13 Jul 2022, 15:49
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!
MA

makhzen

Joined 23.11.2020

My cbot work on backtest but not in the Market
13 Jul 2022, 15:49


Hello, i have some troubles with my cbot, it work on backtest i can see the the past trades but in live account no order are sent

Here the code of my cbot

Thank you for help

 

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class GoldScalperPro : Robot
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }


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

        [Parameter("Risk", DefaultValue = 1)]
        public double Risk { get; set; }


        [Parameter("ATR multiple", DefaultValue = 2)]
        public double ATRfactor { get; set; }
        [Parameter("Start Hour", DefaultValue = 7)]
        public double StartTime { get; set; }

        [Parameter("Stop Hour", DefaultValue = 18)]
        public double StopTime { get; set; }

        public long Volume;
        private DateTime _startTime;
        private DateTime _stopTime;
        private AverageTrueRange _averageTrueRange;
        public ExponentialMovingAverage SlowEMA;
        private MacdCrossOver _MACD;






        protected override void OnStart()
        {

            _MACD = Indicators.MacdCrossOver(26, 12, 9);

            _averageTrueRange = Indicators.AverageTrueRange(14, MovingAverageType.Exponential);
            SlowEMA = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 200);
            Positions.Closed += OnPositionClosed;
            _startTime = Server.Time.Date.AddHours(StartTime);
            _stopTime = Server.Time.Date.AddHours(StopTime);
            Volume = InitialVolume;


        }



        protected override void OnTick()
        {


            var currentHours = Server.Time.TimeOfDay.TotalHours;
            bool tradeTime = StartTime < StopTime ? currentHours > StartTime && currentHours < StopTime : currentHours < StopTime || currentHours > StartTime;

            var longPosition = Positions.Find("Gold Scalper Pro", SymbolName, TradeType.Buy);
            var shortPosition = Positions.Find("Gold Scalper Pro", SymbolName, TradeType.Sell);

            if (Trade.IsExecuting)
                return;

            {
                if (!tradeTime)
                    return;
                {


                    if (longPosition == null && Positions.Count(x => x.TradeType == TradeType.Buy) == 0)
                    {
                        if (MarketSeries.Close.Last(1) > SlowEMA.Result.Last(1) && (_MACD.MACD.Last(1) < _MACD.Signal.Last(1) && _MACD.MACD.Last(0) > _MACD.Signal.Last(0) && _MACD.Signal.Last(0) < 0))
                        {

                            Open(TradeType.Buy);


                        }
                        if (shortPosition == null && Positions.Count(x => x.TradeType == TradeType.Sell) == 0)
                        {
                            if (MarketSeries.Close.Last(1) < SlowEMA.Result.Last(1) && (_MACD.MACD.Last(1) > _MACD.Signal.Last(1) && _MACD.MACD.Last(0) < _MACD.Signal.Last(0) && _MACD.Signal.Last(0) > 0))


                                Open(TradeType.Sell);
                        }
                    }
                }
            }
        }


        private void Close(TradeType tradeType)
        {
            foreach (var position in Positions.FindAll("Gold Scalper Pro", Symbol, tradeType))
                ClosePosition(position);
        }



        private void Open(TradeType tradeType)
        {

            var atrInPips = _averageTrueRange.Result.Last(1) * (Symbol.TickSize / Symbol.PipSize * Math.Pow(10, Symbol.Digits));
            var StopLoss = (MarketSeries.Close.Last(1) + (_averageTrueRange.Result.Last(1) / Symbol.PipSize * ATRfactor));


            var TakeProfit = StopLoss;




            var cBotPositions = Positions.FindAll("Gold Scalper Pro");

            if (cBotPositions.Length >= 1)
                return;


            ExecuteMarketOrder(tradeType, Symbol, Volume, "Gold Scalper Pro", StopLoss, TakeProfit);

        }

        private long GetVolume(double risk, int stopLossPips)
        {


            var volume = Account.Equity / 10000 * Risk;
            var normalizedVolume = Symbol.NormalizeVolume(volume);

            return normalizedVolume;
        }

        private void OnPositionClosed(PositionClosedEventArgs args)
        {
            var risk = 0.25;
            var stopLossPips = 10;
            var volume = GetVolume(risk, stopLossPips);

            Print("Closed");

            var position = args.Position;

            if (position.Label != "Gold Scalper Pro" || position.SymbolCode != Symbol.Code)
                return;

            if (position.GrossProfit > 0)
            {
                Volume = volume;

            }

            else if (position.GrossProfit < 0)
            {
                Volume = position.Volume * 2;

            }
        }
    }
}


@makhzen
Replies

PanagiotisCharalampous
14 Jul 2022, 08:24

Hi makhzen,

Did you check your logs? Do you get any errors?

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

makhzen
14 Jul 2022, 12:41

RE:

PanagiotisCharalampous said:

Hi makhzen,

Did you check your logs? Do you get any errors?

Best Regards,

Panagiotis 

Join us on Telegram and Facebook

Hello Panagiotis ,

 

I restarted ctrader yesterday i forgot to check the log...but since yesterday i have this (from 

 

 

Maybe the problem is about the SL/TP based on ATR ? 

Thank you,

Makhzen


@makhzen

makhzen
14 Jul 2022, 12:50 ( Updated at: 21 Dec 2023, 09:22 )

RE: RE:

makhzen said:

PanagiotisCharalampous said:

Hi makhzen,

Did you check your logs? Do you get any errors?

Best Regards,

Panagiotis 

Join us on Telegram and Facebook

Hello Panagiotis ,

 

I restarted ctrader yesterday i forgot to check the log...but since yesterday i have this (from 

 

 

Maybe the problem is about the SL/TP based on ATR ? 

Thank you,

Makhzen

 

i think error come from here i changed

            var StopLoss = (MarketSeries.Close.Last(1) + (_averageTrueRange.Result.Last(1) / Symbol.PipSize * ATRfactor));
 

by

            var StopLoss = (MarketSeries.Close.Last(1) + (_averageTrueRange.Result.Last(1) / (Symbol.TickSize / Symbol.PipSize * Math.Pow(10, Symbol.Digits) * ATRfactor)));

 

You think it can be the source of the problem ?

 

Thank you


@makhzen

PanagiotisCharalampous
14 Jul 2022, 13:10

Hi makhzen,

Yes, your SL and TP need to be rounded.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous