Trailing stop in Backtesting
Forums › ProRealTime English forum › ProOrder support › Trailing stop in Backtesting
- This topic has 17 replies, 3 voices, and was last updated 8 years ago by klaus.
-
-
05/06/2016 at 12:52 PM #6517
Dear all,
I wrote a system which behaves completely different in Backtesting and Proorder.
I think it’s due to the fact of using trailing stop.
Does somebody know when a trailing stop is executed in backtesting?
to my knowledge in ProOrder it is executed when the situation occurs. e.g. Program runs in 15 min timeframe and trailing stop is reached within the 15 minutes then it’s immediately executed.
thanks for your help
Klaus
05/08/2016 at 12:32 PM #6571Hello Klaus,
This difference occur because conditions are only tested once per bar. In the backtest, trailing stop move stoploss to a price that will be tested only one time each time a new bar will open. In real time, your stoploss will be executed instantly by the broker because your SL is written in his order book.
In a near future, backtest will be improved to be more accurate and better reflect real time trading.
05/09/2016 at 7:11 AM #6593Hi Nicolas,
thanks for the explanation, i just tested again. What I see is, that the close of an order is done random since it’s not always the price at the close of a candle. Just want to find out under which conditions a close is done. In the attached example the trailing stop was 10 points.
05/09/2016 at 8:54 AM #659805/09/2016 at 9:20 AM #6606Thanks Nicolas,
I understood, the ticks are not known. If the trailing stop is reached at the end of the candle, the order will be closed. What I would like to know, at what price the order is closed. Is it the closing price of the candle, or something calculated based on various factors?
By the way, I am new in this forum and have to say it’s very useful. Especially your assistance and knowledge
05/09/2016 at 11:07 AM #6608The order is closed at the price level the trailing stop has moved your stoploss to. But if you are trading (for example) the 1 hour timeframe, your stoploss may has occurred between the beginning of the candle and its end. Since there is no way actually to know at what time it occurred, the backtest only test if the stoploss price was between the high and low of the candlestick and close your trade at the next open bar (in backtest only).
05/10/2016 at 3:17 PM #6694short info about progress:
I modified the program. removed trailing stop and instead check at next open bar if trailing level is reached and closed order. But the result of this version is different to the trailing stop version. Unfortunately. Maybe my mistake have to check code again.
05/10/2016 at 4:43 PM #670205/10/2016 at 4:59 PM #670405/11/2016 at 7:18 AM #6748Here it is Nicolas,
the program run on DAX in 15 minute timeframe
this is with trailing stop
1234567891011121314151617181920212223242526272829303132333435363738394041424344ordersize=1i1=3i2=6i3=9ADXLimit=30//-----------------------------------------------ema = ExponentialAverage[22](Close)ema2 = ExponentialAverage[5](Close)Previousema = ExponentialAverage[22][1]//-----------------------------------------------Indicator1 = MACDline[i1,i2,i3](close)Indicator2 = ExponentialAverage[10](MACDline[i1,i2,i3](close))PreviousIndicator1 = MACDline[i1,i2,i3][1]PreviousIndicator2 = ExponentialAverage[9](MACDline[i1,i2,i3][1])c2=indicator1-indicator2Previousc2=PreviousIndicator1-PreviousIndicator2//-----------------------------------------------myADX=ADX[5]//-----------------------------------------------if hour>7 and hour <18 thenif myADX>ADXLimit thenif ema2<close or Previousc2>0 and c2<0 and Previousema>ema thenif LongOnMarket thensell at MarketendifSELLSHORT ordersize CONTRACTS AT MARKETendifif ema2>close or Previousc2<0 and c2>0 and Previousema<ema thenif ShortOnMarket thenExitShort at MarketendifBUY ordersize CONTRACTS AT MARKETendifendifENDIF//----Close order in case .....if LongOnMarket and c2<0 thensell at Marketendifif ShortOnMarket and c2>0 thenExitShort at Marketendifset stop trailing 9Set Target Profit 50and this one with the simulation
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849ordersize=1i1=3i2=6i3=9ADXLimit=30//-----------------------------------------------ema = ExponentialAverage[22](Close)ema2 = ExponentialAverage[5](Close)Previousema = ExponentialAverage[22][1]//-----------------------------------------------Indicator1 = MACDline[i1,i2,i3](close)Indicator2 = ExponentialAverage[10](MACDline[i1,i2,i3](close))PreviousIndicator1 = MACDline[i1,i2,i3][1]PreviousIndicator2 = ExponentialAverage[9](MACDline[i1,i2,i3][1])c2=indicator1-indicator2Previousc2=PreviousIndicator1-PreviousIndicator2//-----------------------------------------------myADX=ADX[5]//---- try to simulate trailing stop of 9 pointsif LongOnMarket and close<tradeprice-9 thensell at MarketendifIf ShortOnMarket and close>tradeprice+9 thenExitShort at Marketendif//-----------------------------------------------if hour>7 and hour <18 thenif myADX>ADXLimit thenif ema2<close or Previousc2>0 and c2<0 and Previousema>ema thenif LongOnMarket thensell at MarketendifSELLSHORT ordersize CONTRACTS AT MARKETendifif ema2>close or Previousc2<0 and c2>0 and Previousema<ema thenif ShortOnMarket thenExitShort at MarketendifBUY ordersize CONTRACTS AT MARKETendifendifENDIF//----Close order in case .....if LongOnMarket and c2<0 thensell at Marketendifif ShortOnMarket and c2>0 thenExitShort at Marketendif05/11/2016 at 9:01 AM #6767Thanks for the code. What you have written between the lines 19 to 26 is a basic stoploss test at 9 points from the order open price. There is no ‘trailing’ here 😉
I’ll post a complete trailing stop code later in this thread.
05/11/2016 at 9:45 AM #677705/11/2016 at 10:14 AM #6782I have just written an article about trailing stop implementation into a strategy, please find it here :
http://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/
If you need assistance, just ask! 🙂
05/12/2016 at 7:45 AM #6855Nicolas,
I tested your manual trailing stop. Results are quiet different from real trailing stop.
Just read your reply on “Backtest vs Real trading limits and stops”. This explains why we never will have same results
ok. Lets wait for another version of prt.
anyhow, thanks for your help
cu Klaus
07/17/2016 at 4:43 PM #10521Hi Klaus,
How different were your results? In my case it made relatively profitable system to be a significant loss system. Both, i believe, based on the end of bar information and real performance is a big question.
-
AuthorPosts
Find exclusive trading pro-tools on