You are on page 1of 2

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();
//
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 );
}
}
//
SetOption( "MaxOpenPositions", 10 );
//
Buy = Cross( MACD(), Signal() );
Sell = Cross( Signal(), MACD() );
Short = Sell;
Cover = Buy;
SetPositionSize( 10, spsPercentOfEquity )

//If you prefer percent profits instead of dollar profits, just replace GetProfit()
call with GetPercentProfit().

You might also like