System Architecture: Two Sections

This AFL is organised into two _SECTION_BEGIN blocks โ€” TSL (Trailing Stop Level) and SWING โ€” each handling a distinct part of the strategy:

๐ŸŽจ

TSL Section: Color Candles

Uses StochD(8,3,3), MACD histogram, RSI(3), and ValueWhen close comparisons to color each candle green (bull) or red (bear). This gives instant visual trend context without any manual analysis.

๐Ÿ“‰

SWING Section: TSL Signal

The HHV/LLV trailing stop (TSL) is plotted as a staircase line. When close crosses above TSL โ†’ Buy arrow. When close crosses below TSL โ†’ Sell arrow. Clean, simple, effective.

The Multi-Indicator Color Logic

Candle color is determined by a cascading condition check โ€” making it one of the most information-rich visual signals in this AFL library:

Swing-Trading.afl โ€” Candle Color Logic
// Bullish candle: MACD histogram > 0 AND RSI > 50 // AND StochD rising from oversold AND close rising trendup = IIf(MH > 0 OR (MH > 0 AND MH > Ref(MH,-1)) AND RSI(3) > 50 AND SD < 80 AND SD > Ref(SD,-1) AND ValueWhen(C, O < C), colorGreen, colorWhite); // Bearish candle: MACD < 0 AND RSI < 50 AND StochD falling trendcolor = IIf(MH < 0 OR (MH < 0 AND MH < Ref(MH,-1)) AND RSI(3) < 50 AND SD > 20 AND SD < Ref(SD,-1) AND ValueWhen(C, O > C), colorRed, trendup); Plot(C, "Close", trendcolor, styleCandle | styleThick);

Technical Specifications

StochD Period
8, 3, 3
MACD Periods
8, 21, Signal 5
RSI Period
3
Swing Param
5 (1โ€“55)
TSL Style
Staircase (Teal)
Signal Arrows
Up โ–ฒ / Down โ–ผ
EMA Blend
EMA(3) + EMA(5)
Filter / Explorer
Buy OR Sell

Installation & Usage

Install the AFL

Copy Swing-Trading.afl to your AmiBroker\Formulas\Custom\ folder.

Apply on Daily Chart

Insert as an indicator on a Daily chart. Set the Swing parameter (default 5) โ€” higher values make the TSL wider and catch bigger swings.

Read the Signals

Green Up arrow = Buy signal (close crossed above TSL). Red Down arrow = Sell signal. White candles = neutral/sideways โ€” avoid new entries.

Trail Your Stop

Once in a trade, the teal staircase TSL line is your trailing stop. Exit when price closes below TSL (long) or above TSL (short).

๐Ÿ’ก
Pro Tip: Increase the Swing parameter from 5 to 8โ€“10 for Nifty 50 blue-chip stocks to avoid minor pullbacks triggering exits. Use Swing=3โ€“4 for more volatile mid-cap stocks where quicker exits protect capital better.
โš ๏ธ
Swing Trading AFL is optimized for daily and weekly timeframes. Using it on intraday (5min, 15min) will generate excessive noise. For intraday, use the Intraday AFL or ORB System instead.