STOP LOSS / TRAILING STOP PROBLEM – GUARANTEED STOPLOSS
Forums › ProRealTime English forum › ProOrder support › STOP LOSS / TRAILING STOP PROBLEM – GUARANTEED STOPLOSS
- This topic has 16 replies, 2 voices, and was last updated 5 years ago by Nicolas.
-
-
07/04/2018 at 10:09 PM #75236
Hello everyone!
I hope you are well!
I have a problem that I am not able to solve and I don’t see where it is…
Here is my code (I combine some seen on this site and some of mine 🙂 )
I’ve set up a trailing stop for the long positions and another for the shorts but I’d like to get a classic SL (implemented in the code in order not to touch the guaranty SL and pay an extra 1 point 😉 )
But it did not recognize my SL with the capital…
Can you help me? 😀
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115//-------------------------------------------------------------------------// Code principal : DAX 5 Min//-------------------------------------------------------------------------// Trend Surfer DAX// code-ParameterDEFPARAM FlatAfter = 170000// DAX trading windowONCE BuyTimeMorning = 90000 //90000 DAX 15 min / 90000 5 minONCE SellTimeMorning = 121000 // 124000 DAX 15 min / 121000 5 minONCE BuyTimeAfternoon = 133000 //133000 DAX 15 min / 133000 5 minONCE SellTimeAfternoon = 160000 // 160000 DAX 15 min / 160000 5 min// traders dynamic indicatorONCE q = 14ONCE r = 4ONCE t = 29// tdi filter parameterONCE longPriceLevel = 35ONCE shortPriceLevel = 40ONCE middleBandLevel = 40// trading parameterONCE PositionSize = 1// position management during trading windowIF (Time >= BuyTimeMorning AND Time <= SellTimeMorning) OR (Time >= BuyTimeAfternoon AND Time <= SellTimeAfternoon) THEN// calculate TDI indicatorRSIasPrice = RSI[q](customclose)Priceline = Average[r](RSIasPrice)MiddleBand = Average[t]((RSIasPrice))// open position// longIF Not LONGONMARKET AND Priceline > MiddleBand AND Priceline > longPriceLevel AND MiddleBand > middleBandLevel THENBUY PositionSize CONTRACT AT MARKETENDIF// shortIF Not SHORTONMARKET AND Priceline CROSSES UNDER MiddleBand AND Priceline > shortPriceLevel THENSELLSHORT PositionSize CONTRACT AT MARKETENDIF// close positionIF Time = SellTimeMorning OR Time = SellTimeAfternoon THEN// longIF LONGONMARKET THENSELL AT MARKETENDIFIF SHORTONMARKET THENEXITSHORT AT MARKETENDIFENDIFENDIF//************************************************************************SL = 30 // Initial SL//TP = 30TSL = 1 // Use TSL?TrailingDistance1 = A // Distance from close to TSLTrailingStep1 = B // Pips locked at start of TSLTrailingDistance2 = C // Distance from close to TSLTrailingStep2 = D // Pips locked at start of TSL//************************************************************************IF TSL = 1 THEN//reset the stoploss valueIF NOT ONMARKET THENnewSL = 0CAND = 0ENDIF//manage long positionsIF LONGONMARKET THEN//first move (breakeven)IF newSL = 0 AND CLOSE - TRADEPRICE(1) >= TrailingDistance1*PipSize THENnewSL = TRADEPRICE(1) + TrailingStep1*PipSizeENDIF//next movesCAND = BarIndex - TradeIndexIF newSL > 0 AND CLOSE[1] >= HIGHEST[CAND](CLOSE) THENnewSL = CLOSE[1] - TrailingDistance1*PipSizeENDIFENDIF//manage short positionsIF SHORTONMARKET THEN//first move (breakeven)IF newSL = 0 AND TRADEPRICE(1) - CLOSE[1] >= TrailingDistance2*PipSize THENnewSL = TRADEPRICE(1) - TrailingStep2*PipSizeENDIF//next movesCAND = BarIndex - TradeIndexIF newSL > 0 AND CLOSE[1] <= LOWEST[CAND](CLOSE) THENnewSL = CLOSE[1] + TrailingDistance2*PipSizeENDIFENDIF//stop order to exit the positionsIF newSL > 0 THENSELL AT newSL STOPEXITSHORT AT newSL STOPENDIFSET STOP pLOSS SLENDIF//************************************************************************CAPITAL = 10000 //Your initial capitalIF POSITIONPERF < -0.002*(CAPITAL+STRATEGYPROFIT) THENEXITSHORT AT MARKETENDIFTHANKS!
07/05/2018 at 10:25 AM #75278You could try to remove the SET STOP pLOSS instruction and add this code before line 76:
12345678if newSL=0 thenif longonmarket thensell at tradeprice-SL*pointsizeendifif shortonmarket thenexitshort at tradeprice+SL*pointsizeendifendif07/05/2018 at 1:45 PM #75325Thanks for this answer! I slightly change my code and makes it clearer. Here it is:
Do you think I can have a trailing stop, the stop loss you wrote me AND another stop that would the garanty one which is compulsory by PRT in cfd limited accounts?
For now, if you have a look, I have 3 different stops: the trailing, the one you gave me and the the last lines whith the profit of the strategy. In fact I need one trailing stop (already in the code), a stop loss (wich would be above the “garanty” one to avoid 1 point extra spread) and the garanty one (so under my SL)
Do you know what do I have to change?
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139//************************************************************************// Horaire de tradingONCE BuyTimeMorning = 90000 //90000 DAX 15 min / 90000 5 minONCE SellTimeMorning = 121000 // 124000 DAX 15 min / 121000 5 minONCE BuyTimeAfternoon = 133000 //133000 DAX 15 min / 133000 5 minONCE SellTimeAfternoon = 160000 // 160000 DAX 15 min / 160000 5 min// Position management during trading windowIF (Time >= BuyTimeMorning AND Time <= SellTimeMorning) OR (Time >= BuyTimeAfternoon AND Time <= SellTimeAfternoon) THEN//************************************************************************//Début Strategy // DAX 5 Min//************************************************************************// Traders dynamic indicatorONCE q = 14 // Interval RSIONCE r = 4 //Average du RSIONCE t = 29 //2nd Average RSI// TDI filter parameterONCE longPriceLevel = 35ONCE shortPriceLevel = 40ONCE middleBandLevel = 40// Trading parameterONCE PositionSize = 1// Calculate TDI indicatorRSIasPrice = RSI[q](customclose)Priceline = Average[r](RSIasPrice)MiddleBand = Average[t]((RSIasPrice))//************************************************************************// Open position// LongIF Not LONGONMARKET AND Priceline > MiddleBand AND Priceline > longPriceLevel AND MiddleBand > middleBandLevel THENBUY PositionSize CONTRACT AT MARKETENDIF// shortIF Not SHORTONMARKET AND Priceline CROSSES UNDER MiddleBand AND Priceline > shortPriceLevel THENSELLSHORT PositionSize CONTRACT AT MARKETENDIF// Close position if time is upIF Time = SellTimeMorning OR Time = SellTimeAfternoon THEN// longIF LONGONMARKET THENSELL AT MARKETENDIFIF SHORTONMARKET THENEXITSHORT AT MARKETENDIFENDIFENDIF//************************************************************************//End Strategy//************************************************************************SL = 30 // Initial SL//TP = 30 //TP (non utilisé pour le moment)TSL = 1 // Use TSL?TrailingDistance1 = A // Distance from close to unlock TSL1TrailingStep1 = B // Pips locked at start of TSL1TrailingDistance2 = C // Distance from close to unlock TSL2TrailingStep2 = D // Pips locked at start of TSL2//************************************************************************IF TSL = 1 THEN//reset the stoploss valueIF NOT ONMARKET THENnewSL = 0CAND = 0ENDIF//************************************************************************//Set up le Stop Loss//Long Positionsif newSL=0 thenif longonmarket thensell at tradeprice-(SL*pointsize)endif//Short Positionsif shortonmarket thenexitshort at tradeprice+(SL*pointsize)endifendif//************************************************************************//Manage Trailing Stops long positionsIF LONGONMARKET THEN//first move (breakeven)IF newSL = 0 AND CLOSE - TRADEPRICE(1) >= TrailingDistance1*PipSize THENnewSL = TRADEPRICE(1) + TrailingStep1*PipSizeENDIF//next movesCAND = BarIndex - TradeIndexIF newSL > 0 AND CLOSE[1] >= HIGHEST[CAND](CLOSE) THENnewSL = CLOSE[1] - TrailingDistance1*PipSizeENDIFENDIF//Manage Trailing Stops short positionsIF SHORTONMARKET THEN//first move (breakeven)IF newSL = 0 AND TRADEPRICE(1) - CLOSE[1] >= TrailingDistance2*PipSize THENnewSL = TRADEPRICE(1) - TrailingStep2*PipSizeENDIF//next movesCAND = BarIndex - TradeIndexIF newSL > 0 AND CLOSE[1] <= LOWEST[CAND](CLOSE) THENnewSL = CLOSE[1] + TrailingDistance2*PipSizeENDIFENDIF//stop order to exit the positionsIF newSL <> 0 THEN //I put difference, maybe keep “>”?SELL AT newSL STOPEXITSHORT AT newSL STOPENDIFENDIF//************************************************************************CAPITAL = 10000 //Your initial capitalIF POSITIONPERF < -0.002*(CAPITAL+STRATEGYPROFIT) THENEXITSHORT AT MARKET //Nécessaire ?ENDIFTHANKS!!!
07/05/2018 at 2:39 PM #75343Add a line with the SET STOP PLOSS instruction again, but with a price spread (3 points here) between the previous code I gave you and this new one:
1set stop ploss sl+3I moved the topic in the English section! wrong place here…
07/05/2018 at 2:44 PM #75346Ok, thanks! You’re right, it should be in this section 🙂
Is it like this (the insertion of your code)?
And so, are the last lines taked into account as well?
In this code I now have, A trailing Stop, A stop that I define myself, A stop loss for the guaranty one and one with my losse right?
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141//************************************************************************// Horaire de tradingONCE BuyTimeMorning = 90000 //90000 DAX 15 min / 90000 5 minONCE SellTimeMorning = 121000 // 124000 DAX 15 min / 121000 5 minONCE BuyTimeAfternoon = 133000 //133000 DAX 15 min / 133000 5 minONCE SellTimeAfternoon = 160000 // 160000 DAX 15 min / 160000 5 min// Position management during trading windowIF (Time >= BuyTimeMorning AND Time <= SellTimeMorning) OR (Time >= BuyTimeAfternoon AND Time <= SellTimeAfternoon) THEN//************************************************************************//Début Strategy // DAX 5 Min//************************************************************************// Traders dynamic indicatorONCE q = 14 // Interval RSIONCE r = 4 //Average du RSIONCE t = 29 //2nd Average RSI// TDI filter parameterONCE longPriceLevel = 35ONCE shortPriceLevel = 40ONCE middleBandLevel = 40// Trading parameterONCE PositionSize = 1// Calculate TDI indicatorRSIasPrice = RSI[q](customclose)Priceline = Average[r](RSIasPrice)MiddleBand = Average[t]((RSIasPrice))//************************************************************************// Open position// LongIF Not LONGONMARKET AND Priceline > MiddleBand AND Priceline > longPriceLevel AND MiddleBand > middleBandLevel THENBUY PositionSize CONTRACT AT MARKETENDIF// shortIF Not SHORTONMARKET AND Priceline CROSSES UNDER MiddleBand AND Priceline > shortPriceLevel THENSELLSHORT PositionSize CONTRACT AT MARKETENDIF// Close position if time is upIF Time = SellTimeMorning OR Time = SellTimeAfternoon THEN// longIF LONGONMARKET THENSELL AT MARKETENDIFIF SHORTONMARKET THENEXITSHORT AT MARKETENDIFENDIFENDIF//************************************************************************//End Strategy//************************************************************************SL = 30 // Initial SL//TP = 30 //TP (non utilisé pour le moment)TSL = 1 // Use TSL?TrailingDistance1 = A // Distance from close to unlock TSL1TrailingStep1 = B // Pips locked at start of TSL1TrailingDistance2 = C // Distance from close to unlock TSL2TrailingStep2 = D // Pips locked at start of TSL2//************************************************************************IF TSL = 1 THEN//reset the stoploss valueIF NOT ONMARKET THENnewSL = 0CAND = 0ENDIF//************************************************************************set stop ploss sl+3//Set up le Stop Loss//Long Positionsif newSL=0 thenif longonmarket thensell at tradeprice-(SL*pointsize)endif//Short Positionsif shortonmarket thenexitshort at tradeprice+(SL*pointsize)endifendif//************************************************************************//Manage Trailing Stops long positionsIF LONGONMARKET THEN//first move (breakeven)IF newSL = 0 AND CLOSE - TRADEPRICE(1) >= TrailingDistance1*PipSize THENnewSL = TRADEPRICE(1) + TrailingStep1*PipSizeENDIF//next movesCAND = BarIndex - TradeIndexIF newSL > 0 AND CLOSE[1] >= HIGHEST[CAND](CLOSE) THENnewSL = CLOSE[1] - TrailingDistance1*PipSizeENDIFENDIF//Manage Trailing Stops short positionsIF SHORTONMARKET THEN//first move (breakeven)IF newSL = 0 AND TRADEPRICE(1) - CLOSE[1] >= TrailingDistance2*PipSize THENnewSL = TRADEPRICE(1) - TrailingStep2*PipSizeENDIF//next movesCAND = BarIndex - TradeIndexIF newSL > 0 AND CLOSE[1] <= LOWEST[CAND](CLOSE) THENnewSL = CLOSE[1] + TrailingDistance2*PipSizeENDIFENDIF//stop order to exit the positionsIF newSL <> 0 THEN //I put difference, maybe keep “>”?SELL AT newSL STOPEXITSHORT AT newSL STOPENDIFENDIF//************************************************************************CAPITAL = 10000 //Your initial capitalIF POSITIONPERF < -0.002*(CAPITAL+STRATEGYPROFIT) THENEXITSHORT AT MARKET //Nécessaire ?ENDIFThank you one more time for your reactivity Nicolas!
07/05/2018 at 2:48 PM #75347I just recalled of a discussion we had on forum about guaranteed stop: https://www.prorealcode.com/topic/guaranteed-stops-2/
The conclusion is that even exiting position with a pending order like the code I gave will activate the guaranteed stop fee, since it is an order made to act like a stoploss ( understand a contrarian order made to exit the order at market ). So there is no solution to avoid your broker taking fee to protect your order, and that’s fair, IMO.
07/05/2018 at 3:08 PM #75357I agree Nicolas, I agree that we have to get a g SL. But I think that we could get our SL above of this one and get the g SL to prevent our SL in case it is defective, not all the time. For example, if you have a strategy that produce 100 orders a day on the DAX 5EUR/point and 50% of them are profitable it means that you already have 5*(100*0.5)=250€…
That’s didn’t wanted to avoid the g SL, just getting another one above
Is it possible? The trailing stop does not count as a g SL isn’t it?
Thanks
07/05/2018 at 4:29 PM #75369Following the discussions of this thread: https://www.prorealcode.com/topic/guaranteed-stops-2/page/2/#post-65735 , you will understand that because any order at market must have first an attached stoploss and that this one is automatically been “guaranteed”, any other pending stop order set later after the order will be automatically transformed into the new “guaranteed stop”.
07/05/2018 at 5:01 PM #7537307/05/2018 at 5:09 PM #7537907/05/2018 at 5:31 PM #7538407/06/2018 at 10:36 AM #75443I do confirm that the nearest stop order to close an opened market position WILL BE AUTOMATICALLY considered as the guaranteed stop. There is no way to avoid your order to be triggered by a guaranteed stoploss with a limited risk account.
07/06/2018 at 10:37 AM #7544407/06/2018 at 11:14 AM #7544707/06/2018 at 12:29 PM #75467 -
AuthorPosts
Find exclusive trading pro-tools on