trade hours for cbot
trade hours for cbot
09 Mar 2024, 04:45
i want my bot to trade only at these specified times minus 2minuts and plus 2 minutes that means 2minutes before and 2 minutes after. but i will also share my log it only trades at 0000-0059. here is the code. please help(followed by log).
using System;
using cAlgo.API;
using System.Globalization;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
public class TradeTime : Robot
{
private int serverTimeOffsetHours = 3; // Set your desired offset here
[Parameter("Time Duration (minutes)", DefaultValue = 5, Group = "Time Filter")]
public int TimeDurationMinutes { get; set; }
[Parameter("Time 1", DefaultValue = "13:00", Group = "Time Filter")]
public string Time1 { get; set; }
[Parameter("Time 2", DefaultValue = "18:00", Group = "Time Filter")]
public string Time2 { get; set; }
[Parameter("Time 3", DefaultValue = "16:30", Group = "Time Filter")]
public string Time3 { get; set; }
[Parameter("Time 4", DefaultValue = "15:30", Group = "Time Filter")]
public string Time4 { get; set; }
[Parameter("Time 5", DefaultValue = "15:00", Group = "Time Filter")]
public string Time5 { get; set; }
[Parameter("Time 6", DefaultValue = "00:00", Group = "Time Filter")]
public string Time6 { get; set; }
[Parameter("Time 7", DefaultValue = "03:00", Group = "Time Filter")]
public string Time7 { get; set; }
private DateTime CurrentTime
{
get { return Server.Time.AddHours(serverTimeOffsetHours); }
}
protected override void OnTick()
{
// Check each specified time
bool TradeTimes =
(CurrentTime > ParseTime(Time1).AddMinutes(-TimeDurationMinutes) && CurrentTime < ParseTime(Time1).AddMinutes(TimeDurationMinutes)) ||
(CurrentTime > ParseTime(Time2).AddMinutes(-TimeDurationMinutes) && CurrentTime < ParseTime(Time2).AddMinutes(TimeDurationMinutes)) ||
(CurrentTime > ParseTime(Time3).AddMinutes(-TimeDurationMinutes) && CurrentTime < ParseTime(Time3).AddMinutes(TimeDurationMinutes)) ||
(CurrentTime > ParseTime(Time4).AddMinutes(-TimeDurationMinutes) && CurrentTime < ParseTime(Time4).AddMinutes(TimeDurationMinutes)) ||
(CurrentTime > ParseTime(Time5).AddMinutes(-TimeDurationMinutes) && CurrentTime < ParseTime(Time5).AddMinutes(TimeDurationMinutes)) ||
(CurrentTime > ParseTime(Time6).AddMinutes(-TimeDurationMinutes) && CurrentTime < ParseTime(Time6).AddMinutes(TimeDurationMinutes)) ||
(CurrentTime > ParseTime(Time7).AddMinutes(-TimeDurationMinutes) && CurrentTime < ParseTime(Time7).AddMinutes(TimeDurationMinutes));
// Execute if any of the specified times is correct
if (TradeTimes)
{
ExecuteMarketOrder(TradeType.Buy, SymbolName, 1000, "x", 1, 1);
}
}
private DateTime ParseTime(string timeString)
{
DateTime result;
if (!DateTime.TryParseExact(timeString, "HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
{
Print("Invalid time input: " + timeString);
Stop();
}
return result;
}
}
}
log:08/02/2024 03:00:00.000 | Indicator instance [Simple Moving Average, EURUSD, m1] unloaded.
08/02/2024 03:00:00.000 | Indicator instance [Bollinger Bands, EURUSD, m1] unloaded.
08/02/2024 03:00:00.000 | Indicator instance [Exponential Moving Average, EURUSD, m1] unloaded.
08/02/2024 03:00:00.000 | CBot instance [timer, EURUSD, m1] started.
08/03/2024 23:59:00.000 | Executing Market Order to Buy 1000 EURUSD (SL: 1, TP: 1)
08/03/2024 23:59:00.000 | → Executing Market Order to Buy 1000 EURUSD (SL: 1, TP: 1) SUCCEEDED, Position PID1
09/03/2024 00:00:00.000 | Executing Market Order to Buy 1000 EURUSD (SL: 1, TP: 1)
09/03/2024 00:00:00.000 | → Executing Market Order to Buy 1000 EURUSD (SL: 1, TP: 1) SUCCEEDED, Position PID2
09/03/2024 00:01:00.000 | Executing Market Order to Buy 1000 EURUSD (SL: 1, TP: 1)
09/03/2024 00:01:00.000 | → Executing Market Order to Buy 1000 EURUSD (SL: 1, TP: 1) SUCCEEDED, Position PID3
09/03/2024 00:54:00.000 | CBot instance [timer, EURUSD, m1] stopped.
09/03/2024 00:54:00.000 | Indicator instance [Simple Moving Average, EURUSD, m1] loaded.
09/03/2024 00:54:00.000 | Indicator instance [Bollinger Bands, EURUSD, m1] loaded.
09/03/2024 00:54:00.000 | Indicator instance [Exponential Moving Average, EURUSD, m1] loaded.
firemyst
10 Mar 2024, 03:01
How come you don't print the values for :
You don't know what those actual values are. You may think you know what they are, but you should print them out in your log so you know what they actually are.
For instance, you're getting the server time. Do you know what time zone the cTrader server is in? ANd then you're adding hours to that. Have you confirmed through print statements what the actual server time is that you're code is looking at?
If so, prove it :-)
Put it in the output logs.
Also, your “TimeDurationMinutes” is by default set to 5; yet you only want plus/minus 2 minutes. How do we know what value you have the parameter set at when you run your bot?
Again, print this to log.
@firemyst