Skip to content

Working with Arrays and Historical Bars

Part 8 established the one idea AFL is built on: a name like Close holds one value for every bar, and an expression written once is evaluated once per bar. That idea is enough to compare two arrays. It is not enough to ask a question about time — what the price was ten bars ago, how long it has been since a condition last held, what the highest high of the last fifty bars was, or which bar a run of true values started on.

This part supplies the vocabulary for those questions. Seven small functions do almost all of the work: Ref, HHV, LLV, BarsSince, Sum, ValueWhen and Cross. Three more — ExRem, ExRemSpan and Flip — decide what happens to the signals they produce. Every screening formula, every indicator and every trading system in the remaining twenty-eight parts is assembled out of these.

Two of the seven lessons are about functions. The other five are, in one way or another, about a single idea that has no function attached to it at all:

  • A state is a condition that is true over a span of bars. Close > MA(Close, 50) is a state. On a ten-year daily chart it may be true on fourteen hundred bars.
  • An event is the bar on which something became true. Cross(Close, MA(Close, 50)) is an event. Over the same ten years it may be true on forty-six bars.

Both are Boolean arrays. Both have the same length. Both can be assigned to Buy. They mean completely different things, and swapping one for the other produces a formula that compiles cleanly, runs without complaint, and answers a question nobody asked. A state used where an event was wanted produces a signal on every bar of a long run. An event used where a state was wanted produces a filter that is off on 99% of the bars you meant it to be on.

No error message will tell you which mistake you have made. The only defences are knowing the distinction, and having a habit of measuring your arrays before trusting them. Both are built here.

By the end of this part you should be able to:

  • shift any array backwards or forwards in time, and explain from the sign alone whether a given Ref() call is legitimate or is reading data that did not exist yet;
  • express “the highest high of the previous twenty bars” without accidentally including the current bar in the window;
  • count how often a condition occurs, how long ago it last occurred, and how much of the record it covers — and know what each of those numbers does when the condition has never occurred at all;
  • capture the value of anything at the moment an event happened and carry it forward for as long as you need it;
  • look at a Boolean array and say, from its shape, whether it is a state or a set of events;
  • convert deliberately in either direction, and say what information each conversion throws away;
  • diagnose a formula that produces too many signals, too few, or none, without guessing.

Part 8 in full, particularly the array model, Boolean arrays and the lesson on Null and warm-up periods. This part assumes you can already read Close > MA(Close, 50) as an array of ones and zeros rather than as a question with one answer, and that an empty value at the left edge of a chart does not surprise you.

You need AmiBroker installed with any database that has a few years of daily history — the free end-of-day sources from Part 3 are more than enough. Nothing here needs the Professional edition, real-time data or a paid subscription. Most of the example formulas are Explorations rather than charts, because the fastest way to settle an argument about what an array contains is to put its values on screen, one row per bar, and look at them.

The first four lessons are tools, and they are worth working through in order because each one appears inside the next. The fifth lesson is the conceptual centre of the part and probably of the whole AFL half of the course; if you read only one page here, read that one. The sixth turns the distinction into working practice, including an honest account of when the usual fix is the wrong fix. The seventh gives you six formulas that are wrong, with realistic symptoms, and asks you to say why before you look.

One habit to start now, and keep for the rest of the course: when a formula surprises you, do not reason about what it should contain. Put the arrays in an Exploration, one column each, and read what they actually contain. Almost every diagnosis in this part reduces to counting true bars.

Part9 of 36Level3 — AFL DeveloperPages7Estimated time3.4 hours

0 / 7 lessons in this part completed