Tracking the Range of a Time of Day in Amibroker
The high, low and range of the first hour, or any window, and its average over past days.
A common question: how do you track the price range for one time of day, like the first hour of trading, and see what it has been over the last few days?
The AFL below does it in three steps:
- Mark the bars inside the time window.
- Track the highest high and lowest low of those bars each day.
- Take one range per day and average it over a lookback.
It works on intraday bars (1-minute, 5-minute and so on), and the window can be any time of day.
The Code
WindowStart = 093000; // first bar of the window (HHMMSS)
WindowEnd = 103000; // first time after the window (HHMMSS)
LookbackDays = 5;
tn = TimeNum();
InWindow = tn >= WindowStart AND tn < WindowEnd;
WindowFirstBar = InWindow AND NOT Ref( InWindow, -1 );
WindowDone = Ref( InWindow, -1 ) AND NOT InWindow;
// Bars after the window are ignored, so the values stop changing when it ends
WindowHigh = HighestSince( WindowFirstBar, IIf( InWindow, H, -1e10 ) );
WindowLow = LowestSince( WindowFirstBar, IIf( InWindow, L, 1e10 ) );
WindowRange = WindowHigh - WindowLow;
// One value per day, taken on the first bar after the window
DayWindowRange = SparseCompress( WindowDone, WindowRange );
AvgWindowRange = ValueWhen( WindowDone, SparseExpand( WindowDone, MA( DayWindowRange, LookbackDays ) ) );
LastWindowRange = ValueWhen( WindowDone, WindowRange );
After this runs, every bar has:
WindowHighandWindowLow: the high and low of the window so far today. Once the window ends they hold steady for the rest of the day.LastWindowRange: the range of the most recent completed window.AvgWindowRange: the average window range over the lastLookbackDayscompleted windows.
How It Works
Marking the window. TimeNum() gives each bar's time as a number, so 9:30 AM is 093000 and 10:30 AM is 103000. InWindow is true for bars from 9:30 up to, but not including, 10:30. WindowFirstBar is the first of those bars each day, and WindowDone is the first bar after the window ends.
The high and low of the window. HighestSince starts over on WindowFirstBar each day. Bars outside the window count as -1e10 (a very low number), so they never set a new high, and the value stops changing once the window ends. LowestSince does the same for the low with a very high number.
One value per day. SparseCompress keeps only the WindowDone bars, so DayWindowRange has one value per day: that day's completed range. MA( DayWindowRange, LookbackDays ) is then an average over days rather than over bars. SparseExpand puts the result back on the intraday bars, and ValueWhen carries it forward to every bar until the next day's window ends.
Changing the Window and the Lookback
Change WindowStart and WindowEnd for any other window. For example, 100000 and 110000 for 10:00 to 11:00, or 093000 and 094500 for the first 15 minutes. The window has to start and end on the same day.
Pick a bar interval that divides the window evenly. A 9:30 to 9:45 window works on 1-minute, 5-minute and 15-minute bars, but not on 10-minute bars.
Change LookbackDays for a longer or shorter lookback. To get something other than the average, swap MA for another function that takes an array and a period:
HHV( DayWindowRange, LookbackDays ): the largest window range in the lookbackLLV( DayWindowRange, LookbackDays ): the smallestMedian( DayWindowRange, LookbackDays ): the median, which a single wild day moves less than an average
When the Value Is Known
Today's window range isn't known until the window ends. Until then, AvgWindowRange and LastWindowRange still hold the values from the previous days, so an entry at 10:00 sees the average of the last 5 completed first hours, not a number that includes today's.
From the first bar after the window, both include today. That's correct for an entry after 10:30, but keep it in mind if you change the code. Anything that reads today's WindowHigh or WindowLow before the window ends is reading a range that's still forming. See Look-Ahead Bias in Backtesting for more on this.
Adding It to Your Backtest
To have the Strategy Cruncher test these values, add them as setup columns. Adding Setup Columns to Your Amibroker Backtest walks through the three places each column goes.
A dollar range means different things on a $5 stock and a $500 stock, so the Cruncher can compare trades better when the range is a percent of price. In the entry logic that would look like:
trd_avg_window_range_pct = 100 * AvgWindowRange[SignalBarIndex] / C[SignalBarIndex];
Checking the Values
Before you rely on the values, run them as an exploration on one symbol for a few days and look at the numbers bar by bar:
Filter = 1;
AddColumn( InWindow, "InWindow", 1.0 );
AddColumn( WindowHigh, "WindowHigh", 1.2 );
AddColumn( WindowLow, "WindowLow", 1.2 );
AddColumn( LastWindowRange, "LastWindowRange", 1.2 );
AddColumn( AvgWindowRange, "AvgWindowRange", 1.2 );
Check a day or two by hand against a chart: the high and low of the window bars should match WindowHigh and WindowLow from the end of the window onward.