Strategy vault / Trend following
Dynamic Trend Confirmed Inverted Fair Value Gap Quantitative Trading Strategy
This strategy is a quantitative trading system based on Inverted Fair Value Gap (IFVG), combining moving average trend confirmation and dynamic trailing stop mechanisms. The strategy identifies Fair Value Gaps (FVG)…

Overview
This strategy is a quantitative trading system based on Inverted Fair Value Gap (IFVG), combining moving average trend confirmation and dynamic trailing stop mechanisms. The strategy identifies Fair Value Gaps (FVG) in price action and their inversions, executing trades when supported by the trend. This approach ensures trade direction aligns with overall market trends while capturing key reversal points.
Strategy Principles
The core logic includes several key steps:
- FVG Detection: Analyzes the overlap between current and previous candle price ranges to identify fair value gaps.
- IFVG Confirmation: Forms a reversal signal when price breaks beyond FVG's high or low.
- Trend Confirmation: Uses crossover relationship between 50-period and 200-period Simple Moving Averages (SMA) to determine market trend.
- Entry Conditions: Goes long when price is below IFVG low in uptrends; goes short when price is above IFVG high in downtrends.
- Risk Management: Combines fixed stop-loss with ATR-based dynamic trailing stops.
Strategy Advantages
- Multi-dimensional Confirmation: Combines price structure (IFVG) and trend indicators (SMA) for more reliable analysis.
- Dynamic Risk Management: Adjusts trailing stops using ATR indicator, protecting profits while allowing adequate price movement.
- Risk-Reward Optimization: Implements 3R profit targets, pursuing higher returns with reasonable risk control.
- Trend Filtering: Confirms trends through moving average crossovers, avoiding excessive trading in ranging markets.
Strategy Risks
- Slippage Risk: Actual execution prices may deviate from ideal prices during high volatility.
- Trend Lag: Moving averages as lagging indicators may cause slightly delayed entries.
- False Breakout Risk: Price may quickly retrace after breakouts, triggering stops.
- Parameter Sensitivity: Strategy performance is sensitive to SMA periods and ATR multiplier settings.
Strategy Optimization Directions
- Indicator Enhancement: Consider adding volume confirmation signals to improve breakout reliability.
- Parameter Adaptation: Introduce volatility metrics to dynamically adjust SMA periods and ATR multipliers.
- Entry Timing Optimization: Add price pullback confirmation mechanism to avoid chasing highs or lows.
- Position Sizing: Dynamically adjust position sizes based on market volatility and trend strength.
- Profit-Taking Optimization: Use more relaxed trailing stop parameters in strong trends.
Summary
The strategy builds a complete trading system by combining IFVG price structure, trend confirmation, and dynamic risk management. While maintaining simplicity, it thoroughly considers key elements like market trends, risk control, and profit management. Through suggested optimizations, the strategy can further enhance its adaptability and stability. For live trading, thorough backtesting and parameter optimization are recommended, with adjustments made according to specific market characteristics.
Source Pine Script
/*backtest
start: 2025-02-16 17:00:00
end: 2025-02-18 03:00:00
period: 2m
basePeriod: 2m
exchanges: [{"eid":"Binance","currency":"SOL_USDT"}]
*/
//@version=6
strategy("Inverted FVG Strategy with Trend Check and Trailing Stops", overlay=true)
// Function to detect FVG
fvgDetected(src, high, low) =>
var float prevHigh = na
var float prevLow = na
var float prevClose = na
var bool fvg = false
if (not na(src[1]))
prevHigh := high[1]
prevLow := low[1]
prevClose := src[1]
if (src > prevClose and low > prevHigh) or (src < prevClose and high < prevLow)
fvg := true
[fvg, prevHigh, prevLow]
// Detect FVG on the chart
[fvg, fvgHigh, fvgLow] = fvgDetected(close, high, low)
// Detect IFVG - Inversion of FVG
var bool ifvg = false
var float ifvgHigh = na
var float ifvgLow = na
if (fvg)
if not na(fvgHigh) and not na(fvgLow)
if (close > fvgHigh and close[1] < fvgHigh) or (close < fvgLow and close[1] > fvgLow)
ifvg := true
ifvgHigh := close > fvgHigh ? high : na
ifvgLow := close < fvgLow ? low : na
// Plot FVG and IFVG zones for visualization
bgcolor(ifvg ? color.new(color.red, 80) : na)
plot(ifvgHigh, title="IFVG High", color=color.red, linewidth=2, style=plot.style_cross)
plot(ifvgLow, title="IFVG Low", color=color.red, linewidth=2, style=plot.style_cross)
// Trend Check using Simple Moving Averages
smaShort = ta.sma(close, 50) // Short term SMA
smaLong = ta.sma(close, 200) // Long term SMA
var bool uptrend = false
var bool downtrend = false
uptrend := smaShort > smaLong // Up trend if short SMA is above long SMA
downtrend := smaShort < smaLong // Down trend if short SMA is below long SMA
// Plot SMAs for visualization
plot(smaShort, title="SMA Short", color=color.blue, linewidth=1)
plot(smaLong, title="SMA Long", color=color.orange, linewidth=1)
// Trading logic with trend confirmation
longCondition = ifvg and close < ifvgLow and uptrend
shortCondition = ifvg and close > ifvgHigh and downtrend
// Risk Definition
stopLoss = 100 // Example values in points
risk = stopLoss
if (longCondition)
strategy.entry("Long", strategy.long)
strategy.exit("Initial Long Exit", "Long", stop=close - stopLoss, limit=close + (risk * 3)) // Changed to 3R
if (shortCondition)
strategy.entry("Short", strategy.short)
strategy.exit("Initial Short Exit", "Short", stop=close + stopLoss, limit=close - (risk * 3)) // Changed to 3R
// ATR for dynamic trailing stop
atr = ta.atr(14)
// Trailing Stop for Long Position if the trade has moved > 0.5R
if (strategy.position_size > 0)
if (close - strategy.position_avg_price >= risk * 0.5)
trailingStopLong = math.max(strategy.position_avg_price + (risk * 0.5), close - (atr * 2))
strategy.exit("Trailing Stop Long", "Long", stop=trailingStopLong)
// Trailing Stop for Short Position if the trade has moved > 0.5R
if (strategy.position_size < 0)
if (strategy.position_avg_price - close >= risk * 0.5)
trailingStopShort = math.min(strategy.position_avg_price - (risk * 0.5), close + (atr * 2))
strategy.exit("Trailing Stop Short", "Short", stop=trailingStopShort)Published by ianzeng123 on the FMZ strategy square · last modified 2025-02-21. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.
Build this with AgenKit
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Dynamic-Trend-Confirmed-Inverted-Fair-Value-Gap-Quantitative-Trading-Strategy.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch
Understand it first
Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.
Related specs
- Dynamic Moving Average and Bollinger Bands Cross Strategy with Fixed Stop Loss Optimization ModelPine Script · Trend following · 3h
- Dynamic Signal Line Trend Following Strategy Combining ATR and VolumePine Script · Trend following · 1h
- Fixed Range Volume Profile And Anchored VWAP Trend Identification Strategy With Dynamic Stop LossPine Script · Trend following · 1h
- Multi Factor Trend Price Action Strategy with Dynamic Risk Management SystemPine Script · Trend following · 2h
- Multi Period Moving Average Ribbon and MACD Crossover Strategy SystemPine Script · Trend following · 1h
- Relative Volume Trend Following Trading StrategyPine Script · Trend following · 1d