Negative GrossProfit stop robot with closeposition

Created at 30 Jun 2021, 18:39
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!
PR

progy85

Joined 07.01.2021 Blocked

Negative GrossProfit stop robot with closeposition
30 Jun 2021, 18:39


Hi,

 

how can I closeposition and stop robot when GrossProfit is $-10 ?           

This code not works on OnTick ?

  foreach (var position in Positions)
            {
                if (position.GrossProfit < -10)
                {
                    ClosePosition(position);
                     Stop();
                }
            }


Replies

amusleh
01 Jul 2021, 11:37

Hi,

I don't see any issue on your code, it should work fine if there is any position that has less than 10 gross profit, it might not working because the OnTick method is called only for chart symbol ticks, not all position ticks, if you want to manage multiple symbols positions then try this:

        protected override void OnStart()
        {
            foreach (var symbol in Symbols.GetSymbols())
            {
                symbol.Tick += SymbolTick;
            }
        }

        private void SymbolTick(SymbolTickEventArgs obj)
        {
            foreach (var position in Positions)
            {
                if (position.SymbolName.Equals(obj.SymbolName, StringComparison.OrdinalIgnoreCase) && position.GrossProfit <= -10)
                {
                    ClosePosition(position);
                    Stop();
                }
            }
        }

 


@amusleh