Is it actually possible to somehow combine the “set stop ptrailing x” function and a coded trailing stop (e.g. Nicolas coded trailing stop)? The advantage of the “set stop ptrailing x” variant is that it follows the price at a distance when it makes big jumps in the right direction. The coded version doesn’t do that. The advantage of the coded variant is that when the price stagnates above a certain level, it still secures profits and closes the sack.
Is it possible to code a coded trailing stop that secures large price jumps faster and still secures profits when the price stagnates? Only then just slower and time-based?
Or is there something like this already and I haven’t found it yet?
I found this “trailing stop IG style” in my snippets chest:
StopLoss = 23 //StopLoss size
TrailingStopStart = 0 //Trailing Stop start at X points in profit
buystoploss = close-StopLoss*pointsize
sellstoploss = close+StopLoss*pointsize
if not onmarket then
ibuystoploss=buystoploss
isellstoploss=sellstoploss
endif
if longonmarket then
//checking and adjusting stoploss
if close-tradeprice>=trailingstopstart*pointsize then
ibuystoploss = max(ibuystoploss,high-(stoploss+trailingstopstart)*pointsize)
endif
//set the stoploss level
sell at ibuystoploss stop
endif
if shortonmarket then
//checking and adjusting stoploss
if tradeprice-close>=trailingstopstart*pointsize then
isellstoploss = min(isellstoploss,low+(stoploss+trailingstopstart)*pointsize)
endif
//set the stoploss level
exitshort at isellstoploss stop
endif
Oh, interesting code. Thanks, I will test the function tonight.
I have to say it works great. 👍🏼 Can this code be supplemented with another trailing code? For example, that at a certain point when the price stagnates, steps will continue to be secured? For example, if 40 points are in profit but the price is going in the opposite direction, the code closes the sack by still securing more steps.
Maybe hard to put into words… Let’s take an example:
buy at 1000
the price goes up to 1050
the IG trail with setting 25 saves 25 pips
a normal trail that gradually saves 1 pip from 25 has at 1050 depending on the time, a few points secured
Now the price drops back down to 1000 and we have a few points on the normal trail and 25 on the IG trail as a win.
But if, and that’s my point, the price fluctuates around 1050, the IG trail always remains at 25 secured points, but the normal trail gradually secures up to 50 points at best. Now the question… is there any way to combine the benefits of both codes?
WimParticipant
Junior
I might be wrong @Nicolas, but this IG style trailing stop could surprise you. Suppose a strategy that enters into position long after a serious gap down. The last candle with status NOT ONMARKET will assign a value to ibuystoploss of e.g. 110. The entry at the open of the next candle (with gap down) is at 90, and the close of this candle is 100. With values of StopLoss=10 and TrailingStopStart=0, the assignment of ibuystoploss at the close of the entry candle will be 110 (the last assigned value in NOT ONMARKET status). A pending order SELL AT 110 STOP will be launched, while the actual price is around 100, lower!
Second, small remark: the distance at which the trailing stop is following the price is not StopLoss but StopLoss+TrailingStopStart. Why is that?
You have to use:
buystoploss = close–StopLoss*pointsize
sellstoploss = close+StopLoss*pointsize
when you enter at market, in order to anchor the first stoploss to the Close of the candlestick. Then the trailingstop code will do the rest. If you don’t want to enter on a gap, include a condition in your entry decision tree.
WimParticipant
Junior
Sorry @Nicolas, I wasn’t clear. I pointed out a situation, very particular I admit, that is not handled correctly by this code. If you enter long at the open of a bullish candle that immediately triggers the start of the trailing stop, you might end up with a pending sell order ABOVE the actual price. This will be the case if the candle immediately preceding the entry candle closed more than the starting distance above the entry candle.
I will post alternative code that doesn’t have this problem. In my code the equivalent of TrailingStartStop is not added to the following distance.
WimParticipant
Junior
OK, here is my alternative for a trailing stop following at a fixed distance. Like the original IG version only 2 inputs: tsDistance = fixed distance at which the trailing will be done, and tsStart = number of points in profit that will activate the trailing.
tsDistance = 40 // Trailing Stop following at fixed distance
tsStart = 0 // Trailing Stop start at X points in profit
if not onmarket then
tsStarted = 0
endif
if longonmarket then
if not tsStarted and close-tradeprice(1) >= tsStart*pointsize then
tsStarted = 1
tsLong = high-tsDistance*pointsize // First trailing stop level
endif
if tsStarted then
tsLong = max(tsLong, high-tsDistance*pointsize) // Next ts levels
endif
sell at tsLong stop
endif
if shortonmarket then
if not tsStarted and tradeprice(1)-close >= tsStart*pointsize then
tsStarted = 1
tsShort = low+tsDistance*pointsize // First trailing stop level
endif
if tsStarted then
tsShort = max(tsShort, low+tsDistance*pointsize) // Next ts levels
endif
exitshort at tsShort stop
endif
WimParticipant
Junior
Fun fact: you can enter a negative number as tsStart! Suppose you normally have your initial StopLoss at 20 points, you could enter tsStart = -20. The effect being that the trailing will start immediately at your initial StopLoss-level.
Screen captures with tsStart = 0 and one with tsStart = -20
How does it work when
defparam cumulateorders=true
???
If you cumulate orders, you should use POSITIONPRICE instead of TRADEPRICE to place your STOP orders.
POSITIONPRICE returns the average open price of orders.
Hello,
Example:
first buy 100, SL 70 points lower (SL set at 30).
The price drops to 50 and you buy one Lot, gives a position price of 75.
Now also the SL also drops 25 points down?
How does it look if further positions are bought?
Greets
Suffi