Consecutive Winnining or Losing Trades for Amibroker

I brought to you a feature that Amibroker till now is missing, but that other similar softwares have. Sometimes it is useful to know the Consecutive Winning Series Data or the Consecutive Losing Series Data of the trades of a Trading System. In the Backtest Report of Amibroker you can read only the number of Max Consecutive Winning or Losing  trades.

Adding this code to your AFL Formula, you can read the Consecutive Winning Series Data or the Consecutive Losing Series in your Amibroker Backtest Report.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
SetCustomBacktestProc( "" );

function updateStats( winnersOrLosers, profit, longOrShortOrBoth, seriesLength )
{
    signum = 1;

    if ( winnersOrLosers == "Losers" )
        signum = -1;

    if ( signum * profit > 0 )
        seriesLength++;
    else
    {
        VarSet( longOrShortOrBoth + seriesLength, VarGet( longOrShortOrBoth + seriesLength ) + 1 );
        seriesLength = 0;
    }

    return seriesLength;
}

procedure calcStats( bo, type )
{

    metricName = type + "MaxConsecutive";

    // Retrive the max number of consecutive winning trades
    stat = bo.GetPerformanceStats( 0 ); // Get Stats object for all trades
    maxConsecutive = stat.GetValue( metricName );
    stat = bo.GetPerformanceStats( 1 ); // Get Stats object for long trades
    maxConsecutive = Max( maxConsecutive , stat.GetValue( metricName ) );
    stat = bo.GetPerformanceStats( 2 ); // Get Stats object for short trades
    maxConsecutive = Max( maxConsecutive , stat.GetValue( metricName ) );

    for ( i = 1; i <= maxConsecutive ; i++ ) // Remember that "Dynamic variables are always global"
    {
        VarSet( "consecutive" + i, 0 );
        VarSet( "consecutiveLong" + i, 0 );
        VarSet( "consecutiveShort" + i, 0 );
    }

    consecutiveCounter = consecutiveLongCounter = consecutiveShortCounter = 0;

    for ( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )// Loop through all closed trades
    {
        consecutiveCounter = updateStats( type, trade.getProfit(), "consecutive", consecutiveCounter );

        if ( trade.isLong )
            consecutiveLongCounter = updateStats( type, trade.getProfit(), "consecutiveLong", consecutiveLongCounter );
        else
            consecutiveShortCounter = updateStats( type, trade.getProfit(), "consecutiveShort", consecutiveShortCounter );
    }

    for ( i = 1; i <= maxConsecutive; i++ )
        bo.AddCustomMetric( "Consecutive " + type + " #" + i, VarGet( "consecutive" + i ), VarGet( "consecutiveLong" + i ), VarGet( "consecutiveShort" + i ), 0 ); // Add to results display

}

if ( Status( "action" ) == actionPortfolio )
{
    bo = GetBacktesterObject(); // Get backtester object
    bo.Backtest(); // Run backtests

    calcStats( bo, "Winners" );
    calcStats( bo, "Losers" );
}

The result is something like this (look at the bottom of the report):

Statistics | Charts | Trades | Formula | Settings | Symbols

Statistics
All tradesLong tradesShort trades
Initial capital100000.00100000.00100000.00
Ending capital106610.00107400.0099210.00
Net Profit6610.007400.00-790.00
Net Profit %6.61 %7.40 %-0.79 %
Exposure %3.83 %2.44 %1.39 %
Net Risk Adjusted Return %172.58 %302.78 %-56.99 %
Annual Return %1.28 %1.43 %-0.16 %
Risk Adjusted Return %33.49 %58.58 %-11.39 %

All trades4925 (51.02 %)24 (48.98 %)
 Avg. Profit/Loss134.90296.00-32.92
 Avg. Profit/Loss %6.42 %14.10 %-1.57 %
 Avg. Bars Held25.5331.9218.88

Winners10 (20.41 %)7 (14.29 %)3 (6.12 %)
 Total Profit19080.0013400.005680.00
 Avg. Profit1908.001914.291893.33
 Avg. Profit %90.86 %91.16 %90.16 %
 Avg. Bars Held89.2088.4391.00
 Max. Consecutive221
 Largest win5600.005600.005590.00
 # bars in largest win187187129

Losers39 (79.59 %)18 (36.73 %)21 (42.86 %)
 Total Loss-12470.00-6000.00-6470.00
 Avg. Loss-319.74-333.33-308.10
 Avg. Loss %-15.23 %-15.87 %-14.67 %
 Avg. Bars Held9.219.948.57
 Max. Consecutive878
 Largest loss-1230.00-1230.00-1100.00
 # bars in largest loss332

Max. trade drawdown-5120.00-2670.00-5120.00
Max. trade % drawdown-62.20 %-58.77 %-62.20 %
Max. system drawdown-9020.00-5760.00-5720.00
Max. system % drawdown-8.42 %-5.37 %-5.62 %
Recovery Factor0.731.28-0.14
CAR/MaxDD0.150.27-0.03
RAR/MaxDD3.9810.92-2.03
Profit Factor1.532.230.88
Payoff Ratio5.975.746.15
Standard Error1891.261620.881586.40
Risk-Reward Ratio0.330.240.15
Ulcer Index3.343.243.36
Ulcer Performance Index-1.23-1.22-1.65
Sharpe Ratio of trades0.290.55-0.12
K-Ratio0.01340.00980.0059

Consecutive Winners #1743
Consecutive Winners #2110
Consecutive Losers #1131
Consecutive Losers #2300
Consecutive Losers #3100
Consecutive Losers #4001
Consecutive Losers #5010
Consecutive Losers #6100
Consecutive Losers #7010
Consecutive Losers #8201

I added the Runs Test statistics, you can find it here.

Comments

Popular Posts