Trailing stop loss that advances when a new swing high or low are made
Forums › ProRealTime English forum › ProOrder support › Trailing stop loss that advances when a new swing high or low are made
- This topic has 19 replies, 5 voices, and was last updated 4 years ago by
ligand.
-
-
08/10/2019 at 4:42 PM #104533
Hi all,
another post from me. Thanks for all the help thus far, really appreciate it while I’m learning. Hope to be able to give back in due course.
I am trying to code a trailing stop loss that places stops above recent swing highs (shoreside) and swing lows (longside). I have utilised some code I found that calculates recent fractals. It works ok but there is on key missing ingredient that I am not sure how to code.
If we are following the trend then we should only move the stop up/down when a previous swing point is closed through, at which point the trailing stop would move to the closes swing high or low.
Right now using the below code the trailing stop will simply move to the next identified fractal regardless of whether price has moved in our direction and closed above (uptrend) or below (downtrend) the current swing high/low. I only want the trailing stop to advance when price closes through the current swing high/low in our direction.
Does that make sense? This is what I typically do manually when trend following intraday and works a treat. Would be awesome to automate it. Here’s the trailing stop code I currently have which is adapted from a few other sources.
Any help gratefully received
Roj12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152// calculate swing pointsPivotBAR = 2LookBack = 4// Swing lowBarLookBack = PivotBAR + 1IF low[PivotBAR] < lowest[LookBack](low)[BarLookBack] THENIF low[PivotBAR] = lowest[BarLookBack](low) THENSupportPrice = low[PivotBAR]ENDIFENDIF// Swing highIF high[PivotBAR] > highest[LookBack](high)[BarLookBack] THENIF high[PivotBAR] = highest[BarLookBack](high) THENResistancePrice = high[PivotBAR]//high[BarIndex - MyResistance]ENDIFENDIF// Parameters for determining when BE stop kicks inTrailingStart = 1*AverageTrueRange[10] * pipsize //trailing will start @trailinstart points profitTrailingStep = 0 * pipsize// LongsideIF LONGONMARKET THEN// move to breakevenIF newSL = 0 AND CLOSE - TRADEPRICE(1) >= TrailingStart THENnewSL = TRADEPRICE(1) + TrailingStepENDIF// next movesIF newSL > 0 AND close > ResistancePrice AND SupportPrice > newSL THENnewSL = SupportPrice[1]ENDIFENDIF//ShortsideIF SHORTONMARKET THEN// move to breakevenIF newSL = 0 AND TRADEPRICE(1) - CLOSE >= TrailingStart THENnewSL = TRADEPRICE(1) - TrailingStepENDIF// next movesIF newSL > 0 AND close < SupportPrice AND ResistancePrice < newSL THENnewSL = ResistancePrice[1]ENDIFENDIF//stop order to exit the positionsIF newSL > 0 THENSELL AT newSL STOPEXITSHORT AT newSL STOPENDIF12/18/2020 at 3:41 PM #154307Hi,
have you been able to solve your problem? Im looking for exactly the same solution, but I am far less gifted regarding programming than you. Would be very thankful if you could show the complete code, if you have one.
Greetings from the alps!
12/18/2020 at 4:03 PM #154312Line 32 should read:
1newSL = max(newSL,SupportPrice[1])and line 44 should read:
1newSL = min(newSL,ResistancePrice[1])1 user thanked author for this post.
12/19/2020 at 11:48 AM #15440712/19/2020 at 6:34 PM #154445Can I please ask another question which I can’t find the answer to, but I think is simple.
I have programmed a screener and manually select the instruments it suggests. Then I start the trade with a stop below the last swing extreme.
Afterwards the program should kick in to draw the stop automatically.
But an automatic trading system says it needs a buy and sell order. How can I make the algorithm work on an already existing trade?
I think if I try to make an indicator and link the stop-sell (or -buy) order to it, then this will be only pssible intra-day. But my trades normally last longer.
I’d be very thankful for a hint, how to solve this problem.
12/19/2020 at 7:00 PM #154449Perhaps it would work if the last lines are changed from:
1234IF newSL > 0 THENSELL AT newSL STOPEXITSHORT AT newSL STOPENDIFto
1234IF newSL > 0 THENSELLSHORT AT newSL STOPBUY AT newSL STOPENDIF?
MODERATORS EDIT: Your post has been edited. please use the ‘Insert PRT Code’ button when putting code in your posts.
12/19/2020 at 7:25 PM #154454Your solution would do a Stop & Reverse OR accumulate positions.
Manual and Automatic trading cannot be combined.
You can only alter trades opened automatically, though this will shut your strategy.
1 user thanked author for this post.
12/19/2020 at 9:14 PM #15447412/19/2020 at 10:07 PM #154482Indicators can only return numeric values.
Alerts can only be set manually according to what indicators return.
1 user thanked author for this post.
12/20/2020 at 9:57 AM #154535Thanks again, Roberto. But I don’t really understand your sentence: Indicators can only return numeric values.
Is it not the case, that this indicator would return numeric, albeit distinct stop-loss levels?
I’m really thankful for your time and knowledge but I would really love to solve this problem As an amateur I would think that there has to be a way to automate this, since it doesn’t seem to be to complex. Do you please have any idea if I could ask somewhere or if there any other way?
Sorry for bothering you, best regards, Michael
12/20/2020 at 11:11 AM #15455012/20/2020 at 11:48 AM #154553Try what you want to do using the Alert Function with it’s integral Trade Open function.
Try above using a stnadard Indcator … e.g Bollinger using the Close Crosses Over BollDown on the Alert Function.
If above does what you want then you are half way there?
1 user thanked author for this post.
12/20/2020 at 6:27 PM #154622Hello Grahal, thank you! I think your suggestion may be the same I mentioned above (to construct an indicator based on the swing-extreme trailing stop (which would exceed my abilities) and then link this indicator to an alarm and this alarm to a sell trigger)?
If this works, the code has to be changed from trading advice to definition of a indicator and I must say that above my abilities. Could someone of you perhaps help – if this way makes sense?
Another different solution might be to do it completely via an automated trading approach using a simple trade entry advice after the screener suggested a good candidate at bar “[1]”, i.g. for a long trade:
1234567defparam cumulateorder=falseonce nbbarlimit=1if close>close[1] and close< close[1]+0.3*atr[10] thenbuy x shares at marketendif12/20/2020 at 6:43 PM #15462412/22/2020 at 10:07 PM #154923- The code line starting with defparam gives an error , but why?
2. Roberto, I thought you gave a hint to another error in the code from Thally besides “max(newSL,SupportPrice[1])”, but I can’t find it anymore. Did you erase this post?
Thanks, Michael
-
AuthorPosts
Find exclusive trading pro-tools on