ProRealCode - Trading & Coding with ProRealTime™
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? 😀
//-------------------------------------------------------------------------
// Code principal : DAX 5 Min
//-------------------------------------------------------------------------
// Trend Surfer DAX
// code-Parameter
DEFPARAM FlatAfter = 170000
// DAX trading window
ONCE BuyTimeMorning = 90000 //90000 DAX 15 min / 90000 5 min
ONCE SellTimeMorning = 121000 // 124000 DAX 15 min / 121000 5 min
ONCE BuyTimeAfternoon = 133000 //133000 DAX 15 min / 133000 5 min
ONCE SellTimeAfternoon = 160000 // 160000 DAX 15 min / 160000 5 min
// traders dynamic indicator
ONCE q = 14
ONCE r = 4
ONCE t = 29
// tdi filter parameter
ONCE longPriceLevel = 35
ONCE shortPriceLevel = 40
ONCE middleBandLevel = 40
// trading parameter
ONCE PositionSize = 1
// position management during trading window
IF (Time >= BuyTimeMorning AND Time <= SellTimeMorning) OR (Time >= BuyTimeAfternoon AND Time <= SellTimeAfternoon) THEN
// calculate TDI indicator
RSIasPrice = RSI[q](customclose)
Priceline = Average[r](RSIasPrice)
MiddleBand = Average[t]((RSIasPrice))
// open position
// long
IF Not LONGONMARKET AND Priceline > MiddleBand AND Priceline > longPriceLevel AND MiddleBand > middleBandLevel THEN
BUY PositionSize CONTRACT AT MARKET
ENDIF
// short
IF Not SHORTONMARKET AND Priceline CROSSES UNDER MiddleBand AND Priceline > shortPriceLevel THEN
SELLSHORT PositionSize CONTRACT AT MARKET
ENDIF
// close position
IF Time = SellTimeMorning OR Time = SellTimeAfternoon THEN
// long
IF LONGONMARKET THEN
SELL AT MARKET
ENDIF
IF SHORTONMARKET THEN
EXITSHORT AT MARKET
ENDIF
ENDIF
ENDIF
//************************************************************************
SL = 30 // Initial SL
//TP = 30
TSL = 1 // Use TSL?
TrailingDistance1 = A // Distance from close to TSL
TrailingStep1 = B // Pips locked at start of TSL
TrailingDistance2 = C // Distance from close to TSL
TrailingStep2 = D // Pips locked at start of TSL
//************************************************************************
IF TSL = 1 THEN
//reset the stoploss value
IF NOT ONMARKET THEN
newSL = 0
CAND = 0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL = 0 AND CLOSE - TRADEPRICE(1) >= TrailingDistance1*PipSize THEN
newSL = TRADEPRICE(1) + TrailingStep1*PipSize
ENDIF
//next moves
CAND = BarIndex - TradeIndex
IF newSL > 0 AND CLOSE[1] >= HIGHEST[CAND](CLOSE) THEN
newSL = CLOSE[1] - TrailingDistance1*PipSize
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL = 0 AND TRADEPRICE(1) - CLOSE[1] >= TrailingDistance2*PipSize THEN
newSL = TRADEPRICE(1) - TrailingStep2*PipSize
ENDIF
//next moves
CAND = BarIndex - TradeIndex
IF newSL > 0 AND CLOSE[1] <= LOWEST[CAND](CLOSE) THEN
newSL = CLOSE[1] + TrailingDistance2*PipSize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL > 0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
SET STOP pLOSS SL
ENDIF
//************************************************************************
CAPITAL = 10000 //Your initial capital
IF POSITIONPERF < -0.002*(CAPITAL+STRATEGYPROFIT) THEN
EXITSHORT AT MARKET
ENDIF
THANKS!
You could try to remove the SET STOP pLOSS instruction and add this code before line 76:
if newSL=0 then
if longonmarket then
sell at tradeprice-SL*pointsize
endif
if shortonmarket then
exitshort at tradeprice+SL*pointsize
endif
endif
Thanks 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?
//************************************************************************
// Horaire de trading
ONCE BuyTimeMorning = 90000 //90000 DAX 15 min / 90000 5 min
ONCE SellTimeMorning = 121000 // 124000 DAX 15 min / 121000 5 min
ONCE BuyTimeAfternoon = 133000 //133000 DAX 15 min / 133000 5 min
ONCE SellTimeAfternoon = 160000 // 160000 DAX 15 min / 160000 5 min
// Position management during trading window
IF (Time >= BuyTimeMorning AND Time <= SellTimeMorning) OR (Time >= BuyTimeAfternoon AND Time <= SellTimeAfternoon) THEN
//************************************************************************
//Début Strategy // DAX 5 Min
//************************************************************************
// Traders dynamic indicator
ONCE q = 14 // Interval RSI
ONCE r = 4 //Average du RSI
ONCE t = 29 //2nd Average RSI
// TDI filter parameter
ONCE longPriceLevel = 35
ONCE shortPriceLevel = 40
ONCE middleBandLevel = 40
// Trading parameter
ONCE PositionSize = 1
// Calculate TDI indicator
RSIasPrice = RSI[q](customclose)
Priceline = Average[r](RSIasPrice)
MiddleBand = Average[t]((RSIasPrice))
//************************************************************************
// Open position
// Long
IF Not LONGONMARKET AND Priceline > MiddleBand AND Priceline > longPriceLevel AND MiddleBand > middleBandLevel THEN
BUY PositionSize CONTRACT AT MARKET
ENDIF
// short
IF Not SHORTONMARKET AND Priceline CROSSES UNDER MiddleBand AND Priceline > shortPriceLevel THEN
SELLSHORT PositionSize CONTRACT AT MARKET
ENDIF
// Close position if time is up
IF Time = SellTimeMorning OR Time = SellTimeAfternoon THEN
// long
IF LONGONMARKET THEN
SELL AT MARKET
ENDIF
IF SHORTONMARKET THEN
EXITSHORT AT MARKET
ENDIF
ENDIF
ENDIF
//************************************************************************
//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 TSL1
TrailingStep1 = B // Pips locked at start of TSL1
TrailingDistance2 = C // Distance from close to unlock TSL2
TrailingStep2 = D // Pips locked at start of TSL2
//************************************************************************
IF TSL = 1 THEN
//reset the stoploss value
IF NOT ONMARKET THEN
newSL = 0
CAND = 0
ENDIF
//************************************************************************
//Set up le Stop Loss
//Long Positions
if newSL=0 then
if longonmarket then
sell at tradeprice-(SL*pointsize)
endif
//Short Positions
if shortonmarket then
exitshort at tradeprice+(SL*pointsize)
endif
endif
//************************************************************************
//Manage Trailing Stops long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL = 0 AND CLOSE - TRADEPRICE(1) >= TrailingDistance1*PipSize THEN
newSL = TRADEPRICE(1) + TrailingStep1*PipSize
ENDIF
//next moves
CAND = BarIndex - TradeIndex
IF newSL > 0 AND CLOSE[1] >= HIGHEST[CAND](CLOSE) THEN
newSL = CLOSE[1] - TrailingDistance1*PipSize
ENDIF
ENDIF
//Manage Trailing Stops short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL = 0 AND TRADEPRICE(1) - CLOSE[1] >= TrailingDistance2*PipSize THEN
newSL = TRADEPRICE(1) - TrailingStep2*PipSize
ENDIF
//next moves
CAND = BarIndex - TradeIndex
IF newSL > 0 AND CLOSE[1] <= LOWEST[CAND](CLOSE) THEN
newSL = CLOSE[1] + TrailingDistance2*PipSize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL <> 0 THEN //I put difference, maybe keep “>”?
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
ENDIF
//************************************************************************
CAPITAL = 10000 //Your initial capital
IF POSITIONPERF < -0.002*(CAPITAL+STRATEGYPROFIT) THEN
EXITSHORT AT MARKET //Nécessaire ?
ENDIF
THANKS!!!
Add 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:
set stop ploss sl+3
I moved the topic in the English section! wrong place here…
Ok, 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?
//************************************************************************
// Horaire de trading
ONCE BuyTimeMorning = 90000 //90000 DAX 15 min / 90000 5 min
ONCE SellTimeMorning = 121000 // 124000 DAX 15 min / 121000 5 min
ONCE BuyTimeAfternoon = 133000 //133000 DAX 15 min / 133000 5 min
ONCE SellTimeAfternoon = 160000 // 160000 DAX 15 min / 160000 5 min
// Position management during trading window
IF (Time >= BuyTimeMorning AND Time <= SellTimeMorning) OR (Time >= BuyTimeAfternoon AND Time <= SellTimeAfternoon) THEN
//************************************************************************
//Début Strategy // DAX 5 Min
//************************************************************************
// Traders dynamic indicator
ONCE q = 14 // Interval RSI
ONCE r = 4 //Average du RSI
ONCE t = 29 //2nd Average RSI
// TDI filter parameter
ONCE longPriceLevel = 35
ONCE shortPriceLevel = 40
ONCE middleBandLevel = 40
// Trading parameter
ONCE PositionSize = 1
// Calculate TDI indicator
RSIasPrice = RSI[q](customclose)
Priceline = Average[r](RSIasPrice)
MiddleBand = Average[t]((RSIasPrice))
//************************************************************************
// Open position
// Long
IF Not LONGONMARKET AND Priceline > MiddleBand AND Priceline > longPriceLevel AND MiddleBand > middleBandLevel THEN
BUY PositionSize CONTRACT AT MARKET
ENDIF
// short
IF Not SHORTONMARKET AND Priceline CROSSES UNDER MiddleBand AND Priceline > shortPriceLevel THEN
SELLSHORT PositionSize CONTRACT AT MARKET
ENDIF
// Close position if time is up
IF Time = SellTimeMorning OR Time = SellTimeAfternoon THEN
// long
IF LONGONMARKET THEN
SELL AT MARKET
ENDIF
IF SHORTONMARKET THEN
EXITSHORT AT MARKET
ENDIF
ENDIF
ENDIF
//************************************************************************
//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 TSL1
TrailingStep1 = B // Pips locked at start of TSL1
TrailingDistance2 = C // Distance from close to unlock TSL2
TrailingStep2 = D // Pips locked at start of TSL2
//************************************************************************
IF TSL = 1 THEN
//reset the stoploss value
IF NOT ONMARKET THEN
newSL = 0
CAND = 0
ENDIF
//************************************************************************
set stop ploss sl+3
//Set up le Stop Loss
//Long Positions
if newSL=0 then
if longonmarket then
sell at tradeprice-(SL*pointsize)
endif
//Short Positions
if shortonmarket then
exitshort at tradeprice+(SL*pointsize)
endif
endif
//************************************************************************
//Manage Trailing Stops long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL = 0 AND CLOSE - TRADEPRICE(1) >= TrailingDistance1*PipSize THEN
newSL = TRADEPRICE(1) + TrailingStep1*PipSize
ENDIF
//next moves
CAND = BarIndex - TradeIndex
IF newSL > 0 AND CLOSE[1] >= HIGHEST[CAND](CLOSE) THEN
newSL = CLOSE[1] - TrailingDistance1*PipSize
ENDIF
ENDIF
//Manage Trailing Stops short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL = 0 AND TRADEPRICE(1) - CLOSE[1] >= TrailingDistance2*PipSize THEN
newSL = TRADEPRICE(1) - TrailingStep2*PipSize
ENDIF
//next moves
CAND = BarIndex - TradeIndex
IF newSL > 0 AND CLOSE[1] <= LOWEST[CAND](CLOSE) THEN
newSL = CLOSE[1] + TrailingDistance2*PipSize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL <> 0 THEN //I put difference, maybe keep “>”?
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
ENDIF
//************************************************************************
CAPITAL = 10000 //Your initial capital
IF POSITIONPERF < -0.002*(CAPITAL+STRATEGYPROFIT) THEN
EXITSHORT AT MARKET //Nécessaire ?
ENDIF
Thank you one more time for your reactivity Nicolas!
I 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.
I 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
Following 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”.
Ok, now I am lost…
Question very simple: The trailing stop implemented in my code would be considered as a G SL or not?
Thanks
Yes, I think so, as per the tests made. But I’d like to confirm this assumption with a phone call by tomorrow.
Oh, great thanks! You’ll tell me tomorrow?
I 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.
Ok, thank you very much Nicolas for this clear answer!
I just got one question, is it possible then, to include this G SL in the backtest?
If you are meaning include the fee into backtests, just expand the spread with the markup your broker ask you to pay the guaranteed stop.
STOP LOSS / TRAILING STOP PROBLEM – GUARANTEED STOPLOSS
This topic contains 16 replies,
has 2 voices, and was last updated by
Nicolas
7 years, 8 months ago.
| Forum: | ProOrder: Automated Strategies & Backtesting |
| Language: | English |
| Started: | 07/04/2018 |
| Status: | Active |
| Attachments: | No files |
The information collected on this form is stored in a computer file by ProRealCode to create and access your ProRealCode profile. This data is kept in a secure database for the duration of the member's membership. They will be kept as long as you use our services and will be automatically deleted after 3 years of inactivity. Your personal data is used to create your private profile on ProRealCode. This data is maintained by SAS ProRealCode, 407 rue Freycinet, 59151 Arleux, France. If you subscribe to our newsletters, your email address is provided to our service provider "MailChimp" located in the United States, with whom we have signed a confidentiality agreement. This company is also compliant with the EU/Swiss Privacy Shield, and the GDPR. For any request for correction or deletion concerning your data, you can directly contact the ProRealCode team by email at privacy@prorealcode.com If you would like to lodge a complaint regarding the use of your personal data, you can contact your data protection supervisory authority.