// Input arrays Close = C;
AmiBroker Formula Language (AFL) is the backbone of the platform, designed for technical analysis, strategy development, and algorithmic trading. Unlike general-purpose languages, AFL is an array-based language
if (Buy AND currentPos == 0)
Here is a complete rotational system that ranks and selects the top 10 stocks based on Levy Relative Strength, includes a crisis filter, and uses position sizing:
BuyPrice = Close; // Execute at closing price SellPrice = Close; Use code with caution. 3. Handling Position Sizing AFL allows you to define strict money management rules. PositionSize = -10; // Allocate 10% of equity per trade Use code with caution. Common Use Cases for AFL Code amibroker afl code
+ , - , * , / : Performed across all elements of the array simultaneously.
Comments start with // for single lines or /* ... */ for multi-line blocks. 3. Creating Custom Indicators in AFL // Input arrays Close = C; AmiBroker Formula
Use the built-in optimizer carefully to avoid curve-fitting.
Below is a complete, executable AFL script demonstrating a Bollinger Band breakout strategy. Handling Position Sizing AFL allows you to define
// Define moving averages FastMA = MA(Close, 10); SlowMA = MA(Close, 30); // Define Buy/Sell rules using the Cross function Buy = Cross(FastMA, SlowMA); Sell = Cross(SlowMA, FastMA); // Visualizing on the chart Plot(Close, "Price", colorDefault, styleCandle); Plot(FastMA, "Fast MA", colorRed); Plot(SlowMA, "Slow MA", colorBlue); // Add arrows for signals PlotShapes(IIf(Buy, shapeUpArrow, shapeNone), colorGreen, 0, L, -15); PlotShapes(IIf(Sell, shapeDownArrow, shapeNone), colorRed, 0, H, -15); Use code with caution. 2. Advanced Risk Management & Position Sizing