OPEN-SOURCE SCRIPT

Optimized ICT Buy and Sell Signals

I understand that you're seeing too many signals, which can make the strategy seem cluttered or overly reactive. To **optimize the strategy** and reduce the frequency of buy and sell signals, we can add a few improvements:

### Optimization Approach:
1. **Increase Lookback Periods**: By increasing the lookback periods for **Market Structure**, **Order Blocks**, and **FVGs**, we can make the strategy less sensitive to every little fluctuation and focus more on stronger trend shifts.
2. **Trend Confirmation with EMA**: Use the **EMA 9** as a trend filter. Only generate signals when the price is above the **EMA 9** for bullish signals and below the **EMA 9** for bearish signals.
3. **Limit Signals to Stronger Price Action**: For example, we can reduce the number of signals by requiring price to be closer to significant **order blocks** or **FVGs**, or only trigger signals when there is a **break of structure** (BoS) and confirmation of a trend reversal.

---

### Optimized Code

```pinescript
//version=5
indicator("Optimized ICT Buy and Sell Signals", overlay=true)

// --- Input Parameters ---
emaLength = input.int(9, title="EMA Length")
fvgLookback = input.int(10, title="Fair Value Gap Lookback Period")
orderBlockLookback = input.int(30, title="Order Block Lookback Period")
minGapSize = input.float(0.5, title="Min Gap Size (FVG)") // Minimum gap size for FVG detection

// --- EMA 9 (for trend direction) ---
ema9 = ta.ema(close, emaLength)
plot(ema9, title="EMA 9", color=color.blue, linewidth=2)

// --- Market Structure (ICT) ---
// Identify Higher Highs (HH) and Higher Lows (HL) for Bullish Market Structure
hhCondition = high > ta.highest(high, orderBlockLookback)[1] // Higher High
hlCondition = low > ta.lowest(low, orderBlockLookback)[1] // Higher Low
isBullishStructure = hhCondition and hlCondition // Bullish Market Structure condition

// Identify Lower Highs (LH) and Lower Lows (LL) for Bearish Market Structure
lhCondition = high < ta.highest(high, orderBlockLookback)[1] // Lower High
llCondition = low < ta.lowest(low, orderBlockLookback)[1] // Lower Low
isBearishStructure = lhCondition and llCondition // Bearish Market Structure condition

// --- Order Blocks (ICT) ---
// Bullish Order Block (consolidation before upmove)
var float bullishOrderBlock = na
if isBullishStructure
bullishOrderBlock := ta.valuewhen(low == ta.lowest(low, orderBlockLookback), low, 0)

// Bearish Order Block (consolidation before downmove)
var float bearishOrderBlock = na
if isBearishStructure
bearishOrderBlock := ta.valuewhen(high == ta.highest(high, orderBlockLookback), high, 0)

// --- Fair Value Gap (FVG) ---
// Bullish Fair Value Gap (FVG): Gap between a down-close followed by an up-close
fvgBullish = (low[fvgLookback] < high[1]) and (high[1] - low[fvgLookback] > minGapSize) // Bullish FVG condition with gap size filter

// Bearish Fair Value Gap (FVG): Gap between an up-close followed by a down-close
fvgBearish = (high[fvgLookback] > low[1]) and (high[fvgLookback] - low[1] > minGapSize) // Bearish FVG condition with gap size filter

// --- Entry Conditions ---
// Buy Condition: Bullish Market Structure and Price touching a Bullish Order Block or FVG, and above EMA
longCondition = isBullishStructure and (close <= bullishOrderBlock or fvgBullish) and close > ema9

// Sell Condition: Bearish Market Structure and Price touching a Bearish Order Block or FVG, and below EMA
shortCondition = isBearishStructure and (close >= bearishOrderBlock or fvgBearish) and close < ema9

// --- Plot Buy and Sell Signals on the Chart ---
plotshape(series=longCondition, location=location.belowbar, color=color.green, style=shape.labelup, title="Buy Signal", text="BUY", size=size.small)
plotshape(series=shortCondition, location=location.abovebar, color=color.red, style=shape.labeldown, title="Sell Signal", text="SELL", size=size.small)

// --- Highlight Order Blocks ---
plot(bullishOrderBlock, color=color.green, linewidth=1, title="Bullish Order Block", style=plot.style_circles)
plot(bearishOrderBlock, color=color.red, linewidth=1, title="Bearish Order Block", style=plot.style_circles)

// --- Highlight Fair Value Gaps (FVG) ---
bgcolor(fvgBullish ? color.new(color.green, 90) : na, title="Bullish FVG", transp=90)
bgcolor(fvgBearish ? color.new(color.red, 90) : na, title="Bearish FVG", transp=90)

// --- Alerts ---
// Alert for Buy Signals
alertcondition(longCondition, title="Long Signal", message="Optimized ICT Buy Signal: Bullish structure, price touching bullish order block or FVG, and above EMA.")

// Alert for Sell Signals
alertcondition(shortCondition, title="Short Signal", message="Optimized ICT Sell Signal: Bearish structure, price touching bearish order block or FVG, and below EMA.")
```

### Key Optimizations:

1. **Increased Lookback Periods**:
- The `orderBlockLookback` has been increased to **30**. This allows for a larger window for identifying **order blocks** and **market structure** changes, reducing sensitivity to small price fluctuations.
- The `fvgLookback` has been increased to **10** to ensure that only larger price gaps are considered.

2. **Min Gap Size for FVG**:
- A new input parameter `minGapSize` has been introduced to **filter out small Fair Value Gaps (FVGs)**. This ensures that only significant gaps trigger buy or sell signals. The default value is **0.5** (you can adjust it as needed).

3. **EMA Filter**:
- Added a trend filter using the **EMA 9**. **Buy signals** are only triggered when the price is **above the EMA 9** (indicating an uptrend), and **sell signals** are only triggered when the price is **below the EMA 9** (indicating a downtrend).
- This helps reduce noise by confirming that signals are aligned with the broader market trend.

### How to Use:
1. **Apply the script** to your chart and observe the reduced number of buy and sell signals.
2. **Buy signals** will appear when:
- The price is in a **bullish market structure**.
- The price is near a **bullish order block** or filling a **bullish FVG**.
- The price is **above the EMA 9** (confirming an uptrend).
3. **Sell signals** will appear when:
- The price is in a **bearish market structure**.
- The price is near a **bearish order block** or filling a **bearish FVG**.
- The price is **below the EMA 9** (confirming a downtrend).

### Further Fine-Tuning:
- **Adjust Lookback Periods**: If you still find the signals too frequent or too few, you can tweak the `orderBlockLookback` or `fvgLookback` values further. Increasing them will give more weight to the larger market structures, reducing noise.
- **Test Different Timeframes**: This optimized version is still suited for lower timeframes like 15-minutes or 5-minutes but will also work on higher timeframes (1-hour, 4-hour, etc.). Test across different timeframes for better results.
- **Min Gap Size**: If you notice that the FVG condition is too restrictive or too lenient, adjust the `minGapSize` parameter.

### Conclusion:
This version of the strategy should give you fewer, more meaningful signals by focusing on larger market movements and confirming the trend direction with the **EMA 9**. If you find that the signals are still too frequent, feel free to further increase the lookback periods or tweak the FVG gap size.
Chart patterns

Script de código aberto

No verdadeiro espírito do TradingView, o autor desse script o publicou como código aberto, para que os traders possam compreendê-lo e analisá-lo. Parabéns ao autor! Você pode usá-lo gratuitamente, mas a reutilização desse código em publicações é regida pelas Regras da Casa. Você pode favoritá-lo para usá-lo em um gráfico.

Quer usar esse script no gráfico?

Aviso legal