cBot "Process was unexpectedly terminated"

Created at 01 Mar 2023, 08:36
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!
AL

allen.dk

Joined 01.03.2023

cBot "Process was unexpectedly terminated"
01 Mar 2023, 08:36


Hi, I have an issue where my cBot just stops running after a few seconds with an error in the log "CBot instance process was unexpectedly terminated." 

Here is my cBot:

 

using System;
using cAlgo.API;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TokyoSessionStrategy : Robot
    {
        [Parameter("Lot Size", DefaultValue = 2.0)]
        public double LotSize { get; set; }

        [Parameter("Stop Loss (pips)", DefaultValue = 410)]
        public double StopLossInPips { get; set; }

        [Parameter("Take Profit (pips)", DefaultValue = 460)]
        public double TakeProfitInPips { get; set; }

        private bool _isSessionStarted;

        protected override void OnStart()
        {
            _isSessionStarted = false;
            Print("Waiting for Tokyo session to start...");
        }

        protected override void OnTick()
        {
            var serverTime = Server.Time;
            var tokyoStart = new DateTime(serverTime.Year, serverTime.Month, serverTime.Day, 9, 0, 0, DateTimeKind.Utc);
            var tokyoEnd = new DateTime(serverTime.Year, serverTime.Month, serverTime.Day, 16, 0, 0, DateTimeKind.Utc);

            // Convert Tokyo session start and end times to the robot's timezone
            tokyoStart = TimeZoneInfo.ConvertTimeFromUtc(tokyoStart, TimeZone);
            tokyoEnd = TimeZoneInfo.ConvertTimeFromUtc(tokyoEnd, TimeZone);

            // Check if Tokyo session has started
            if (!_isSessionStarted && serverTime >= tokyoStart && serverTime <= tokyoEnd)
            {
                _isSessionStarted = true;
                // Calculate stop loss and take profit levels in points
                var symbol = Chart.Symbol;
                var stopLoss = symbol.Bid - symbol.PipSize * StopLossInPips;
                var takeProfit = symbol.Bid + symbol.PipSize * TakeProfitInPips;

                // Place long order
                var result = ExecuteMarketOrder(TradeType.Buy, symbol.Name, LotSize, "Tokyo Session Long", stopLoss, takeProfit);

                if (result.IsSuccessful)
                {
                    Print("Long order opened at " + symbol.Ask + " with stop loss at " + stopLoss + " and take profit at " + takeProfit);
                }
                else
                {
                    Print("Failed to open long order: " + result.Error);
                }
            }
            // Check if Tokyo session has ended
            else if (_isSessionStarted && serverTime > tokyoEnd)
            {
                _isSessionStarted = false;
                foreach (var position in Positions)
                {
                    if (position.SymbolName == Chart.Symbol.Name && position.TradeType == TradeType.Buy)
                    {
                        ClosePosition(position);
                    }
                }

                Print("Tokyo session has ended. No trades will be opened.");
            }
        }
    }
}

 

 

Thanks in advanced, if someone can help out.


@allen.dk