SVKParticipant
Junior
Hi all,
I would really appreciate if any of you experienced people can point me in the right direction of Trailing stop loss.
I’m aware of the Snippet Link Library and I have now tested several of them and it must be me but none of them are doing what I’m looking for.
Maybe some of them do and I’m just far too stupid (spent 6 hours yesterday and 4 hours today and frustration is getting better of me).
Can you please point me to Trailing stop loss which does following ?
Example:
1; Stop Loss = 20 points
2; Trailing starts after price moves 5 points in my favor
3; Trailing step is 5 points
So every 5 points in my favor stop moves up by 5 points (simple mechanical thing).
Stop Loss
Trailing start
Trailing step
Vonas has done something close to that but there is not trailing step (if his startegy didn’t have minimum stop distance it would does what I’m after).
sl = 144 //Stop loss distance
slmove = 5 //Price move needed to move stop
minstop = 10 //Minimum stop distance allowed
Thank you very much
Lines 17-56 of this snippet by Nicolas is doing exactly what you are looking for https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/.
Just Copy & Paste those lines at the end of your code and replace 20 with 5 in trailingstart (trailingstep is already set as 5).
SVKParticipant
Junior
Hi Roberto,
thank you very much for your response.
I’m using Nicolas’s trailing stop with your modification.
1; Nicolas’s trailing stop was stopping strategy 100% of the time as soon as profit reached Trailingstart
2; I have searched for hours and found your post I think in German language where you have Distance = 4 * pipsize. This has helped massively as the strategy stopped quitting after each succesfull trade.
3; Trailing stop never trails. 100% of profitable trades exit at the point of reaching trailingstart.
DAX settings
Trailing start = 30 points
Trailing step = 6 points (tested with, 7, 8, 9, 10, 11 makes zero difference)
I have observed behavior of this trailing stop:
1; Trailing start reached (30 points)
2; it correctly moves stop loss to break even point
3; immediatelly it moves stop to the next step 6 points
4; another 6 points and another until it reaches 30 points and then it sells at that point (100% trades exited at 30 point)
What I’m after:
Stop loss = -20 points
Step size = 5 points
Trailing start = 30
30 points move up = New stop loss -15
Another 30 points up = New stop loss -10
Thank you very much for your help
SVKParticipant
Junior
Roberto,
you are a gentleman.
Thank you very much for your help
Try this one:
//----------------------------------------------------------------------------------------------------
// Trailing Stop (classic, no breakeven)
//
NewTrade = (OnMarket AND Not OnMarket[1]) OR (LongOnMarket AND ShortOnMarket[1]) OR (LongOnMarket[1] AND ShortOnMarket)
//
IF Not OnMarket OR NewTrade THEN
MainSL = 400 * PipSize
TrailingStart = 100 * PipSize
TrailingStep = 30 * PipSize
Distance = 10 * PipSize
ENDIF
//
// LONG side
//
MyProfit = positionperf * positionprice
IF LongOnMarket THEN
IF (Not OnMarket[1]) OR ShortOnMarket[1] THEN
TrailingSL = TradePrice - MainSL
ENDIF
IF ((close - TradePrice) >= TrailingStart) AND (TrailingStart > 0) THEN
TrailingSL = TrailingSL + TrailingStart
SavedPips = TrailingStart
TrailingStart = 0
ENDIF
WHILE ((MyProfit - SavedPips) >= TrailingStep)
TrailingSL = TrailingSL + TrailingStep
SavedPips = SavedPips + TrailingStep
WEND
ENDIF
//
// SHORT side
//
IF ShortOnMarket THEN
IF (Not OnMarket[1]) OR LongOnMarket[1] THEN
TrailingSL = TradePrice + MainSL
ENDIF
IF ((TradePrice - close) >= TrailingStart) AND (TrailingStart > 0) THEN
TrailingSL = TrailingSL - TrailingStart
SavedPips = TrailingStart
TrailingStart = 0
ENDIF
WHILE ((MyProfit - SavedPips) >= TrailingStep)
TrailingSL = TrailingSL - TrailingStep
SavedPips = SavedPips + TrailingStep
WEND
ENDIF
//
// LONG Exit
IF LongOnMarket THEN
IF close > (TrailingSL + Distance) THEN
SELL AT TrailingSL STOP
ELSIF close < (TrailingSL - Distance) THEN
SELL AT TrailingSL LIMIT
ELSE
SELL AT Market
ENDIF
ENDIF
// SHORT Exit
IF ShortOnMarket THEN
IF (close - Distance) > TrailingSL THEN
EXITSHORT AT TrailingSL LIMIT
ELSIF (close + Distance) < TrailingSL THEN
EXITSHORT AT TrailingSL STOP
ELSE
EXITSHORT AT Market
ENDIF
ENDIF
SVKParticipant
Junior
Roberto,
thank you very much for your help.
I will try it tomorrow and will report back.
Would have never manage to code something like this.