Amibroker Afl Code (2026)
// ======================== // Moving Average Crossover System // ========================// Parameters PeriodFast = 9; PeriodSlow = 21; StopLoss = 2; // percent
// Indicator calculation FastMA = MA(C, PeriodFast); SlowMA = MA(C, PeriodSlow);
// Entry conditions Buy = Cross(FastMA, SlowMA) AND C > SlowMA; Sell = Cross(SlowMA, FastMA); amibroker afl code
// Stop loss ApplyStop(stopTypeLoss, stopModePercent, StopLoss, 1);
// Plotting Plot(C, "Price", colorBlack, styleCandle); Plot(FastMA, "Fast MA", colorBlue, styleLine); Plot(SlowMA, "Slow MA", colorRed, styleLine); PlotShapes(IIf(Buy, shapeUpArrow, shapeNone), colorGreen); PlotShapes(IIf(Sell, shapeDownArrow, shapeNone), colorRed); The core of any system relies on defining
// Backtest settings SetPositionSize(1000, spsShares); SetOption("InitialCapital", 100000);
The core of any system relies on defining two arrays: Buy and Sell.
Example Strategy: Buy when the 50-period Moving Average crosses above the 200-period MA. Sell when it crosses below. Example Strategy: Buy when the 50-period Moving Average
// Define Moving Averages
MA_Short = MA(Close, 50);
MA_Long = MA(Close, 200);
// Generate Signals
Buy = Cross(MA_Short, MA_Long);
Sell = Cross(MA_Long, MA_Short);
// Visualizing Signals
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone), colorGreen, 0, Low, -15);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone), colorRed, 0, High, -15);
PivotHigh = H > Ref(H, -1) AND H > Ref(H, 1);
PlotShapes(PivotHigh * shapeHollowCircle, colorOrange, 0, H, 10);