Skip to content

AFL Programming Fundamentals

Up to this point the course has asked you to look at things. From here it asks you to describe them precisely enough that a machine can carry them out over ten thousand bars without you watching. That is what programming is for in this subject: not cleverness, but the elimination of the small inconsistencies that creep into any judgement repeated by hand a few hundred times.

This is the part where most people who abandon AmiBroker abandon it, and almost always for the same reason. They arrive expecting a programming language that works like the ones they have heard about — do this, then do that, one step at a time — and AFL is not built that way. A single line of AFL does not calculate a value. It calculates a value for every bar on the chart, all at once, and hands you back the whole column of results. Until that idea lands, every formula behaves inexplicably. After it lands, most of AFL is small details.

What one line of AFL actually does

Three lines of code, three whole columns of numbers. The values are from the worked example in AmiBroker's own Understanding how AFL works chapter.
Bar012345
Close1.231.261.241.281.251.25
MA( Close, 3 )NullNull1.2431.2601.2571.260
Close > MA( Close, 3 )NullNull0100
Three lines of code, three whole columns of numbers. The values are from the worked example in AmiBroker's own Understanding how AFL works chapter. Prices in this diagram are invented for the illustration. They are not market data and nothing should be inferred from them.

Every remaining part of this course is downstream of what happens here. Parts 9 to 11 extend the array vocabulary and turn it into reusable code. Parts 12 to 16 run that code across hundreds of symbols. Parts 27 to 35 use it to test whether an idea survives contact with evidence. If your model of what a formula does is wrong, all of that inherits the error, and the symptom will not be an error message — it will be a backtest that looks plausible and means nothing.

So this part moves slowly, and it repeats itself deliberately. The array model appears in the fifth lesson with its own diagram-heavy treatment, then again in the sixth as Boolean arrays, again in the seventh as element-wise selection, and again in the eighth as the empty values that spread through everything. That is not padding. It is the same idea approached from four directions, because one pass is rarely enough.

By the end of the nine lessons you should be able to:

  • open the Formula Editor, write a formula, check its syntax, apply it to a chart, and know where AmiBroker put the file;
  • read any line of AFL and say what shape its result has — a single number, or one value per bar;
  • explain, to someone else, why Close > MA( Close, 50 ) is not a yes-or-no answer;
  • combine conditions with AND, OR and NOT and get the grouping right the first time;
  • choose a value per bar with IIf() without reaching for a loop, and recognise the rare cases where a loop is genuinely the right tool;
  • find the empty warm-up values at the start of every indicator, predict what they will do to whatever you build on top, and deal with them on purpose;
  • read an AmiBroker error message as information rather than as an insult, and debug a misbehaving formula by narrowing it down instead of guessing.

AmiBroker itself, with the database you built in Part 3, and nothing else. Everything in this part runs on end-of-day data in the Standard edition; no lesson here needs a live feed, a data subscription or the Professional edition.

Work with the software open. Every formula in this part is short enough to type, and typing it is not a waste of time — the errors you make while typing are the errors the last lesson teaches you to read. The complete formulas are also downloadable, but use the download to check yourself, not to skip the typing.

The first two lessons get you running code as fast as honestly possible: what AFL is and where it runs, then a complete working formula on a chart before any theory. Lessons three and four cover the mechanical rules — semicolons, comments, names, assignment, arithmetic, precedence — quickly, because they are simple and because nothing interesting can be said until they are out of the way.

Lesson five is the centre of the part and of the course. Lessons six, seven and eight are its consequences: comparisons produce arrays, logic combines arrays, and missing data propagates through arrays. Lesson nine is what to do when the result is not what you expected, which will be often, and which is normal.

Part8 of 36Level3 — AFL DeveloperPages9Estimated time4.2 hours

0 / 9 lessons in this part completed

Sources for this lesson

2 verified · checked 2026-08-31

  1. 01AmiBroker User's Guide — Understanding how AFL worksamibroker.com/guide/h_understandafl.html2026-08-31
  2. 02AmiBroker User's Guide — AFL Reference Manualamibroker.com/guide/a_language.html2026-08-31

Every technical claim on this page was checked against the official AmiBroker documentation on the date shown. Where the course disagrees with folklore, the source is how you can tell which one to trust.