// WRONG result = IIf(Variable = 10 , High, Low);
// Verified Simple Moving Average Crossover Strategy // Target: Daily Charts, Long-Only SetFormulaName("Verified SMA Crossover"); // 1. Parameter Setup FastPeriod = Param("Fast MA Period", 20, 5, 50, 1); SlowPeriod = Param("Slow MA Period", 50, 20, 200, 1); // 2. Core Indicators FastMA = MA(Close, FastPeriod); SlowMA = MA(Close, SlowPeriod); // 3. Trading Logic Buy = Cross(FastMA, SlowMA); Sell = Cross(SlowMA, FastMA); // 4. Look-Ahead Bias Verification // Ensuring entry and exit happen at the next day's Open price SetTradeDelays(1, 1, 1, 1); BuyPrice = Open; SellPrice = Open; // 5. Plotting Charts Plot(Close, "Price", colorDefault, styleCandle); Plot(FastMA, "Fast MA", colorGreen, styleLine); Plot(SlowMA, "Slow MA", colorRed, styleLine); // Plot Buy and Sell Signals PlotShapes(IIf(Buy, shapeUpArrow, shapeNone), colorGreen, 0, Low, -15); PlotShapes(IIf(Sell, shapeDownArrow, shapeNone), colorRed, 0, High, -15); Use code with caution. Step 3: Eliminate Look-Ahead Bias
//============================================================================== // STRATEGY NAME: Verified Moving Average Crossover // TYPE: Trend Following / Backtest Ready //============================================================================== // STEP 1: SYSTEM SETTINGS & BACKTESTER OPTIONS SetOption("InitialCapital", 100000); SetOption("DefaultPositions", 5); SetOption("CommissionMode", 1); // 1 = per trade fixed dollar amount SetOption("CommissionAmount", 7); SetTradeDelays(1, 1, 1, 1); // Crucial for verification: Explains execution delays // STEP 2: PARAMETERS & INPUTS ShortPeriod = Param("Short MA Period", 15, 5, 50, 1); LongPeriod = Param("Long MA Period", 45, 20, 200, 1); // STEP 3: CORE INDICATOR CALCULATIONS ShortMA = MA( Close, ShortPeriod ); LongMA = MA( Close, LongPeriod ); // STEP 4: TRADING LOGIC & SIGNAL GENERATION Buy = Cross( ShortMA, LongMA ); Sell = Cross( LongMA, ShortMA ); // Short and Cover lines are mandatory even if empty to verify system type Short = 0; Cover = 0; // STEP 5: SIGNAL CLEANING (Prevents repetitive signals) Buy = ExRem( Buy, Sell ); Sell = ExRem( Sell, Buy ); // STEP 6: ENTRY / EXIT PRICES BuyPrice = Open; // Executed on the open of the next bar due to SetTradeDelays SellPrice = Open; // STEP 7: CHART PLOTTING & VISUALIZATION Plot( Close, "Price Chart", colorCandle, styleCandle ); Plot( ShortMA, "Short MA (" + WriteVal(ShortPeriod, 1.0) + ")", colorBlue, styleLine | styleThick ); Plot( LongMA, "Long MA (" + WriteVal(LongPeriod, 1.0) + ")", colorRed, styleLine | styleThick ); // Plot Signals on Chart PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorGreen, 0, Low, -15 ); PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorRed, 0, High, -15 ); Use code with caution. 4. Crucial Steps to Verify Your Code Eliminate Look-Ahead Bias amibroker afl code verified
Run a backtest and check the trade log. Ensure your code does not use functions like Ref(Close, 1) to trigger trades today based on tomorrow's prices. Use Ref(Close, -1) instead. Step 3: Apply the Indicator Check
Unverified code leads to "phantom profits" in backtests that turn into devastating losses in live trading. This comprehensive guide details how to write, debug, optimize, and verify your AmiBroker AFL code to build bulletproof trading systems. 1. What Does "Verified" AmiBroker AFL Code Mean? Verified AFL code meets four strict criteria: // WRONG result = IIf(Variable = 10 ,
Messages are routed to the internal Log Window (Window → Log) the AFL preference “send _TRACE output to internal log” is enabled (Tools → Preferences → AFL). Without that check, output goes to an external debug viewer like DebugView++.
function to create sliders, allowing you to stress-test the script’s sensitivity to different input values in real-time. 3. Performance Benchmarking Trading Logic Buy = Cross(FastMA, SlowMA); Sell =
printf("VERIFICATION REPORT\n"); printf("Total Buy signals: " + NumToStr( LastValue(Cum(Buy)), 1.0 ) ); printf("Repaint test: " + IIf( LastValue(Cum(RepaintDetected)) == 0, "PASS", "FAIL" ));
Without verification, they optimize opt1 , opt2 , and opt3 simultaneously over 10 years of data. The result? A beautifully curved-fit curve that fails next week. Verified code divides data into IS (in-sample) and OOS (out-of-sample) periods automatically.