Topics
24 Aug 2021, 16:17
 734
 2
Replies

cAIgoBuddy
12 Feb 2022, 10:52

RE:

m4trader4 said:

Hi

I run CBot in vps with 6 threads/16 GB RAM which has ample resources, normally the cpu utilization is about 30-40% and ram utilization is 30%. This CBot runs on 20-30 symbols on Range Bars, whenever there is high volatility CTrader freezes and in task manager "SystemInterrupts" cpu utilization goes up 80-90%.

When there is high volatility/Event CBot of different symbol opens trade. Cumulatively ~ 10 -20 positions with stoploss are present and at every bar based on logic new positions(orders) are opened or StopLoss is moved or Positions are scaled (new orders)

In the CBot logs errors 

Failed to get symbol 'XAUUSD': symbol has no quotes.
Crashed in BarOpened with NullReferenceException: Object reference not set to an instance of an object.

The symbols are random and CBot stops.

In the attached code, I could point out to StopLoss calculation function.

If there is no high volatality/Event CBot works without any issue.

Please let me know what might be causing the issue. 

Appreciate your insights

Regards

Ahmed

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ForumCTraderSL : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        double GlobalTotalSl;

        public double CheckSLPositions()
        {

            //Print(Symbol.Name + "CheckSlPositions Executed");
            var positionsCBS = Positions.FindAll("");
            double TotalSl = 0;

            //var positionsCBS = Positions.FindAll("");
            if (positionsCBS.Length != 0)
            {
                foreach (var psnCBS in positionsCBS)
                {
                    // psnCBS.StopLoss doesnt return 0 if there is no StopLoss for open position, so have to convert to string

                    if (psnCBS.StopLoss.ToString() != "")
                    {

                        var GetSymbolName = psnCBS.SymbolName;
                        var GetSymbolNameLotSize = Symbols.GetSymbol(GetSymbolName).LotSize;
                        var GetSymbolPipValueSize = Symbols.GetSymbol(GetSymbolName).PipValue / Symbols.GetSymbol(GetSymbolName).PipSize;
                        var GetSymbolNameQty = psnCBS.Quantity;

                        switch (psnCBS.TradeType)
                        {

                            case TradeType.Buy:
                                var Bpips = (double)(psnCBS.StopLoss - psnCBS.EntryPrice);
                                TotalSl = Bpips * GetSymbolNameLotSize * GetSymbolPipValueSize * GetSymbolNameQty;
                                break;

                            case TradeType.Sell:
                                var Spips = (double)(psnCBS.EntryPrice - psnCBS.StopLoss);
                                TotalSl = Spips * GetSymbolNameLotSize * GetSymbolPipValueSize * GetSymbolNameQty;
                                break;


                        }


                    }


                }
                Print("CheckSlPositions TotalStopLossPips:  " + TotalSl.ToString("F6"));

            }

            return TotalSl;
        }

        private void Bars_BarOpened_CheckSLPositions(BarOpenedEventArgs obj)
        {
            GlobalTotalSl = 0;

            GlobalTotalSl = CheckSLPositions();


        }


        private void Bars_BarOpened_StopLossMove(BarOpenedEventArgs obj)
        {
           
            //Logic to move stop loss
        }

        private void Bars_BarOpened_AutoOrder(BarOpenedEventArgs obj)
        {

            //Logic place order there are some calculations used 
            //When the condition GlobalTotalSl is executed, due to computations of above statements,  sync of GlobalTotalSl should happen
            //Condition Check GlobalTotalSl should be within limit
        }

        private void Bars_BarOpened_AutoScale(BarOpenedEventArgs obj)
        {

            //Logic scale up orders
        }



        protected override void OnStart()
        {
            GlobalTotalSl = 0;


            Bars.BarOpened += Bars_BarOpened_CheckSLPositions;
            Bars.BarOpened += Bars_BarOpened_StopLossMove;
            Bars.BarOpened += Bars_BarOpened_AutoOrder;
            Bars.BarOpened += Bars_BarOpened_AutoScale;

            // Put your initialization logic here
        }

        protected override void OnTick()
        {
            // Put your core logic here
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

 

 

Hello,

For starters, change

psnCBS.StopLoss.ToString() != ""

to 

psnCBS.StopLoss.HasValue

Since StopLoss is a nullable double, it can crash when invoking ToString() when it's null.

 


@cAIgoBuddy