backtesting and optimization not possible

Created at 11 May 2015, 17:05
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!
TR

tradermatrix

Joined 24.07.2012

backtesting and optimization not possible
11 May 2015, 17:05


to delay the startup or slow certain function of my robot,
j 'uses this solution.

using System.Diagnostics;
using System.Threading;

[Parameter("minute", DefaultValue = 1)]
        public int Minute { get; set; }

   protected override void OnStart()

{

 var stopwatch = Stopwatch.StartNew();
            System.Threading.Thread.Sleep(Minute * 60000);
            Console.WriteLine(stopwatch.ElapsedMilliseconds);

......

it works well,

.but I am having a problem ...
I can not make Bactesting (endless), or optimization.

do you think that there is a solution?

I'll give you an example with sample martingale.
I have changed the code to delay between winning trades.


it works very well in demo or real, but backtesting or optimization impossible ...


 cordially

/////////////////////////////////////////////////////////////////////////////////////////////////////////

// -------------------------------------------------------------------------------------------------
//
//    This code is a cAlgo API sample.
//
//    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
//
//    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;
using System.Diagnostics;
using System.Threading;


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

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

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

        [Parameter("delay minute", DefaultValue = 1)]
        public int Minute { get; set; }

        private Random random = new Random();

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

            Order1();
        }
        private void Order1()
        {
            var stopwatch = Stopwatch.StartNew();
            System.Threading.Thread.Sleep(Minute * 60000);
            Console.WriteLine(stopwatch.ElapsedMilliseconds);


            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.Pips > 0)
                    Order1();

                if (position.GrossProfit > 0)
                {
                    ExecuteOrder(InitialVolume, GetRandomTradeType());
                }
                else
                {


                    ExecuteOrder((int)position.Volume * 2, position.TradeType);
                }
            }
        }


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





 


@tradermatrix
Replies

bosma
11 May 2015, 19:41

It would be great if cAlgo would override System Time while back-testing to equal the time it would have been. I'm not positive what you are trying to do in OnPositionClosed, so I don't get your functionality completely, but something like this would work (have to use 1min charts).

 

// -------------------------------------------------------------------------------------------------
//
//    This code is a cAlgo API sample.
//
//    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
//
//    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 Volume", DefaultValue = 10000, MinValue = 0)]
        public int InitialVolume { get; set; }
 
        [Parameter("Stop Loss", DefaultValue = 10)]
        public int StopLoss { get; set; }
 
        [Parameter("Take Profit", DefaultValue = 10)]
        public int TakeProfit { get; set; }
 
        [Parameter("delay minute", DefaultValue = 1)]
        public int Minute { get; set; }
 
        private Random random = new Random();
        
        private DateTime StartTimer;
        private bool OpenAfterTimer = false;
 
        protected override void OnStart()
        {
            Positions.Closed += OnPositionsClosed;
        }
        
        protected override void OnBar()
        {
            // OpenAfterTimer is only true only when a position has been closed (and before another opened)
            if (OpenAfterTimer)
            {
                // If Minute number of minutes has passed
                if (MarketSeries.OpenTime.LastValue.Subtract(StartTimer).TotalMinutes >= Minute)
                    ExecuteOrder(InitialVolume, GetRandomTradeType());
            }
            else
            {
                // Remember when we've opened our position
                StartTimer = MarketSeries.OpenTime.LastValue;
                OpenAfterTimer = false;
                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)
        {
            var position = args.Position;
            {
                if (position.Label != "Martingale" || position.SymbolCode != Symbol.Code)
                    return;
                if (position.Pips > 0)
                    OpenAfterTimer = true;
 
                if (position.GrossProfit > 0)
                {
                    ExecuteOrder(InitialVolume, GetRandomTradeType());
                }
                else
                {
                    ExecuteOrder((int)position.Volume * 2, position.TradeType);
                }
            }
        }
 
        private TradeType GetRandomTradeType()
        {
            return random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell;
        }
    }
}

 


@bosma

tradermatrix
17 May 2015, 19:27

thank you,
it's a shame that the stopwatch system is not considered in backtesting(delayed backtesting) and optimization.il is very simple to use.
cordially.


@tradermatrix