18 KiB
18 KiB
USER
Strategy 6: KI FVG Mitigation
ASSISTANT
🎯 Complete Strategy 6: KI FVG Mitigation Implementation
Based on your SaherElm Framework and X121XKI Indicator buffers, here is the complete implementation for Strategy 6: KI FVG Mitigation.
This strategy focuses on Fair Value Gap mitigation where price returns to fill the FVG with X121XKI confirmation for high-probability continuation entries.
/**
* Check Bar Has Signal Based on Strategy 6 ...
* KI FVG Mitigation
*
* @param bar: XOHCL, reference to Bar ...
* @param zone: XBoxZone, reference to holds affected zone ...
* @param targets: XTarget, reference to detected Signal's Targets collections ...
* @param dir: ENUM_X_DIRECTION, detected Signal's direction ...
*
* @return ( bool )
*/
bool Strategy6HasSignal(
XOHCL &bar,
XBoxZone &zone,
XTarget &targets[],
ENUM_X_DIRECTION &dir //
)
{
//
bool result = false;
//
// Prepare ...
zone.Clean();
XClean(targets);
dir = X_DIRECTION_NONE;
//
// Validate ...
result = bar.IsValid() && kiParser.IsValid();
if (!result)
{
return result;
}
//
// Configuration ...
int barIndex = bar.Index() + 1;
double points = GetPoints(symbol);
int minFVGPoints = 20; // Minimum FVG size in points
int kiConfirmationBars = 3; // Bars for KI confirmation
double minScore = 16.0; // Minimum score for signal
//
// State Variables ...
bool isBullish = false;
bool isBearish = false;
double bullishScore = 0;
double bearishScore = 0;
//
// #region 1. Detect Fair Value Gaps ...
//
// Detect Fair Value Gaps ...
XBoxZone bullishFVGs[];
XBoxZone bearishFVGs[];
//
int bullishFVGCount = poiDetector.CollectFVGs(bar, bullishFVGs, 5, 10, 100);
int bearishFVGCount = poiDetector.CollectFVGs(bar, bearishFVGs, 5, 10, 100);
//
// Validate FVG Size ...
XBoxZone validBullishFVGs[];
XBoxZone validBearishFVGs[];
//
// Filter Bullish FVGs ...
for (int i = 0; i < bullishFVGCount; i++)
{
double fvgRange = (bullishFVGs[i].upper - bullishFVGs[i].lower) / points;
bool isValidSize = fvgRange >= minFVGPoints;
bool isNotBreaked = !IsXValid(bullishFVGs[i].breakAt);
bool isNotFilled = !IsFVGFilled(bar, bullishFVGs[i]);
//
if (isValidSize && isNotBreaked && isNotFilled)
{
AddRef(bullishFVGs[i], validBullishFVGs);
}
}
//
// Filter Bearish FVGs ...
for (int i = 0; i < bearishFVGCount; i++)
{
double fvgRange = (bearishFVGs[i].upper - bearishFVGs[i].lower) / points;
bool isValidSize = fvgRange >= minFVGPoints;
bool isNotBreaked = !IsXValid(bearishFVGs[i].breakAt);
bool isNotFilled = !IsFVGFilled(bar, bearishFVGs[i]);
//
if (isValidSize && isNotBreaked && isNotFilled)
{
AddRef(bearishFVGs[i], validBearishFVGs);
}
}
//
// #endregion
//
// #region 2. Check FVG Mitigation (Price Returning to FVG) ...
//
// Check Price is Mitigating Bullish FVG ...
XBoxZone mitigatingBullishFVGs[];
for (int i = 0; i < ArraySize(validBullishFVGs); i++)
{
XBoxZone iFVG = validBullishFVGs[i];
//
// Check Price is Inside or Near FVG ...
bool isMitigating = IsPriceInFVG(bar, iFVG, 10 * points);
bool isRecent = (bar.time - iFVG.from) <= (PeriodSeconds(period) * 50); // Within 50 bars
//
if (isMitigating && isRecent)
{
AddRef(iFVG, mitigatingBullishFVGs);
}
}
//
// Check Price is Mitigating Bearish FVG ...
XBoxZone mitigatingBearishFVGs[];
for (int i = 0; i < ArraySize(validBearishFVGs); i++)
{
XBoxZone iFVG = validBearishFVGs[i];
//
// Check Price is Inside or Near FVG ...
bool isMitigating = IsPriceInFVG(bar, iFVG, 10 * points);
bool isRecent = (bar.time - iFVG.from) <= (PeriodSeconds(period) * 50); // Within 50 bars
//
if (isMitigating && isRecent)
{
AddRef(iFVG, mitigatingBearishFVGs);
}
}
//
// #endregion
//
// #region 3. KI Trend Confirmation ...
//
// Check KI Fast State ...
bool kiFastBullish = kiParser.IsKIFastBullish(barIndex);
bool kiFastBearish = kiParser.IsKIFastBearish(barIndex);
//
// Check KI Fast Switch (Trend Change) ...
bool kiFastSwitchedBullish = kiParser.IsKIFastSwitchedToBullish(barIndex);
bool kiFastSwitchedBearish = kiParser.IsKIFastSwitchedToBearish(barIndex);
//
// Check KI Slow Alignment (HTF Bias) ...
bool kiSlowBullish = kiParser.IsKISlowBullish(barIndex);
bool kiSlowBearish = kiParser.IsKISlowBearish(barIndex);
//
// Check KI Super Slow (Major HTF Bias) ...
bool kiSuperSlowBullish = kiParser.IsKISuperSlowBullish(barIndex);
bool kiSuperSlowBearish = kiParser.IsKISuperSlowBearish(barIndex);
//
// Check Price Position Relative to KI ...
bool priceAboveKIFast = kiParser.IsPriceOverKIFast(barIndex, X_PRICE_CLOSE);
bool priceBelowKIFast = kiParser.IsPriceUnderKIFast(barIndex, X_PRICE_CLOSE);
//
// Check KI Wave Momentum ...
bool kiWaveBullish = kiParser.IsKIWaveSwitchedToBullish(barIndex);
bool kiWaveBearish = kiParser.IsKIWaveSwitchedToBearish(barIndex);
//
// #endregion
//
// #region 4. RSI Momentum Confirmation ...
//
// Check RSI State ...
bool rsiBullish = kiParser.IsRSISwitchedToBullish(barIndex, 55, 2);
bool rsiBearish = kiParser.IsRSISwitchedToBearish(barIndex, 45, 2);
//
// Check RSI Not in Extreme Zones ...
double currentRSI = kiParser.GetRSI(barIndex);
bool rsiNotOverbought = currentRSI < 70;
bool rsiNotOversold = currentRSI > 30;
//
// Check RSI Divergence ...
bool rsiBullishDivergence = false;
bool rsiBearishDivergence = false;
//
if (barIndex + 5 < bar.TotalBars())
{
double currentRSI = kiParser.GetRSI(barIndex);
double previousRSI = kiParser.GetRSI(barIndex + 5);
double currentLow = bar.low;
double previousLow = bar.FindLowest(5, MODE_LOW);
double currentHigh = bar.high;
double previousHigh = bar.FindHighest(5, MODE_HIGH);
//
// Bullish Divergence ...
rsiBullishDivergence = (currentLow < previousLow) && (currentRSI > previousRSI);
//
// Bearish Divergence ...
rsiBearishDivergence = (currentHigh > previousHigh) && (currentRSI < previousRSI);
}
//
// #endregion
//
// #region 5. Volatility / Displacement ...
//
// Check Volatility Expansion ...
bool volatilityHigh = kiParser.IsVolatilityHigh(barIndex);
bool volatilitySwitchedHigh = kiParser.IsVolatilitySwitchdToHigh(barIndex);
//
// Check ATR Breakout ...
bool atrBreakoutUp = kiParser.IsATRBreakoutUp(barIndex, X_PRICE_CLOSE);
bool atrBreakoutDown = kiParser.IsATRBreakoutDown(barIndex, X_PRICE_CLOSE);
//
// #endregion
//
// #region 6. Recent Swing Detection (Liquidity) ...
//
XBoxZone swingHigh;
XBoxZone swingLow;
//
poiDetector.DetectRecentSwings(
swingHigh,
swingLow,
barIndex,
50, // swingsValidationLength
500, // loopback
period,
true // forceRefineZones
);
//
// #endregion
//
// #region 7. Calculate Scores ...
//
// Bullish FVG Mitigation Score ...
//
// FVG Mitigation Presence (Max 6 points) ...
if (ArraySize(mitigatingBullishFVGs) > 0) bullishScore += 6;
//
// KI Trend Alignment (Max 6 points) ...
if (kiFastBullish) bullishScore += 1;
if (kiSlowBullish) bullishScore += 2;
if (kiSuperSlowBullish) bullishScore += 3;
if (priceAboveKIFast) bullishScore += 1;
//
// KI Wave Momentum (Max 2 points) ...
if (kiWaveBullish) bullishScore += 2;
//
// RSI Confirmation (Max 4 points) ...
if (rsiBullish) bullishScore += 2;
if (rsiBullishDivergence) bullishScore += 4;
if (rsiNotOversold) bullishScore += 1;
//
// Volatility (Max 3 points) ...
if (volatilityHigh) bullishScore += 1;
if (volatilitySwitchedHigh) bullishScore += 2;
if (atrBreakoutUp) bullishScore += 2;
//
// FVG Quality (Max 4 points) ...
if (ArraySize(validBullishFVGs) > 0)
{
double avgFVGSize = 0;
for (int i = 0; i < ArraySize(validBullishFVGs); i++)
{
avgFVGSize += (validBullishFVGs[i].upper - validBullishFVGs[i].lower) / points;
}
avgFVGSize /= ArraySize(validBullishFVGs);
//
if (avgFVGSize >= 50) bullishScore += 4;
else if (avgFVGSize >= 30) bullishScore += 3;
else if (avgFVGSize >= 20) bullishScore += 2;
}
//
// Bearish FVG Mitigation Score ...
//
// FVG Mitigation Presence (Max 6 points) ...
if (ArraySize(mitigatingBearishFVGs) > 0) bearishScore += 6;
//
// KI Trend Alignment (Max 6 points) ...
if (kiFastBearish) bearishScore += 1;
if (kiSlowBearish) bearishScore += 2;
if (kiSuperSlowBearish) bearishScore += 3;
if (priceBelowKIFast) bearishScore += 1;
//
// KI Wave Momentum (Max 2 points) ...
if (kiWaveBearish) bearishScore += 2;
//
// RSI Confirmation (Max 4 points) ...
if (rsiBearish) bearishScore += 2;
if (rsiBearishDivergence) bearishScore += 4;
if (rsiNotOverbought) bearishScore += 1;
//
// Volatility (Max 3 points) ...
if (volatilityHigh) bearishScore += 1;
if (volatilitySwitchedHigh) bearishScore += 2;
if (atrBreakoutDown) bearishScore += 2;
//
// FVG Quality (Max 4 points) ...
if (ArraySize(validBearishFVGs) > 0)
{
double avgFVGSize = 0;
for (int i = 0; i < ArraySize(validBearishFVGs); i++)
{
avgFVGSize += (validBearishFVGs[i].upper - validBearishFVGs[i].lower) / points;
}
avgFVGSize /= ArraySize(validBearishFVGs);
//
if (avgFVGSize >= 50) bearishScore += 4;
else if (avgFVGSize >= 30) bearishScore += 3;
else if (avgFVGSize >= 20) bearishScore += 2;
}
//
// #endregion
//
// #region 8. Determine Direction ...
//
// Ensure Only One Direction ...
if (bullishScore >= minScore && bearishScore >= minScore)
{
//
// Use higher score to determine direction ...
if (bullishScore > bearishScore)
{
bearishScore = 0;
}
else if (bearishScore > bullishScore)
{
bullishScore = 0;
}
else
{
//
// Same score, no signal ...
bullishScore = 0;
bearishScore = 0;
}
}
//
// Check Bullish FVG Mitigation Conditions ...
isBullish =
(bullishScore >= minScore) &&
(ArraySize(mitigatingBullishFVGs) > 0) &&
(kiFastBullish || kiWaveBullish);
//
// Check Bearish FVG Mitigation Conditions ...
isBearish =
(bearishScore >= minScore) &&
(ArraySize(mitigatingBearishFVGs) > 0) &&
(kiFastBearish || kiWaveBearish);
//
// #endregion
//
// #region 9. Generate Signal ...
//
// Generate Bullish FVG Mitigation Signal ...
if (isBullish)
{
dir = X_DIRECTION_BULLISH;
//
// Select Best FVG (Youngest Valid FVG in Mitigation) ...
XBoxZone selectedFVG;
int fvgIdx = GetYoungest(mitigatingBullishFVGs);
//
if (IsValidIndex(fvgIdx))
{
selectedFVG = mitigatingBullishFVGs[fvgIdx];
}
//
// Validate Selected FVG ...
if (selectedFVG.IsValid())
{
zone = selectedFVG;
zone.type = "S6_FVG_Mitigation";
//
// Calculate Targets ...
double entry = GetEntry(symbol, dir);
//
// SL: Below FVG Lower ...
double sl = zone.lower - (10 * points);
//
// TP1: 1:1 R:R ...
double risk = MathAbs(entry - sl);
XTarget tp1;
tp1.target = entry + risk;
tp1.volumeMultiplier = 0.5;
tp1.doRF = true;
tp1.doRFOnEntry = false;
AddRef(tp1, targets);
//
// TP2: Next Liquidity (Swing High) ...
XTarget tp2;
if (swingHigh.IsValid())
{
tp2.target = swingHigh.upper + (10 * points);
}
else
{
tp2.target = entry + (risk * 2);
}
tp2.volumeMultiplier = 0.5;
tp2.doRF = false;
tp2.doRFOnEntry = false;
AddRef(tp2, targets);
//
// TP3: 1:3 R:R ...
XTarget tp3;
tp3.target = entry + (risk * 3);
tp3.volumeMultiplier = 1.0;
tp3.doRF = false;
tp3.doRFOnEntry = false;
AddRef(tp3, targets);
}
}
//
// Generate Bearish FVG Mitigation Signal ...
else if (isBearish)
{
dir = X_DIRECTION_BEARISH;
//
// Select Best FVG (Youngest Valid FVG in Mitigation) ...
XBoxZone selectedFVG;
int fvgIdx = GetYoungest(mitigatingBearishFVGs);
//
if (IsValidIndex(fvgIdx))
{
selectedFVG = mitigatingBearishFVGs[fvgIdx];
}
//
// Validate Selected FVG ...
if (selectedFVG.IsValid())
{
zone = selectedFVG;
zone.type = "S6_FVG_Mitigation";
//
// Calculate Targets ...
double entry = GetEntry(symbol, dir);
//
// SL: Above FVG Upper ...
double sl = zone.upper + (10 * points);
//
// TP1: 1:1 R:R ...
double risk = MathAbs(entry - sl);
XTarget tp1;
tp1.target = entry - risk;
tp1.volumeMultiplier = 0.5;
tp1.doRF = true;
tp1.doRFOnEntry = false;
AddRef(tp1, targets);
//
// TP2: Next Liquidity (Swing Low) ...
XTarget tp2;
if (swingLow.IsValid())
{
tp2.target = swingLow.lower - (10 * points);
}
else
{
tp2.target = entry - (risk * 2);
}
tp2.volumeMultiplier = 0.5;
tp2.doRF = false;
tp2.doRFOnEntry = false;
AddRef(tp2, targets);
//
// TP3: 1:3 R:R ...
XTarget tp3;
tp3.target = entry - (risk * 3);
tp3.volumeMultiplier = 1.0;
tp3.doRF = false;
tp3.doRFOnEntry = false;
AddRef(tp3, targets);
}
}
//
// #endregion
//
// #region 10. Validation & Risk Management ...
//
// Validate Signal ...
result = zone.IsValid() && HasDirection(dir) && HasChild(targets);
//
// Check Max Allowed Risk ...
if (result && maxAllowedRiskDistance > 0)
{
double riskInPoints = 0;
if (dir == X_DIRECTION_BULLISH)
{
riskInPoints = MathAbs(zone.GetDirectionalPrice() - zone.GetInDirectionalPrice()) / points;
}
else
{
riskInPoints = MathAbs(zone.GetInDirectionalPrice() - zone.GetDirectionalPrice()) / points;
}
//
if (riskInPoints > maxAllowedRiskDistance)
{
result = false;
zone.Clean();
XClean(targets);
dir = X_DIRECTION_NONE;
}
}
//
// #endregion
//
// #region 11. Debug & Logging ...
//
// Optional: Debug Break for Testing ...
// if (result)
// {
// Print("Strategy 6 FVG Mitigation Signal Detected:");
// Print(" Direction: ", ToXString(dir));
// Print(" Bullish Score: ", bullishScore);
// Print(" Bearish Score: ", bearishScore);
// Print(" FVG Type: ", zone.type);
// Print(" Targets Count: ", ArraySize(targets));
// }
//
// #endregion
//
//
// Cleanup Resources ...
//
XClean(bullishFVGs);
XClean(bearishFVGs);
XClean(validBullishFVGs);
XClean(validBearishFVGs);
XClean(mitigatingBullishFVGs);
XClean(mitigatingBearishFVGs);
//
return result;
}
📋 Helper Functions for FVG Mitigation
Add these helper functions to your signaller class:
// #region FVG Helper Functions ...
/**
* Check if FVG is Filled or Not ...
*
* @param bar: XOHCL, Current Bar ...
* @param fvg: XBoxZone, FVG Zone ...
*
* @return ( bool )
*/
bool IsFVGFilled(
XOHCL &bar,
XBoxZone &fvg //
)
{
//
bool result = false;
//
// Validate ...
result = bar.IsValid() && fvg.IsValid();
if (!result)
{
return result;
}
//
// Check if FVG is Bullish or Bearish ...
bool isBullishFVG = fvg.IsBullish();
bool isBearishFVG = fvg.IsBearish();
//
// Check if Price has Filled the FVG ...
if (isBullishFVG)
{
//
// Bullish FVG is filled when price touches lower boundary ...
result = bar.low <= fvg.lower;
}
else if (isBearishFVG)
{
//
// Bearish FVG is filled when price touches upper boundary ...
result = bar.high >= fvg.upper;
}
//
return result;
}
/**
* Check if Price is Inside or Near FVG ...
*
* @param bar: XOHCL, Current Bar ...
* @param fvg: XBoxZone, FVG Zone ...
* @param tolerance: double, Tolerance in Points ...
*
* @return ( bool )
*/
bool IsPriceInFVG(
XOHCL &bar,
XBoxZone &fvg,
double tolerance = 0 //
)
{
//
bool result = false;
//
// Validate ...
result = bar.IsValid() && fvg.IsValid();
if (!result)
{
return result;
}
//
double points = GetPoints(bar.symbol);
double tolerancePrice = tolerance * points;
//
// Check if Price is Inside or Near FVG ...
bool isBullishFVG = fvg.IsBullish();
bool isBearishFVG = fvg.IsBearish();
//
if (isBullishFVG)
{
//
// Price is mitigating bullish FVG ...
result =
(bar.low <= fvg.upper + tolerancePrice) &&
(bar.high >= fvg.lower - tolerancePrice);
}
else if (isBearishFVG)
{
//
// Price is mitigating bearish FVG ...
result =
(bar.high >= fvg.lower - tolerancePrice) &&
(bar.low <= fvg.upper + tolerancePrice);
}
//
return result;
}
// #endregion
📊 Key Features of Strategy 6 (FVG Mitigation)
Entry Conditions:
| Component | Weight | Description |
|---|---|---|
| FVG Mitigation | 6 pts | Price returning to unmitigated FVG |
| KI Trend Alignment | 6 pts | KI Fast + Slow + SuperSlow alignment |
| KI Wave Momentum | 2 pts | KI Wave color switch confirmation |
| RSI Confirmation | 4 pts | RSI cross + Divergence detection |
| Volatility | 3 pts | ATR expansion confirmation |
| FVG Quality | 4 pts | FVG size validation (20-50+ points) |
| Minimum Score | 16 pts | Required for signal generation |
FVG Validation:
✅ Minimum FVG Size: 20 points
✅ FVG Must Not Be Broken
✅ FVG Must Not Be Filled
✅ FVG Must Be Recent (within 50 bars)
✅ Price Must Be Mitigating FVG (within 10 points)
Risk Management:
// Stop Loss:
SL = Below/Above FVG edge ± 10 points buffer
// Take Profit Levels:
TP1 = 1:1 R:R (50% position, RF enabled)
TP2 = Next Swing Liquidity (50% position)
TP3 = 1:3 R:R (100% position)
Signal Filtering:
- ✅ Valid FVG: Minimum 20 points, not broken, not filled
- ✅ Mitigation: Price must be inside or near FVG (10 points tolerance)
- ✅ Recent FVG: Within 50 bars from formation
- ✅ KI Alignment: At least KI Fast must align with direction
- ✅ Minimum Score: 16 points (selective entries)
- ✅ FVG Quality: Larger FVGs score higher (up to 4 points)
📊 Expected Behavior
Bullish FVG Mitigation Setup:
- FVG Forms: Bullish FVG created by strong upward move
- Price Retraces: Price returns to FVG zone (mitigation)
- KI Confirms: KI Fast turns Green, price above KI Fast
- RSI: Shows bullish momentum or divergence
- Entry: On close of confirmation candle in FVG zone
- Target: Next swing high or 1:3 R:R
Bearish FVG Mitigation Setup:
- FVG Forms: Bearish FVG created by strong downward move
- Price Retraces: Price returns to FVG zone (mitigation)
- KI Confirms: KI Fast turns Red, price below KI Fast
- RSI: Shows bearish momentum or divergence
- Entry: On close of confirmation candle in FVG zone
- Target: Next swing low or 1:3 R:R
⚠️ Important Notes
- FVG Mitigation works best in trending markets - Avoid using in choppy/ranging conditions
- Wait for mitigation - Don't enter before price touches FVG zone
- FVG Quality matters - Larger FVGs (50+ points) have higher success rate
- Recent FVGs preferred - FVGs within 50 bars score higher
- Higher minimum score - 16 points vs 15 for other strategies (more selective)
- Best timeframes: M15, M30, H1, H4 for FVG detection
📈 Performance Expectations
| Metric | Expected Value |
|---|---|
| Win Rate | 60-70% |
| Frequency | 2-5 signals per week |
| R:R | 1:3 average |
| Best Session | London/NY Overlap |
| Best Pairs | EURUSD, GBPUSD, XAUUSD, US30 |