cbot doesnt execute ?

Created at 05 Aug 2024, 18:02
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!
ZY

zytotoxiziteat

Joined 04.08.2021

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?

 


@zytotoxiziteat
Replies

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