Modified Nicolas' Trailing Stop code
Forums › ProRealTime English forum › ProOrder support › Modified Nicolas' Trailing Stop code
- This topic has 4 replies, 3 voices, and was last updated 3 years ago by robertogozzi.
Tagged: trailing stop
-
-
06/15/2018 at 10:42 AM #73306
Sometimes it may happen that your target is not reached, but price went in your favour and you want to save some of the profits earned so far, that’s why the Trailing Stop exist!
But in automated trading (at least until MTF will be supported) and mainly on higher TFs, Nicolas’code to support a good Trailing Stop is executed only when a bar closes, leaving room for price to pullback or even reverse, despite your trailing stop may have been triggered while the bar was forming, simply because when the bar closes price has retraced to a lower point < to your trailing stop treshold and the code is fooled into believing the price never reached that treshold!
So, while awaiting MTF to come, I replaced CLOSE with HIGH and LOW, respectively, for Long and Short trades. So the code will compare TRADEPRICE with a higher/lower price than before, the price you would use manually when in front of your charts.
When the bar closes and the code is executed, it will set a new SL according to those new prices in relation with TRADEPRICE and set a new SL where it wouldn’t have been set before, or closing immediately ifa price has fallen below TRADEPRICE!
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657//************************************************************************// trailing stop functiontrailingstart = 10 //10 trailing will start @trailinstart points profittrailingstep = 5 //5 trailing step to move the "stoploss"////reset the stoploss valueIF NOT ONMARKET THENnewSL=0ENDIF//manage long positionsIF LONGONMARKET THEN//first move (breakeven)IF newSL=0 AND HIGH-tradeprice(1)>=trailingstart*pipsize THEN //close --> HIGHnewSL = tradeprice(1)+trailingstep*pipsize// new codingIF newSL > close THEN //if current closing price is < new SL then exit IMMEDIATELY!SELL AT MARKETENDIF// end new codingENDIF//next movesIF newSL>0 AND close-newSL>=trailingstep*pipsize THENnewSL = newSL+trailingstep*pipsize// new codingIF newSL > close THEN //if current closing price is < new SL then exit IMMEDIATELY!SELL AT MARKETENDIF// end new codingENDIFENDIF//manage short positionsIF SHORTONMARKET THEN//first move (breakeven)IF newSL=0 AND tradeprice(1)-LOW>=trailingstart*pipsize THEN //close --> LOWnewSL = tradeprice(1)-trailingstep*pipsize// new codingIF newSL < close THEN //if current closing price is > new SL then exit IMMEDIATELY!EXITSHORT AT MARKETENDIF// end new codingENDIF//next movesIF newSL>0 AND newSL-close>=trailingstep*pipsize THENnewSL = newSL-trailingstep*pipsize// new codingIF newSL < close THEN //if current closing price is > new SL then exit IMMEDIATELY!EXITSHORT AT MARKETENDIF// end new codingENDIFENDIF//stop order to exit the positionsIF newSL>0 THENSELL AT newSL STOPEXITSHORT AT newSL STOPENDIF//************************************************************************Should anyone take some further risk of a higher loss and aim at closing at breakeven instead of a loss, in case price has fallen below entry point, lines 17 and 26 can be replaced by:
1SELL AT newSL LIMITand lines 38 and 47 with:
1EXITSHORT AT newSL LIMITI tested it a bit and found out that it yields slightly poorer performances, but less losing trades and less drawdown.
In a few months this post will, hopefully, be superseded by MTF.
6 users thanked author for this post.
06/15/2018 at 11:07 AM #73311The last lines to replace some code should be used IF price has fallen below ENTRY POINT, otherwise with the original lines the trade would be closed in profit, as from the screenshot that I attached.
06/15/2018 at 11:52 AM #73318That is a very useful improvement Roberto, thank you so much!
I have added, as row 41, to here Snippet Link Library
3 users thanked author for this post.
07/04/2021 at 4:14 PM #173093Hi and thanks for above trailing stop loss code.
Is there a multi time frame trailing stop loss code snippet that you can share or direct me to please?
Thanks
Sachin
07/04/2021 at 4:41 PM #173097To use it in an MTF system, simply add as first line:
1Timeframe(default)then use any other TF in your system.
Usually this snippet is appended to strategies. -
AuthorPosts
Find exclusive trading pro-tools on