Riddle is the Best Alternative to Typeform in 2026
Typeform is great for forms and surveys - but it wasn't built for audience engagement…
Read moreUnlocking Trading Strategies with AmiBroker Formula Language (AFL)
AmiBroker Formula Language (AFL) is a high-performance programming language used to build custom indicators, scan for market opportunities, and backtest complex trading systems within the AmiBroker platform. Designed with a syntax similar to C and JScript, AFL is optimized for speed by minimizing the need for manual loops, making it an essential tool for quantitative traders. Core Functions of AFL Code
Traders use AFL to automate almost every aspect of their technical analysis:
Amibroker – 20 Essential Things You Should Know Before You Start amibroker afl code
Since you asked for an AFL code to write a blog post, the most interesting approach is to create a tool for Amibroker users. There is no native "Export to WordPress" function in Amibroker, so writing a script that generates a formatted HTML blog post based on your chart analysis is a powerful utility.
Here is the AFL code. This script performs a technical analysis of your chart and automatically generates HTML code for a blog post (including the chart image) that you can paste directly into your website.
Ref() FunctionUnlike Excel, AFL uses Ref() to look back or forward.
Example: Buy when today’s volume is double yesterday’s volume.
Buy = V > 2 * Ref(V, -1); This example adds an RSI condition to only
Most traders use AmiBroker for charting, but the Analysis Window has two other critical tabs: Scan and Explore.
Let's move beyond the basics. Below is a complete Mean Reversion + Trend Filter system.
RSIperiod = 14;
Cond = RSI(RSIperiod) > 70;
Filter = Cond;
AddColumn(C, "Close");
AddColumn(RSI(RSIperiod), "RSI");
A standard AFL script usually consists of three main sections: Parameters, Calculations, and Plotting/Execution. to simulate next-bar execution.
The most powerful feature of AFL is its ability to define trading systems for backtesting. This relies on specific array logic.
Here's a more complex example that includes an RSI filter:
// Parameters
ShortPeriod = Param("Short Period", 10, 2, 100, 1);
LongPeriod = Param("Long Period", 30, 2, 100, 1);
RSILevel = Param("RSI Level", 70, 1, 100, 1);
// Calculate Indicators
ShortMA = MA(Close, ShortPeriod);
LongMA = MA(Close, LongPeriod);
RSI = RSI(Close, 14);
// Conditions
Buy = Cross(ShortMA, LongMA) AND RSI < RSILevel;
Sell = Cross(LongMA, ShortMA) OR RSI > 100 - RSILevel;
// Plot
Plot(ShortMA, "Short MA", colorRed);
Plot(LongMA, "Long MA", colorGreen);
PlotBuy(Buy, "Buy", colorGreen, styleShapeTriangleUp);
PlotSell(Sell, "Sell", colorRed, styleShapeTriangleDown);
This example adds an RSI condition to only buy when the RSI is below a certain level (usually considered oversold) and sell when it's above another level (usually considered overbought), alongside the MA crossover strategy.
Here’s a detailed write-up on Amibroker AFL (Analysis Formula Language) code, including what it is, key features, common use cases, and a sample code example.
Writing beautiful AmiBroker AFL code is only half the battle. The path to profitability requires:
SetOption("CommissionMode", 3); and SetOption("CommissionAmount", 0.01); for $0.01 per share.SetTradeDelays(1, 1, 1, 1); to simulate next-bar execution.