how to apply Multi-Symbol Backtesting ?

Created at 16 Dec 2019, 14:20
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!
US

useretinv

Joined 15.04.2016

how to apply Multi-Symbol Backtesting ?
16 Dec 2019, 14:20


how to apply new Multi-Symbol Back testing code at this ctrader Sample Martingale Robot Plz  

 

 

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class marto : Robot
    {
        [Parameter("Initial Volume", DefaultValue = 10000, MinValue = 0)]
        public int InitialVolume { get; set; }

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

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

        private Random random = new Random();

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

            ExecuteOrder(InitialVolume, GetRandomTradeType());
        }

        private void ExecuteOrder(long volume, TradeType tradeType)
        {
            var result = ExecuteMarketOrder(tradeType, Symbol, volume, "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.SymbolCode != Symbol.Code)
                return;

            if (position.GrossProfit > 0)
            {
                ExecuteOrder(InitialVolume, GetRandomTradeType());
            }
            else
            {
                if (position.TradeType == TradeType.Buy)
                    ExecuteOrder((int)position.Volume * 2, TradeType.Sell);
                else
                    ExecuteOrder((int)position.Volume * 2, TradeType.Buy);
            }
        }

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

 


@useretinv
Replies

PanagiotisCharalampous
16 Dec 2019, 14:28

Hi useretinv,

I am not sure what do you mean when you say.

to apply new Multi-Symbol Back testing code

This cBot is not a multisymbol cBot. It just trades one symbol. Do you want this cBot to trade on many symbols at the same time?

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

useretinv
16 Dec 2019, 14:35

RE:

PanagiotisCharalampous said:

Hi useretinv,

I am not sure what do you mean when you say.

to apply new Multi-Symbol Back testing code

This cBot is not a multisymbol cBot. It just trades one symbol. Do you want this cBot to trade on many symbols at the same time?

Best Regards,

Panagiotis 

 yes , i want this code to trade on many symbols at the same time .


@useretinv

PanagiotisCharalampous
16 Dec 2019, 14:50

RE:

Hi useretinv,

See an example below with the cBot trading on two symbols.

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class marto : Robot
    {
        [Parameter("Initial Volume", DefaultValue = 10000, MinValue = 0)]
        public int InitialVolume { get; set; }

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

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

        private Random random = new Random();

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

            ExecuteOrder("EURUSD", InitialVolume, GetRandomTradeType());
            ExecuteOrder("GBPUSD", InitialVolume, GetRandomTradeType());
        }

        private void ExecuteOrder(string symbol, long volume, TradeType tradeType)
        {
            var result = ExecuteMarketOrder(tradeType, symbol, volume, "Martingale", StopLoss, TakeProfit);

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

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

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

            if (position.GrossProfit > 0)
            {
                ExecuteOrder(position.SymbolName, InitialVolume, GetRandomTradeType());
            }
            else
            {
                if (position.TradeType == TradeType.Buy)
                    ExecuteOrder(position.SymbolName, (int)position.Volume * 2, TradeType.Sell);
                else
                    ExecuteOrder(position.SymbolName, (int)position.Volume * 2, TradeType.Buy);
            }
        }

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

You can modify this accordingly

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous