该策略是一个基于反转公平价值缺口(Inverted Fair Value Gap, IFVG)的量化交易系统,结合了移动平均线趋势确认和动态追踪止损机制。策略通过识别价格行为中的公平价值缺口(FVG)及其反转形态,并在趋势支持的情况下进行交易。这种方法既确保了交易方向与整体市场趋势保持一致,又能够捕捉市场中的关键反转点。
策略的核心逻辑包含以下几个关键步骤: 1. FVG检测:通过分析当前蜡烛线与前一根蜡烛线的价格区间重叠情况来识别公平价值缺口。 2. IFVG确认:当价格突破FVG的高点或低点时,形成反转信号。 3. 趋势确认:使用50期和200期简单移动平均线(SMA)的交叉关系来确定市场趋势。 4. 入场条件:在上升趋势中,当价格低于IFVG低点时做多;在下降趋势中,当价格高于IFVG高点时做空。 5. 风险管理:采用固定止损和基于ATR的动态追踪止损相结合的方式。
该策略通过结合IFVG价格结构、趋势确认和动态风险管理,构建了一个完整的交易系统。策略在保持简洁性的同时,充分考虑了市场趋势、风险控制和利润管理等关键要素。通过建议的优化方向,策略可以进一步提升其适应性和稳定性。在实盘交易中,建议进行充分的回测和参数优化,并根据具体市场特征做相应调整。
/*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)