Backtesting strategy based on Asia session
Backtesting strategy based on Asia session
26 Aug 2024, 23:48
hello everyone
I already have a code that works well for placing orders at right price.
My code is based on Asia range
So it’s first calculate the Asia high and low ( 1am to 6 am UTC+2 ) ok
Then it will place stops order based on that and stop losses based on that also. BUT idk why, one day, the server time of CTRADER will change. Then, with the same parameter that I have in my code for calculating Asia range, the result will change and it will be by exemple from 2 am to 7 am instead to keep from 1 to 6
And It keeps like that during weeks then it comes back to from 1 to 6
Or from 0 to 5
Do you have any idea why ???
Also my code is based on server.time and not local time or UTC+2.
This where my code calculates asia range etc :
private string label = "AsianSessionHighLowBot";
private DateTime _asianSessionStart;
private DateTime _asianSessionEnd;
private double _sessionHigh;
private double _sessionLow;
private bool _sessionCalculated;
private PendingOrder _buyStopOrder;
private PendingOrder _sellStopOrder;
protected override void OnStart()
{
InitializeSessionTimes();
ResetSessionVariables();
Positions.Opened += OnPositionOpened;
}
protected override void OnTick()
{
DateTime serverTime = Server.Time;
if (serverTime.Hour == VerificationHour && serverTime.Minute == VerificationMinute)
{
Print("heure de vérification-----------------------------", serverTime);
}
if (!_sessionCalculated && serverTime >= _asianSessionEnd)
{
CalculateAsianSessionHighLow();
_sessionCalculated = true;
PlaceStopOrders();
}
if (serverTime >= _asianSessionEnd.AddHours(ExpiryHours))
{
CancelStopOrders();
ResetSessionVariables();
_asianSessionStart = _asianSessionStart.AddDays(1);
_asianSessionEnd = _asianSessionEnd.AddDays(1);
}
// Close all positions at the specified hour and minute
if (serverTime.Hour == CloseHour && serverTime.Minute == CloseMinute)
{
CloseAllPositions();
}
}
private void InitializeSessionTimes()
{
DateTime serverTime = Server.Time;
_asianSessionStart = new DateTime(serverTime.Year, serverTime.Month, serverTime.Day, 23, 0, 0, DateTimeKind.Utc).AddHours(AddHour);
_asianSessionEnd = _asianSessionStart.AddHours(5).AddMinutes(0);
}
private void ResetSessionVariables()
{
_sessionHigh = double.MinValue;
_sessionLow = double.MaxValue;
_sessionCalculated = false;
_buyStopOrder = null;
_sellStopOrder = null;
}
private void CalculateAsianSessionHighLow()
{
_sessionHigh = double.MinValue;
_sessionLow = double.MaxValue;
foreach (var bar in Bars)
{
if (bar.OpenTime >= _asianSessionStart && bar.OpenTime < _asianSessionEnd)
{
if (bar.High > _sessionHigh)
_sessionHigh = bar.High;
if (bar.Low < _sessionLow)
_sessionLow = bar.Low;
}
}
Print("Asian Session High: ", _sessionHigh);
Print("Asian Session Low: ", _sessionLow);
}
private void PlaceStopOrders()
{
if (_buyStopOrder != null)
{
CancelPendingOrder(_buyStopOrder);
}
if (_sellStopOrder != null)
{
CancelPendingOrder(_sellStopOrder);
}
Thank you for help !
PanagiotisCharalampous
27 Aug 2024, 06:39
Hi there,
It sounds like daylight saving time. What timezone do you use? How can we reproduce this behavior?
Best regards,
Panagiotis
@PanagiotisCharalampous