Advanced Opportunity Space Oscillator Strategy: A Quantitative Trading System Based on Fair Value Gaps

FVG ATR SMA VOL BFVG SFVG
Created on: 2025-02-21 10:43:26 Modified on: 2025-02-24 14:55:25
Copy: 1 Number of hits: 362
avatar of ianzeng123 ianzeng123
2
Follow
319
Followers

 Advanced Opportunity Space Oscillator Strategy: A Quantitative Trading System Based on Fair Value Gaps  Advanced Opportunity Space Oscillator Strategy: A Quantitative Trading System Based on Fair Value Gaps

Overview

This strategy is an innovative trading system based on Fair Value Gaps (FVG), designed to capture potential trading opportunities by identifying price gaps and volume anomalies in the market. The strategy combines dynamic counting mechanisms and normalization processing, not only accurately identifying buy and sell signals but also helping traders better understand market structure through visualization.

Strategy Principles

The core of the strategy is to identify potential trading opportunities by monitoring price gaps between consecutive candles. Specifically: 1. Bullish FVG (BFVG) forms when the current candle’s low is higher than the high of two candles ago 2. Bearish FVG (SFVG) forms when the current candle’s high is lower than the low of two candles ago 3. The strategy incorporates a verification mechanism based on volume and gap size, where only FVGs meeting verification conditions trigger trading signals 4. Uses a 50-period dynamic counting window to accumulate the number of bullish and bearish FVGs 5. Through normalization processing, gap widths are transformed into more intuitive indicator values

Strategy Advantages

  1. The system has a comprehensive signal verification mechanism, improving signal quality through dual confirmation of volume and gap magnitude
  2. Dynamic counting window effectively captures market trend changes
  3. Normalization processing makes signals comparable across different periods
  4. Strategy includes automatic position management, closing reverse positions before opening new ones
  5. Excellent visualization effects, making it easier for traders to understand market conditions

Strategy Risks

  1. FVG signals may generate false signals in highly volatile markets
  2. Fixed verification parameters may not be suitable for all market environments
  3. Lack of stop-loss and take-profit mechanisms may lead to significant drawdowns
  4. Frequent trading may result in high transaction costs It is recommended to manage these risks by setting appropriate stop-loss levels and introducing market environment filters.

Optimization Directions

  1. Introduce adaptive parameter adjustment mechanisms to better adapt to different market environments
  2. Add trend filters to trade only in the direction of strong trends
  3. Design more sophisticated position management systems, including staged position building and dynamic stop-loss
  4. Consider transaction costs and optimize trading frequency
  5. Combine with other technical indicators to improve signal reliability

Summary

This is an innovative trading strategy based on price structure, capturing market opportunities through intelligent identification and verification of fair value gaps. The strategy’s design concept is clear, implementation is professional, and it has good scalability. Through the suggested optimization directions, the strategy’s stability and profitability can be further enhanced.

Strategy source code
/*backtest
start: 2024-02-22 00:00:00
end: 2025-02-19 08:00:00
period: 1h
basePeriod: 1h
exchanges: [{"eid":"Binance","currency":"ETH_USDT"}]
*/

// ----------------------------------------------------------------------------
// This Pine Script™ code is subject to the terms of the Mozilla Public License
// 2.0 at https://mozilla.org/MPL/2.0/
// © OmegaTools
// ----------------------------------------------------------------------------

//@version=5
strategy("FVG Oscillator Strategy", 
     shorttitle="FVG Osc v5 [Strategy]", 
     overlay=false, 
     initial_capital=100000, 
     default_qty_type=strategy.percent_of_equity, 
     default_qty_value=100)

//------------------------------------------------------------------------------
// 1) Input Parameters
//------------------------------------------------------------------------------
lnt   = input.int(50, "Bars Back")
area  = input.bool(true, "Show Areas")
upcol = input.color(#2962ff, "Positive Color")
dncol = input.color(#e91e63, "Negative Color")

//------------------------------------------------------------------------------
// 2) FVG Detection
//    bfvg = bullish FVG, sfvg = bearish FVG
//------------------------------------------------------------------------------
bfvg = low > high[2]
sfvg = high < low[2]

//------------------------------------------------------------------------------
// 3) Additional Conditions - FVG Verification (Volume, Gap Size)
//------------------------------------------------------------------------------
vol  = volume > ta.sma(volume, 10)
batr = (low - high[2]) > ta.sma(low - high[2], lnt) * 1.5
satr = (high - low[2]) > ta.sma(high - low[2], lnt) * 1.5

//------------------------------------------------------------------------------
// 4) Sum of Bullish / Bearish FVG within the Last lnt Bars
//------------------------------------------------------------------------------
countup   = math.sum(bfvg ? 1 : 0, lnt)      // +1 for each BFVG
countdown = math.sum(sfvg ? -1 : 0, lnt)     // -1 for each SFVG

//------------------------------------------------------------------------------
// 5) Verification (e.g., Require Higher Volume or Large Gap)
//------------------------------------------------------------------------------
verifyb = (bfvg and vol[1]) or (bfvg and batr)
verifys = (sfvg and vol[1]) or (sfvg and satr)

//------------------------------------------------------------------------------
// 6) Normalized Gap Values
//------------------------------------------------------------------------------
normb = ((low - high[2]) * countup * 0.75) / ta.highest(low - high[2], lnt)
norms = ((high - low[2]) * countdown * 0.75) / ta.lowest(high - low[2], lnt)

//------------------------------------------------------------------------------
// 7) Total Net FVG Count + Calculation of Maximum for fill()
//------------------------------------------------------------------------------
totcount = countup + countdown
max      = math.max(
               ta.highest(countup, 200), 
               ta.highest(math.abs(countdown), 200)
           )

//------------------------------------------------------------------------------
// 8) Plotting Values (as in an indicator – can be kept for visualization)
//------------------------------------------------------------------------------
up   = plot(countup,     "Buy FVG",  color=upcol,  display=display.none)
down = plot(countdown,   "Sell FVG", color=dncol,  display=display.none)
zero = plot(0, "", color.new(color.gray, 100), display=display.none, editable=false)

// Net Value (sum of FVG)
plot(totcount, "Net Value", color=color.new(color.gray, 50))

// Filling areas above/below zero

plot(verifyb ? normb : na, "Long Pattern Width",  color=upcol,  linewidth=1, style=plot.style_histogram)
plot(verifys ? norms : na, "Short Pattern Width", color=dncol, linewidth=1, style=plot.style_histogram)

//------------------------------------------------------------------------------
// 9) Simple Trading Logic (STRATEGY)
//------------------------------------------------------------------------------
// - If "verifyb" is detected, go long.
// - If "verifys" is detected, go short.
//
// You can extend this with Stop Loss, Take Profit, 
// closing old positions, etc.
//------------------------------------------------------------------------------
bool goLong  = verifyb
bool goShort = verifys

// Basic example: Open Long if verifyb, Open Short if verifys.
if goLong
    // First close any short position if it exists
    if strategy.position_size < 0
        strategy.close("Short FVG")
    // Then open Long
    strategy.entry("Long FVG", strategy.long)

if goShort
    // First close any long position if it exists
    if strategy.position_size > 0
        strategy.close("Long FVG")
    // Then open Short
    strategy.entry("Short FVG", strategy.short)