cbot doesnt execute ?
Created at 05 Aug 2024, 18:02
ZY
cbot doesnt execute ?
05 Aug 2024, 18:02
Hello!
I have absolutely no idea of coding so I tried my luck with the AI.
I basically read a bit about machine learning. Downloaded Visual Studios, trained my ML Model in it with the cTrader Chart Data and asked the AI to write me a code.
This is what happened:
using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using AI_104;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class MLModelTradingBot : Robot
{
private const int PredictionHorizon = 5; // Adjust based on your needs
private const double BuyThreshold = 0.02; // 2% increase prediction
private const double SellThreshold = -0.02; // 2% decrease prediction
private const int StopLossPips = 20;
private const int TakeProfitPips = 40;
protected override void OnStart()
{
// Any initialization code can go here
}
protected override void OnTick()
{
if (Positions.Count == 0)
{
var prediction = GetModelPrediction();
if (prediction > BuyThreshold)
{
ExecuteMarketOrder(TradeType.Buy, SymbolName, CalculatePositionSize(), "Buy", StopLossPips, TakeProfitPips);
}
else if (prediction < SellThreshold)
{
ExecuteMarketOrder(TradeType.Sell, SymbolName, CalculatePositionSize(), "Sell", StopLossPips, TakeProfitPips);
}
}
}
private double GetModelPrediction()
{
var input = new MLModel1.ModelInput
{
ClosePrice = (float)Symbol.Bid // Use current price as input
};
var output = MLModel1.Predict(input, PredictionHorizon);
// Calculate percentage change prediction
float currentPrice = (float)Symbol.Bid;
float predictedPrice = output.ClosePrice[PredictionHorizon - 1]; // Last predicted price
return (predictedPrice - currentPrice) / currentPrice;
}
private double CalculatePositionSize()
{
// Implement your position sizing logic here
// This is a simple example using a fixed lot size
return Symbol.VolumeInUnitsMin;
}
}
}
There were no error found to my astonishment when building the bot in cTrader. But after backtesting it I noticed that the cBot does not make any trades. See picture.
Anyone any ideas why?
PanagiotisCharalampous
06 Aug 2024, 05:53
Hi there,
The most probable reason is that your conditions are never met. Try debugging your strategy to check what values you are getting from the model.
Best regards,
Panagiotis
@PanagiotisCharalampous