ATRratchet (Adaptive Trailing Stop) - Voorbeeld code met toe

Vragen en suggesties over handelssystemen en indicatoren

Moderator: Perry

ATRratchet (Adaptive Trailing Stop) - Voorbeeld code met toe

Berichtdoor FM » wo 08 jan 2003, 11:40

Een voorbeeld hoe een mooie exit te bouwen, bijvoorbeeld als alternatief voor het ProfitLevel in ROCEMA.
Let wel op, de code is niet direct te gebruiken, je moet het even aanpassen aan je eigen situatie. Voor de werking van de stop heb ik een toelichting toegevoegd.

veel plezier,

Frans.



{***************************************************
ATR Ratchet (Adapative Trailing Stop)
Date : 20030107
***************************************************}

value function zATRRatchet (
?value xATRmult ? =0.05,
?value xATRlength =5,
?begin
? ?value xPosHigh[],
? ?xValue1[],
? ?xATRratchet[],
? ?xProfitfloor(10000);

If BarsSinceEntry = 0 Then begin
xPosHigh = High;
xValue1 = 0;
xATRratchet = Low - 2 * AvgTrueRange(xATRlength);
xProfitFloor = AvgTrueRange(xATRlength);
End;

If MarketPosition = 1 Then Begin
If High > xPosHigh Then
xPosHigh = High;
If xPosHigh - EntryPrice > xProfitFloor Then begin
xValue1 = Lowest(Low,10) + ATRmult*AvgTrueRange(xATRlength) * BarsSinceEntry ;
If xValue1 > xATRratchet then xATRratchet = xValue1;
End;

vExitLong('MyOrder',Contracts,When,OrderType,LimitPrice);

End;



The basic idea is quite simple. We first pick a logical
starting point and then add daily units of ATR to the
starting point to produce a trailing stop that moves
consistently higher while also adapting to changes in
volatility. The advantage of this strategy over the
original Parabolic based exit is that when using the ATR
Ratchet we have much more control of the starting point and
the acceleration. We also found that the ATR based exit has
a fast and appropriate reaction to changes in volatility
that will enable us to lock in more profit than most
conventional trailing exits.
Here is an example of the strategy: After the trade has
reached a profit target of at least one ATR or more, we pick
a recent low point (such as the lowest low of the last ten
days). Then we add some small daily unit of ATR (0.05 ATR
for example) to that low point for each day in the trade.
If we have been in the trade for 15 days we would multiply
0.05 ATRs by 15 days and add the resulting 0.75 ATRs to the
starting point. After 20 days in the trade we would now be
adding 1.0 ATRs (.05 times 20) to the lowest low of the last
ten days. The ATR Ratchet is very simple in its logic but
you will quickly discover that there are lots of moving
parts that perform a lot of interesting and useful
functions; much more than we expected.
We particularly like this strategy because, unlike the
Parabolic, the ATR Ratchet can easily be implemented any
time we want during the trade. We can start implementing
the stop the very first day of the trade or we can wait
until some specific event prompts us to implement a
profit-taking exit. I would suggest waiting to use the exit
until some minimum level of profitability has been reached
because, as you will see, this stop has a way of moving up
very rapidly under favorable market conditions.
The ATR Ratchet begins very quietly and moves up steadily
each day because we are adding one small unit of ATR for
each bar in the trade. However the starting point from
which the stop is being calculated (the 10 day low in our
example) also moves up on a regular basis as long as the
market is headed in the right direction. So now we have a
constantly increasing number of units of ATR being added to
a constantly rising ten day low. Each time the 10-day low
increases our ATR Ratchet moves higher so we typically have
a small but steady increase in the daily stop followed by
much larger jumps as the 10 day low moves higher. It is
important to emphasize that we are constantly adding our
daily acceleration to an upward moving starting point that
produces a unique dual acceleration feature for this exit.
We have a rising stop that is being accelerated by both time
and price. In addition, the ATR Ratchet will often add
substantial additional acceleration in response to increases
in volatility during the trade.
The acceleration due to range expansions is an important
feature of the ATR Ratchet. Because markets often tend to
show wider ranges as the trend accelerates the ATR will tend
to expand very rapidly during our best profit runs. In a
fast moving market you will typically find many gaps and
large range bars. Because we are adding multiple units of
ATR to our starting point, any increase in the size of the
underlying ATR causes the stop to suddenly make a very large
jump that brings it closer to the high point of the trade.
If we have been in the trade for forty days any increase in
the ATR will have a forty-fold impact on the cumulative
daily acceleration. That is exactly what we want it to do.
We found that when a market was making a good profit run the
ATR Ratchet moved up surprisingly fast and did an excellent
job of locking in open profits.
Keep in mind that this exit strategy is a new one (even to
us) so our experience and observations about it are still
very limited. However I am going to discuss a few
observations about the variables that might help you to
understand and apply this exit successfully.
Starting Price: One of the nice features about the ATR
Ratchet is that we can start it any place we want. For
example we can start it at some significant low point just
as the Parabolic does. Or we can start it at a swing low, a
support level, and a channel low or at our entry point minus
some ATR unit. If we wait until the trade is fairly
profitable we could start it at the entry point or even
somewhere above our entry point. The possible starting
points are unlimited; use your imagination and your logic to
find a starting point that makes sense for your time frame
and for what you want your system to accomplish. Our idea
of starting the Ratchet from the x day low makes it move up
faster than a fixed starting point (as in the Parabolic)
because the starting point rises repeatedly in a strong
market. If you prefer, you could just as easily start the
Ratchet at something like 2 ATRs below the entry price and
then the starting point would remain fixed. In this case
the Ratchet would move up only as the result of accumulating
additional time in the trade and as the result of possible
expansions of the ATR itself.
When to Start: We can very easily initiate the exit strategy
based on time rather than price or combine the two ideas.
For example, we can start the exit only after the trade has
been open for at least 10 days and is profitable by more
than one ATR. My general impression at this point is that
it is best to implement the ATR Ratchet only after a fairly
large profit objective has been reached. The ATR Ratchet
looks like a very good profit taking exit but I suspect it
will kick you out of a trade much too soon if you start it
before the trade is profitable.
As I mentioned, one of the things I like best about the ATR
Ratchet is its flexibility and adaptability. Here is
another idea on how to start it. We can start it after
fifteen bars but we don't necessarily have to add fifteen
ratchets. The logic for the coding would be to start the
Ratchet after 15 bars in the trade but multiply the ATR
units by the number of bars in the trade minus ten or divide
the number of days in the trade by some constant before
multiplying the ATR units. This procedure will reduce the
number of ratchets, particularly at the beginning of the
trade when the exit is first implemented. Play around with
the ATR Ratchet and see what creative ideas you can come up
with.
Daily Ratchet Amount: After testing it the daily Ratchet
amount we chose when we were first doing our research turned
out to be much too large for our intended application. The
large Ratchet amount (percentage of ATR) moved the stop up
too fast for the time frame we wanted to trade. After some
trial and error we found that a Ratchet amount in the
neighborhood of 0.05 or 0.10 (5% or 10% of one 20-day
average true range) multiplied by the number of bars the
trade has been open will move the stop up much faster than
you might expect.
As a variation on this strategy the very small initial
Ratchet can always be increased later in the trade once the
profits are very high. We could start with a small Ratchet
and then after a large amount of profit we could use a
larger daily Ratchet increment. There are all sorts of
interesting possibilities.
ATR Length: As we have learned in our previous uses of ATR,
the length that we use to average the ranges can be very
important. If we want the ATR to be highly responsive to
short term variations in the size of the range we should use
a short length for the average (4 or 5 bars). If we want a
smoother ATR with less reaction to one or two days of
unusual volatility we should use a longer average (20 to 50
bars). For most of my work with the ATR I use 20 days for
the average unless I have a good reason to make it more or
less sensitive.
Summary: We have just scratched the surface on our
understanding of the possibilities and variations of the ATR
Ratchet as a profit taking tool. We particularly like the
flexibility it offers and we suspect that each trader will
wind up using a slightly different variation. As you can
see, there are many important variables to tinker with. Be
sure to code the Ratchet so it gets plotted on a chart when
your are first learning and experimenting with it. The ATR
Ratchet is full of pleasant surprises and the plot on the
chart will quickly teach you a great deal about its unusual
characteristics.
FM
 
Berichten: 113
Geregistreerd op: do 15 aug 2002, 12:58

ATRratchet (Adaptive Trailing Stop)

Berichtdoor poekmeister » wo 08 jan 2003, 21:16

Frans, bedankt voor deze interessante bijdrage ;). Hij lijkt een beetje of the chandlier exit zoals die door Charles LeBeau ook wordt beschreven op zijn traderclub.com forum.
Je code werkt alleen niet helemaal, zie de definitie van de funtie. Je bent ergens een "')" vergeten en de functie compileert dus niet. Wil je eens kijken wat er mis is (dat scheelt mij weer een hoop trial and error)?
Dank, Michel
poekmeister
 
Berichten: 88
Geregistreerd op: za 17 aug 2002, 19:50

ATRratchet (Adaptive Trailing Stop)

Berichtdoor acp010107 » wo 08 jan 2003, 23:46

Michel,
De derde regel, value xATRLength = 5, sluit met een komma. Moet dat geen haakje zijn ?
Dus geen ',' maar een ")".
Aad ?
acp010107
 
Berichten: 101
Geregistreerd op: di 05 maart 2002, 23:48

ATRratchet (Adaptive Trailing Stop)

Berichtdoor FM » do 09 jan 2003, 0:57

Michel,

Deze functie is inderdaad door Chuck LeBeau bedacht en heet ATRratchet. De formule heb ik werkend gemaakt, en Aad je opmerking klopte maar er zat nog een klein foutje in. De functie werkt nu goed.
Zoals de omschrijving aangeeft zul je nog een beetje moeten spelen met de parameters. Per aandeel, of periode verschilen de instellingen. Ik heb er voor de duidelijkheid twee plots aan toegevoegd.

met groet,

Frans.
{***************************************************
ATR Ratchet (Adapative Trailing Stop)
Date : 20030107
***************************************************}

value function zATRRatchet (
?value xATRmult ? =0.05,
?value xATRlength =5)
?begin value xPosHigh[],
? ?xValue1[],
? ?xATRratchet[],
? ?xProfitfloor=10000;

If BarsSinceEntry = 0 Then begin
xPosHigh = High;
xValue1 = 0;
xATRratchet = Low - 2 * AvgTrueRange(xATRlength);
xProfitFloor = AvgTrueRange(xATRlength);
End;

If MarketPosition = 1 Then Begin
If High > xPosHigh Then
xPosHigh = High;
If xPosHigh - EntryPrice > xProfitFloor Then begin
xValue1 = Lowest(Low,10) + xATRmult*AvgTrueRange(xATRlength) * BarsSinceEntry ;
If xValue1 > xATRratchet then xATRratchet = xValue1;
End;
{vExitLong('MyOrder',Contracts,When,OrderType,LimitPrice); }
End;
plot1(xPosHigh,'');
PLot2(xATRratchet,'');
End;
FM
 
Berichten: 113
Geregistreerd op: do 15 aug 2002, 12:58

ATRratchet (Adaptive Trailing Stop)

Berichtdoor geert udema » do 09 jan 2003, 17:08

Ik heb de zaak ook draaiend maar heb alleen [] bij xPosHigh[] nog moeten verwijderen tot xPosHigh.
Dat moet m.i. ook nog bij xATRratchet[] om 1 trade te simuleren :
------------------------------
if currentbar=54 {zoek een mooie dag uit} then begin
?vEnterLong('',1,When,xOrderType,xLimitPrice);
end;
-------------------------------
if ?Close < xATRratchet then begin
? ?vExitLong('',xContracts,When,xOrderType,xLimitPrice);
?end;
-------------------------------

Een echte verbetering t.o.v. de oude SAR Parabolic !
Frans, bedankt, zo wordt het een echt Forum !

Groetend, Geert
geert udema
 
Berichten: 114
Geregistreerd op: ma 31 dec 2001, 11:45

ATRratchet (Adaptive Trailing Stop)

Berichtdoor artisan » zo 12 jan 2003, 20:48

Misschien een wat vreemde vraag, maar moet de functie zATRRatchet() op een gegeven moment geen waarde toegewezen krijgen ?met een statement in de functie van

zATRRatchet:=.........

of iets dergelijks??

Volgens mij krijg je geen waarde terug wanneer je de functie aanroept omdat bovengenoemd statement niet aanwezig is.
Of is het de bedoeling dat je de functie zATRRatchet in het systeem zelf opneemt en niet als aparte functie wegschrijft ?
artisan
 
Berichten: 12
Geregistreerd op: zo 11 aug 2002, 14:05

ATRratchet (Adaptive Trailing Stop)

Berichtdoor FM » zo 12 jan 2003, 21:20

zATRRatchet moet onderdeel uitmaken van een handelssysteem. Op zichzelf zegt het niet zoveel.

met groet,

Frans.
FM
 
Berichten: 113
Geregistreerd op: do 15 aug 2002, 12:58


Keer terug naar Handelssystemen + Indicatoren

Wie is er online

Gebruikers op dit forum: Geen geregistreerde gebruikers en 12 gasten

cron