Trailing stoploss

Created at 29 Jul 2020, 22:26
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!
SA

Samm193

Joined 11.05.2020

Trailing stoploss
29 Jul 2020, 22:26


Hi All,

Can someone see whats wrong in my code please? 

            if (TrailingSL)
            {
                if (pos.NetProfit / pos.VolumeInUnits / Symbol.PipSize <= Stoploss)
                {
                    return;
                }
                double newstoploss = pos.TradeType == TradeType.Buy ? Bars.ClosePrices.LastValue - Stoploss * Symbol.PipSize : Bars.ClosePrices.LastValue + Stoploss * Symbol.PipSize;
                newstoploss = Math.Round(newstoploss, Symbol.Digits);

                TradeResult result = ModifyPosition(pos, newstoploss, pos.TakeProfit.Value);

                if (result.IsSuccessful)
                {
                    Print(string.Format("Position {0}: stoploss updated to {1}.", pos.Id, pos.StopLoss.Value));
               

                else
                {
                    Print(string.Format("Position {0}: stoploss could not be updated to {1}.", pos.Id, newstoploss));
                }
            }
        }

 

It always give a fault in ctrader that the "pos" isn't accepted. 

 

Thank you,


@Samm193
Replies

PanagiotisCharalampous
31 Jul 2020, 08:34

Hi Samm193,

You need to explain first what is the problem.

  1. Please provide the complete cBot code.
  2. Explain what do you expect the code to do
  3. Explain what is it doing instead and why do you think it is wrong.

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

... Deleted by UFO ...