Setting TP and SL in pips not working as expected

Created at 27 Oct 2024, 12:36
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!
MM

mmackay911

Joined 01.10.2024

Setting TP and SL in pips not working as expected
27 Oct 2024, 12:36


Hi folks, I'm quite new to cBot Algo programming and with assistance from ChatGPT the bot includes the following code snippets, which I'm trying to use trading XAUUSD.

The idea is to take a fixed number of pips as a TP, but when the order is executed the TP and SL pip sizes set are very much larger than that specified in the parameters. I've even tried to REM the  (TakeProfitPips * Symbol.PipSize); to TakeProfitPips;// * Symbol.PipSize); to attempt to force the pip size, but it makes no difference and the orders are still placed in the same way. Is there some kind of override that IC Markets is doing with my order that's changing what I'm specifying, or something else that I haven't grasped yet?

        [Parameter("Volume (Lots)", DefaultValue = 0.1)]
        public double LotSize { get; set; }        

        [Parameter("Take Profit Pips", DefaultValue = 200)]
        public double TakeProfitPips { get; set; }

        [Parameter("Risk-Reward Ratio for SL", DefaultValue = 1.5)]
        public double RiskRewardRatio { get; set; }

                protected override void OnBar()
                {                

                double takeProfit = currentCandleClose + (TakeProfitPips * Symbol.PipSize);
                double stopLossDistance = TakeProfitPips * RiskRewardRatio;
                double stopLoss = currentCandleClose - (stopLossDistance * Symbol.PipSize);

                var result = ExecuteMarketOrder(TradeType.Buy, SymbolName, Symbol.QuantityToVolumeInUnits(LotSize), PositionLabel, stopLoss, takeProfit);


@mmackay911
Replies

firemyst
28 Oct 2024, 23:46

The “takeprofit” parameter in the execute market order is in Pips; you're supplying an actual price.

Replace “takeProfit” in the ExecuteMarketOrder with your parameter “TakeProfitPips”. 

OR what you need to do is convert “takeProfit” back to pips as a distance from the current candle close, which seems pointless in this scenario as you have it since TP is measured from the current “close” price when the order is placed. 


@firemyst

mmackay911
30 Oct 2024, 10:38 ( Updated at: 30 Oct 2024, 13:21 )

RE: Setting TP and SL in pips not working as expected

firemyst said: 

The “takeprofit” parameter in the execute market order is in Pips; you're supplying an actual price.

Replace “takeProfit” in the ExecuteMarketOrder with your parameter “TakeProfitPips”. 

OR what you need to do is convert “takeProfit” back to pips as a distance from the current candle close, which seems pointless in this scenario as you have it since TP is measured from the current “close” price when the order is placed. 

Thanks @firemyst, changing to TakeProfitPips worked a treat. Your explanation makes perfect sense. Much appreciated.

I wonder if the ExecuteMarketOrder used to be coded to expect the price, rather than pips, since ChatGPT seems to think that's how it works.


@mmackay911

firemyst
30 Oct 2024, 23:36

RE: RE: Setting TP and SL in pips not working as expected

mmackay911 said: 

firemyst said: 

The “takeprofit” parameter in the execute market order is in Pips; you're supplying an actual price.

Replace “takeProfit” in the ExecuteMarketOrder with your parameter “TakeProfitPips”. 

OR what you need to do is convert “takeProfit” back to pips as a distance from the current candle close, which seems pointless in this scenario as you have it since TP is measured from the current “close” price when the order is placed. 

Thanks @firemyst, changing to TakeProfitPips worked a treat. Your explanation makes perfect sense. Much appreciated.

I wonder if the ExecuteMarketOrder used to be coded to expect the price, rather than pips, since ChatGPT seems to think that's how it works.

Set the pips, and then immediately call:

ModifyTakeProfitPrice()

to set the price if you want to set it by exact price instead of pips.


@firemyst