Adding Setup Columns to Your Amibroker Backtest
Keeping strategy-specific columns alongside the MabeKit column library.
A backtest you send to the Strategy Cruncher has two sets of columns:
- The column library: the same set of columns added to every backtest. For most people this is the MabeKit Column Library, plus any shared columns of your own.
- Setup columns: a smaller set of columns that describe one strategy's signal.
Setup columns describe the signal itself, so they don't belong in a column library. They only make sense for that one strategy and its entry method.
I wrote more about the idea in Using Setup Columns in a Backtest.
An Example
Say the entry is a hammer candle after a downtrend, entering at the open of the next candle. Some possible setup columns are:
- The height of the hammer bar in ATR
- The hammer bar's volume as a ratio of average daily volume
- The range of the downtrend in ATR
- The change over the downtrend in ATR
- The height of the hammer relative to the downtrend
None of these mean anything for a strategy without a hammer, which is why they live in the strategy rather than the library.
It's fine if a setup column overlaps with a column in your library. The Strategy Cruncher finds the most predictive columns and rules, so the overlap doesn't cause problems.
Choosing Setup Columns
- Look at charts of the setup.
- Ask what properties of a setup might make it more or less likely to work.
- Look for properties that make trades of the same setup behave differently.
- Check whether your column library already covers each property.
- If it doesn't, add a setup column so the Cruncher has one more variable to test.
Where Setup Columns Go in the AFL
The add_metrics.py script from the column library fills in three areas of your strategy, each between a pair of comment markers:
// START_SIGNAL_AREAand// END_SIGNAL_AREA// START_METRIC_AREAand// END_METRIC_AREA// START_BACKTEST_METRIC_AREAand// END_BACKTEST_METRIC_AREA
Every time it runs, the script replaces everything between each pair of markers. Anything outside the markers is left alone.
Put your setup columns just above each START_ marker. They stay in your strategy file, and you can rerun add_metrics.py whenever the library changes without losing them.
The demo.afl file in the column library download already does this with three setup columns: Gap Percent, Yest Dojiness and Volume Today. The example below adds two more, the stop distance as a percent of the entry price and the minutes since the open at entry.
Each setup column needs a line in all three areas.
1. Save the value when the trade is entered
In the entry logic, store the value in a trd_ variable above // START_SIGNAL_AREA:
// define variables for metrics
trd_hod = HOD[SignalBarIndex];
trd_lod = LOD[SignalBarIndex];
trd_volume_today = VolumeToday[SignalBarIndex];
trd_stop_distance_pct = 100 * initial_stop_distance / BuyPrice[i];
trd_minutes_since_open = MinutesSinceOpen[i];
// START_SIGNAL_AREA
// END_SIGNAL_AREA
2. Add the value to the metric string when the trade exits
In the exit logic, append the values to metric_value above // START_METRIC_AREA:
metric_value = exit_reason
+ "," + TodayGap[i] + "," + YesterdayDojiness[i] + "," + trd_volume_today
+ "," + trd_stop_distance_pct + "," + trd_minutes_since_open
// START_METRIC_AREA
// END_METRIC_AREA
;
3. Add the column to the trade list
In the custom backtest code, read each value back out of the string and add it as a column above // START_BACKTEST_METRIC_AREA:
trade_metric_value = StaticVarGetText( "Ex" + trade.Symbol + DateTimeToStr( trade.ExitDateTime ) );
exit_reason = StrExtract( trade_metric_value, 0 );
trade.AddCustomMetric( "Exit Reason", exit_reason );
gap_percent = StrToNum( StrExtract( trade_metric_value, 1 ) );
trade.AddCustomMetric( "Gap Percent", gap_percent );
yesterday_dojiness = StrToNum( StrExtract( trade_metric_value, 2 ) );
trade.AddCustomMetric( "Yest Dojiness", yesterday_dojiness );
today_volume = StrToNum( StrExtract( trade_metric_value, 3 ) );
trade.AddCustomMetric( "Volume Today", today_volume, 0 );
stop_distance_pct = StrToNum( StrExtract( trade_metric_value, 4 ) );
trade.AddCustomMetric( "Stop Distance Pct", stop_distance_pct );
minutes_since_open = StrToNum( StrExtract( trade_metric_value, 5 ) );
trade.AddCustomMetric( "Entry Minutes Since Open", minutes_since_open, 0 );
// START_BACKTEST_METRIC_AREA
// END_BACKTEST_METRIC_AREA
The number in each StrExtract is the value's position in metric_value, counting from 0. Exit Reason is 0, Gap Percent is 1, and so on. Add the setup columns in the same order in steps 2 and 3, and number them in order without skipping any.
The last argument to AddCustomMetric is the number of decimal places to show. Leave it off to get 2.
4. Run add_metrics.py
Run the script as usual:
python scripts\add_metrics.py --target-file my_strategy.afl
The script finds the last StrExtract number above // START_BACKTEST_METRIC_AREA and numbers the library columns after it. In this example the setup columns end at 5, so the library starts at 6:
minutes_since_open = StrToNum( StrExtract( trade_metric_value, 5 ) );
trade.AddCustomMetric( "Entry Minutes Since Open", minutes_since_open, 0 );
// START_BACKTEST_METRIC_AREA
metric_MK_Price = StrToNum( StrExtract( trade_metric_value, 6 ) );
trade.AddCustomMetric( "MK_Price", metric_MK_Price, 2 );
If you add a setup column later, add its three lines above the markers and run add_metrics.py again. The library columns are renumbered to make room for it.
Don't pass --start-index when your strategy has setup columns. It overrides this numbering, and the library columns can end up reading the wrong values.
Setup Columns or Your Own Library Columns?
The column library page also shows how to keep your own columns in a separate file like my_columns.afl and add them with --column-file and --also-prefix.
- If a column makes sense for any strategy, put it in your own column file so every backtest gets it.
- If a column only makes sense for this strategy's signal, make it a setup column above the markers.