Topics

Forum Topics not found

Replies

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