Is it possible to create a custom backtest metric that would display the metric (CAR/MDD and profit %)
for a specific data range during an optimization? i.e. I am optimizing a strategy from 12/2017 - 12/2018 and I want to display the CAR/MDD and profit % for 10/2018 - 12/2018
Here's a little background on what I'm trying to accomplish... Before seeing the light and finding Amibroker, I used a different optimization software, and am trying to figure out how to replicate a nice feature called "paper trading" (but not the typical definition of paper trading). This would be used in Walkforward testing, or regular optimizations. Basically you specify a "Paper Trading" date range after the In-Sample optimization date range, but before the Out-Of-Sample backtest date-range. The software would sort your optimization backtests by how well they performed in the "paper trading" period, then use the best one for the out-of-sample backtest. It sounds odd but actually helps dramatically in picking the best optimization to run live when dealing with a strategy with a lot of variables.
I don't know if it would be possible to replicate this functionality in Amibroker, but it should be fairly easy to do something similar with a custom-backtest metric by looking at the performance during a shorter period of time at the end of the In-Sample optimization period.
My programming skills are pretty poor, but seems like it should be possible to use this per-symbol profit loss as Tomasz has shown in this example:
http://www.amibroker.com/kb/2014/10/13/per-symbol-profitloss-in-a-portfolio-backtest/
and do this only for a specified date range. Or, for that matter, it would be nice to have ANY backtest metric be able to be displayed for a different date-range than the optimization range.
Here is the code I'm attempting to run but it's not working:
function ProcessTrade( trade )
{
global tradedSymbols;
symbol = trade.Symbol;
//
if( ! StrFind( tradedSymbols, "," + symbol + "," ) )
{
tradedSymbols += symbol + ",";
}
//
// HINT: you may replace it with GetPercentProfit if you wish
//profit = trade.GetProfit();
profit = trade.GetPercentProfit();
//Here is where I am trying to limit to a specific date window:
dt = DateTime();
start = _DT( "2018-12-01" );
end = _DT( "2019-1-20" );
datewindow = dt >= start AND dt <= end;
profit = iif(datewindow,profit,0);
//
if( trade.IsLong() )
{
varname = "long_" + symbol;
VarSet( varname, Nz( VarGet( varname ) ) + profit );
}
else
{
varname = "short_" + symbol;
VarSet( varname, Nz( VarGet( varname ) ) + profit );
}
}
//
SetCustomBacktestProc( "" );
//
/* Now custom-backtest procedure follows */
//
if ( Status( "action" ) == actionPortfolio )
{
bo = GetBacktesterObject();
//
bo.Backtest(); // run default backtest procedure
//
tradedSymbols = ",";
//
//iterate through closed trades
for ( trade = bo.GetFirstTrade( ); trade; trade = bo.GetNextTrade( ) )
{
ProcessTrade( trade );
}
//
//iterate through open positions
for ( trade = bo.GetFirstOpenPos( ); trade; trade = bo.GetNextOpenPos( ) )
{
ProcessTrade( trade );
}
//
//iterate through the list of traded symbols and generate custom metrics
for ( i = 1; ( sym = StrExtract( tradedSymbols, i ) ) != ""; i++ )
{
longprofit = VarGet( "long_" + sym );
shortprofit = VarGet( "short_" + sym );
allprofit = Nz( longprofit ) + Nz( shortprofit );
// metric uses 2 decimal points and
// 3 (calculate sum) as a "combine method" for walk forward out-of-sample
bo.AddCustomMetric( "Profit for " + sym, allprofit, longprofit, shortprofit, 2, 3 );
}
}
I really appreciate any help with this, thank you