PureBytes Links
Trading Reference Links
|
Nik,
Try something like this:
//--------------------------------
buyat=BuyPrice; //the price you bought at
MinGainPct=.04; //minimum gain % before activating trail
trailPct=1.5; //% trailing stop
//if not at min%, set it impossibly high...
pct=IIf((C-buyat)/buyat > MinGainPct,trailPct,9999);
ApplyStop(stopTypeTrailing,stopModeRisk,pct,2,True);
//---------------------
Experiment with the different % using the "optimize" and
the other settings for "ApplyStop" command.
Walt
--- In amibroker@xxxxxxxxxxxxxxx, nikku <nikku@xxxx> wrote:
> Hi Terry
>
> I really want to say thanks for taking the time to compose this
detailed
> reply. I realize it must have taken you a while.
>
> I now understand the basics of what is being attempted here. I too
am
> unsure if it is doing what it is meant to do since I am not getting
> results which match the expectation given the content of your
explanation.
>
> I'll try to get some more insight into this but at the same time I
am
> going to try to take what you gave me and construct another bit of
stop
> code to see if I can get it to do what I want.
>
> I just wonder how others deal with this problem since the inbuilt
stops
> in the Settings--stops window don't deal with it and the ApplyStop
> function doesn't deal with it (I tried implementing the stops from
the
> code level as well); there must be a way of telling AB to act in
the way
> I want. I cannot believe that this can't be done in this software.
>
> For those who don't want to scroll all the way through to figure
out
> what I am referring to, it's easy - my trailing 'percent of profit'
> stops are kicking in when a position goes even $0.05 profitable and
I am
> subsequently taken out if the trade gives back eve a tiny amount
(i.e.
> if it retraces $0.02 I am stopped out). How do you let a
trade 'breathe
> a bit' in the beginning? How do you implement a trailing 'percent
of
> profit' stop that only kicks in after a trade gets past a certain
> threshold of profitability?
>
> Thanks, Nikku
>
> Terry wrote:
>
> > 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@xxxx>
> > > 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
> >
> >
> >
> > Check AmiBroker web page at:
> > http://www.amibroker.com/
> >
> > Check group FAQ at:
> > http://groups.yahoo.com/group/amibroker/files/groupfaq.html
> >
> >
> > *Yahoo! Groups Sponsor*
> > ADVERTISEMENT
> >
<http://us.ard.yahoo.com/SIG=1297o32hl/M=294855.5468653.6549235.300117
6/D=groups/S=1705632198:HM/EXP=1102483308/A=2455396/R=0/SIG=119u9qmi7/
*http://smallbusiness.yahoo.com/domains/>
> >
> >
> >
> > *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
> > <mailto:amibroker-unsubscribe@xxxxxxxxxxxxxxx?
subject=Unsubscribe>
> >
> > * Your use of Yahoo! Groups is subject to the Yahoo! Terms of
> > Service <http://docs.yahoo.com/info/terms/>.
> >
> >
>
>
> --
> The majority meet with failure because of their lack of persistence
in creating new plans to take the place of those which fail.
------------------------ 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/
|