amibroker

HomeKnowledge Base

How to identify signal that triggered entry/exit if multiple signals are used

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 by one of three independent rules:

Buy1 CrossMACD(), Signal() );
Buy2 CrossCloseMA(Close50) );
Buy3 CrossRSI(), 30 );
//
Buy buy1 OR Buy2 OR Buy3

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

In case a custom chart is used, we can do the following:

  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.

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:

Buy1 CrossMACD(), Signal() );
Buy2 CrossCloseMA(Close,50) );
Buy3 CrossRSI(), 30 );
//
Buy buy1 OR Buy2 OR Buy3;
//
// Standard price plot
PlotClose"Close"colorBlackstyleCandle);
//
// Custom title definition
BuyReason EncodeColor(colorGreen ) + WriteIf(Buy,"Buy signals: ","")
           + 
WriteIf(buy1"Buy1 """) +WriteIf(buy2"Buy2""")
           + 
WriteIf(buy3"Buy3""");
Title StrFormat"{{NAME}} - {{INTERVAL}} {{DATE}} Close %g ",Close ) +BuyReason;
//
// Plotshapes function calls
PlotShapes(Buy*shapeUpArrowcolorGreen0Low);
PlotShapes(Buy1*shapedigit1colorGreen0Low,-30);
PlotShapes(Buy2*shapedigit2colorGreen0Low,-45);
PlotShapes(Buy3*shapedigit3colorGreen0Low,-60);
//
//
// Custom text labels displayed with PlotText
if( SelectedValue(Buy) )
{
   
SelectedValueBarIndex() );
   
maxy Status("axismaxy");
   
miny Status("axisminy");
   
0.15 * (maxy miny) + miny;
   
text WriteIf(buy1], "\nBuy1 """)
          +  
WriteIf(buy2], "\nBuy2 """)
          +  
WriteIf(buy3], "\nBuy3 """);
   
PlotTexttexti,  ycolorWhitecolorGreen );

The chart below shows how to use signal visualization technique implemented in the formula.

Chart Example 1

The other method is to use the Exploration feature of Analysis 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

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:

Filter Buy;
AddColumnBuy1"Buy1"1colorDefaultIIfBuy1colorGreencolorDefault ) );
AddColumnBuy2"Buy2"1colorDefaultIIfBuy2colorGreencolorDefault ) );
AddColumnBuy3"Buy3"1colorDefaultIIfBuy3colorGreencolorDefault ) )

Exploration Signal tracking

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 variable.

Sell1 CrossSignal(), MACD() );
sell2 CrossMA(Close50), Close );
Sell Sell1 10 Sell2 20

The above expression will result in assigning value of 10 to Sell 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.

These values will be indicated in the trade list:

Backtest exit signal tracking

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:

  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

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.

This is further discussed here: http://www.amibroker.com/guide/afl/equity.html

Comments are closed.