Refer to last closed position

Created at 18 Feb 2018, 14:37
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!
DR

Drummond360

Joined 22.12.2017

Refer to last closed position
18 Feb 2018, 14:37


Hi All,

I want to close all open positions if one position hits it's stop loss, the code below seems logical but I fear it is outdated... What would the current approach be?

 

        var lastPosition = Trade.Result.Last(1);
            
            if (lastPosition.NetProfit < 0)
            {
                foreach (Position position in symbolSearch)
                {
                    ClosePosition(position);
                }
            }

 


@Drummond360
Replies

PanagiotisCharalampous
20 Feb 2018, 11:53

Hi Drummond360,

You might consider something like the below

        protected override void OnStart()
        {
            Positions.Closed += OnPositionsClosed;
        }

        void OnPositionsClosed(PositionClosedEventArgs obj)
        {
            if (obj.Position.NetProfit < 0)
            {
                foreach (var position in Positions)
                {
                    ClosePosition(position);
                }
            }
        }

However note that the code above does not guarantee that the position has been closed by a stop loss. It could be a manual close as well.

Best Regards,

Panagiotis


@PanagiotisCharalampous