{"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> 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> 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><\/code>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>