backtesting and UTC

Created at 02 Jul 2018, 12:25
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 UTC
02 Jul 2018, 12:25


Hello
I use this method (below,adapted for example to Sample RSI cBot) to pause my robot on Friday: for example at 16h.
the problem is the backtesting over several years ....
because UTC changes every 6 months ..
is there another method that takes into account UTC automatically...?
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 RSI cBot" will create a buy order when the Relative Strength Index indicator crosses the  level 30, 
//    and a Sell order when the RSI indicator crosses the level 70. The order is closed be either a Stop Loss, defined in 
//    the "Stop Loss" parameter, or by the opposite RSI crossing signal (buy orders close when RSI crosses the 70 level 
//    and sell orders are closed when RSI crosses the 30 level). 
//
//    The cBot can generate only one Buy or Sell order at any given time.
//
// -------------------------------------------------------------------------------------------------

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 SampleRSIcBot : Robot
    {

        [Parameter("Current Time  UTC", DefaultValue = 2)]
        public double UTC { get; set; }

        [Parameter("Break time     (hour)", DefaultValue = 16, MinValue = 0, MaxValue = 23)]
        public int StopTime { get; set; }

        [Parameter("Source")]
        public DataSeries Source { get; set; }

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

        [Parameter("Quantity (Lots)", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

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

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


        private RelativeStrengthIndex rsi;
        private DateTime _stopTime;
        protected override void OnStart()
        {
            _stopTime = Server.Time.Date.AddHours(StopTime);

            rsi = Indicators.RelativeStrengthIndex(Source, Periods);
        }

        protected override void OnTick()
        {

            var currentHours = Server.Time.TimeOfDay.Hours + UTC;
            var currentDay = Server.Time.DayOfWeek;

            if (currentDay >= DayOfWeek.Friday && currentHours >= StopTime)

                return;

            if (rsi.Result.LastValue < 30)
            {
                Close(TradeType.Sell);
                Open(TradeType.Buy);
            }
            else if (rsi.Result.LastValue > 70)
            {
                Close(TradeType.Buy);
                Open(TradeType.Sell);
            }
        }

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

        private void Open(TradeType tradeType)
        {
            var position = Positions.Find("SampleRSI", Symbol, tradeType);
            var volumeInUnits = Symbol.QuantityToVolume(Quantity);

            if (position == null)
                ExecuteMarketOrder(tradeType, Symbol, volumeInUnits, "SampleRSI", SL, TP);
        }
    }
}

 


@tradermatrix
Replies

PanagiotisCharalampous
03 Jul 2018, 12:02

Hi tradermatrix,

What does the UTC parameter do?

Best Regards,

Panagiotis


@PanagiotisCharalampous

solark
03 Jul 2018, 20:41

Easiest would be to just use a local timezone by modifying

 

[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]

 

Now that may cause an issue as I believe JPY adjusts for DST out of sync with UK so it depends on what time you're targeting.  If you'd rather not change that attribute the next best thing is to use the DateTime.ToLocalTime() instance method, perform the time modification and then use the DateTime.ToUniversalTime() instance method if needed.

To acount for various timezones you would do something like

 

var timeZone = TimeZoneInfo.GetSystemTimeZones().Where(x => true).First(); // set an appropriate where condition
var d = new DateTime(someDate.Ticks, DateTimeKind.Unspecified) + someTimeSpan;
var utcTime = TimeZoneInfo.ConvertTimeToUtc(d, timeZone);

 

Hope that helps!


@solark

tradermatrix
04 Jul 2018, 12:06

Thank you for your answers
I specify that [Parameter ("Break time (hour)" does not stop the robot. The cbot continues to work until he hits SL and TP or net earnings ... etc .... Then he will not be able to place new orders until the opening of the markets the following week.
UTC = User Time Offset
that I change bottom right of the platform ... UTC + 2 in summer ... UTC + 1 in winter (France)

http://help.spotware.com/user-time-offset

my code works very well but I have to manually adjust to my time zone "UTC" (left on my cBot) so that "StopTime" is adapted to the place where I work.
it is not a problem except for the backtesting over several years because I have to choose between summer time UTC + 2 and UTC + 1 winters
that's why I'm looking for a code that automatically adapts to the time zone.
I miss something or it's not possible.

thank you for everything

example with UTC Friday 8 June

[Parameter("Current Time UTC", DefaultValue = 2)]

public double UTC { get; set; }

[Parameter("Break time (hour)", DefaultValue = 16, MinValue = 0, MaxValue = 23)]

public int StopTime { get; set; }

var currentHours = Server.Time.TimeOfDay.Hours + UTC;

 

08/06/2018 08:27	EURUSD	€ 1k	Vendre	1,17766	1,17608	08/06/2018 10:22	1,27	1028,97
08/06/2018 08:37	EURUSD	€ 1k	Acheter	1,17843	1,17603	08/06/2018 10:22	-2,13	1026,84
08/06/2018 09:39	EURUSD	€ 3k	Vendre	1,17691	1,17608	08/06/2018 10:22	1,85	1028,69
08/06/2018 10:21	EURUSD	€ 4k	Vendre	1,17619	1,17608	08/06/2018 10:22	0,02	1028,71
08/06/2018 10:22	EURUSD	€ 1k	Acheter	1,17608	1,17694	08/06/2018 11:20	0,66	1029,37
08/06/2018 10:43	EURUSD	€ 2k	Acheter	1,17679	1,17694	08/06/2018 11:20	0,08	1029,45
08/06/2018 11:20	EURUSD	€ 1k	Vendre	1,17694	1,17609	08/06/2018 12:02	0,65	1030,1
08/06/2018 12:02	EURUSD	€ 1k	Acheter	1,17609	1,1728	08/06/2018 13:13	-2,9	1027,2
08/06/2018 12:05	EURUSD	€ 1k	Vendre	1,1748	1,17285	08/06/2018 13:13	1,59	1028,79
08/06/2018 12:58	EURUSD	€ 3k	Vendre	1,1738	1,17285	08/06/2018 13:13	2,16	1030,95
08/06/2018 13:11	EURUSD	€ 4k	Vendre	1,17309	1,17285	08/06/2018 13:13	0,46	1031,41
08/06/2018 13:13	EURUSD	€ 1k	Vendre	1,1728	1,17534	08/06/2018 14:03	-2,25	1029,16
08/06/2018 13:20	EURUSD	€ 1k	Acheter	1,17366	1,17529	08/06/2018 14:03	1,31	1030,47
08/06/2018 13:42	EURUSD	€ 3k	Acheter	1,17439	1,17529	08/06/2018 14:03	2,03	1032,5
08/06/2018 13:51	EURUSD	€ 4k	Acheter	1,17512	1,17529	08/06/2018 14:03	0,22	1032,72
10/06/2018 23:05	EURUSD	€ 1k	Acheter	1,17742	1,17816	11/06/2018 01:41	0,55	1033,27

example code without UTC
the bot continues to place orders after 2 pm until 4 pm
2 hours of +

421	08/06/2018 12:59	EURUSD	€ 1k	Vendre	1,1735	1,17568	08/06/2018 14:27	-1,95	1163,87
421	08/06/2018 13:11	EURUSD	€ 2k	Vendre	1,17309	1,17568	08/06/2018 14:27	-4,61	1159,26
421	08/06/2018 13:26	EURUSD	€ 1k	Acheter	1,174	1,17563	08/06/2018 14:27	1,31	1160,57
421	08/06/2018 13:42	EURUSD	€ 4k	Acheter	1,17439	1,17563	08/06/2018 14:27	3,88	1164,45
421	08/06/2018 13:44	EURUSD	€ 5k	Acheter	1,17484	1,17563	08/06/2018 14:27	2,94	1167,39
421	08/06/2018 14:03	EURUSD	€ 6k	Acheter	1,17534	1,17563	08/06/2018 14:27	0,95	1168,34
421	08/06/2018 14:27	EURUSD	€ 1k	Acheter	1,17568	1,17379	08/06/2018 14:43	-1,7	1166,64
421	08/06/2018 14:32	EURUSD	€ 1k	Vendre	1,17521	1,17384	08/06/2018 14:43	1,09	1167,73
421	08/06/2018 14:35	EURUSD	€ 3k	Vendre	1,17474	1,17384	08/06/2018 14:43	2,03	1169,76
421	08/06/2018 14:40	EURUSD	€ 4k	Vendre	1,17431	1,17384	08/06/2018 14:43	1,25	1171,01
421	08/06/2018 14:43	EURUSD	€ 1k	Acheter	1,17384	1,17479	08/06/2018 15:00	0,73	1171,74
421	08/06/2018 14:45	EURUSD	€ 2k	Acheter	1,17429	1,17479	08/06/2018 15:00	0,68	1172,42
421	08/06/2018 14:57	EURUSD	€ 3k	Acheter	1,17461	1,17479	08/06/2018 15:00	0,18	1172,6
421	08/06/2018 15:00	EURUSD	€ 1k	Acheter	1,17484	1,17605	08/06/2018 15:14	0,96	1173,56
421	08/06/2018 15:02	EURUSD	€ 1k	Vendre	1,17443	1,1761	08/06/2018 15:14	-1,51	1172,05
421	08/06/2018 15:11	EURUSD	€ 3k	Acheter	1,17516	1,17605	08/06/2018 15:14	2	1174,05
421	08/06/2018 15:12	EURUSD	€ 4k	Acheter	1,17549	1,17605	08/06/2018 15:14	1,56	1175,61
421	08/06/2018 15:14	EURUSD	€ 1k	Acheter	1,1761	1,17731	08/06/2018 16:34	0,96	1176,57
421	08/06/2018 15:18	EURUSD	€ 1k	Vendre	1,17574	1,17736	08/06/2018 16:34	-1,47	1175,1
421	08/06/2018 15:21	EURUSD	€ 3k	Acheter	1,17648	1,17731	08/06/2018 16:34	1,85	1176,95
421	08/06/2018 15:30	EURUSD	€ 4k	Acheter	1,17688	1,17731	08/06/2018 16:34	1,11	1178,06
421	11/06/2018 01:41	EURUSD	€ 3k	Acheter	1,17821	1,17896	11/06/2018 02:43	1,65	1179,21

 


@tradermatrix