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>
display the signal in custom chart title
use PlotShapes function to indicate certain buy rule
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>
<\/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>
These values will be indicated in the trade list:<\/p>
normal exit
maximum loss stop
profit target stop
trailing stop
n-bar stop
ruin stop (losing 99.96% of entry value)
reserved
reserved
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":"