February 4, 2015
How to add MAE / MFE dates to the backtest report
If we want to identify dates, when MAE and MFE levels have been reached during the trade lifetime – we can use the code example presented below.
The formula will process the trades one-by-one, read BarsInTrade property to know how many bars it took since trade entry till exit, then use HHVBars / LLVBars functions to identify how many bars have passed since lowest low or highest high within trade length.
With the information that highest or lowest value was observed N-bars ago – it will shift Date/Time array accordingly – so with use of Lookup() function pointing at the exitbar – we can read the date when HHV/LLV was observed within trade lifetime (BarsInTrade).
SetCustomBacktestProc( "" );
function processTrade( trade )
{
dt = DateTime();
SetForeign( trade.Symbol );
llvDate = Lookup( Ref( dt, - LLVBars( Low, trade.BarsInTrade + 1 ) ), trade.ExitDateTime );
hhvDate = Lookup( Ref( dt, - HHVBars( High, trade.BarsInTrade + 1 ) ), trade.ExitDateTime );
if ( trade.IsLong() )
{
maeDate = llvDate;
mfeDate = hhvDate;
}
else
{
maeDate = hhvDate;
mfeDate = llvDate;
}
RestorePriceArrays();
trade.AddCustomMetric( "MFE Date", DateTimeToStr( mfeDate ) );
trade.AddCustomMetric( "MAE Date", DateTimeToStr( maeDate ) );
}
if ( Status( "action" ) == actionPortfolio )
{
bo = GetBacktesterObject();
bo.Backtest( 1 ); // run default backtest procedure
for ( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )
{
processTrade( trade );
}
for ( trade = bo.GetFirstOpenPos(); trade; trade = bo.GetNextOpenPos() )
{
processTrade( trade );
}
bo.ListTrades();
}
Buy = Cross( MACD(), Signal() );
Sell = Cross( Signal(), MACD() )
Filed by Tomasz Janeczko at 5:16 pm under Custom Backtest
Comments Off on How to add MAE / MFE dates to the backtest report