Trailing Stop Help

Created at 01 Apr 2014, 17:19
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!
TE

Tempestshade

Joined 29.03.2014

Trailing Stop Help
01 Apr 2014, 17:19


Hello,

I am having a problem using the sample trailing stop code provided in the sample section. I have an EA that current opens a new trade on every bar as long as the signal is still valid. So far that is done properly. I at first experimented with a set TP and SL; that functioned properly. However, I am trying to implement a trailing stop for each individual trade that can occur. What seems to be happening with my code below is that ALL of the trades are getting set with the same SL irregardless of their entry price. And the stoploss modification only occurs once per trade. 

Can anyone give me a hand in making it so EACH individual trade has its own trailing stop, and that the trailing stop follows the trade as long as it doesn't retrace back into and stop out?

 

Thanks,

David

 

var tposition = Positions.FindAll(label);

            foreach (var position in tposition)
            {
                if (position == null)
                    return;
                if (position.TradeType == TradeType.Buy)
                {
                    double distance = Symbol.Bid - position.EntryPrice;

                    if (distance >= Trigger * Symbol.PipSize)
                    {
                        double newStopLossPrice = Math.Round(Symbol.Bid - TrailingStop * Symbol.PipSize, Symbol.Digits);

                        if (position.StopLoss == null || newStopLossPrice > position.StopLoss)
                        {
                            ModifyPosition(position, newStopLossPrice, null);
                        }
                    }
                }
                else
                {
                    double distance = position.EntryPrice - Symbol.Ask;

                    if (distance >= Trigger * Symbol.PipSize)
                    {
                        double newStopLossPrice = Math.Round(Symbol.Ask + TrailingStop * Symbol.PipSize, Symbol.Digits);

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

 


@Tempestshade
Replies

Spotware
02 Apr 2014, 10:17

As we can see newStopLossPrice doesn't depend on any position properties:

double newStopLossPrice = Math.Round(Symbol.Bid - TrailingStop * Symbol.PipSize, Symbol.Digits);

Therefore it is expected that all positions (with the same trade type and distance more than Trigger) will have the same value of stop loss.

Could you please be more specific?


@Spotware

Tempestshade
02 Apr 2014, 15:38

Thanks for replying! 

 

I will try to be more specific.

 

My system opens a new trade every bar that a signal is valid. I want each trade to have an individual trailing stop. Essentially, I want every trade to be managed separately instead of as a basket like it is now.

 

Cheers,

David


@Tempestshade

Tempestshade
03 Apr 2014, 17:18

Still haven't been able to figure this out. Have tried tweaking it every which way but with no luck... :/


@Tempestshade

Spotware
03 Apr 2014, 17:28

Unfortunately, we don't understand why you are not satisfied with your current solution.


@Spotware

tradermatrix
03 Apr 2014, 19:32

your robot; the trailing stop works with OnTick or OnBar?


@tradermatrix