{"id":362,"date":"2014-09-24T12:30:13","date_gmt":"2014-09-24T17:30:13","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/?p=362"},"modified":"2014-12-04T08:21:17","modified_gmt":"2014-12-04T13:21:17","slug":"how-to-identify-which-signal-triggers","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2014\/09\/24\/how-to-identify-which-signal-triggers\/","title":{"rendered":"How to identify signal that triggered entry\/exit if multiple signals are used"},"content":{"rendered":"

When designing a trading system we often need to quickly identify which of the rules used in the code triggered the particular Buy or Sell signal. Here are some techniques that may be useful in such identification.<\/p>

For the purpose of this demonstration let us use a sample formula, where the Buy<\/strong> signal may be triggered by one of three independent rules:<\/p>Buy1 <\/span>= <\/span>Cross<\/span>( <\/span>MACD<\/span>(), <\/span>Signal<\/span>() );
<\/span>Buy2 <\/span>= <\/span>Cross<\/span>( <\/span>Close<\/span>, <\/span>MA<\/span>(<\/span>Close<\/span>, <\/span>50<\/span>) );
<\/span>Buy3 <\/span>= <\/span>Cross<\/span>( <\/span>RSI<\/span>(), <\/span>30 <\/span>);
<\/span>\/\/
<\/span>Buy <\/span>= <\/span>buy1 <\/span>OR <\/span>Buy2 <\/span>OR <\/span>Buy3<\/span><\/code>

To determine which of those three rules generates the entry signal, we can either visualize signals in the chart or use Exploration<\/strong> feature of the Analysis<\/strong> window.<\/p>

In case a custom chart is used, we can do the following:<\/p>

  1. display the signal in custom chart title
  2. use PlotShapes function to indicate certain buy rule
  3. use PlotText to add pre-defined text labels.<\/ol>

    The formula below shows sample implementations of these three techniques. This is actually one of many ways that can be used for coding such custom output:<\/p>Buy1 <\/span>= <\/span>Cross<\/span>( <\/span>MACD<\/span>(), <\/span>Signal<\/span>() );
    <\/span>Buy2 <\/span>= <\/span>Cross<\/span>( <\/span>Close<\/span>, <\/span>MA<\/span>(<\/span>Close<\/span>,<\/span>50<\/span>) );
    <\/span>Buy3 <\/span>= <\/span>Cross<\/span>( <\/span>RSI<\/span>(), <\/span>30 <\/span>);
    <\/span>\/\/
    <\/span>Buy <\/span>= <\/span>buy1 <\/span>OR <\/span>Buy2 <\/span>OR <\/span>Buy3<\/span>;
    <\/span>\/\/
    \/\/ Standard price plot
    <\/span>Plot<\/span>( <\/span>Close<\/span>, <\/span>"Close"<\/span>, <\/span>colorBlack<\/span>, <\/span>styleCandle<\/span>);
    <\/span>\/\/
    \/\/ Custom title definition
    <\/span>BuyReason <\/span>= <\/span>EncodeColor<\/span>(<\/span>colorGreen <\/span>) + <\/span>WriteIf<\/span>(<\/span>Buy<\/span>,<\/span>"Buy signals: "<\/span>,<\/span>""<\/span>)
               + <\/span>WriteIf<\/span>(<\/span>buy1<\/span>, <\/span>"Buy1 "<\/span>, <\/span>""<\/span>) +<\/span>WriteIf<\/span>(<\/span>buy2<\/span>, <\/span>"Buy2"<\/span>, <\/span>""<\/span>)
               + <\/span>WriteIf<\/span>(<\/span>buy3<\/span>, <\/span>"Buy3"<\/span>, <\/span>""<\/span>);
    <\/span>Title <\/span>= <\/span>StrFormat<\/span>( <\/span>"{{NAME}} - {{INTERVAL}} {{DATE}} Close %g "<\/span>,<\/span>Close <\/span>) +<\/span>BuyReason<\/span>;
    <\/span>\/\/
    \/\/ Plotshapes function calls
    <\/span>PlotShapes<\/span>(<\/span>Buy<\/span>*<\/span>shapeUpArrow<\/span>, <\/span>colorGreen<\/span>, <\/span>0<\/span>, <\/span>Low<\/span>);
    <\/span>PlotShapes<\/span>(<\/span>Buy1<\/span>*<\/span>shapedigit1<\/span>, <\/span>colorGreen<\/span>, <\/span>0<\/span>, <\/span>Low<\/span>,-<\/span>30<\/span>);
    <\/span>PlotShapes<\/span>(<\/span>Buy2<\/span>*<\/span>shapedigit2<\/span>, <\/span>colorGreen<\/span>, <\/span>0<\/span>, <\/span>Low<\/span>,-<\/span>45<\/span>);
    <\/span>PlotShapes<\/span>(<\/span>Buy3<\/span>*<\/span>shapedigit3<\/span>, <\/span>colorGreen<\/span>, <\/span>0<\/span>, <\/span>Low<\/span>,-<\/span>60<\/span>);
    <\/span>\/\/
    \/\/
    \/\/ Custom text labels displayed with PlotText
    <\/span>if( <\/span>SelectedValue<\/span>(<\/span>Buy<\/span>) )
    {
       <\/span>i <\/span>= <\/span>SelectedValue<\/span>( <\/span>BarIndex<\/span>() );
       <\/span>maxy <\/span>= <\/span>Status<\/span>(<\/span>"axismaxy"<\/span>);
       <\/span>miny <\/span>= <\/span>Status<\/span>(<\/span>"axisminy"<\/span>);
       <\/span>y <\/span>= <\/span>0.15 <\/span>* (<\/span>maxy <\/span>- <\/span>miny<\/span>) + <\/span>miny<\/span>;
       <\/span>text <\/span>= <\/span>WriteIf<\/span>(<\/span>buy1<\/span>[ <\/span>i <\/span>], <\/span>"\\nBuy1 "<\/span>, <\/span>""<\/span>)
              +  <\/span>WriteIf<\/span>(<\/span>buy2<\/span>[ <\/span>i <\/span>], <\/span>"\\nBuy2 "<\/span>, <\/span>""<\/span>)
              +  <\/span>WriteIf<\/span>(<\/span>buy3<\/span>[ <\/span>i <\/span>], <\/span>"\\nBuy3 "<\/span>, <\/span>""<\/span>);
       <\/span>PlotText<\/span>( <\/span>text<\/span>, <\/span>i<\/span>,  <\/span>y<\/span>, <\/span>colorWhite<\/span>, <\/span>colorGreen <\/span>);
    <\/code>

    The chart below shows how to use signal visualization technique implemented in the formula.<\/p>

    \"Chart<\/p>

    The other method is to use the Exploration<\/strong> feature of Analysis<\/strong> window that allows to generate tabular output, where we can display the values of selected variables. The detailed tutorial explaining this feature is available at:
    http:\/\/www.amibroker.com\/guide\/h_exploration.html<\/a><\/p>

    For the discussed purpose of tracking the signals that triggered entry or exit, we can add the following code to our trading system to show the values of each Buy1, Buy2, Buy3 variables: <\/p>Filter <\/span>= <\/span>Buy<\/span>;
    <\/span>AddColumn<\/span>( <\/span>Buy1<\/span>, <\/span>"Buy1"<\/span>, <\/span>1<\/span>, <\/span>colorDefault<\/span>, <\/span>IIf<\/span>( <\/span>Buy1<\/span>, <\/span>colorGreen<\/span>, <\/span>colorDefault <\/span>) );
    <\/span>AddColumn<\/span>( <\/span>Buy2<\/span>, <\/span>"Buy2"<\/span>, <\/span>1<\/span>, <\/span>colorDefault<\/span>, <\/span>IIf<\/span>( <\/span>Buy2<\/span>, <\/span>colorGreen<\/span>, <\/span>colorDefault <\/span>) );
    <\/span>AddColumn<\/span>( <\/span>Buy3<\/span>, <\/span>"Buy3"<\/span>, <\/span>1<\/span>, <\/span>colorDefault<\/span>, <\/span>IIf<\/span>( <\/span>Buy3<\/span>, <\/span>colorGreen<\/span>, <\/span>colorDefault <\/span>) )<\/code>

    \"Exploration<\/p>

    With regard to exit signals they can be visualized in a similar way as shown above, but there is also an additional functionality in the backtester, which allows to indicate the exit condition directly in the trade list. This can be done by assigning values higher than 1 (but not more than 127) to Sell<\/strong> variable.<\/p>Sell1 <\/span>= <\/span>Cross<\/span>( <\/span>Signal<\/span>(), <\/span>MACD<\/span>() );
    <\/span>sell2 <\/span>= <\/span>Cross<\/span>( <\/span>MA<\/span>(<\/span>Close<\/span>, <\/span>50<\/span>), <\/span>Close <\/span>);
    <\/span>Sell <\/span>= <\/span>Sell1 <\/span>* <\/span>10 <\/span>+ <\/span>Sell2 <\/span>* <\/span>20<\/span><\/code>

    The above expression will result in assigning value of 10 to Sell<\/strong> variable for the bars where Sell1 is true, 20 for the bars where Sell2 is true and 30 for the bars where both conditions are true.<\/p>

    These values will be indicated in the trade list:<\/p>

    \"Backtest<\/p>

    It is worth to mention that values 1 to 9 are reserved for built-in stops and used internally by the backtester, and have special meaning:<\/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)
    7. reserved
    8. reserved
    9. reserved<\/ol>

      Note also that you must not assign value greater than 127 to Sell or Cover variable. If you assign bigger value it will be truncated.<\/p>

      This is further discussed here: http:\/\/www.amibroker.com\/guide\/afl\/equity.html<\/a><\/p>","protected":false},"excerpt":{"rendered":"

      When designing a trading system we often need to quickly identify which of the rules used in the code triggered the particular Buy or Sell signal. Here are some techniques that may be useful in such identification.For the purpose of this demonstration let us use a sample formula, where the Buy signal may be triggered […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[53,14,15],"_links":{"self":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/362"}],"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=362"}],"version-history":[{"count":1,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/362\/revisions"}],"predecessor-version":[{"id":817,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/362\/revisions\/817"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}