Backtesting when using DateTime produces zero trades
Backtesting when using DateTime produces zero trades
01 Apr 2022, 10:14
Hi,
I have made modifications to a grid-style bot which only places new trades after a certain hour of the day, I am currently using the following code to open the first trade:
if ((DateTime.Now.Hour == HourOpen) && (DateTime.Now.Minute == MinuteOpen) && (DateTime.Now.Second >= 0))
ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, "Label", null, null);
While this works as intended when the bot is running, backtesting produces no trades, so I can only forward test my bot.
Is there a different way to write the above code that will work with backtesting?
Replies
amusleh
01 Apr 2022, 11:24
Hi,
You should never use DateTime.Now or DateTimeOffset.Now in backtest environment, because those two are .NET BCL APIs that uses your system time.
Instead you should use Server.Time and Server.TimeInUtc which will give you the current time of the server is live mode and the current time in backtest environment.
@amusleh
Mr4x
02 Apr 2022, 02:32
RE: RE:
Shares4UsDevelopment said:
if ((Bars[index].OpenTime.Hour == HourOpen) && (Bars[index].OpenTime..Minute == MinuteOpen) && (Bars[index].OpenTime.Second >= 0))
DateTime.Now is the current time
Thank you for your prompt reply Shares. I have tried substituting with the following code:
if (Positions.Count(x => x.Label == ThiscBotLabel) == 0)
{
if ((Bars[1].OpenTime.Hour == HourOpen) && (Bars[1].OpenTime.Minute == MinuteOpen) && (Bars[1].OpenTime.Second >= 0))
{
ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, ThiscBotLabel, null, InitialTakeProfit);
LastBuyTradeTime = MarketSeries.OpenTime.Last(0);
}
}
And I am still not getting any results in the backtest... Do you have any other suggestions?
@Mr4x
Mr4x
02 Apr 2022, 02:32
RE:
amusleh said:
Hi,
You should never use DateTime.Now or DateTimeOffset.Now in backtest environment, because those two are .NET BCL APIs that uses your system time.
Instead you should use Server.Time and Server.TimeInUtc which will give you the current time of the server is live mode and the current time in backtest environment.
Thank you for your feedback. For this I do want the bot to execute according to my local system time
@Mr4x
amusleh
02 Apr 2022, 11:04
RE: RE:
Mr4x said:
amusleh said:
Hi,
You should never use DateTime.Now or DateTimeOffset.Now in backtest environment, because those two are .NET BCL APIs that uses your system time.
Instead you should use Server.Time and Server.TimeInUtc which will give you the current time of the server is live mode and the current time in backtest environment.
Thank you for your feedback. For this I do want the bot to execute according to my local system time
Hi,
Server.Time gives the current time in your set time zone via TimeZone property of Robot attribute, change that to your local time zone.
As I said using DateTime.Now doesn't make any sense in back test.
@amusleh
Shares4UsDevelopment
01 Apr 2022, 10:45
RE:
if ((Bars[index].OpenTime.Hour == HourOpen) && (Bars[index].OpenTime..Minute == MinuteOpen) && (Bars[index].OpenTime.Second >= 0))
DateTime.Now is the current time
@Shares4UsDevelopment