Code trailing stop like “set stop ptrailing x”
Forums › ProRealTime English forum › ProOrder support › Code trailing stop like “set stop ptrailing x”
- This topic has 12 replies, 4 voices, and was last updated 1 year ago by Suffi.
-
-
06/07/2023 at 2:28 PM #215760
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?
06/07/2023 at 2:33 PM #215762I found this “trailing stop IG style” in my snippets chest:
1234567891011121314151617181920212223242526StopLoss = 23 //StopLoss sizeTrailingStopStart = 0 //Trailing Stop start at X points in profitbuystoploss = close-StopLoss*pointsizesellstoploss = close+StopLoss*pointsizeif not onmarket thenibuystoploss=buystoplossisellstoploss=sellstoplossendifif longonmarket then//checking and adjusting stoplossif close-tradeprice>=trailingstopstart*pointsize thenibuystoploss = max(ibuystoploss,high-(stoploss+trailingstopstart)*pointsize)endif//set the stoploss levelsell at ibuystoploss stopendifif shortonmarket then//checking and adjusting stoplossif tradeprice-close>=trailingstopstart*pointsize thenisellstoploss = min(isellstoploss,low+(stoploss+trailingstopstart)*pointsize)endif//set the stoploss levelexitshort at isellstoploss stopendif06/07/2023 at 3:04 PM #21576706/08/2023 at 4:36 PM #215870I 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.
06/08/2023 at 6:39 PM #215879Maybe 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?
09/21/2023 at 2:58 PM #221394I 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?09/21/2023 at 4:17 PM #221398You have to use:
buystoploss = close–StopLoss*pointsizesellstoploss = close+StopLoss*pointsizewhen 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.09/21/2023 at 7:15 PM #221408Sorry @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.
09/21/2023 at 7:22 PM #221409OK, 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.
Trailing stop following at fixed distance1234567891011121314151617181920212223242526tsDistance = 40 // Trailing Stop following at fixed distancetsStart = 0 // Trailing Stop start at X points in profitif not onmarket thentsStarted = 0endifif longonmarket thenif not tsStarted and close-tradeprice(1) >= tsStart*pointsize thentsStarted = 1tsLong = high-tsDistance*pointsize // First trailing stop levelendifif tsStarted thentsLong = max(tsLong, high-tsDistance*pointsize) // Next ts levelsendifsell at tsLong stopendifif shortonmarket thenif not tsStarted and tradeprice(1)-close >= tsStart*pointsize thentsStarted = 1tsShort = low+tsDistance*pointsize // First trailing stop levelendifif tsStarted thentsShort = max(tsShort, low+tsDistance*pointsize) // Next ts levelsendifexitshort at tsShort stopendif1 user thanked author for this post.
09/21/2023 at 7:27 PM #221410Fun 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 = -2009/24/2023 at 7:44 PM #22152709/25/2023 at 7:43 AM #22153609/25/2023 at 12:49 PM #221554Hello,
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 -
AuthorPosts
Find exclusive trading pro-tools on