Referencing Time
Referencing Time
22 Sep 2012, 11:04
Hello,
I'm trying to write a piece of code that does stuff depending on the current time e.g. if current time > 22:00 don't open new positions
any suggestions on how to do that?
Cheers
Replies
admin
24 Sep 2012, 10:50
Hello TETL,
DateTime.Now is the current date and time on your machine
Server.Time is the current date and time on the server.
You can access various members of the Datetime like Date, Time, Hour, etc. as well as functions like AddHours(), AddMinutes(), etc.
In a Robot you need to initialize your time variable in the OnStart event and check the condition in either the OnTick or the OnBar event, depending on your algorithm.
Look at the code snippet below as an example of how you can use DateTime and Server.Time.
private DateTime stopTime; protected override void OnStart() { stopTime = DateTime.Now.Date.AddHours(22); } protected override void OnTick() { // if (Server.Time < stopTime) { // Open new positions }
@admin
admin
26 Sep 2012, 15:10
Hello,
For backtesting purposes you can use MarketSeries.OpenTime:
int index = MarketSeries.OpenTime.Count - 1; var stopTime = MarketSeries.OpenTime[index].Date.AddHours(22); if (MarketSeries.OpenTime.LastValue < stopTime) { Print("Server Time = {0}", Server.Time); // Open new positions }
@admin
romiko
30 Sep 2012, 10:36
Referencing Time
Is this ok to use, I use this condition to trade and specfic times. I tried to stay away from the index accessor.
protected override void OnBar()
{
if (MarketSeries.OpenTime.LastValue.Hour >= 21 || (MarketSeries.OpenTime.LastValue.Hour >= 0 && MarketSeries.OpenTime.LastValue.Hour <= 13))
Initialize();
}
@romiko
rkokerti
23 Sep 2012, 23:05
Hello,
Please check out the code of this robot. /algos/show/115
I hope it will help you.
@rkokerti