{"id":1079,"date":"2015-02-04T17:16:24","date_gmt":"2015-02-04T22:16:24","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/?p=1079"},"modified":"2015-02-11T17:21:51","modified_gmt":"2015-02-11T22:21:51","slug":"how-to-add-mae-mfe-dates-to-the-backtest-report","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2015\/02\/04\/how-to-add-mae-mfe-dates-to-the-backtest-report\/","title":{"rendered":"How to add MAE \/ MFE dates to the backtest report"},"content":{"rendered":"

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.<\/p>

The formula will process the trades one-by-one, read BarsInTrade<\/strong> property to know how many bars it took since trade entry till exit, then use HHVBars<\/strong> \/ LLVBars<\/strong> functions to identify how many bars have passed since lowest low or highest high within trade length.<\/p>

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<\/strong>() function pointing at the exitbar – we can read the date when HHV\/LLV was observed within trade lifetime (BarsInTrade).<\/p>SetCustomBacktestProc<\/span>( <\/span>"" <\/span>);

function <\/span>processTrade<\/span>( <\/span>trade <\/span>)
{
    <\/span>dt <\/span>= <\/span>DateTime<\/span>();

    <\/span>SetForeign<\/span>( <\/span>trade<\/span>.<\/span>Symbol <\/span>);

    <\/span>llvDate <\/span>= <\/span>Lookup<\/span>( <\/span>Ref<\/span>( <\/span>dt<\/span>, - <\/span>LLVBars<\/span>( <\/span>Low<\/span>, <\/span>trade<\/span>.<\/span>BarsInTrade <\/span>+ <\/span>1 <\/span>) ), <\/span>trade<\/span>.<\/span>ExitDateTime <\/span>);
    <\/span>hhvDate <\/span>= <\/span>Lookup<\/span>( <\/span>Ref<\/span>( <\/span>dt<\/span>, - <\/span>HHVBars<\/span>( <\/span>High<\/span>, <\/span>trade<\/span>.<\/span>BarsInTrade <\/span>+ <\/span>1 <\/span>) ), <\/span>trade<\/span>.<\/span>ExitDateTime <\/span>);

    if ( <\/span>trade<\/span>.<\/span>IsLong<\/span>() )
    {
        <\/span>maeDate <\/span>= <\/span>llvDate<\/span>;
        <\/span>mfeDate <\/span>= <\/span>hhvDate<\/span>;
    }
    else
    {
        <\/span>maeDate <\/span>= <\/span>hhvDate<\/span>;
        <\/span>mfeDate <\/span>= <\/span>llvDate<\/span>;
    }

    <\/span>RestorePriceArrays<\/span>();

    <\/span>trade<\/span>.<\/span>AddCustomMetric<\/span>( <\/span>"MFE Date"<\/span>, <\/span>DateTimeToStr<\/span>( <\/span>mfeDate <\/span>) );
    <\/span>trade<\/span>.<\/span>AddCustomMetric<\/span>( <\/span>"MAE Date"<\/span>, <\/span>DateTimeToStr<\/span>( <\/span>maeDate <\/span>) );
}

if ( <\/span>Status<\/span>( <\/span>"action" <\/span>) == <\/span>actionPortfolio <\/span>)
{
    <\/span>bo <\/span>= <\/span>GetBacktesterObject<\/span>();

    <\/span>bo<\/span>.<\/span>Backtest<\/span>( <\/span>1 <\/span>); <\/span>\/\/ run default backtest procedure

    <\/span>for ( <\/span>trade <\/span>= <\/span>bo<\/span>.<\/span>GetFirstTrade<\/span>(); <\/span>trade<\/span>; <\/span>trade <\/span>= <\/span>bo<\/span>.<\/span>GetNextTrade<\/span>() )
    {
      <\/span>processTrade<\/span>( <\/span>trade <\/span>);

    }

    for ( <\/span>trade <\/span>= <\/span>bo<\/span>.<\/span>GetFirstOpenPos<\/span>(); <\/span>trade<\/span>; <\/span>trade <\/span>= <\/span>bo<\/span>.<\/span>GetNextOpenPos<\/span>() )
    {
      <\/span>processTrade<\/span>( <\/span>trade <\/span>);
    }

    <\/span>bo<\/span>.<\/span>ListTrades<\/span>();
}

<\/span>Buy <\/span>= <\/span>Cross<\/span>( <\/span>MACD<\/span>(), <\/span>Signal<\/span>() );
<\/span>Sell <\/span>= <\/span>Cross<\/span>( <\/span>Signal<\/span>(), <\/span>MACD<\/span>() )<\/code>","protected":false},"excerpt":{"rendered":"

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 […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1079"}],"collection":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/comments?post=1079"}],"version-history":[{"count":2,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1079\/revisions"}],"predecessor-version":[{"id":1081,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1079\/revisions\/1081"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=1079"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=1079"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=1079"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}