LoadMoreHistory example for MultiTimeFrame with Backtesting workaround

Created at 04 Sep 2020, 18: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!
XS

xsteel1

Joined 23.10.2016

LoadMoreHistory example for MultiTimeFrame with Backtesting workaround
04 Sep 2020, 18:20


Hi,

   I have a multi timeframe robot that requires more history on Monthly and Weekly charts than is available in Backtesting when I start at the date I want to test from..

Following this thread I was able to set a much earlier start date in Backtesting while wrapping my code in a DateTime condition check OnTick : 

 

This allows me to get more history on higher timeframes like weekly etc while preventing my code from running until the backtesting date passes the date I want to start my code 'AnalysisStartDate' which is set in the parameters.

When Running in RealTime this cBot loads all the history it can on eight timeframes (this can take several minutes) and is intended as a crude example of a multi time frame implimentation.

I've been struggling to figure this out for a while so though I'd share my example to help others :)

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter("Timeframe0", DefaultValue = "Minute")]
        public TimeFrame tf0 { get; set; }
        [Parameter("Timeframe1", DefaultValue = "Minute5")]
        public TimeFrame tf1 { get; set; }
        [Parameter("Timeframe2", DefaultValue = "Minute15")]
        public TimeFrame tf2 { get; set; }
        [Parameter("Timeframe3", DefaultValue = "Hour")]
        public TimeFrame tf3 { get; set; }
        [Parameter("Timeframe4", DefaultValue = "Hour4")]
        public TimeFrame tf4 { get; set; }
        [Parameter("Timeframe5", DefaultValue = "Daily")]
        public TimeFrame tf5 { get; set; }
        [Parameter("Timeframe6", DefaultValue = "Weekly")]
        public TimeFrame tf6 { get; set; }
        [Parameter("Timeframe7", DefaultValue = "Monthly")]
        public TimeFrame tf7 { get; set; }

        TimeFrame[] Frames = new TimeFrame[8];

        DateTime Frames0StartDate;
        DateTime Frames1StartDate;
        DateTime Frames2StartDate;
        DateTime Frames3StartDate;
        DateTime Frames4StartDate;
        DateTime Frames5StartDate;
        DateTime Frames6StartDate;
        DateTime Frames7StartDate;

        [Parameter(DefaultValue = 0)]
        public int Minute { get; set; }
        [Parameter(DefaultValue = 0)]
        public int Hour { get; set; }
        [Parameter(DefaultValue = 19)]
        public int Day { get; set; }
        [Parameter(DefaultValue = 3)]
        public int Month { get; set; }
        [Parameter(DefaultValue = 2020)]
        public int Year { get; set; }

        DateTime AnalysisStartDate;

        protected override void OnStart()
        {
            AnalysisStartDate = new DateTime(Year, Month, Day, Hour, Minute, 0);
            // Insert normal OnStart code for RealTime
            if (RunningMode == RunningMode.RealTime)
            {
                Frames[0] = tf0;
                Frames[1] = tf1;
                Frames[2] = tf2;
                Frames[3] = tf3;
                Frames[4] = tf4;
                Frames[5] = tf5;
                Frames[6] = tf6;
                Frames[7] = tf7;

                Bars series0 = MarketData.GetBars(Frames[0]);
                Bars series1 = MarketData.GetBars(Frames[1]);
                Bars series2 = MarketData.GetBars(Frames[2]);
                Bars series3 = MarketData.GetBars(Frames[3]);
                Bars series4 = MarketData.GetBars(Frames[4]);
                Bars series5 = MarketData.GetBars(Frames[5]);
                Bars series6 = MarketData.GetBars(Frames[6]);
                Bars series7 = MarketData.GetBars(Frames[7]);

                Print("Frames0StartDate = " + Frames0StartDate);
                while (Frames0StartDate != series0.OpenTimes[1])
                {
                    Frames0StartDate = series0.OpenTimes[1];
                    Print("Frames0StartDate = " + Frames0StartDate);
                    series0.LoadMoreHistory();
                }
                Print("Finnished loading more history for a total of " + series0.Count + " bars going back to " + series0.OpenTimes[1]);
                Print("Frames1StartDate = " + Frames1StartDate);
                while (Frames1StartDate != series1.OpenTimes[1])
                {
                    Frames1StartDate = series1.OpenTimes[1];
                    Print("Frames1StartDate = " + Frames1StartDate);
                    series1.LoadMoreHistory();
                }
                Print("Finnished loading more history for a total of " + series1.Count + " bars going back to " + series1.OpenTimes[1]);
                Print("Frames2StartDate = " + Frames2StartDate);
                while (Frames2StartDate != series2.OpenTimes[1])
                {
                    Frames2StartDate = series2.OpenTimes[1];
                    Print("Frames2StartDate = " + Frames2StartDate);
                    series2.LoadMoreHistory();
                }
                Print("Finnished loading more history for a total of " + series2.Count + " bars going back to " + series2.OpenTimes[1]);
                Print("Frames3StartDate = " + Frames3StartDate);
                while (Frames3StartDate != series3.OpenTimes[1])
                {
                    Frames3StartDate = series3.OpenTimes[1];
                    Print("Frames3StartDate = " + Frames3StartDate);
                    series3.LoadMoreHistory();
                }
                Print("Finnished loading more history for a total of " + series3.Count + " bars going back to " + series3.OpenTimes[1]);
                Print("Frames4StartDate = " + Frames4StartDate);
                while (Frames4StartDate != series4.OpenTimes[1])
                {
                    Frames4StartDate = series4.OpenTimes[1];
                    Print("Frames4StartDate = " + Frames4StartDate);
                    series4.LoadMoreHistory();
                }
                Print("Finnished loading more history for a total of " + series4.Count + " bars going back to " + series4.OpenTimes[1]);
                Print("Frames5StartDate = " + Frames5StartDate);
                while (Frames5StartDate != series5.OpenTimes[1])
                {
                    Frames5StartDate = series5.OpenTimes[1];
                    Print("Frames5StartDate = " + Frames5StartDate);
                    series5.LoadMoreHistory();
                }
                Print("Finnished loading more history for a total of " + series5.Count + " bars going back to " + series5.OpenTimes[1]);
                Print("Frames6StartDate = " + Frames6StartDate);
                while (Frames6StartDate != series6.OpenTimes[1])
                {
                    Frames6StartDate = series6.OpenTimes[1];
                    Print("Frames6StartDate = " + Frames6StartDate);
                    series6.LoadMoreHistory();
                }
                Print("Finnished loading more history for a total of " + series6.Count + " bars going back to " + series6.OpenTimes[1]);
                Print("Frames7StartDate = " + Frames7StartDate);
                while (Frames7StartDate != series7.OpenTimes[1])
                {
                    Frames7StartDate = series7.OpenTimes[1];
                    Print("Frames7StartDate = " + Frames7StartDate);
                    series7.LoadMoreHistory();
                }
                Print("Finnished loading more history for a total of " + series7.Count + " bars going back to " + series7.OpenTimes[1]);

                Print("Finnished loading more history on series0 for a total of " + series0.Count + " bars going back to " + series0.OpenTimes[1]);
                Print("Finnished loading more history on series1 for a total of " + series1.Count + " bars going back to " + series1.OpenTimes[1]);
                Print("Finnished loading more history on series2 for a total of " + series2.Count + " bars going back to " + series2.OpenTimes[1]);
                Print("Finnished loading more history on series3 for a total of " + series3.Count + " bars going back to " + series3.OpenTimes[1]);
                Print("Finnished loading more history on series4 for a total of " + series4.Count + " bars going back to " + series4.OpenTimes[1]);
                Print("Finnished loading more history on series5 for a total of " + series5.Count + " bars going back to " + series5.OpenTimes[1]);
                Print("Finnished loading more history on series6 for a total of " + series6.Count + " bars going back to " + series6.OpenTimes[1]);
                Print("Finnished loading more history on series7 for a total of " + series7.Count + " bars going back to " + series7.OpenTimes[1]);
            }
        }

        protected override void OnTick()
        {
            if (RunningMode != RunningMode.RealTime)
            {
                // Insert normal OnStart code for RealTime to only run after Server.Time.Date >= AnalysisStartDate
                if (Server.Time.Date >= AnalysisStartDate)
                {
                    //Print("Finnished " + AnalysisStartDate);
                }
            }
        }

 

 

 


@xsteel1