Only one trade per day
Only one trade per day
21 Aug 2017, 20:57
Good afternoon,
how can I limit a bot to only make one trade per day? Even though during the day there are several opportunities for new entrances.
Thank You
Replies
GammaQuant
21 Sep 2017, 04:12
[Parameter("Start Hour", DefaultValue = 6.0)] public double StartTime { get; set; } [Parameter("Stop Hour", DefaultValue = 18.0)] public double StopTime { get; set; } private bool _todaysOrderExecuted; protected override void OnBar() { var currentHours = Server.Time.TimeOfDay.TotalHours; bool tradeTime = StartTime < StopTime ? currentHours > StartTime && currentHours < StopTime : currentHours < StopTime || currentHours > StartTime; if (tradeTime && !_todaysOrderExecuted) { ExecuteStrategy(); } if (!tradeTime && _todaysOrderExecuted { _todaysOrderExecuted = false; } } private void ExecuteStrategy() { *** Execute strategy code**** if (result.IsSuccessful) { _todaysOrderExecuted = true; } }
this will allow you to only trade between certain times of the day and will only open one trade per day. it will use the servertime in calgo. dont use Datetime as that is the time in your computer and not the brokers Server time.
@GammaQuant
GammaQuant
21 Sep 2017, 15:12
if (!tradeTime && _todaysOrderExecuted) { _todaysOrderExecuted = false; }
Sorry missing Parentheses
@GammaQuant
germansousa
21 Jul 2021, 17:01
RE: Can this code be modified for two trades a day?
GammaQuant said:
[Parameter("Start Hour", DefaultValue = 6.0)] public double StartTime { get; set; } [Parameter("Stop Hour", DefaultValue = 18.0)] public double StopTime { get; set; } private bool _todaysOrderExecuted; protected override void OnBar() { var currentHours = Server.Time.TimeOfDay.TotalHours; bool tradeTime = StartTime < StopTime ? currentHours > StartTime && currentHours < StopTime : currentHours < StopTime || currentHours > StartTime; if (tradeTime && !_todaysOrderExecuted) { ExecuteStrategy(); } if (!tradeTime && _todaysOrderExecuted { _todaysOrderExecuted = false; } } private void ExecuteStrategy() { *** Execute strategy code**** if (result.IsSuccessful) { _todaysOrderExecuted = true; } }this will allow you to only trade between certain times of the day and will only open one trade per day. it will use the servertime in calgo. dont use Datetime as that is the time in your computer and not the brokers Server time.
@germansousa
DelFonseca
21 Aug 2017, 23:06
Im trying this, but dont work
@DelFonseca