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
Hello 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.
Hi 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.
It’s because trailing are made on the same candle that the trade opened and Probacktest has no clue of how the ticks went on this candle in real time.
Thanks 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
The 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).
short 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.
If you can post your code here, I could have a look about what’s going wrong.
Hi Nicolas,
thanks a lot for your help. I’ll return home tomorrow. then will post ist.
cu Klaus
Here it is Nicolas,
the program run on DAX in 15 minute timeframe
this is with trailing stop
ordersize=1
i1=3
i2=6
i3=9
ADXLimit=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-indicator2
Previousc2=PreviousIndicator1-PreviousIndicator2
//-----------------------------------------------
myADX=ADX[5]
//-----------------------------------------------
if hour>7 and hour <18 then
if myADX>ADXLimit then
if ema2<close or Previousc2>0 and c2<0 and Previousema>ema then
if LongOnMarket then
sell at Market
endif
SELLSHORT ordersize CONTRACTS AT MARKET
endif
if ema2>close or Previousc2<0 and c2>0 and Previousema<ema then
if ShortOnMarket then
ExitShort at Market
endif
BUY ordersize CONTRACTS AT MARKET
endif
endif
ENDIF
//----Close order in case .....
if LongOnMarket and c2<0 then
sell at Market
endif
if ShortOnMarket and c2>0 then
ExitShort at Market
endif
set stop trailing 9
Set Target Profit 50
and this one with the simulation
ordersize=1
i1=3
i2=6
i3=9
ADXLimit=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-indicator2
Previousc2=PreviousIndicator1-PreviousIndicator2
//-----------------------------------------------
myADX=ADX[5]
//---- try to simulate trailing stop of 9 points
if LongOnMarket and close<tradeprice-9 then
sell at Market
endif
If ShortOnMarket and close>tradeprice+9 then
ExitShort at Market
endif
//-----------------------------------------------
if hour>7 and hour <18 then
if myADX>ADXLimit then
if ema2<close or Previousc2>0 and c2<0 and Previousema>ema then
if LongOnMarket then
sell at Market
endif
SELLSHORT ordersize CONTRACTS AT MARKET
endif
if ema2>close or Previousc2<0 and c2>0 and Previousema<ema then
if ShortOnMarket then
ExitShort at Market
endif
BUY ordersize CONTRACTS AT MARKET
endif
endif
ENDIF
//----Close order in case .....
if LongOnMarket and c2<0 then
sell at Market
endif
if ShortOnMarket and c2>0 then
ExitShort at Market
endif
Thanks 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.
yes, the idea was to simulate what backtest is doing, as you described in one of the previous posts.
nice try. 🙂
I 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! 🙂
Nicolas,
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
Hi 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.