Introduction: Why AFL is the Backbone of Quantitative Trading In the world of retail algorithmic trading, few platforms offer the perfect blend of power, speed, and customization like AmiBroker . For over two decades, professional traders and hobbyists alike have relied on AmiBroker for backtesting, scanning, and real-time trading. The secret sauce behind this dominance is AFL (AmiBroker Formula Language) .
// 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; } } } Avoid curve-fitting. This snippet sets up WFO parameters: amibroker afl code
RSI_14 = RSI(14); VolumeSurge = V > MA(V, 50) * 1.5; Filter = RSI_14 < 30 AND VolumeSurge; AddColumn(C, "Close", 1.2); AddColumn(RSI_14, "RSI", 1.2); AddColumn(V, "Volume", 1.0); Run this on 5,000 stocks. AmiBroker will return a list of only those meeting the criteria. Exploration allows you to output historical values into a grid. Introduction: Why AFL is the Backbone of Quantitative
// --- Entry Conditions --- BuySignal = C < BBLower AND C > TrendMA; // Price below lower band but above 200 MA Buy = ExRem(BuySignal, SellSignal); // Remove consecutive buy signals // Adjust position sizing based on volatility for (sig = bo
// --- 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); AmiBroker is not just for EOD (End of Day) trading. It supports real-time feeds via Plugin (e.g., IB, eSignal, Forex). 5.1 Real-Time AFL Logic To run code every second, use StaticVar and StaticVarGet to preserve variables between bars.
// --- Multi-Timeframe (Requires both charts open) --- TimeFrameSet(inHourly); // Switch to hourly HourlyTrend = MA(C, 50) > Ref(MA(C, 50), -1); TimeFrameRestore(); // Expand the hourly signal to 5-minute bars HourlyTrendExp = TimeFrameExpand(HourlyTrend, inHourly, expandFirst);
This article will serve as your encyclopedic guide. We will cover the syntax, logic, advanced scanning, portfolio backtesting, and real-time trading integration. Before writing sophisticated strategies, you must understand how AFL thinks. 1.1 Vector Processing vs. Iterative Looping Unlike Python or C++, AFL is inherently vector-based . This means an operation applies to the entire price array simultaneously.