Blog
Amibroker Afl Code -
// Calculate MACD macd = MACD(fastMACD, slowMACD); signal = Signal(fastMACD, slowMACD, signalMACD); hist = macd - signal;
Short = Sell; // optional shorting Cover = Buy;
ApplyStop(stopTypeLoss, stopModePercent, stopLossPct, True); ApplyStop(stopTypeProfit, stopModePercent, targetPct, True); SetPositionSize(2, spsPercentOfEquity); // 2% risk per trade SetOption("MaxOpenPositions", 5); SetOption("CommissionMode", 1); // per share SetOption("CommissionAmount", 0.01); // $0.01 per share 🧪 Complete Example: MACD + RSI Strategy // MACD parameters fastMACD = 12; slowMACD = 26; signalMACD = 9; // RSI parameters rsiPeriod = 14; rsiOB = 70; rsiOS = 30; amibroker afl code
Buy = Cross(maFast, maSlow); Sell = Cross(maSlow, maFast); Buy = Cross(maFast, maSlow); Sell = 0; // Apply stops stopLossPct = 2; targetPct = 5;
// Signals Buy = Cross(maFast, maSlow); Sell = Cross(maSlow, maFast); // Calculate MACD macd = MACD(fastMACD, slowMACD); signal
Plot(rsi, "RSI", colorBlue, styleLine); Plot(oversold, "Oversold", colorGreen, styleDashed); Plot(overbought, "Overbought", colorRed, styleDashed); Scan for RSI < 30 rsiPeriod = 14; rsi = RSI(rsiPeriod); filter = rsi < 30; AddColumn(Close, "Close", 1.2); AddColumn(rsi, "RSI", 1.2); Scan for Golden Cross (MA crossover) fastMA = 20; slowMA = 50; maFast = MA(Close, fastMA); maSlow = MA(Close, slowMA); filter = Cross(maFast, maSlow); AddColumn(Close, "Close"); AddColumn(maFast, "Fast MA"); AddColumn(maSlow, "Slow MA"); 🛠️ Advanced AFL Patterns Parameter Optimization // Use Param() for interactive optimization fast = Param("Fast MA", 10, 5, 50, 1); slow = Param("Slow MA", 30, 20, 200, 5); maFast = MA(Close, fast); maSlow = MA(Close, slow);
// Plot Plot(Close, "Price", colorBlack, styleCandle); Plot(macd, "MACD", colorRed, styleLine); Plot(signal, "Signal", colorBlue, styleLine); Plot(hist, "Histogram", colorGreen, styleHistogram); Arrays vs Scalars AFL is array-based – most
AFL (Analysis Formula Language) is the scripting language used in AmiBroker – a popular technical analysis and backtesting platform. It allows you to create custom indicators, scans, explorations, trading systems, and backtests. 🧠 Basic Syntax & Core Concepts 1. Arrays vs Scalars AFL is array-based – most operations work on entire price series.
// Entry conditions Buy = Cross(macd, signal) AND rsi < rsiOS; Sell = Cross(signal, macd) OR rsi > rsiOB;