DynamicSwing vwap
- インディケータ
- バージョン: 1.0
- アクティベーション: 5
EWMA-VWAP that automatically re-anchors at swing pivots, color-codes trend direction, and carries the prior segment forward until price invalidates it.
▶ WHAT IT DOES
Dynamic Swing VWAP is an adaptive Volume-Weighted Average Price indicator that eliminates the guesswork of manual VWAP anchor selection. Instead of anchoring to a fixed session or calendar reset, it anchors automatically whenever the market forms a confirmed swing pivot — a Higher High, Lower Low, Lower High, or Higher Low — then re-anchors with every new structural swing.
The result is two simultaneous VWAP lines on your chart at all times:
• Active Segment (DS-VWAP) — the VWAP running from the most recent swing anchor forward, colored lime (bullish) or red (bearish) according to the current trend direction.
• Previous Segment (DS-VWAP prev) — the VWAP from the segment just retired, continuing to advance and draw forward until price closes beyond its outer ATR band, at which point it fades. This "ghost" line lets you see at a glance whether the previous structure is still acting as support or resistance.
Each line is accompanied by optional ATR-scaled bands (upper and lower, dotted) that define dynamic support/resistance zones. Swing pivot labels (LL, HH, HL, LH, EL, EH, IL, IH) are placed directly on the chart at each pivot bar.
An on-chart dashboard panel (configurable corner) displays the current trend bias, live VWAP value, last swing type, and timeframe/version info.
▶ CORE FORMULA & DETECTION LOGIC
The VWAP computation uses an Exponential Weighted Moving Average (EWMA) formulation rather than a simple cumulative sum. The smoothing factor α is derived from an Adaptive Period Time (APT) parameter:
α = 1 − exp(−ln(2) / max(1, APT))
Each bar, the numerator (price × volume) and denominator (volume) are updated:
P_new = (1−α)·P_old + α·HLC3·vol_capped
V_new = (1−α)·V_old + α·vol_capped
VWAP = P_new / V_new
Price used is the HLC3 typical price (High + Low + Close) / 3.
Volume is capped at 3× the 20-bar SMA of volume, with a fallback to the 50-bar rolling median when real volume is unavailable (tick volume is used automatically on instruments without real volume).
ATR bands: Band = VWAP ± (EWMA_ATR × BandMultiplier), where EWMA_ATR is a parallel exponential-smoothed ATR computed over 50 bars.
Adaptive APT (optional): When InpUseAdapt is enabled, APT is inversely scaled by the ratio of current ATR to its 50-bar average, raised to the power of InpVolBias. High-volatility environments tighten the APT (faster response); low-volatility environments widen it.
Swing detection: A bar is classified as a swing high (or low) if it is the highest high (lowest low) within the preceding InpSwingPeriod bars, evaluated on bar close. The most recent swing high and low bar indices are tracked; whichever was more recent determines the current trend direction (g_dir). A flip in g_dir triggers an anchor handoff.
Anchor re-seeding: When a new anchor fires, the active segment state is handed to the previous-segment slot, and the new active segment is seeded from the pivot bar with a 5-bar ramp-up smoothing (when InpSmoothAnchor is enabled) to suppress the sharp initial VWAP discontinuity.
Previous-segment invalidation: The previous VWAP continues advancing each bar using the same EWMA formula. It is invalidated — and stops drawing — when the closing price crosses beyond the previous segment's outer band (VWAP_prev ± EWMA_ATR_prev × BandMult).
▶ WHAT MAKES THIS DIFFERENT
1. DUAL-SEGMENT DISPLAY — most swing-anchored VWAP tools show only the current segment. This indicator also carries the prior segment forward dynamically, giving you a second contextual reference without cluttering the chart with permanent historical lines.
2. EWMA FORMULATION — unlike cumulative-sum VWAP (which gives equal weight to every bar from anchor to present), the EWMA formulation makes recent price/volume interactions more influential. This makes the VWAP more responsive to intraday structure changes.
3. SELF-INVALIDATING PREVIOUS SEGMENT — the prior VWAP does not linger indefinitely. It disappears the moment price commits beyond its band, so the chart stays clean and the remaining line is always actionable.
4. VOLUME CAPPING — volume spikes (news events, open/close surges) are capped at 3× the rolling SMA to prevent a single high-volume bar from distorting the VWAP anchor.
5. STRUCTURAL PIVOT LABELS — every swing inflection is labeled with its market structure classification (LL/HH/HL/LH/EL/EH/IL/IH), giving a built-in market structure map alongside the VWAP.
6. OPTIONAL ANCHOR FILTER — the InpOnlyLLHH flag restricts re-anchoring to only confirmed Lower Lows and Higher Highs, filtering out internal retracements when you only want anchors at major structure breaks.
▶ KEY PARAMETERS TABLE
┌──────────────────┬──────────────┬────────────────────────────────────────────────────────────────┐
│ Parameter │ Default │ Description │
├──────────────────┼──────────────┼────────────────────────────────────────────────────────────────┤
│ InpSwingPeriod │ 55 │ Lookback bars for swing high/low detection. Larger = fewer, │
│ │ │ more significant pivots. Range 5–200 recommended. │
├──────────────────┼──────────────┼────────────────────────────────────────────────────────────────┤
│ InpBaseAPT │ 21.0 │ Base Adaptive Period Time. Controls EWMA smoothing speed. │
│ │ │ Lower = faster response, higher = smoother. Range 5–300. │
├──────────────────┼──────────────┼────────────────────────────────────────────────────────────────┤
│ InpUseAdapt │ false │ Enable adaptive APT scaling by ATR ratio. When true, APT │
│ │ │ automatically tightens in high-volatility, widens in quiet. │
├──────────────────┼──────────────┼────────────────────────────────────────────────────────────────┤
│ InpVolBias │ 10.0 │ Exponent applied to ATR ratio in adaptive mode. Higher values │
│ │ │ amplify the adaptive effect. Active only when InpUseAdapt=true. │
├──────────────────┼──────────────┼────────────────────────────────────────────────────────────────┤
│ InpVolCap │ true │ Cap volume at 3× 20-bar SMA to suppress volume-spike distortion.│
├──────────────────┼──────────────┼────────────────────────────────────────────────────────────────┤
│ InpSmoothAnchor │ true │ Apply 5-bar ramp-up when seeding a new anchor to reduce the │
│ │ │ visible VWAP discontinuity at re-anchor events. │
├──────────────────┼──────────────┼────────────────────────────────────────────────────────────────┤
│ InpOnlyLLHH │ false │ Restrict re-anchoring to confirmed LL and HH pivots only. │
│ │ │ Ignores HL/LH internal swings for a higher-timeframe feel. │
├──────────────────┼──────────────┼────────────────────────────────────────────────────────────────┤
│ InpShowBands │ true │ Show ATR-scaled upper and lower deviation bands (dotted lines). │
├──────────────────┼──────────────┼────────────────────────────────────────────────────────────────┤
│ InpBandMult │ 0.618 │ ATR multiplier for band width. 0.618 (phi) for tight zones; │
│ │ │ 1.0–1.5 for wider standard-deviation style bands. │
├──────────────────┼──────────────┼────────────────────────────────────────────────────────────────┤
│ InpShowPivots │ true │ Display swing pivot labels (LL/HH/HL/LH/EL/EH/IL/IH) on chart. │
├──────────────────┼──────────────┼────────────────────────────────────────────────────────────────┤
│ InpBullColor │ Lime │ Color for bullish (uptrend) VWAP and bands. │
├──────────────────┼──────────────┼────────────────────────────────────────────────────────────────┤
│ InpBearColor │ Red │ Color for bearish (downtrend) VWAP and bands. │
├──────────────────┼──────────────┼────────────────────────────────────────────────────────────────┤
│ InpLineWidth │ 2 │ Width of the main VWAP line (1–5). Bands always draw at width 1.│
├──────────────────┼──────────────┼────────────────────────────────────────────────────────────────┤
│ InpShowDash │ true │ Show the on-chart info dashboard panel. │
├──────────────────┼──────────────┼────────────────────────────────────────────────────────────────┤
│ InpDashPos │ "Top Right" │ Dashboard position: "Top Right", "Top Left", "Bottom Right", │
│ │ │ "Bottom Left". │
├──────────────────┼──────────────┼────────────────────────────────────────────────────────────────┤
│ InpLookbackBars │ 2000 │ Maximum bars to process on full recalculation. Set to 0 for │
│ │ │ unlimited (may be slow on long histories). │
└──────────────────┴──────────────┴────────────────────────────────────────────────────────────────┘
▶ COMPATIBILITY
• Platform: MetaTrader 5 (build 2755+)
• Instruments: All — Forex, Indices, Commodities, Crypto, Stocks
• Timeframes: All — works on M1 through MN; recommended M5, M15, H1, H4
• Real volume / tick volume: Auto-detected (uses real volume when available, tick volume as fallback)
• Buffers used: 12 (6 data + 6 color-index buffers for DRAW_COLOR_LINE)
• Chart type: Main window overlay
▶ HOW TO USE
1. INSTALLATION
Copy DynamicSwingVWAP.mq5 to: [MT5 Data Folder]\MQL5\Indicators\
In MetaEditor, compile the file (F7). The indicator appears in the Navigator panel.
2. ATTACH
Drag onto any chart. The default settings (InpSwingPeriod=55, InpBaseAPT=21) are tuned for M15–H1 charts on major Forex pairs.
3. READING THE CHART
• Lime (green) VWAP line = current bullish segment; price above = bullish bias
• Red VWAP line = current bearish segment; price below = bearish bias
• The dimmer/older-colored line is the previous segment (labeled "DS-VWAP prev")
• Dotted lines above/below = ATR bands — support/resistance zones
• Triangle labels = pivot classification (LL, HH, HL, LH, EL, EH, IL, IH)
4. TUNE FOR TIMEFRAME
• Scalping (M1–M5): InpSwingPeriod=21, InpBaseAPT=10
• Intraday (M15–H1): InpSwingPeriod=55, InpBaseAPT=21 (defaults)
• Swing (H4–D1): InpSwingPeriod=89–144, InpBaseAPT=34–55
5. USE BANDS AS ENTRIES
Price returning to the active VWAP line or its lower band (in a bullish segment) is
a mean-reversion zone. Price rejecting the upper band can signal a continuation pullback.
6. PREVIOUS SEGMENT AS S/R
The previous-segment VWAP often acts as support after a bullish re-anchor, and
resistance after a bearish one. Watch for it to vanish — that confirms the prior
structure has been invalidated.
7. ALERTS (not present in v1.0 — see roadmap)
Alert support for anchor events and band breaks is planned for v1.1.
