Problem BOT does not open operation in backtest
Problem BOT does not open operation in backtest
25 Jul 2024, 12:21
Ho programmato il mio BOT, il codice non mi dà errori e dice che è tutto ok, ma quando lo provo in backtest non mette trade, quindi vorrei capire se è un problema del mio codice o se è del backtest. Se qualcuno può aiutarmi gliene sarei grato
Replies
emafondex69
26 Jul 2024, 12:45
( Updated at: 28 Jul 2024, 06:43 )
BOT
using cAlgo.API;
using cAlgo.API.Internals;
using System;
namespace cAlgo
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class DailyOrderBot : Robot
{
[Parameter("Account Balance", DefaultValue = 10000.0)]
public double AccountBalance { get; set; } // Saldo iniziale
[Parameter("Risk Percentage", DefaultValue = 1.0)]
public double RiskPercentage { get; set; } // Percentuale di rischio per trade
[Parameter("Stop Loss (pips)", DefaultValue = 9)]
public int StopLoss { get; set; } // Stop Loss in pips
[Parameter("Take Profit (pips)", DefaultValue = 0.5)]
public double TakeProfit { get; set; } // Take Profit in pips
private bool hasTradedToday;
// Ora in cui il bot deve operare (CET)
private const int OperationHour = 20; // 22:00 CEST è 20:00 UTC
protected override void OnStart()
{
// Resetta il flag all'avvio del bot
hasTradedToday = false;
}
protected override void OnTick()
{
// Ottieni l'ora attuale nel fuso orario del server
DateTime currentTime = Server.Time;
Print("Current Server Time: ", currentTime);
// Controlla se sono le 22:00 ora italiana (20:00 UTC)
if (currentTime.Hour == OperationHour && currentTime.Minute == 0 && !hasTradedToday)
{
double riskAmount = AccountBalance * (RiskPercentage / 100);
double pipValue = Symbol.PipValue;
// Calcolo della dimensione del lotto (Volume) per rischiare esattamente 100€
double volume = riskAmount / (pipValue * StopLoss);
Print("Calculated Volume: ", volume);
// Esegui l'ordine Buy
ExecuteMarketOrder(TradeType.Buy, SymbolName, volume, "DailyBuy", StopLoss, TakeProfit);
Print("Buy order placed at: ", currentTime);
// Esegui l'ordine Sell
ExecuteMarketOrder(TradeType.Sell, SymbolName, volume, "DailySell", StopLoss, TakeProfit);
Print("Sell order placed at: ", currentTime);
// Imposta il flag per evitare ulteriori trade nello stesso giorno
hasTradedToday = true;
}
// Resetta il flag all'inizio di un nuovo giorno
if (currentTime.Hour == 0 && currentTime.Minute == 0)
{
hasTradedToday = false;
}
}
protected override void OnStop()
{
// Codice per gestire la chiusura del bot, se necessario
Print("Bot stopped at: ", Server.Time);
}
}
}
@emafondex69
emafondex69
26 Jul 2024, 12:46
( Updated at: 28 Jul 2024, 06:43 )
Thank you so mush sir
using cAlgo.API;
using cAlgo.API.Internals;
using System;
namespace cAlgo
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class DailyOrderBot : Robot
{
[Parameter("Account Balance", DefaultValue = 10000.0)]
public double AccountBalance { get; set; } // Saldo iniziale
[Parameter("Risk Percentage", DefaultValue = 1.0)]
public double RiskPercentage { get; set; } // Percentuale di rischio per trade
[Parameter("Stop Loss (pips)", DefaultValue = 9)]
public int StopLoss { get; set; } // Stop Loss in pips
[Parameter("Take Profit (pips)", DefaultValue = 0.5)]
public double TakeProfit { get; set; } // Take Profit in pips
private bool hasTradedToday;
// Ora in cui il bot deve operare (CET)
private const int OperationHour = 20; // 22:00 CEST è 20:00 UTC
protected override void OnStart()
{
// Resetta il flag all'avvio del bot
hasTradedToday = false;
}
protected override void OnTick()
{
// Ottieni l'ora attuale nel fuso orario del server
DateTime currentTime = Server.Time;
Print("Current Server Time: ", currentTime);
// Controlla se sono le 22:00 ora italiana (20:00 UTC)
if (currentTime.Hour == OperationHour && currentTime.Minute == 0 && !hasTradedToday)
{
double riskAmount = AccountBalance * (RiskPercentage / 100);
double pipValue = Symbol.PipValue;
// Calcolo della dimensione del lotto (Volume) per rischiare esattamente 100€
double volume = riskAmount / (pipValue * StopLoss);
Print("Calculated Volume: ", volume);
// Esegui l'ordine Buy
ExecuteMarketOrder(TradeType.Buy, SymbolName, volume, "DailyBuy", StopLoss, TakeProfit);
Print("Buy order placed at: ", currentTime);
// Esegui l'ordine Sell
ExecuteMarketOrder(TradeType.Sell, SymbolName, volume, "DailySell", StopLoss, TakeProfit);
Print("Sell order placed at: ", currentTime);
// Imposta il flag per evitare ulteriori trade nello stesso giorno
hasTradedToday = true;
}
// Resetta il flag all'inizio di un nuovo giorno
if (currentTime.Hour == 0 && currentTime.Minute == 0)
{
hasTradedToday = false;
}
}
protected override void OnStop()
{
// Codice per gestire la chiusura del bot, se necessario
Print("Bot stopped at: ", Server.Time);
}
}
}
@emafondex69
PanagiotisCharalampous
28 Jul 2024, 07:08
Check your logs, your problem is with the volume. You need to round your volume before you use it
@PanagiotisCharalampous
emafondex69
28 Jul 2024, 15:52
RE: Problem BOT does not open operation in backtest
Check your logs, your problem is with the volume. You need to round your volume before you use it
PanagiotisCharalampous thank you so much but also if I've corrected the volume, settin a fixed value (1.2 Lots), it doesn't work, can you correct it for me?
Here are the main charateristics
- open long and short at 22.00 UTC+2 (Rome)
- 9 pip stop
- 0.5 pip tp
- 1.2 lots (volume)
here there is my new code, I've tryed my best because I don't know how to program sir, thank you early, have a nice working day.
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
using System;
using System.Collections.Generic;
using System.Linq;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class TimeBasedOrder : Robot
{
private DateTime targetTime;
private bool orderPlaced = false;
[Parameter("Volume (Lots)", DefaultValue = 1.2)]
public double Volume { get; set; }
[Parameter("Stop Loss (Pips)", DefaultValue = 9)]
public int StopLoss { get; set; }
[Parameter("Take Profit (Pips)", DefaultValue = 0.5)]
public double TakeProfit { get; set; }
protected override void OnStart()
{
// Set the target time to 22:00 UTC
targetTime = Server.Time.Date.AddHours(22);
if (Server.Time > targetTime)
targetTime = targetTime.AddDays(1);
Print("Script started. Order will be placed at {0}", targetTime);
}
protected override void OnTick()
{
if (!orderPlaced && Server.Time >= targetTime)
{
PlaceOrder();
orderPlaced = true;
// Set the target time to 22:00 UTC the next day
targetTime = targetTime.AddDays(1);
}
}
private void PlaceOrder()
{
var stopLossInPips = StopLoss;
var takeProfitInPips = TakeProfit;
var stopLossPrice = Symbol.Ask + stopLossInPips * Symbol.PipSize;
var takeProfitPrice = Symbol.Ask + takeProfitInPips * Symbol.PipSize;
var result = ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, "TimeBasedOrder", stopLossInPips, takeProfitInPips);
if (result.IsSuccessful)
Print("Order placed successfully at {0}", Server.Time);
else
Print("Order placement failed: {0}", result.Error);
}
}
}
this was the photo that you sent to me, now i'll send you the new messages logs…
@emafondex69
PanagiotisCharalampous
29 Jul 2024, 05:27
RE: RE: Problem BOT does not open operation in backtest
emafondex69 said:
Check your logs, your problem is with the volume. You need to round your volume before you use it
PanagiotisCharalampous thank you so much but also if I've corrected the volume, settin a fixed value (1.2 Lots), it doesn't work, can you correct it for me?
Here are the main charateristics
- open long and short at 22.00 UTC+2 (Rome)
- 9 pip stop
- 0.5 pip tp
- 1.2 lots (volume)
here there is my new code, I've tryed my best because I don't know how to program sir, thank you early, have a nice working day.
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
using System;
using System.Collections.Generic;
using System.Linq;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class TimeBasedOrder : Robot
{
private DateTime targetTime;
private bool orderPlaced = false;
[Parameter("Volume (Lots)", DefaultValue = 1.2)]
public double Volume { get; set; }
[Parameter("Stop Loss (Pips)", DefaultValue = 9)]
public int StopLoss { get; set; }
[Parameter("Take Profit (Pips)", DefaultValue = 0.5)]
public double TakeProfit { get; set; }
protected override void OnStart()
{
// Set the target time to 22:00 UTC
targetTime = Server.Time.Date.AddHours(22);
if (Server.Time > targetTime)
targetTime = targetTime.AddDays(1);
Print("Script started. Order will be placed at {0}", targetTime);
}
protected override void OnTick()
{
if (!orderPlaced && Server.Time >= targetTime)
{
PlaceOrder();
orderPlaced = true;
// Set the target time to 22:00 UTC the next day
targetTime = targetTime.AddDays(1);
}
}
private void PlaceOrder()
{
var stopLossInPips = StopLoss;
var takeProfitInPips = TakeProfit;
var stopLossPrice = Symbol.Ask + stopLossInPips * Symbol.PipSize;
var takeProfitPrice = Symbol.Ask + takeProfitInPips * Symbol.PipSize;
var result = ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, "TimeBasedOrder", stopLossInPips, takeProfitInPips);
if (result.IsSuccessful)
Print("Order placed successfully at {0}", Server.Time);
else
Print("Order placement failed: {0}", result.Error);
}
}
}
this was the photo that you sent to me, now i'll send you the new messages logs…
The volume is set in units, not in Lots. Use Symbol.QuantityToVolume to do the conversion between lots and volume.
@PanagiotisCharalampous
emafondex69
02 Aug 2024, 13:07
( Updated at: 02 Aug 2024, 13:23 )
RE: RE: RE: Problem BOT does not open operation in backtest
Thank you sir If I have other problems can I contact you here?
@emafondex69
PanagiotisCharalampous
04 Aug 2024, 06:42
RE: RE: RE: RE: Problem BOT does not open operation in backtest
emafondex69 said:
Thank you sir If I have other problems can I contact you here?
Yes of course, create a new thread for each issue you might have
@PanagiotisCharalampous
PanagiotisCharalampous
26 Jul 2024, 05:56
Hi there,
Please share your cBot code so that we can check what happens.
Best regards,
Panagiotis
@PanagiotisCharalampous