Breakeven & Trailing Stop (fixed)
Forums › ProRealTime English forum › ProOrder support › Breakeven & Trailing Stop (fixed)
- This topic has 9 replies, 3 voices, and was last updated 5 years ago by Luce.
-
-
06/19/2019 at 9:36 AM #101015
Hi Paul – I wonder wether you can help here.
I’ve got this far, can you advise on the next step..?
I’ve coded the below as far as I can get but seem to be just missing one minor detail! I’m Still working on a way to combine a trailing STOP with a trailing STEP that becomes a fixed STOP – i.e Trade is a buy open with a 10pt stop, if it moves 2 up, your stop is now 8, if it moves another 2, it’s now 6 but when it gets to 10, so break even on a 1-1, it becomes fixed. Currently I only get a trailing to stop at breakeven, so still risking the full 10.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102// Conditions to enter long positionsDEFPARAM CumulateOrders = False // Cumulating positions deactivatedpositionsize=2// Prevents the system from creating new orders to enter the market or increase position size before the specified timenoEntryBeforeTime = 080000timeEnterBefore = time >= noEntryBeforeTime// Conditions to enter long positionsindicator1 = SuperTrend[2.1,21]c1 = (close CROSSES OVER indicator1)IF c1 AND timeEnterBefore THENBUY positionsize contract AT MARKETSET STOP pLOSS 13ENDIF// Conditions to exit long positionsindicator2 = SuperTrend[2.1,21]c2 = (close CROSSES UNDER indicator2)IF c2 THENSELL AT MARKETENDIF// Conditions to enter short positionsindicator3 = SuperTrend[2.1,21]c3 = (close CROSSES UNDER indicator3)IF c3 AND timeEnterBefore THENSELLSHORT positionsize contract AT MARKETSET STOP pLOSS 13ENDIF// Conditions to exit short positionsindicator4 = SuperTrend[2.1,21]c4 = (close CROSSES OVER indicator4)IF c4 THENEXITSHORT AT MARKETENDIF//************************************************************************//trailing stop functiontrailingstart = 10 //trailing will start @trailinstart points profittrailingstep = 0 //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 close-tradeprice(1)>=trailingstart*pipsize THENnewSL = tradeprice(1)+trailingstep*pipsizeENDIF//next movesIF newSL>0 AND close-newSL>=trailingstep*pipsize THENnewSL = newSL+trailingstep*pipsizeENDIFENDIF//manage short positionsIF SHORTONMARKET THEN//first move (breakeven)IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THENnewSL = tradeprice(1)-trailingstep*pipsizeENDIF//next movesIF newSL>0 AND newSL-close>=trailingstep*pipsize THENnewSL = newSL-trailingstep*pipsizeENDIFENDIF//stop order to exit the positionsIF newSL>0 THENSELL AT newSL STOPEXITSHORT AT newSL STOPENDIF//************************************************************************GRAPH newSL as “trailing”06/19/2019 at 9:54 AM #10101806/19/2019 at 9:58 AM #101021To write code, please use the <> “insert PRT code” button, to make code easier to read.
Thank you.
06/19/2019 at 9:59 AM #101022I created another topic since this one has nothing to do with the original post, it’s a completely different code.
Who’s the PAUL you are referring to?
06/19/2019 at 10:05 AM #101023As to what you asked, you may search the forum, but I think all code snippets/examples deal with breakeven first, not really a true trailing stop!
So what you want is to trail stop loss until breakeven, then leave it fixed till the next trade, is that correct?
06/19/2019 at 10:14 AM #101025Luce – you have posted an identical question here:
https://www.prorealcode.com/topic/hard-coded-stop-loss-becoming-trailing-stop-loss/#post-101013
One of the forum rules is that you do not double post as it leads to wasted effort and time for those trying to answer you.
Please refrain from double posting in future.
06/19/2019 at 10:27 AM #101027Hi Roberto
Yes basically – I have managed to code it so it trails to the 10 point target point which then becomes fixed, that part is now fine.
What I’m trying to achieve however is whilst it is reaching the trailing stop target (10) it steps up by 2 each time until it reaches the target. So, you enter the trade, you have a trailing stop for 10 but it triggers immediately by a step of 2 if it moves in your favour by 2, then this becomes 8. If it moves another 2 then the target is now 6. It it moves another 2 the target is now 4 etc until it hits 10, and then it ALL stops and the stop is now fixed.
Does that make sense..?
06/19/2019 at 10:52 AM #101029This is the trailing stop updated so that it doesn’t move once at breakeven, you’ll have to add a further value, InitialSL which must be the same one used throughout the code (13 in your case):
Trailing Stop till Breakeven123456789101112131415161718192021222324252627282930313233343536373839404142434445//************************************************************************// trailing stop function (fixed after breakeven)//reset the stoploss valueIF NOT ONMARKET THENtrailingstart = 6 //6 trailing will start @trailinstart points profittrailingstep = 2 //2 trailing step to move the "stoploss"initialSL = 13 //13 initial SL when a trade is enterednewSL = 0ENDIF//manage long positionsIF LONGONMARKET THENCurrentProfit = close - tradeprice(1)//first moveIF newSL = 0 AND CurrentProfit >= (trailingstart * pipsize) THENnewSL = tradeprice(1) + (trailingstep * pipsize) - (initialSL * pipsize)InitialSL = initialSL - (trailingstep * pipsize)ENDIF//next movesIF newSL > 0 AND (close - newSL) >= (trailingstep * pipsize) AND initialSL > 0 THENnewSL = newSL + min(trailingstep * pipsize,initialSL * pipsize)initialSL = max(0,initialSL - (trailingstep * pipsize))ENDIFENDIF//manage short positionsIF SHORTONMARKET THENCurrentProfit = tradeprice(1) - close//first moveIF newSL = 0 AND CurrentProfit >= (trailingstart * pipsize) THENnewSL = tradeprice(1) - (trailingstep * pipsize) + (initialSL * pipsize)InitialSL = initialSL - (trailingstep * pipsize)ENDIF//next movesIF newSL > 0 AND (newSL - close) >= (trailingstep * pipsize) AND initialSL > 0 THENnewSL = newSL - min(trailingstep * pipsize,initialSL * pipsize)initialSL = max(0,initialSL - (trailingstep * pipsize))ENDIFENDIF//stop order to exit the positionsIF newSL > 0 THENSELL AT newSL STOPEXITSHORT AT newSL STOPENDIFInitialSL, your SL, is used as a counter and decremented each TRAILINGSTEP pips till it reaches 0, then it stops working (NewSL, the updated SL price still operates till it, or the target price, is hit).
06/19/2019 at 10:54 AM #101030This your complete code:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485// Conditions to enter long positionsDEFPARAM CumulateOrders = False // Cumulating positions deactivatedpositionsize=2// Prevents the system from creating new orders to enter the market or increase position size before the specified timenoEntryBeforeTime = 080000timeEnterBefore = time >= noEntryBeforeTime// Conditions to enter long positionsindicator1 = SuperTrend[2.1,21]c1 = (close CROSSES OVER indicator1)IF c1 AND timeEnterBefore THENBUY positionsize contract AT MARKETSET STOP pLOSS 13ENDIF// Conditions to exit long positionsindicator2 = SuperTrend[2.1,21]c2 = (close CROSSES UNDER indicator2)IF c2 THENSELL AT MARKETENDIF// Conditions to enter short positionsindicator3 = SuperTrend[2.1,21]c3 = (close CROSSES UNDER indicator3)IF c3 AND timeEnterBefore THENSELLSHORT positionsize contract AT MARKETseT STOP pLOSS 13ENDIF// Conditions to exit short positionsindicator4 = SuperTrend[2.1,21]c4 = (close CROSSES OVER indicator4)IF c4 THENEXITSHORT AT MARKETENDIF//************************************************************************// trailing stop function (fixed after breakeven)//reset the stoploss valueIF NOT ONMARKET THENtrailingstart = 6 //6 trailing will start @trailinstart points profittrailingstep = 2 //2 trailing step to move the "stoploss"initialSL = 13 //13 initial SL when a trade is enterednewSL = 0ENDIF//manage long positionsIF LONGONMARKET THENCurrentProfit = close - tradeprice(1)//first moveIF newSL = 0 AND CurrentProfit >= (trailingstart * pipsize) THENnewSL = tradeprice(1) + (trailingstep * pipsize) - (initialSL * pipsize)InitialSL = initialSL - (trailingstep * pipsize)ENDIF//next movesIF newSL > 0 AND (close - newSL) >= (trailingstep * pipsize) AND initialSL > 0 THENnewSL = newSL + min(trailingstep * pipsize,initialSL * pipsize)initialSL = max(0,initialSL - (trailingstep * pipsize))ENDIFENDIF//manage short positionsIF SHORTONMARKET THENCurrentProfit = tradeprice(1) - close//first moveIF newSL = 0 AND CurrentProfit >= (trailingstart * pipsize) THENnewSL = tradeprice(1) - (trailingstep * pipsize) + (initialSL * pipsize)InitialSL = initialSL - (trailingstep * pipsize)ENDIF//next movesIF newSL > 0 AND (newSL - close) >= (trailingstep * pipsize) AND initialSL > 0 THENnewSL = newSL - min(trailingstep * pipsize,initialSL * pipsize)initialSL = max(0,initialSL - (trailingstep * pipsize))ENDIFENDIF//stop order to exit the positionsIF newSL > 0 THENSELL AT newSL STOPEXITSHORT AT newSL STOPENDIF//graph OnMarketgraph Tradeprice(1)graph CurrentProfitgraph trailingstartgraph trailingstepgraph initialSLgraph newSLI tested it a bit and it seems to be working.
Be warned that tight stops and targets may imply your orders being rejected or entered at market, due to the broker’s policy about minimum distance.
06/19/2019 at 4:25 PM #101062Wow HUGE thanks Roberto – That’s amazing skills – Hugely appreciated!!
1 user thanked author for this post.
-
AuthorPosts
Find exclusive trading pro-tools on