[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [amibroker] Re: Dynamic trailing stop - ploting vs backtesting giving different values


  • To: amibroker@xxxxxxxxxxxxxxx
  • Subject: Re: [amibroker] Re: Dynamic trailing stop - ploting vs backtesting giving different values
  • From: Flávio Veloso <flavso@xxxxxxxxx>
  • Date: Tue, 15 Jul 2008 23:48:02 -0300

PureBytes Links

Trading Reference Links

Hi Tomasz.

Thanks for replying. I already read that article. I applied the ideas 
presented there but unfortunately they did not solve the issue.

Problem is: the example provided in the article shows how a 
stopModePercent trailing stop can be ploted. Just to be more clear: the 
issue is that it's not clear how one know the *values* to set the stops 
at (or plot in the charts) when using a *variable stopModePoints 
trailing stop*.

I updated my sample AFL with to use the technique presented in that 
article. Here's the updated system:

------------snip-------------
SetOption("InitialEquity", 100000);

BuyPrice = SellPrice = Open;
SetTradeDelays(1, 1, 1, 1);

Buy = Cross(Signal(), MACD());
Sell = Cross(MA(C, 200), C);

TS = 4 * ATR(20);
ApplyStop(stopTypeTrailing, stopModePoint, TS, 1, TRUE);

Equity( 1, 0 ); // evaluate stops, all quotes

InTrade = Flip( Buy, Sell );

SetOption("EveryBarNullCheck", True );
stopline = IIf( InTrade, HighestSince( Buy, High ) - TS, Null );

PlotShapes(Buy*shapeUpArrow,colorGreen,0,Low);
PlotShapes(Sell*shapeDownArrow,colorRed,0,High);

#include "Formulas/Basic Charts/Price.afl"
Plot( stopline, "trailing stop line", colorRed, styleDots | styleNoLine);
------------snip-------------

It seems that the problem is in this statement:

   stopline = IIf( InTrade, HighestSince( Buy, High ) - TS, Null );

When using a stopModePercent trailing stop, calculating stopline is 
obvious: just multiply the highest high value by the stop percent factor 
and you're done. That's what the code in the article does.

However, when dealing with dynamic stopModePoints trailing stops, the 
math is a bit different: the stop value should be *subtracted* from the 
highest high. Well, at least that's what I thought how it should be done 
until I tried to plot it and saw different numbers.

Try it for yourself backtesting the DJI using the example above, which 
is basically the code in the article updated to use a pretty basic 4 x 
ATR(20) chandelier trailing stop.

If I did cut'n'paste correctly, you will get a trade started in 
2004-09-21 @ 10204.5 that is stopped out on 2005-03-29 @ 10373.5 in the 
backtester. However, the "trailing stop line" plot never reached that 
exit value. Actually, if the ts-line was used to trail the stop, the 
trade would be stopped much earlier in 2004-10-24 @ 9732.29, which is 
almost five months before!

NB: you may want to change the stopline formula to the following:

   stopline = IIf( InTrade, HighestSince( Buy, High - TS ), Null );

I think that the form above is much more appropriate for a dynamic 
trailing stop (TS changes on every bar). It also makes the plot much 
more easy to follow as the line always go up (note: this changes does 
not alter backtester results).

So the question is: what math one should use to match the values used by 
ApplyStop(stopTypeTrailing, stopModePoints, ARRAY, 1, TRUE) in AB? Is it 
possible? Or maybe that's not how dynamic ApplyStop() works?

Thanks in advance.

Tomasz Janeczko escreveu:
> 
> 
> Knowledge Base:
> 
> http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/ 
> <http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/>
> 
> Best regards,
> Tomasz Janeczko
> amibroker.com
> ----- Original Message -----
> From: "Flávio Veloso" <flavso@xxxxxxxxx <mailto:flavso%40gmail.com>>
> To: <amibroker@xxxxxxxxxxxxxxx <mailto:amibroker%40yahoogroups.com>>
> Sent: Monday, July 14, 2008 3:17 PM
> Subject: [amibroker] Re: Dynamic trailing stop - ploting vs backtesting 
> giving different values
> 
>  > Hi all.
>  >
>  > Anybody cared to test this???
>  >
>  > Is anyone trailing stops manually on the broker
>  > matchingApplyStop(stopModeTrailing...) behavior?
>  >
>  > If so, how are you doing it? I.e. how do you know the exact value to set
>  > your stops at?
>  >
>  > Flávio Veloso escreveu:
>  >> Hi all.
>  >>
>  >> I'm trying to plot the trailing stop used by the ApplyStop() function
>  >> but had no success so far. Consider this simple system:
>  >>
>  >> ---------------------snip-------------------
>  >>
>  >> SetOption("InitialEquity", 100000);
>  >>
>  >> BuyPrice = SellPrice = Open;
>  >> SetTradeDelays(1, 1, 1, 1);
>  >>
>  >> Buy = Cross(Signal(), MACD());
>  >> Sell = Cross(MA(C, 200), C);
>  >>
>  >> TS = 4 * ATR(20);
>  >> ApplyStop(stopTypeTrailing, stopModePoint, TS, 1, TRUE);
>  >>
>  >> #include "Formulas/Basic Charts/Price.afl";
>  >>
>  >> Plot(High - TS, "Trailing Stop", colorRed, styleDots | styleNoLine);
>  >>
>  >> ---------------------snip-------------------
>  >>
>  >> For example, when I run this system on DJI from 2003 to 2007, I get a
>  >> trade started on 2006-08-14 with entry price equals to 11089.10. This
>  >> trade ends by triggering the backtester trailing stop in 2007-02-27 with
>  >> exit price at 12148.50.
>  >>
>  >> However, the trailing stop plot never reach the 12148.50 value exactly.
>  >> Actually, the highest value of the trailing stop plot since trade start
>  >> is 12237.11 (at 2007-02-20), which is higher than backtester exit price!
>  >>
>  >> Another example:
>  >>
>  >> Entry: 2004-09-21 @ 10204.50
>  >> Exit (trail): 2005-03-29 @ 10373.50
>  >> If following plot: trailing stop reached 9732.29 in 2004-10-04, which
>  >> would cause the trade to be stopped much earlier in 2004-10-25.
>  >>
>  >> (Note: I'm using Yahoo free EOD data -- if you use another feed then
>  >> your numbers may be different.)
>  >>
>  >> This is frustating because I developed and optimized a system that
>  >> perform somewhat well in the backtester, but I can't apply it in real
>  >> life because I don't know how to move the stops in my broker to match
>  >> the values used by AB.
>  >>
>  >> So... what's wrong in my code? How to plot the ACTUAL price that
>  >> ApplyStop(stotTypeTrailing, stopModePoint...) consider when exiting 
> trades?
>  >>
>  >> Thanks in advance.
>  >>
>  >
>  >
>  > --
>  > Flávio
>  >
>  > ------------------------------------
>  >
>  > Please note that this group is for discussion between users only.
>  >
>  > To get support from AmiBroker please send an e-mail directly to
>  > SUPPORT {at} amibroker.com
>  >
>  > For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
>  > http://www.amibroker.com/devlog/ <http://www.amibroker.com/devlog/>
>  >
>  > For other support material please check also:
>  > http://www.amibroker.com/support.html 
> <http://www.amibroker.com/support.html>
>  > Yahoo! Groups Links
>  >
>  >
>  >
> 
> 


-- 
Flávio

------------------------------------

Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to 
SUPPORT {at} amibroker.com

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/

For other support material please check also:
http://www.amibroker.com/support.html
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/amibroker/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:amibroker-digest@xxxxxxxxxxxxxxx 
    mailto:amibroker-fullfeatured@xxxxxxxxxxxxxxx

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/