Afl Code | Amibroker

Scanning 10,000 stocks with complex AFL can be slow. Optimize:

SetBarsRequired(200, 50); // Only 200 historical, 50 future bars

Slow code:

for(i=0; i<BarCount; i++)
    myArray[i] = MA(C, 200)[i];

Fast code:

myArray = MA(C, 200); // Vectorized

Most traders use AmiBroker for charting, but the Analysis Window has two other critical tabs: Scan and Explore.

AFL (Analysis Formula Language) is the proprietary scripting language used by Amibroker — a popular technical analysis and backtesting platform for traders and investors. AFL allows users to create custom indicators, trading systems, scans, and explorations without needing external programming tools. amibroker afl code

AFL is C-like in syntax but specifically designed for financial data arrays (price, volume, etc.), making it efficient for processing large historical datasets.


You can override AmiBroker’s core logic using SetCustomBacktestProc.

SetCustomBacktestProc("");

if (Status("action") == actionPortfolio) bo = GetBacktestObject(); bo.Backtest(); // Run standard backtest first

// Add custom metrics
stats = bo.GetPerformanceStats(0); // 0 = Long positions
maxDD = stats.GetValue("Max system % drawdown");
// Adjust position sizing based on volatility
for (sig = bo.GetFirstSignal(); sig; sig = bo.GetNextSignal())
if (sig.IsEntry() AND sig.Symbol == "SPY")
volatility = ATR(10) / C;
        posSize = 10000 / volatility; // Inverse volatility sizing
        sig.PosSize = posSize;

Unlike traditional programming languages that deal in single values, AFL breathes in arrays. Every variable is a river—a parallel timeline stretching from the first bar of data to the present. When you write Buy = Cross(RSI(14), 30);, you are not checking one moment. You are scanning the entire history of the market, finding every single instant where hope rekindled from despair.

This is the deep magic of AFL: it teaches you to think temporally. Your edge is not a static rule. It is a pattern that repeats across different seasons, different volatility regimes, different faces of the same human fear. The array is a diary of the market’s moods. Your code is a psychiatrist trying to predict the next breakdown.

// Define conditions
Buy = Cross(RSI(14), 30);           // Buy when RSI crosses above 30
Sell = Cross(70, RSI(14));          // Sell when RSI crosses below 70

// Plot signals on price chart PlotShapes(IIf(Buy, shapeUpArrow, shapeNone), colorGreen); PlotShapes(IIf(Sell, shapeDownArrow, shapeNone), colorRed); Scanning 10,000 stocks with complex AFL can be slow

And here is where the abyss opens. You write a system. You backtest. The equity curve rises like a prayer. You add parameters—length of moving average, RSI threshold, stop-loss percentage. You optimize. The curve becomes vertical. You are a god.

But AFL, in its cruel honesty, offers a function called WalkForward() or prompts you to use out-of-sample testing. Most ignore it. They fall into the curvature of overfitting, worshipping the ghost of past volatility. The deep truth: AFL does not judge your logic. It merely executes it. You can code a system that wins 99% of the time in-sample and loses everything live. The language is innocent. The trader is the fool.