Only one trade per day

Created at 21 Aug 2017, 20:57
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
DelFonseca's avatar

DelFonseca

Joined 25.06.2017

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


@DelFonseca
Replies

DelFonseca
21 Aug 2017, 23:06

Im trying this, but dont work

        protected override void OnBar()
        {
            DateTime _lastExecutedOrder = DateTime.Now;
            if (_lastExecutedOrder.AddDays(-1) < DateTime.Now)
            {
                ExecuteStrategy();
            }
            else
            {
                return;
            }
        }

 


@DelFonseca

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