Hi,

Actually, your workaround is flawed. There are conditions under which it
will not produce the correct values.  Do the following with the default
database that comes with AmiBroker (i.e. the evaluation database)

1. Create a watchlist containing symbols: CAT, AXP, BA, C

2. Run a backtest filtered by this watchlist for the period  Jan 1/07 -
Jun 15/07 (initial equity 500,000, Long only, daily periodicity,  min 1
share, 1 round lot size, all trades on Open with 1 delay, all stops
disabled).

3. Observe the chart. Notice that my version will show {empty}  for as
long as the equity line is initially flat, then begin counting at 1 on
the first day that equity drops below initial equity, incrementing until
equity crosses above initial equity (i.e. correctly captures initial
drawdown after a period of inactivity). Whereas your line will show 0
for as long as the equity line is flat (this is fine), then show 0 on
the first day that equity drops below initial equity (wrong, should be
1), then shows 5 on the second day instead of 2, and remains 3 bars
exagerated throughout the first drawdown until equity finally recovers
and passes above initial equity.

What the code needs to capture is the number of bars since equity first
dropped below initial equity in the event that no new high has yet been
met. The statement BarsSince (ExRem(eq < initialEq, eq >= initialEq)) +
1 will capture the that information (i.e remove redundencies then count
how many bars since the event, plus 1 for the current bar).

Buy = DayOfWeek() == 1;
Sell = DayOfWeek() == 5;

initialEq = GetOption("initialEquity");

eq = Foreign("~~~Equity", "C");
highEq = Highest(eq);
initialDD = ExRem(eq < initialEq, eq >= initialEq);
duration = IIF(highEq > initialEq, HighestBars(eq), BarsSince(initialDD)
+ 1);
DDduration = IIf(BarsSince(Highest(eq) == Lowest(eq)) <= 1, 0,
HighestBars(eq));
longest = LastValue(Highest(duration));

Plot(duration, "sfclimbers", colorRed, styleOwnScale);
Plot(DDduration, "wml67", colorBlue, styleOwnScale);
Plot(eq, "Equity", colorDarkGrey);

SetCustomBacktestProc("");

if (Status("action") == actionPortfolio) {
    bo = GetBacktesterObject();
    bo.Backtest();

    initialEq = GetOption("initialEquity");

    eq = Foreign("~~~Equity", "C");
    highEq = Highest(eq);
    initialDD = ExRem(eq < initialEq, eq >= initialEq);
    duration = IIF(highEq > initialEq, HighestBars(eq),
BarsSince(initialDD) + 1);
    longest = LastValue(Highest(duration));

    bo.addCustomMetric("Longest DD", longest);
}

Mike

P.S. To capture formatted and color coded text, go to Tools |
Preferences... | Editor and select "Copy as HTML". If posting via the
forum (as opposed to a mailer) you will need to post using the Rich Text
Editor. Note that the Rich Text Editor does not work correctly when
using Google Chrome as your browser, perhaps others too. Works fine from
IE.


--- In [email protected], "wml67" <y...@...> wrote:
>
> Mike,
>
> Thanks for taking time to look into this. Your input seem to confirm
> that HighestBar() has a problem, and it's not me doing something
wrong.
>
> As for the workaround, here's mine, I think it's more intuitive
(that's
> subjective, of course):
>
> eq = Foreign("~~~EQUITY", "C");
> // DDduration = HighestBars(eq); // This would've worked if not for
> HighestBars problem
> DDduration = IIf(BarsSince(Highest(eq) == Lowest(eq)) <= 1, 0,
> HighestBars(eq)); // workaround
> longestDD = LastValue(Highest(DDduration));
>
> BTW, sorry for the stupid question, how do you paste formatted code
with
> colors etc into your posts?
>


Reply via email to