Amibroker Afl Code Verified
, this journey revolves around code.
The "License Verified" badge on official forums is used to confirm legitimate users, which often leads to better support from the community and the developer. Pros and Cons Pros Cons
: The script never references future data to make past trading decisions.
if( BarCount > 0 AND IsNull( Close ) == 0 ) // Run secure calculations Use code with caution. Summary Checklist for Verified AFL Code amibroker afl code verified
: The first "Verify" button. It ensures the code is readable by the engine, checking for typos or logic errors in the identifiers.
Look-ahead bias occurs when a formula uses future data to generate a signal today. Functions like Ref( Close, 1 ) or poorly constructed Zig() indicators look forward in time. This creates spectacular backtest results that are impossible to replicate in live trading. 2. Incorrect Trade Delays
Filter = 1; AddColumn(Close, "Close", 1.2); AddColumn(mySignal, "Signal", 1.2); AddColumn(Buy, "Buy", 1.0); , this journey revolves around code
Follow this systematic checklist to audit and verify any AmiBroker script. Step 1: Use the "Check" Tool
//============================================================================ // VERIFIED AFL TRADING SYSTEM TEMPLATE // Strategy: Dual Moving Average Crossover // Architecture: Array-optimized, clean signal routing, fully visualized //============================================================================ //--- System Setup & Portfolio Settings --- SetOption("InitialEquity", 100000); SetOption("CommissionMode", 1); // 1 = per share/contract, 2 = percentage SetOption("CommissionAmount", 0.01); // $0.01 per share SetOption("MinShares", 1); SetOption("MaxOpenPositions", 5); //--- User Parameters (Optimizable & Verifiable) --- _SECTION_BEGIN("System Parameters"); FastPeriod = Param("Fast MA Period", 15, 2, 100, 1); SlowPeriod = Param("Slow MA Period", 50, 10, 300, 1); _SECTION_END(); //--- Indicator Calculations (Array Based) --- FastMA = MA( Close, FastPeriod ); SlowMA = MA( Close, SlowPeriod ); //--- Handle Null Values Safely --- FastMA = SetInfill(FastMA, Null); SlowMA = SetInfill(SlowMA, Null); //--- Core Trading Logic (No Look-Ahead Bias) --- Buy = Cross( FastMA, SlowMA ); Sell = Cross( SlowMA, FastMA ); //--- Short Selling Logic (Optional, set to 0 if Long-Only) --- Short = 0; Cover = 0; //--- Order Execution Fixes (Execute on Next Bar Open to prevent bias) --- SetTradeDelays( 1, 1, 1, 1 ); // Delay entry/exit by 1 bar for realistic execution BuyPrice = Open; SellPrice = Open; //--- Position Sizing --- PositionSize = -20; // Invest 20% of current equity per position //--- Visual Verification Section --- _SECTION_BEGIN("Chart Graphics"); Plot( Close, "Price Chart", colorCandle, styleCandle ); Plot( FastMA, "Fast MA(" + FastPeriod + ")", colorBlue, styleLine | styleThick ); Plot( SlowMA, "Slow MA(" + SlowPeriod + ")", colorOrange, styleLine | styleThick ); // Plot Signals directly onto the chart for manual verification PlotShapes( IIf(Buy, shapeUpArrow, shapeNone), colorGreen, 0, Low, -15 ); PlotShapes( IIf(Sell, shapeDownArrow, shapeNone), colorRed, 0, High, -15 ); _SECTION_END(); Use code with caution. Beyond the Code: Out-of-Sample Verification
//============================================================================ // SYSTEM NAME: Verified Dual Moving Average Crossover //============================================================================ // 1. System Settings SetOption("InitialEquity", 100000); SetOption("DefaultPositions", 5); SetTradeDelays( 1, 1, 1, 1 ); // Standardize delays to avoid look-ahead bias // 2. Core Indicators & Parameters fastPeriod = Param("Fast MA Period", 10, 2, 50, 1); slowPeriod = Param("Slow MA Period", 30, 10, 200, 1); fastMA = MA( Close, fastPeriod ); slowMA = MA( Close, slowPeriod ); // 3. Trading Logic (Signals) Buy = Cross( fastMA, slowMA ); Sell = Cross( slowMA, fastMA ); Short = 0; Cover = 0; // 4. Execution Prices BuyPrice = Open; // Executed on the next bar's open due to SetTradeDelays SellPrice = Open; // 5. Code Verification & Debugging Section Filter = Buy OR Sell; AddColumn( Close, "Close Price", 1.2 ); AddColumn( fastMA, "Fast MA", 1.2 ); AddColumn( slowMA, "Slow MA", 1.2 ); // 6. Chart Plotting Plot( Close, "Price", colorCandle, styleCandle ); Plot( fastMA, "Fast MA", colorGreen, styleLine ); Plot( slowMA, "Slow MA", colorRed, styleLine ); PlotShapes( Buy * shapeUpArrow, colorGreen, 0, Low ); PlotShapes( Sell * shapeDownArrow, colorRed, 0, High ); Use code with caution. 4. Advanced Verification Strategies if( BarCount > 0 AND IsNull( Close )
AFL processes entire arrays of data simultaneously for maximum speed. Mixing arrays (like Close ) with scalar values (like a single static number) inside conditional statements like if-else without using a loop (such as a for loop) causes logical routing failures. AmiBroker will often only evaluate the very first element of the array, rendering the rest of the trading signals incorrect. Flawed Custom Backtester (CBT) Implementations
// AI writes this confidently, but it's broken. Buy = Cross(StochK(), StochD()) AND Close > Highest(H, 50); // Uses future high!
Zoom in on historical charts to manually verify if a signal triggers exactly when your mathematical conditions are met.