Martingale sample bot backtest issue

Created at 24 Feb 2021, 22:34
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!
CT

ctid3179522

Joined 21.01.2021

Martingale sample bot backtest issue
24 Feb 2021, 22:34


   I have the latest Ctrader version, and while backtesting this bot the optimization backtest didnt match the simple bactktest at all- it gave completely different results, also it gave different result each time I ran it . I deleted Ctrader cache. The optimization gives slightly different result each time I ran it. I dont have this problem with other bots.  Maybe it happens to others or its something wrong with my system? I am building a bot based on it so I can't fully test the bot


@ctid3179522
Replies

PanagiotisCharalampous
25 Feb 2021, 09:21

Hi ctid3179522,

Can you share the cBot code?

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

ctid3179522
25 Feb 2021, 12:37

RE:

PanagiotisCharalampous said:

Hi ctid3179522,

Can you share the cBot code?

Best Regards,

Panagiotis 

Join us on Telegram

  It is the Sample Martingale bot that comes with Ctrader installation. nothing changed

 


@ctid3179522

ctid3179522
25 Feb 2021, 12:40

RE: RE:

ctid3179522 said:

PanagiotisCharalampous said:

Hi ctid3179522,

Can you share the cBot code?

Best Regards,

Panagiotis 

Join us on Telegram

  It is the Sample Martingale bot that comes with Ctrader installation. nothing changed but if you think i mistakenly changed something here it is:

// -------------------------------------------------------------------------------------------------
//
//    This code is a cTrader Automate API example.
//
//    This cBot is intended to be used as a sample and does not guarantee any particular outcome or
//    profit of any kind. Use it at your own risk.
//    
//    All changes to this file might be lost on the next application update.
//    If you are going to modify this file please make a copy using the "Duplicate" command.
//
//    The "Sample Martingale cBot" creates a random Sell or Buy order. If the Stop loss is hit, a new 
//    order of the same type (Buy / Sell) is created with double the Initial Volume amount. The cBot will 
//    continue to double the volume amount for  all orders created until one of them hits the take Profit. 
//    After a Take Profit is hit, a new random Buy or Sell order is created with the Initial Volume amount.
//
// -------------------------------------------------------------------------------------------------

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 SampleMartingalecBot : Robot
    {
        [Parameter("Initial Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double InitialQuantity { get; set; }

        [Parameter("Stop Loss", Group = "Protection", DefaultValue = 40)]
        public int StopLoss { get; set; }

        [Parameter("Take Profit", Group = "Protection", DefaultValue = 40)]
        public int TakeProfit { get; set; }


        private Random random = new Random();

        protected override void OnStart()
        {
            Positions.Closed += OnPositionsClosed;

            ExecuteOrder(InitialQuantity, GetRandomTradeType());
        }

        private void ExecuteOrder(double quantity, TradeType tradeType)
        {
            var volumeInUnits = Symbol.QuantityToVolumeInUnits(quantity);
            var result = ExecuteMarketOrder(tradeType, SymbolName, volumeInUnits, "Martingale", StopLoss, TakeProfit);

            if (result.Error == ErrorCode.NoMoney)
                Stop();
        }

        private void OnPositionsClosed(PositionClosedEventArgs args)
        {
            Print("Closed");
            var position = args.Position;

            if (position.Label != "Martingale" || position.SymbolName != SymbolName)
                return;

            if (position.GrossProfit > 0)
            {
                ExecuteOrder(InitialQuantity, GetRandomTradeType());
            }
            else
            {
                ExecuteOrder(position.Quantity * 2, position.TradeType);
            }
        }

        private TradeType GetRandomTradeType()
        {
            return random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell;
        }
    }
}

@ctid3179522

PanagiotisCharalampous
25 Feb 2021, 14:23

Hi ctid3179522,

If you notice the Sample Martingale cBot chooses trading direction randomly

        private TradeType GetRandomTradeType()
        {
            return random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell;
        }

So you cannot expect the results to be repeatable.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous