Skip to content

Real-Time AFL

Up to this point every AFL variable you have written has been an array: one value for every bar, computed from data that had already been stored. This part introduces the handful of functions that do not work that way at all — functions that report a single number describing this instant, functions that decide how often your formula runs, and functions that tell your formula which part of AmiBroker is currently asking it for an answer.

That is a small vocabulary. GetRTData(), GetRTDataForeign(), RequestTimedRefresh() and Status() are most of it. The difficulty is not the syntax. It is that these functions break assumptions the rest of AFL let you keep: that a value has a history, that a formula runs once, that the answer is the same no matter who asked.

Four pages. The first is the field list and the contract around it — what GetRTData() returns, what it returns when nothing is streaming, and why the answer is one number and not an array. The second is Status(), which is how one formula behaves sensibly in a chart, a scan, an Exploration and a backtest without pretending they are the same thing. The third is RequestTimedRefresh() and the cost of asking a formula to run itself every second. The fourth is a project that puts all three together into a quote panel.

GetRTData() and GetRTDataForeign() are documented as Professional-edition functions that work only when the database is fed by a real-time data plugin. On any other setup they report nothing. That is not a problem this course can code around, and it would be dishonest to pretend otherwise.

It is also not a reason to skip the part. Two of the four pages need no feed at all: Status() works identically on an end-of-day database, and RequestTimedRefresh() is documented to refresh a pane “regardless of data source used or connection state”. The project ships an offline mode that is a designed feature rather than a fallback — it is how anyone without a subscription runs the dashboard, and it labels itself as not live in larger type than anything else on the panel.

The thing you cannot get without a feed is live bid and ask. The thing you can get, and which matters more for the quality of your code, is the discipline of writing formulas that behave correctly when a value is absent. Most of the real-time formulas circulating on forums fail precisely there: they call Nz() on a missing quote, turn it into a zero, and compute a spread of several thousand per cent without complaining.

Part 17 for the architecture, Part 21 for the difference between the feed updating and the chart refreshing, and Part 22 for the Real-Time Quote window — which is the only documented way to check whether your vendor supplies a given field before you rely on it in AFL. From the programming side you need Parts 8 to 11: arrays, IIf(), user functions and static variables all appear here without further explanation.

Alerts are deliberately not in this part. AlertIf(), duplicate suppression and the alert state machine are Part 25, and the two subjects tangle badly if taught together.

Read the four pages in order; each one is used by the next. Run the field probe from the first lesson even if you know it will come back empty — an empty result is the contract you are coding against, and seeing it once is worth more than reading about it. Build the project panel in offline mode first, whatever your subscription status, because a panel that only works when the market is open is a panel you cannot debug.

Part23 of 36Level5 — Real-Time AmiBroker UserPages4Estimated time2.4 hours

2 of 4 pages in this part need the Professional edition or a live data feed, marked below. Every one of them has a Level A path that uses Bar Replay and historical data instead, so the part can be completed in full without a subscription.

0 / 4 lessons in this part completed