The Plot() function is the cornerstone of visual indicators:
The Ultimate Guide to AmiBroker AFL Code: Architecture, Syntax, and Advanced Optimization
Avoid curve-fitting. This snippet sets up WFO parameters:
if (Sell AND currentPos == 1)
Explain the for trading. Let me know what you'd like to explore next!
// --- Walk Forward Settings --- OptimizeInSample = Param("In Sample Years", 5, 1, 20, 1); OptimizeStep = Param("Step Years", 1, 1, 5, 1); SetOption("Optimization", "WalkForward"); SetOption("OptimizationInSample", OptimizeInSample * 252); // Trading days SetOption("OptimizationStep", OptimizeStep * 252);
Control backtester behavior programmatically:
Filtering thousands of stocks to find those matching specific criteria.
// Optimize the Fast EMA from 5 to 30 steps of 1, and Slow EMA from 20 to 100 steps of 5 FastPeriod = Optimize( "Fast Period", 12, 5, 30, 1 ); SlowPeriod = Optimize( "Slow Period", 26, 20, 100, 5 ); Use code with caution.
AFL is case-insensitive. close , CLOSE , and Close mean the same thing.
The PlotShapes() function places visual signals directly on charts:
Represented as Null , indicating missing or undefined data for a specific bar (often seen at the beginning of indicator plots before enough data exists to calculate a moving average).
: Built-in function that detects the exact moment Array1 crosses above Array2 . Plot() : Renders lines, candles, or bars onto your screen.
The official AmiBroker Knowledge Base remains the most authoritative learning source. Experienced users recommend studying each archived article carefully, then rewriting formulas from memory without looking back—a method that significantly reinforces understanding.
// --- Exploration for Equity Curve Analysis --- SetBarsRequired(500, 0); Equity = Foreign("~~~EQUITY", "C"); // Internal equity array MonthlyReturn = (Equity - Ref(Equity, -20)) / Ref(Equity, -20) * 100; Filter = 1; AddColumn(MonthlyReturn, "20-Period Return %", 2.2); AddColumn(Stdev(MonthlyReturn, 20), "Volatility", 2.2);
Disclaimer: Trading involves financial risk. Past backtest performance does not guarantee future results. Always verify AFL code in a paper trading environment before live execution.
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.