Amibroker Afl Code -

// Section 1: Parameters (User adjustable)
MAfastPeriod = Param("Fast MA Period", 10, 2, 50, 1);
MAslowPeriod = Param("Slow MA Period", 30, 10, 200, 1);

// Section 2: Calculate Indicators FastMA = MA(C, MAfastPeriod); SlowMA = MA(C, MAslowPeriod);

// Section 3: Define Signals Buy = Cross(FastMA, SlowMA); Sell = Cross(SlowMA, FastMA);

// Section 4: Visualization Plot(C, "Price", colorBlack, styleCandle); Plot(FastMA, "Fast MA", colorGreen, styleLine); Plot(SlowMA, "Slow MA", colorRed, styleLine); PlotShapes(Buy * shapeUpArrow, colorBrightGreen, 0, Low, -15); PlotShapes(Sell * shapeDownArrow, colorRed, 0, High, -15);

This is the "Hello World" of AmiBroker AFL code. It works, but it is not profitable. Let’s move to advanced logic.


In the pantheon of trading platforms, most are theaters of noise—flashing lights, screaming news feeds, and the ghostly whispers of gurus promising holy grails. But Amibroker is different. Amibroker is a cathedral of logic. And its language, AFL, is not a script. It is a scalpel. A confession. A spell cast over raw market data to summon order from chaos.

To write AFL code is to become an architect of silence.

AFL is deceptively simple yet infinitely deep. The code snippets in this article range from beginner to expert. Your job is to copy them, break them, fix them, and eventually invent your own.

Whether you are trading futures, forex, or a basket of tech stocks, mastering AmiBroker AFL code gives you a direct line to the market’s hidden inefficiencies. amibroker afl code

Next Steps:


Disclaimer: Trading involves financial risk. Past backtest performance does not guarantee future results. Always verify AFL code in a paper trading environment before live execution.

A proper review of AmiBroker Formula Language (AFL) code requires evaluating both the technical soundness of the script and its practical effectiveness for trading. AmiBroker Community Forum 1. Reviewing Technical Soundness

Ensure the code is robust and efficient by checking the following: AFL Syntax and Logic: Verify that the code follows proper AFL syntax and that logic for is correctly defined. Variable Scope:

Ensure variables are appropriately scoped and initialized to prevent unexpected values during execution. Array Handling: Since AFL is array-based, verify that functions like are applied correctly to historical data arrays. Optimization of "Foreign" Data: SetForeign()

is used correctly for index filters or cross-symbol analysis without causing data alignment issues. Debugging: Use built-in tools like to log variable values and the AFL Debugger to step through the logic. AmiBroker Community Forum 2. Evaluating Trading Strategy Performance

A good review must assess if the strategy is viable in a live market environment:

For a "proper" piece of AmiBroker Formula Language (AFL) code, it is essential to include structural elements that define signals, handle visualization, and manage trading parameters. Essential AFL Structure // Section 1: Parameters (User adjustable) MAfastPeriod =

A robust AFL script generally consists of these key sections:

Formula Parameters: Allow you to tweak values (like period or multiplier) through the user interface without editing code.

Indicator Logic: The mathematical definitions (e.g., EMA or Supertrend).

Trading Rules: Logic for Buy, Sell, Short, and Cover signals.

Visual Output: Using Plot() or PlotShapes() to show indicators and entry/exit arrows on the chart.

Backtest Settings: Functions like SetPositionSize() to define how many shares or what percentage of equity to trade. Example: RSI-Based Mean Reversion

Below is a standard AFL template for a simple Relative Strength Index (RSI) strategy.

// 1. Parameters period = Param("RSI Period", 14, 2, 50, 1); overbought = Param("Overbought Level", 70, 50, 90, 1); oversold = Param("Oversold Level", 30, 10, 50, 1); // 2. Indicator Logic rsiValue = RSI(period); // 3. Trading Rules Buy = Cross(rsiValue, oversold); // Enter when RSI crosses above oversold Sell = Cross(rsiValue, overbought); // Exit when RSI crosses above overbought // Prevent multiple consecutive signals Buy = ExRem(Buy, Sell); Sell = ExRem(Sell, Buy); // 4. Visuals Plot(rsiValue, "RSI", colorBlue, styleLine); Plot(overbought, "OB", colorRed, styleDashed); Plot(oversold, "OS", colorGreen, styleDashed); PlotShapes(IIf(Buy, shapeUpArrow, shapeNone), colorGreen, 0, Low, -15); PlotShapes(IIf(Sell, shapeDownArrow, shapeNone), colorRed, 0, High, -15); // 5. Backtest Settings SetPositionSize(10, spsPercentOfEquity); // Risk 10% of equity per trade Use code with caution. Copied to clipboard Installation and Usage This is the "Hello World" of AmiBroker AFL code

Open Formula Editor: In AmiBroker, go to the Analysis tab and select Formula Editor.

Paste & Save: Copy the code above, click Verify to check for syntax errors, and save it in your /Formulas/Custom folder.

Apply to Chart: Right-click on a blank chart and select your saved formula from the Indicators menu to see it in action.

AI responses may include mistakes. For financial advice, consult a professional. Learn more Position Sizing In Amibroker With SetPositionSize Function


Scanning 10,000 stocks with complex AFL can be slow. Optimize:

SetBarsRequired(200, 50); // Only 200 historical, 50 future bars

Slow code:

for(i=0; i<BarCount; i++)
    myArray[i] = MA(C, 200)[i];

Fast code:

myArray = MA(C, 200); // Vectorized

As your AmiBroker AFL code grows, readability saves hours. Adopt this standard:

Ooooh, ultrawide! 😍