How to exactly calculate relativeTakeProfit or relativeStopLoss

Created at 10 Dec 2021, 09:31
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!
DU

dudegrowth

Joined 06.12.2021

How to exactly calculate relativeTakeProfit or relativeStopLoss
10 Dec 2021, 09:31


Hi,

Something weird it's happening or i'm not knowing how to calculate stop loss or take profit.

For example in EUR USD i have a message like this

"SELLING EURUSD ENTRY AT 1.12891 and TP 1.12791"

But when i see my order the take profit is near my entry.

How i'm calculating it is: 

let takeProfit = bar2.closePrice - 0.00100

bar2.closePrice // returns 1.12891


@dudegrowth
Replies

amusleh
13 Dec 2021, 09:51

Hi,

Are you using Open API or cTrader automate API?

If you are using Open API then please check the order docs: https://spotware.github.io/open-api-docs/models/#protooaorder

To change relative stop loss or take profit to price levels you should divide the relative value to 100000 and then round it to symbol digits:

Round(relative / 100000.0, symbol.Digits);

 


@amusleh

kostya.bartchenkov
15 Mar 2022, 14:32

RE:

amusleh said:

Hi,

Are you using Open API or cTrader automate API?

If you are using Open API then please check the order docs: https://spotware.github.io/open-api-docs/models/#protooaorder

To change relative stop loss or take profit to price levels you should divide the relative value to 100000 and then round it to symbol digits:

Round(relative / 100000.0, symbol.Digits);

 

Hi Amusleh,

 

Thanks a lot for your help in this forum.

Does it mean that in ETHUSD example, If I wanted to set relativeStopLoss to 2% of the price which was 2536.75 I would use the following formula?

```

2536.75 * 0.02 * 100000 = 5073500

```

I guess not, since I get "Relative stop loss has invalid precision" error. For me, I couldn't quite get what "Specified in 1/100_000 of unit of a price" meant.


@kostya.bartchenkov

amusleh
16 Mar 2022, 08:42 ( Updated at: 16 Mar 2022, 08:43 )

Hi,

If ETHUSD current price is 2537 and you want to open a buy position with a stop loss at 2500 you will do it like this:

stopLossInRelative = Round(2537 - 2500, symbol.Digits) * 100000

These methods might help you understand how different units works on Open API:

public static double GetTickSize(this ProtoOASymbol symbol) => 1 / Math.Pow(10, symbol.Digits);

public static double GetPipSize(this ProtoOASymbol symbol) => 1 / Math.Pow(10, symbol.PipPosition);

public static long GetRelativeFromPips(this ProtoOASymbol symbol, double pips)
{
    var pipsInPrice = pips * symbol.GetPipSize();
    return (long)Math.Round(pipsInPrice * 100000, symbol.Digits);
}

public static double GetPriceFromRelative(this ProtoOASymbol symbol, long relative) => Math.Round(relative / 100000.0, symbol.Digits);

public static double GetPipsFromRelative(this ProtoOASymbol symbol, long relative) => Math.Round((relative / 100000.0) / symbol.GetPipSize(), symbol.Digits - symbol.PipPosition);

public static double GetPipsFromPoints(this ProtoOASymbol symbol, long points) => symbol.GetPipsFromPrice(points * symbol.GetTickSize());

public static long GetPointsFromPips(this ProtoOASymbol symbol, double pips) => System.Convert.ToInt64(pips * Math.Pow(10, symbol.Digits - symbol.PipPosition));

public static double GetPipsFromPrice(this ProtoOASymbol symbol, double price) => Math.Round(price * Math.Pow(10, symbol.PipPosition), symbol.Digits - symbol.PipPosition);

Points are used for setting slippage on Market range and stop limit orders.

If you got the precision error then it means something is wrong with decimal places and rounding.


@amusleh