close position on profit

Created at 07 Mar 2022, 22:08
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!
R.

r.stipriaan

Joined 05.02.2021

close position on profit
07 Mar 2022, 22:08


Hi, to my close orders I would like to add to only close a position when profit is above zero.

must be something like (UnrealizedNetProfit > 0)

does anyone know how to enter this correctly?

 

For example:

 

            if ((Positions.Find("Short", SymbolName, TradeType.Sell)) != null & MACDline > prevMACDline)
            {
                ClosePosition(Positions.Find("Short", SymbolName, TradeType.Sell));
            }
            if ((Positions.Find("Long", SymbolName, TradeType.Buy)) != null & MACDline < prevMACDline)
            {
                ClosePosition(Positions.Find("Long", SymbolName, TradeType.Buy));
            }
        }

 


@r.stipriaan
Replies

amusleh
08 Mar 2022, 08:57

Hi,

The position has net profit and gross profit properties, and you can use them, ex:

            var sellPosition = Positions.Find("Short", SymbolName, TradeType.Sell);

            if (sellPosition != null && sellPosition.NetProfit > 0 && MACDline > prevMACDline)
            {
                ClosePosition(sellPosition);
            }

            var buyPosition = Positions.Find("Long", SymbolName, TradeType.Sell);

            if (buyPosition != null && buyPosition.NetProfit > 0 && MACDline < prevMACDline)
            {
                ClosePosition(buyPosition);
            }

Please check the API references example codes.


@amusleh