amibroker

HomeKnowledge Base

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 
processTradetrade )
{
    
dt DateTime();

    
SetForeigntrade.Symbol );

    
llvDate LookupRefdt, - LLVBarsLowtrade.BarsInTrade ) ), trade.ExitDateTime );
    
hhvDate LookupRefdt, - HHVBarsHightrade.BarsInTrade ) ), trade.ExitDateTime );

    if ( 
trade.IsLong() )
    {
        
maeDate llvDate;
        
mfeDate hhvDate;
    }
    else
    {
        
maeDate hhvDate;
        
mfeDate llvDate;
    }

    
RestorePriceArrays();

    
trade.AddCustomMetric"MFE Date"DateTimeToStrmfeDate ) );
    
trade.AddCustomMetric"MAE Date"DateTimeToStrmaeDate ) );
}

if ( 
Status"action" ) == actionPortfolio )
{
    
bo GetBacktesterObject();

    
bo.Backtest); // run default backtest procedure

    
for ( trade bo.GetFirstTrade(); tradetrade bo.GetNextTrade() )
    {
      
processTradetrade );

    }

    for ( 
trade bo.GetFirstOpenPos(); tradetrade bo.GetNextOpenPos() )
    {
      
processTradetrade );
    }

    
bo.ListTrades();
}

Buy CrossMACD(), Signal() );
Sell CrossSignal(), MACD() )

Comments are closed.