{"id":1288,"date":"2016-01-20T15:16:50","date_gmt":"2016-01-20T14:16:50","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/?p=1288"},"modified":"2016-01-28T15:22:24","modified_gmt":"2016-01-28T14:22:24","slug":"number-of-stopped-out-trades-as-a-custom-metric","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2016\/01\/20\/number-of-stopped-out-trades-as-a-custom-metric\/","title":{"rendered":"Number of stopped-out trades as a custom metric"},"content":{"rendered":"

For the purpose of counting trades closed by particular stop we can refer to ExitReason property of the trade object in the custom backtester<\/a>. The custom backtest formula presented below iterates through the list of closed trades, then counts the trades, which indicate exit reason = 2, that is stop-loss.<\/p>

The following values are used for indication of the particular exit reason:<\/p>

  1. normal exit
  2. maximum loss stop
  3. profit target stop
  4. trailing stop
  5. n-bar stop
  6. ruin stop (losing 99.96% of entry value)<\/ol>SetCustomBacktestProc<\/span>( <\/span>"" <\/span>);

    <\/span>\/* Now custom-backtest procedure follows *\/
    <\/span>if( <\/span>Status<\/span>( <\/span>"action" <\/span>) == <\/span>actionPortfolio <\/span>)
    {
        <\/span>bo <\/span>= <\/span>GetBacktesterObject<\/span>();

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

        \/\/ initialize counter
        <\/span>stoplossCountLong <\/span>= <\/span>stoplossCountShort <\/span>= <\/span>0<\/span>;

        <\/span>\/\/ iterate through closed trades
        <\/span>for( <\/span>trade <\/span>= <\/span>bo<\/span>.<\/span>GetFirstTrade<\/span>(); <\/span>trade<\/span>; <\/span>trade <\/span>= <\/span>bo<\/span>.<\/span>GetNextTrade<\/span>() )
        {

          <\/span>\/\/ check for stop-loss exit reason
            <\/span>if( <\/span>trade<\/span>.<\/span>ExitReason <\/span>== <\/span>2 <\/span>)
            {
             <\/span>\/\/ increase long or short counter respectively
                <\/span>if( <\/span>trade<\/span>.<\/span>IsLong<\/span>() )
                    <\/span>stoplossCountLong<\/span>++;
                else
                    <\/span>stoplossCountShort<\/span>++;
            }
        }

       <\/span>\/\/ add the custom metric
        <\/span>bo<\/span>.<\/span>AddCustomMetric<\/span>( <\/span>"Stoploss trades"<\/span>, <\/span>stoplossCountLong <\/span>+ <\/span>stoplossCountShort<\/span>,
                             <\/span>stoplossCountLong<\/span>, <\/span>stoplossCountShort<\/span>, <\/span>0 <\/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>() );
    <\/span>Short <\/span>= <\/span>Sell<\/span>;
    <\/span>Cover <\/span>= <\/span>Buy<\/span>;
    <\/span>ApplyStop<\/span>( <\/span>stopTypeLoss<\/span>, <\/span>stopModePercent<\/span>, <\/span>2<\/span>, <\/span>1 <\/span>)<\/code>","protected":false},"excerpt":{"rendered":"

    For the purpose of counting trades closed by particular stop we can refer to ExitReason property of the trade object in the custom backtester. The custom backtest formula presented below iterates through the list of closed trades, then counts the trades, which indicate exit reason = 2, that is stop-loss.The following values are used for […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[53,30,21],"_links":{"self":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1288"}],"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=1288"}],"version-history":[{"count":2,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1288\/revisions"}],"predecessor-version":[{"id":1290,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1288\/revisions\/1290"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=1288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=1288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=1288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}