cBot randomly stopping respectively switching instance?

Created at 26 Aug 2024, 18:01
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 randomly stopping respectively switching instance?
26 Aug 2024, 18:01


Hello, I use my cbot in a VPS and recently I noticed that the bot just randomly stopping the instance which I started or switching to an other instance even though I did not start the other instance.

Here is the cbot code:

using System;
using System.Collections.Generic;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class TradingBot : Robot
    {
        [Parameter("Risk Percentage", DefaultValue = 1, MinValue = 0.1, MaxValue = 10)]
        public double RiskPercentage { get; set; }

        [Parameter("Stop Loss (Pips)", DefaultValue = 40, MinValue = 0, MaxValue = 100)]
        public double StopLossPips { get; set; }

        [Parameter("Take Profit (Pips)", DefaultValue = 20, MinValue = 0, MaxValue = 200)]
        public double TakeProfitPips { get; set; }

        private AI_101.ML101.ModelInput _modelInput;
        private double _lastPrediction;

        protected override void OnStart()
        {
            _modelInput = new AI_101.ML101.ModelInput();
        }

        protected override void OnTick()
        {
            // Ensure only one open position per currency pair
            if (Positions.FindAll("ML Prediction", Symbol.Name).Length > 0)
                return;

            // Update model input with the latest close price
            _modelInput.ClosePrice = (float)Symbol.Bid;  // Use Symbol.Bid instead of Symbol.LastTick.Bid

            // Get prediction
            var prediction = AI_101.ML101.Predict(_modelInput);

            // Calculate the predicted price change
            double predictedChange = prediction.ClosePrice[0] - _modelInput.ClosePrice;

            // Determine if we should open a position
            if (Math.Abs(predictedChange) > Symbol.PipSize)
            {
                if (predictedChange > 0 && _lastPrediction <= 0)
                {
                    OpenPosition(TradeType.Buy);
                }
                else if (predictedChange < 0 && _lastPrediction >= 0)
                {
                    OpenPosition(TradeType.Sell);
                }
            }

            _lastPrediction = predictedChange;
        }

        private void OpenPosition(TradeType tradeType)
        {
            // Calculate position size based on risk
            double riskAmount = Account.Balance * (RiskPercentage / 100);
            double volumeInUnits = riskAmount / (StopLossPips * Symbol.PipValue);

            // Ensure volume is within acceptable range and increments
            volumeInUnits = Symbol.NormalizeVolumeInUnits(volumeInUnits, RoundingMode.ToNearest);

            // Check if the volume is valid
            if (volumeInUnits < Symbol.VolumeInUnitsMin || volumeInUnits > Symbol.VolumeInUnitsMax)
            {
                Print("Volume is out of range: " + volumeInUnits);
                return;
            }

            // Open the position
            ExecuteMarketOrder(tradeType, Symbol.Name, volumeInUnits, "ML Prediction", StopLossPips, TakeProfitPips);
        }
    }
}

Is there any indication why it does that?

Thank you !

 


@zytotoxiziteat
Replies

PanagiotisCharalampous
27 Aug 2024, 06:33

Hi there,

Unfortunately no. Did you check the log of the stopped instance? Any useful information there?

Best regards

Panagiotis


@PanagiotisCharalampous