automatically close part of position and move stops to BE?

Created at 15 Jan 2014, 14:16
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!
HG

hgorski

Joined 15.01.2014

automatically close part of position and move stops to BE?
15 Jan 2014, 14:16


after reaching certain price


@hgorski
Replies

Spotware
15 Jan 2014, 17:31 ( Updated at: 21 Dec 2023, 09:20 )

RE:

hgorski said:

after reaching certain price

You can start with the following sample: /forum/calgo-reference-samples/2247

If you like to set it manually, you may click on the security button next to the settings to the left of a position in the positions tab.


@Spotware

hgorski
17 Jan 2014, 10:19

Thanks,

 

And if I wanted to close, let's say 50k of 100k position in the same time to moving stops to BE? How should I modify this example code?


@hgorski

Spotware
17 Jan 2014, 10:35

You can call the ClosePosition method and specify the volume to close:

ClosePosition(position, 50000); // hardcoded volume
var volumeToClose = Symbol.NormalizeVolume(position.Volume * 0.5) // calculated volume
ClosePosition(position, volumeToClose);

/api/reference/robot/closeposition-8635

 


@Spotware

hgorski
17 Jan 2014, 16:24

This is all I need :) Thanks


@hgorski

jeex
18 Jan 2014, 13:05

BE

I think after closing half the position you still need to set SL to BE:

private double calcBE(Position p)
 {
            double totalCosts = Symbol.Spread + ((position.GrossProfit - position.NetProfit) / position.Volume / Symbol.PipValue);
            if (p.TradeType == TradeType.Buy)
                return p.EntryPrice + totalCosts;
            else
                return p.EntryPrice - totalCosts;
}

and then modify the position with the calculated new SL of calcBE()


@jeex