Hello,
If someone can find why this code works in live but not in backtesting. In the Log i can see that the bot have been activated.
using System;
using System.Linq;
using System.Text;
using cAlgo.API;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class OpenCloseTimer : Robot
{
[Parameter("Open Hour", DefaultValue = 14, MinValue = 0, MaxValue = 23)]
public int OpenHour { get; set; }
[Parameter("Open Minute", DefaultValue = 30, MinValue = 0, MaxValue = 59)]
public int OpenMinute { get; set; }
[Parameter("Take Profit", DefaultValue = 50)]
public int TakeProfit { get; set; }
[Parameter("Stop Loss", DefaultValue = 10)]
public int StopLoss { get; set; }
[Parameter("Volume", DefaultValue = 1000, MinValue = 1000)]
public int Volume { get; set; }
[Parameter("Seconds Before", DefaultValue = 10, MinValue = 1)]
public int SecondsBefore { get; set; }
private DateTime _triggerTimeInServerTimeZone;
private bool _positionOpened = false; // Ajout de la variable de contrôle
private const string Label = "Robot";
protected override void OnStart()
{
Timer.Start(1);
var triggerTimeInLocalTimeZone = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, OpenHour, OpenMinute, 0);
if (triggerTimeInLocalTimeZone < DateTime.Now)
triggerTimeInLocalTimeZone = triggerTimeInLocalTimeZone.AddDays(1);
_triggerTimeInServerTimeZone = TimeZoneInfo.ConvertTime(triggerTimeInLocalTimeZone, TimeZoneInfo.Local, TimeZone);
}
protected override void OnTimer()
{
if (!_positionOpened && Server.Time <= _triggerTimeInServerTimeZone && (_triggerTimeInServerTimeZone - Server.Time).TotalSeconds <= SecondsBefore)
{
var buyOrderTargetPrice = Symbol.Ask;
ChartObjects.DrawHorizontalLine("buy target", buyOrderTargetPrice, Colors.Blue, 1, LineStyle.DotsVeryRare);
PlaceStopOrder(TradeType.Buy, Symbol, Volume, buyOrderTargetPrice, Label, StopLoss, TakeProfit);
_positionOpened = true; // Mettez la variable à true pour indiquer que la position a été ouverte
}
}
protected override void OnTick()
{
// Code à exécuter à chaque tick
}
protected override void OnStop()
{
// Code à exécuter lorsque le robot est arrêté
}
}
}