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.
The result is something like this (look at the bottom of the report):
I added the Runs Test statistics, you can find it here.
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 trades | Long trades | Short trades | |
---|---|---|---|
Initial capital | 100000.00 | 100000.00 | 100000.00 |
Ending capital | 106610.00 | 107400.00 | 99210.00 |
Net Profit | 6610.00 | 7400.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 trades | 49 | 25 (51.02 %) | 24 (48.98 %) |
Avg. Profit/Loss | 134.90 | 296.00 | -32.92 |
Avg. Profit/Loss % | 6.42 % | 14.10 % | -1.57 % |
Avg. Bars Held | 25.53 | 31.92 | 18.88 |
Winners | 10 (20.41 %) | 7 (14.29 %) | 3 (6.12 %) |
Total Profit | 19080.00 | 13400.00 | 5680.00 |
Avg. Profit | 1908.00 | 1914.29 | 1893.33 |
Avg. Profit % | 90.86 % | 91.16 % | 90.16 % |
Avg. Bars Held | 89.20 | 88.43 | 91.00 |
Max. Consecutive | 2 | 2 | 1 |
Largest win | 5600.00 | 5600.00 | 5590.00 |
# bars in largest win | 187 | 187 | 129 |
Losers | 39 (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 Held | 9.21 | 9.94 | 8.57 |
Max. Consecutive | 8 | 7 | 8 |
Largest loss | -1230.00 | -1230.00 | -1100.00 |
# bars in largest loss | 3 | 3 | 2 |
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 Factor | 0.73 | 1.28 | -0.14 |
CAR/MaxDD | 0.15 | 0.27 | -0.03 |
RAR/MaxDD | 3.98 | 10.92 | -2.03 |
Profit Factor | 1.53 | 2.23 | 0.88 |
Payoff Ratio | 5.97 | 5.74 | 6.15 |
Standard Error | 1891.26 | 1620.88 | 1586.40 |
Risk-Reward Ratio | 0.33 | 0.24 | 0.15 |
Ulcer Index | 3.34 | 3.24 | 3.36 |
Ulcer Performance Index | -1.23 | -1.22 | -1.65 |
Sharpe Ratio of trades | 0.29 | 0.55 | -0.12 |
K-Ratio | 0.0134 | 0.0098 | 0.0059 |
Consecutive Winners #1 | 7 | 4 | 3 |
Consecutive Winners #2 | 1 | 1 | 0 |
Consecutive Losers #1 | 1 | 3 | 1 |
Consecutive Losers #2 | 3 | 0 | 0 |
Consecutive Losers #3 | 1 | 0 | 0 |
Consecutive Losers #4 | 0 | 0 | 1 |
Consecutive Losers #5 | 0 | 1 | 0 |
Consecutive Losers #6 | 1 | 0 | 0 |
Consecutive Losers #7 | 0 | 1 | 0 |
Consecutive Losers #8 | 2 | 0 | 1 |
I added the Runs Test statistics, you can find it here.
Comments
Post a Comment