In AFL, deep features are constructed by nesting functions, using array processing to eliminate loops, or pulling data from multiple timeframes or symbols. Stack Overflow Multiple Timeframe Integration
fdde = DDEInitiate("Excel", "Sheet1"); DDEPoke(fdde, "R1C1", "BUY"); DDEPoke(fdde, "R1C2", Symbol()); DDEPoke(fdde, "R1C3", WriteVal(C)); DDETerminate(fdde);
Functions like Ref(Close, 1) access future data. This will invalidate backtest results by making them look unrealistically profitable. Use negative values like Ref(Close, -1) to look at past data.
Clean up your visual workspace programmatically at the top of your scripts using SetChartOptions(0, chartShowDates | chartShowArrows); .
Before we dive into complex trading systems, we must understand the unique structure of AFL. Unlike traditional procedural languages like C++ or Python, AFL is fundamentally . A variable in AFL is not a single value; it is an array containing values for every bar in your selected time series. amibroker afl code
// 1. System Parameters RSI_Period = 14; BuyThreshold = 30; SellThreshold = 70; // 2. Indicator Calculation RsiVal = RSI( RSI_Period ); // 3. Entry and Exit Rules Buy = Cross( RsiVal, BuyThreshold ); // Buy when RSI crosses above 30 Sell = Cross( SellThreshold, RsiVal ); // Sell when RSI crosses below 70 Short = 0; // Long-only strategy Cover = 0; // 4. Clean up repetitive signals Buy = ExRem( Buy, Sell ); Sell = ExRem( Sell, Buy ); // 5. Position Sizing & Money Management SetPositionSize( 10, spsPercentOfEquity ); // Allocate 10% equity per trade ApplyStop( stopTypeLoss, stopModePercent, 2.0, True ); // 2% Hard Stop Loss Use code with caution. Advanced Backtest Settings:
Sell : An array indicating where long positions should close. Short : An array initiating short positions. Cover : An array closing out short positions. Complete Backtest Template with Risk Management
This is the "Hello World" of . It works, but it is not profitable. Let’s move to advanced logic.
The PlotShapes function plots labels on the chart for buy and sell signals. In AFL, deep features are constructed by nesting
// Plot signals PlotBuy(Buy, "Buy", colorGreen, styleShapeTriangleUp); PlotSell(Sell, "Sell", colorRed, styleShapeTriangleDown);
Traders use AFL to design everything from simple moving average crossovers to complex volatility-based breakout systems .
: Filter stocks using data like Earnings Per Share (EPS), debt levels, and net income.
Summary
Mastering AmiBroker AFL Code: A Comprehensive Guide to System Development
To find the best parameters for your system without manual trial and error, use the Optimize() function. In normal backtest mode, it returns a default value, but when you run an "Optimization" from the Automatic Analysis window, it iterates through all combinations.
Avoid curve-fitting. This snippet sets up WFO parameters:
// Plotting on Chart Plot(Close, "Price", colorDefault, styleCandle); Plot(FastEMA, "Fast EMA", colorRed); Plot(SlowEMA, "Slow EMA", colorBlue); Use negative values like Ref(Close, -1) to look at past data