Coding help needed – flexible stop loss
Forums › ProRealTime English forum › ProOrder support › Coding help needed – flexible stop loss
- This topic has 7 replies, 2 voices, and was last updated 2 weeks ago by
robertogozzi.
-
-
03/29/2025 at 12:05 PM #245405
Good morning,
i have a piece of code for a strategy which has a moving profit target but a fixed stop loss.
i would like some help please that would keep the original target in place but in addition, add a parameter that when a different $profit is hit, the stop loss is moved to ensure i lock in a profit.
example: target profit is 20*AverageTrueRange[15](close)
however, if the trade is in profit say £150, then move the stop loss to £100 in profit and close trade on that pull back.
thanks in advance, hope the above makes sense
03/29/2025 at 3:34 PM #245406There you go:
1234567891011121314151617181920212223242526ONCE TP = 150 //150 currency unit (€, $, etc...)ONCE SL = 450 //450 currency unit (€, $, etc...)ONCE SLflag = 1Sma100 = average[100,0](close)myLongConditions = close CROSSES OVER Sma100myShortConditions = close CROSSES UNDER Sma100IF Not OnMarket THENIF myLongConditions THENBUY 1 CONTRACT AT MARKETELSIF myShortConditions THENSELLSHORT 1 CONTRACT AT MARKETENDIFSET TARGET PROFIT 50*AverageTrueRange[15](close)SET STOP $LOSS SLSLflag = 1tempProfit = 0ENDIFSET TARGET PROFIT 50*AverageTrueRange[15](close)IF OnMarket THENtempProfit = TradePrice * PositionPerf * PipValue * abs(CountOfPosition)IF (tempProfit >= TP) AND SLflag THENSLflag = 0SET STOP $LOSS 100ENDIFENDIF//graph tempProfit2 users thanked author for this post.
04/01/2025 at 11:41 AM #245481many thanks for coming back to me, as always, much appreciated.
however, the code moves the stop loss to a 100 loss, but still a loss – i would like the stop loss moved to a target that is positive so the trade at least makes some money – so in essence once a target is hit (the code you kindly provided), i then have 2 take profit targets, the original one as set out in initial trade and one which is lower than the current profit – ie, replacement of the original stop loss.
in my earlier example, if my running profit hits £150 then my old stop loss which is naturally negative is replaced by a number which is positive so i lock in a definite gain – like £100 in the event of a pull back. and i also have my original target still in place so i win either way.
i dont want a trailing stop loss per se as i am happy with 2 distinct take profit targets
hope thats clear
thanks in advance for your help
04/01/2025 at 9:33 PM #245499My fault, here’s the correct one:
123456789101112131415161718192021222324252627282930313233ONCE TP = 150 //150 currency unit (€, $, etc...)ONCE SL = 450 //450 currency unit (€, $, etc...)ONCE SLflag = 1Sma100 = average[100,0](close)myLongConditions = close CROSSES OVER Sma100myShortConditions = close CROSSES UNDER Sma100IF Not OnMarket THENIF myLongConditions THENBUY 1 CONTRACT AT MARKETELSIF myShortConditions THENSELLSHORT 1 CONTRACT AT MARKETENDIFSET TARGET PROFIT 50*AverageTrueRange[15](close)SET STOP $LOSS SLSLflag = 1tempProfit = 0ENDIFSET TARGET PROFIT 50*AverageTrueRange[15](close)IF OnMarket THENtempProfit = TradePrice * PositionPerf * PipValue * abs(CountOfPosition)IF (tempProfit >= TP) AND SLflag THENSLflag = 0IF LongOnMarket THENNewSL = TradePrice + (100 / abs(CountOfPosition) / PipValue)ELSENewSL = TradePrice - (100 / abs(CountOfPosition) / PipValue)ENDIFSET STOP PRICE NewSLENDIFENDIF//graph tempProfit//graph NewSL//graph TradePrice1 user thanked author for this post.
04/03/2025 at 3:26 PM #245556many thanks Roberto that is amazing – really appreciate your help – that works perfectly.
However, the structure of the second take profit/movable stop loss has given me an idea.
can a second one be added so it looks something like:
if temp profit goes to 100, move stop loss/new take profit to +50
if temp profit goes to 150, move stop loss/new take profit to +100
make sense? the numbers dont really matter at this stage i am just trying to limit losses and maximise the gain but allow flexibility for some volatility
thank you so much in advance
04/03/2025 at 9:41 PM #245562This version support 2 profitable SLs, the first one is triggered at a $100 gain and sets a SL to +$50, the second one is triggered at a $150 gain and sets a SL to +$100.
When a major Gain occurs and the first profitable trigger is the SECOND trigger, obviously there will not be a FIRST (worse) SL1.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950ONCE Trigger1 = 100 //100 $ gain is second trigger for SL 2ONCE TP1 = 50 //50 currency unit (€, $, etc...)ONCE Trigger2 = 150 //150 $ gain is second trigger for SL 2ONCE TP2 = 100 //100 currency unit (€, $, etc...)ONCE SL = 450 //450 currency unit (€, $, etc...)ONCE SLflag1 = 1ONCE SLflag2 = 1Sma100 = average[100,0](close)myLongConditions = close CROSSES OVER Sma100myShortConditions = close CROSSES UNDER Sma100IF Not OnMarket THENIF myLongConditions THENBUY 1 CONTRACT AT MARKETELSIF myShortConditions THENSELLSHORT 1 CONTRACT AT MARKETENDIFSET TARGET PROFIT 50*AverageTrueRange[15](close)SET STOP $LOSS SLSLflag1 = 1SLflag2 = 1tempProfit = 0ENDIFSET TARGET PROFIT 50*AverageTrueRange[15](close)IF OnMarket THENtempProfit = TradePrice * PositionPerf * PipValue * abs(CountOfPosition)IF (tempProfit >= Trigger2) AND SLflag2 THENSLflag1 = 0SLflag2 = 0IF LongOnMarket THENNewSL = TradePrice + (TP2 / abs(CountOfPosition) / PipValue)ELSENewSL = TradePrice - (TP2 / abs(CountOfPosition) / PipValue)ENDIFSET STOP PRICE NewSLENDIFIF (tempProfit >= Trigger1) AND SLflag1 THENSLflag1 = 0IF LongOnMarket THENNewSL = TradePrice + (TP1 / abs(CountOfPosition) / PipValue)ELSENewSL = TradePrice - (TP1 / abs(CountOfPosition) / PipValue)ENDIFSET STOP PRICE NewSLENDIFENDIF//graph tempProfit//graph NewSL//graph TradePrice//graph SLflag1//graph SLflag2 coloured("Red")1 user thanked author for this post.
04/04/2025 at 5:01 PM #245596thank you once again Roberto for the code – works perfectly well
much appreciated as always for your excellent help
1 user thanked author for this post.
04/05/2025 at 2:48 PM #245612Sorry for the incorrect comment in line 1, I should have written SL 1.
-
AuthorPosts
Find exclusive trading pro-tools on