Error Message when initiating Trailing Stop Loss

Created at 04 Aug 2021, 21: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!
TH

thecaffeinatedtrader

Joined 22.07.2020

Error Message when initiating Trailing Stop Loss
04 Aug 2021, 21:08


Hi,

I have finally completed my bot, or so I had thought. I ran it last night and it executed a trade perfectly. Had the initial hard stop loss set to the proper amount of pips which was awesome. 

However.... when it finally reached the trigger point in pips to remove the stop loss and set a trailing stop instead, it gave me an error leaving my position with no stop loss at all !!! 

We cannot be having this lol.... 

If someone could help me figure out why this has happened that would be amazing! Below is the code for the trailing stop that I have set and also the error message that was given in my trading journal.

       

private void SetTrailingStop()
        {

            var sellPositions = Positions.FindAll(Label);


            foreach (var position in sellPositions)
            {
                double distance = position.EntryPrice + Symbol.Ask;

                if (distance < TrailingStopTrigger * Symbol.PipSize)
                    continue;

                double newStopLossPrice = Symbol.Ask + (TrailingStopStep * Symbol.PipSize);

                if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
                {
                    ModifyPosition(position, newStopLossPrice, null);
                }
            }
            {
                var buyPositions = Positions.FindAll(Label);

                foreach (var position in buyPositions)
                {
                    double distance = Symbol.Bid - position.EntryPrice;

                    if (distance < TrailingStopTrigger * Symbol.PipSize)
                        continue;

                    double newStopLossPrice = Symbol.Bid - (TrailingStopStep * Symbol.PipSize);

                    if (position.StopLoss == null || newStopLossPrice > position.StopLoss)
                    {
                        ModifyPosition(position, newStopLossPrice, null);
                    }
                }
            }
        }

Error Message

04/08/2021 03:46:05.799 | → Request to amend position PID8541749 (SL: 151.203) is REJECTED with error "New SL for BUY position should be <= current BID price. current BID: 77.2, SL: 151.203;  "

 

With that said... I do understand that it tried to place the new stop loss above price which obviously cannot be done in a buy position. However, when I run it through back testing, it works perfectly fine, but live it doesn't.. I've tried to adjust it and ran it through the back testing again and it did not work... So I am a little confused as to what I am doing wrong... 


@thecaffeinatedtrader
Replies

PanagiotisCharalampous
05 Aug 2021, 08:27

Hi thecaffeinatedtrader,

The message is clear about what happened. You have not provided the complete source code therefore we cannot know what is the exact problem. However it seems that you do not distinguish anywhere buy positions from sell positions. This might be the source of your problem. See below

 var sellPositions = Positions.FindAll(Label);
var buyPositions = Positions.FindAll(Label);

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

thecaffeinatedtrader
05 Aug 2021, 08:33 ( Updated at: 06 Aug 2021, 06:55 )

RE:

.


@thecaffeinatedtrader

PanagiotisCharalampous
05 Aug 2021, 08:51

Hi thecaffeinatedtrader,

Did you read my reply above? I mentioned what the problem is

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous