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

Re: [amibroker] request for help with trailing stops



PureBytes Links

Trading Reference Links

Here's what it says in plain English (English following each line of code
with no > in front of the English part):
 
> /* a sample low-level implementation of trailing stop in AFL: */
> Short = your rule here
> 
> 
> trailstop =0;
First set the entire array called trailstop to 0.
This means all bars for all of your data.

> for( i = 1; i < BarCount; i++ )
Loop through all bars of data one by one (instead of using array function
which does all bars of data simultaneously).
> {
All statements between {} pairs go with the loop

> if( trailstop == 0 && Short[ i ] ) trailstop = High[ i ];
If trailstop is set to 0, which it is, AND IF we are Short set trailstop to
the High of the day so it has room to move against you.

> if( trailstop > 0 AND High[ i ] > trailstop )
Now test to see if the trailstop is > zero, which it will be since we just
set it to the High of the day AND the current High > trailstop, which it
can't be because we just set it == the High
> {
If the test was True, which it can't be because we just forced the trailstop
to be equal to the High, then
> Cover[ i ] = True;
set Cover to True so we exit.
> CoverPrice[ i ] = trailstop;
Set CoverPrice = trailstop (which was equal to the High)
> trailstop= 0;
Re-zero the trailstop value. This makes the trailstop variable appear to be
numeric, but I think it must be an array??
> }
> else Cover[ i ] = 0;
If the test above was False, then set Cover = False (same as = 0)
 
> if( trailstop > 0 )
Now, if tne trailstop is still set to the high, then set the trailstop equal
to the lower of yesterday's high or today's high (since trailstop = today's
high)
> {
> trailstop = Min( High[ i - 1 ], trailstop );
> }
Previous set of {} are unnecessary since there's only a single statement.

> }
Matching } to beginning of loop.

So, now that I wrote the English version, I question (would need to test)
the logic. It seems that the stop will never be hit since the stop is set to
the high of the day and then it tests to see if the stop has been exceeded,
but I don't see how that's ever possible. Second observation is, in my
experience, when you set a variable as in the very first statement, this
becomes an array variable. This may be an exception because there are many
reserved variables in AB. I don't know where the "list" of these variables
is, if there is a list. Thus, it could be a single numeric variable if it
has been predefined by AB somewhere.

I always hate to doubt the AB boys. They are nearly ALWAYS right! If Marcin
is reading, maybe he can clarify these points.
-- 
Terry

> From: nikku <nikku@xxxxxxxxxxxx>
> Reply-To: amibroker@xxxxxxxxxxxxxxx
> Date: Mon, 06 Dec 2004 23:50:49 -0500
> To: amibroker@xxxxxxxxxxxxxxx
> Subject: [amibroker] request for help with trailing stops
> 
> 
> Hi all
> 
> I hope someone can help me out with my trailing stops problem. Now that
> I have purchased the s/w I can backtest multiple securities. However,
> when I backtest trying to use the 'percent of profit' type stops, I have
> a problem. Let's say I short MMM at the open at 50.00, with a trailing
> 'percent of profit' stop of 20%. If MMM trades down to 49.80 by days
> end, all is well. However, if it opens at 49.90 the next day, I am
> stopped out because $0.10 is 50% of my total profit which is $0.20.
> 
> I asked Marcin for some help with this and he sent me the following
> code. I am not sure if he meant this as a fix for my specific problem or
> not. The trouble is... and I am sorry about this... the following
> doesn't mean much to me, being a total AFL newbie.
> 
> Can anyone review the following and either give me a plain English idea
> of what it says or simply re-read the above and suggest a way that I can
> tell AB that I want to give my trades a bit of 'room to breathe' and
> that even if a trade goes immediately positive, then backs up and maybe
> even goes slightly negative, that I want to stay in? I can envision
> using an n-bars stop, but how can I tell AB to not allow the 'percent of
> profit' stop to kick in until xx actual profit has been made (a
> threshold large enough that I am not taken out prematurely)?
> 
> Yes, I have read all the helps re: trailing stops and if someone can
> show me where this specific issue is addressed, I would welcome it.
> 
> I wish I could offer more (or more accurately, anything at all) in
> return for the help... maybe someday.
> 
> Regards, Nikku



------------------------ Yahoo! Groups Sponsor --------------------~--> 
$4.98 domain names from Yahoo!. Register anything.
http://us.click.yahoo.com/Q7_YsB/neXJAA/yQLSAA/GHeqlB/TM
--------------------------------------------------------------------~-> 

Check AmiBroker web page at:
http://www.amibroker.com/

Check group FAQ at: http://groups.yahoo.com/group/amibroker/files/groupfaq.html 
Yahoo! Groups Links

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

<*> 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/