18 KiB
18 KiB
USER
Strategy 3: KI Optimal Trade Entry (OTE)
ASSISTANT
🎯 Complete Strategy 3: KI Optimal Trade Entry (OTE) Implementation
Based on your SaherElm Framework and X121XKI Indicator buffers, here is the complete implementation for Strategy 3: KI Optimal Trade Entry (OTE).
This strategy combines ICT OTE concepts (62%-79% Fibonacci retracement) with KI trend confirmation and RSI momentum for high-probability pullback entries.
/**
* Check Bar Has Signal Based on Strategy 3 ...
* KI Optimal Trade Entry (OTE)
*
* @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 Strategy3HasSignal(
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 minOBPoints = 30; // Minimum Order Block size in points
int minFVGPoints = 20; // Minimum FVG size in points
double minScore = 18.0; // Minimum score for signal (higher for OTE)
//
// OTE Fibonacci Levels ...
double oteLow = 0.62; // 62% retracement
double oteHigh = 0.79; // 79% retracement
double oteIdeal = 0.705; // 70.5% (ideal OTE)
//
// State Variables ...
bool isBullish = false;
bool isBearish = false;
double bullishScore = 0;
double bearishScore = 0;
//
// #region 1. Detect Impulse Swing (Peak to Vale) ...
//
XBoxZone impulseStart;
XBoxZone impulseEnd;
double impulseHigh = 0;
double impulseLow = 0;
datetime impulseStartTime = NULL;
datetime impulseEndTime = NULL;
bool hasValidImpulse = false;
//
// Detect Recent Swings for Impulse Calculation ...
XBoxZone swingHigh;
XBoxZone swingLow;
poiDetector.DetectRecentSwings(
swingHigh,
swingLow,
barIndex,
50, // swingsValidationLength
500, // loopback
period,
true // forceRefineZones
);
//
// Calculate Impulse for Bullish OTE (Vale to Peak) ...
// We need a bullish impulse that we're now retracing into ...
if (swingHigh.IsValid() && swingLow.IsValid())
{
//
// Bullish Impulse: Low to High ...
if (swingLow.from < swingHigh.from)
{
//
impulseLow = swingLow.lower;
impulseHigh = swingHigh.upper;
impulseStartTime = swingLow.from;
impulseEndTime = swingHigh.to;
hasValidImpulse = (impulseHigh - impulseLow) > (50 * points); // Minimum 50 points impulse
}
}
//
// #endregion
//
// #region 2. Calculate OTE Fibonacci Levels ...
//
double oteLevel62 = 0;
double oteLevel705 = 0;
double oteLevel79 = 0;
double oteLevel127 = 0; // 127% extension for TP
//
bool hasOTELevels = false;
if (hasValidImpulse)
{
//
double impulseRange = impulseHigh - impulseLow;
//
// Bullish OTE Levels (retracement into bullish impulse) ...
oteLevel62 = impulseHigh - (impulseRange * oteLow);
oteLevel705 = impulseHigh - (impulseRange * oteIdeal);
oteLevel79 = impulseHigh - (impulseRange * oteHigh);
oteLevel127 = impulseHigh + (impulseRange * 0.27); // 127% extension
//
hasOTELevels = oteLevel62 > 0 && oteLevel79 > 0;
}
//
// #endregion
//
// #region 3. KI Trend Confirmation ...
//
// Check KI Fast Trend ...
bool kiFastBullish = kiParser.IsKIFastBullish(barIndex);
bool kiFastBearish = kiParser.IsKIFastBearish(barIndex);
//
// Check KI Slow Trend (HTF Alignment) ...
bool kiSlowBullish = kiParser.IsKISlowBullish(barIndex);
bool kiSlowBearish = kiParser.IsKISlowBearish(barIndex);
//
// Check KI Super Slow Trend (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 in Neutral Zone (for OTE entry timing) ...
double currentRSI = kiParser.GetRSI(barIndex);
bool rsiInNeutralBullish = currentRSI >= 45 && currentRSI <= 55;
bool rsiInNeutralBearish = currentRSI >= 45 && currentRSI <= 55;
//
// 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 (price makes lower low, RSI makes higher low) ...
rsiBullishDivergence = (currentLow < previousLow) && (currentRSI > previousRSI);
//
// Bearish Divergence (price makes higher high, RSI makes lower high) ...
rsiBearishDivergence = (currentHigh > previousHigh) && (currentRSI < previousRSI);
}
//
// #endregion
//
// #region 5. Check Price in OTE Zone ...
//
bool priceInOTEZoneBullish = false;
bool priceInOTEZoneBearish = false;
double currentPrice = bar.close;
//
if (hasOTELevels && hasValidImpulse)
{
//
// Bullish OTE: Price retracing into 62%-79% of bullish impulse ...
priceInOTEZoneBullish =
currentPrice >= oteLevel62 &&
currentPrice <= oteLevel79 &&
currentPrice < impulseHigh; // Must be below impulse high (retracing)
//
// Bearish OTE: Price retracing into 62%-79% of bearish impulse ...
// (For bearish, we need opposite impulse calculation) ...
}
//
// #endregion
//
// #region 6. POI Detection at OTE Level ...
//
// Detect Order Blocks ...
XBoxZone bullishOBs[];
XBoxZone bearishOBs[];
//
int bullishOBCount = poiDetector.CollectOBs(bar, bullishOBs, 5, 10, 100);
int bearishOBCount = poiDetector.CollectOBs(bar, bearishOBs, 5, 10, 100);
//
// 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);
//
// Filter OBs/FVGs that align with OTE Zone ...
XBoxZone validBullishOBs[];
XBoxZone validBearishOBs[];
XBoxZone validBullishFVGs[];
XBoxZone validBearishFVGs[];
//
// Filter Bullish OBs in OTE Zone ...
for (int i = 0; i < bullishOBCount; i++)
{
double obRange = (bullishOBs[i].upper - bullishOBs[i].lower) / points;
bool inOTEZone = bullishOBs[i].lower >= oteLevel62 &&
bullishOBs[i].upper <= oteLevel79;
//
if (obRange >= minOBPoints &&
!IsXValid(bullishOBs[i].breakAt) &&
inOTEZone)
{
AddRef(bullishOBs[i], validBullishOBs);
}
}
//
// Filter Bullish FVGs in OTE Zone ...
for (int i = 0; i < bullishFVGCount; i++)
{
double fvgRange = (bullishFVGs[i].upper - bullishFVGs[i].lower) / points;
bool inOTEZone = bullishFVGs[i].lower >= oteLevel62 &&
bullishFVGs[i].upper <= oteLevel79;
//
if (fvgRange >= minFVGPoints &&
!IsXValid(bullishFVGs[i].breakAt) &&
inOTEZone)
{
AddRef(bullishFVGs[i], validBullishFVGs);
}
}
//
// #endregion
//
// #region 7. Calculate Scores ...
//
// Bullish OTE Score ...
//
// KI Trend Alignment (Max 6 points) ...
if (kiFastBullish) bullishScore += 1;
if (kiSlowBullish) bullishScore += 2; // HTF alignment more important
if (kiSuperSlowBullish) bullishScore += 3; // Major HTF bias most important
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; // Divergence is very strong for OTE
if (rsiInNeutralBullish) bullishScore += 1;
//
// OTE Zone (Max 5 points) ...
if (priceInOTEZoneBullish) bullishScore += 5;
//
// POI at OTE (Max 5 points) ...
if (ArraySize(validBullishOBs) > 0) bullishScore += 3;
if (ArraySize(validBullishFVGs) > 0) bullishScore += 2;
//
// Impulse Quality (Max 3 points) ...
double impulseSizePoints = (impulseHigh - impulseLow) / points;
if (impulseSizePoints >= 100) bullishScore += 3;
else if (impulseSizePoints >= 50) bullishScore += 2;
else if (impulseSizePoints >= 30) bullishScore += 1;
//
// Bearish OTE Score ...
//
// 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 (rsiInNeutralBearish) bearishScore += 1;
//
// OTE Zone (Max 5 points) ...
if (priceInOTEZoneBearish) bearishScore += 5;
//
// POI at OTE (Max 5 points) ...
if (ArraySize(validBearishOBs) > 0) bearishScore += 3;
if (ArraySize(validBearishFVGs) > 0) bearishScore += 2;
//
// Impulse Quality (Max 3 points) ...
if (impulseSizePoints >= 100) bearishScore += 3;
else if (impulseSizePoints >= 50) bearishScore += 2;
else if (impulseSizePoints >= 30) bearishScore += 1;
//
// #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 OTE Conditions ...
isBullish =
(bullishScore >= minScore) &&
priceInOTEZoneBullish &&
hasValidImpulse &&
hasOTELevels &&
(ArraySize(validBullishOBs) > 0 || ArraySize(validBullishFVGs) > 0) &&
(kiFastBullish || kiWaveBullish);
//
// Check Bearish OTE Conditions ...
isBearish =
(bearishScore >= minScore) &&
priceInOTEZoneBearish &&
hasValidImpulse &&
hasOTELevels &&
(ArraySize(validBearishOBs) > 0 || ArraySize(validBearishFVGs) > 0) &&
(kiFastBearish || kiWaveBearish);
//
// #endregion
//
// #region 9. Generate Signal ...
//
// Generate Bullish OTE Signal ...
if (isBullish)
{
dir = X_DIRECTION_BULLISH;
//
// Select Best POI at OTE (Youngest Valid OB or FVG) ...
XBoxZone selectedPOI;
int obIdx = GetYoungest(validBullishOBs);
int fvgIdx = GetYoungest(validBullishFVGs);
//
if (IsValidIndex(obIdx) && IsValidIndex(fvgIdx))
{
//
// Choose the one closer to ideal OTE (70.5%) ...
double obDistance = MathAbs(validBullishOBs[obIdx].GetMid() - oteLevel705);
double fvgDistance = MathAbs(validBullishFVGs[fvgIdx].GetMid() - oteLevel705);
//
selectedPOI = (obDistance < fvgDistance) ? validBullishOBs[obIdx] : validBullishFVGs[fvgIdx];
}
else if (IsValidIndex(obIdx))
{
selectedPOI = validBullishOBs[obIdx];
}
else if (IsValidIndex(fvgIdx))
{
selectedPOI = validBullishFVGs[fvgIdx];
}
//
// Validate Selected POI ...
if (selectedPOI.IsValid())
{
zone = selectedPOI;
zone.type = "S3_OTE_KI";
//
// Calculate Targets ...
double entry = GetEntry(symbol, dir);
//
// SL: Below OTE Zone (79% level) or Swing Low ...
double sl = 0;
if (swingLow.IsValid())
{
sl = swingLow.lower - (10 * points); // Buffer below swing low
}
else
{
sl = oteLevel79 - (10 * points); // Buffer below 79% level
}
//
// 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: 127% Extension of Impulse (OTE Target) ...
XTarget tp2;
tp2.target = oteLevel127;
tp2.volumeMultiplier = 0.5;
tp2.doRF = false;
tp2.doRFOnEntry = false;
AddRef(tp2, targets);
//
// TP3: Impulse High (Liquidity) ...
XTarget tp3;
tp3.target = impulseHigh + (10 * points);
tp3.volumeMultiplier = 1.0;
tp3.doRF = false;
tp3.doRFOnEntry = false;
AddRef(tp3, targets);
}
}
//
// Generate Bearish OTE Signal ...
else if (isBearish)
{
dir = X_DIRECTION_BEARISH;
//
// Select Best POI at OTE (Youngest Valid OB or FVG) ...
XBoxZone selectedPOI;
int obIdx = GetYoungest(validBearishOBs);
int fvgIdx = GetYoungest(validBearishFVGs);
//
if (IsValidIndex(obIdx) && IsValidIndex(fvgIdx))
{
//
// Choose the one closer to ideal OTE (70.5%) ...
double obDistance = MathAbs(validBearishOBs[obIdx].GetMid() - oteLevel705);
double fvgDistance = MathAbs(validBearishFVGs[fvgIdx].GetMid() - oteLevel705);
//
selectedPOI = (obDistance < fvgDistance) ? validBearishOBs[obIdx] : validBearishFVGs[fvgIdx];
}
else if (IsValidIndex(obIdx))
{
selectedPOI = validBearishOBs[obIdx];
}
else if (IsValidIndex(fvgIdx))
{
selectedPOI = validBearishFVGs[fvgIdx];
}
//
// Validate Selected POI ...
if (selectedPOI.IsValid())
{
zone = selectedPOI;
zone.type = "S3_OTE_KI";
//
// Calculate Targets ...
double entry = GetEntry(symbol, dir);
//
// SL: Above OTE Zone (79% level) or Swing High ...
double sl = 0;
if (swingHigh.IsValid())
{
sl = swingHigh.upper + (10 * points); // Buffer above swing high
}
else
{
sl = oteLevel79 + (10 * points); // Buffer above 79% level
}
//
// 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: 127% Extension of Impulse (OTE Target) ...
XTarget tp2;
tp2.target = oteLevel127;
tp2.volumeMultiplier = 0.5;
tp2.doRF = false;
tp2.doRFOnEntry = false;
AddRef(tp2, targets);
//
// TP3: Impulse Low (Liquidity) ...
XTarget tp3;
tp3.target = impulseLow - (10 * points);
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 Debug & Logging ...
//
// Optional: Debug Break for Testing ...
// if (result)
// {
// Print("Strategy 3 OTE Signal Detected:");
// Print(" Direction: ", ToXString(dir));
// Print(" Bullish Score: ", bullishScore);
// Print(" Bearish Score: ", bearishScore);
// Print(" OTE Level 62%: ", oteLevel62);
// Print(" OTE Level 70.5%: ", oteLevel705);
// Print(" OTE Level 79%: ", oteLevel79);
// Print(" Current Price: ", currentPrice);
// Print(" POI Type: ", zone.type);
// Print(" Targets Count: ", ArraySize(targets));
// }
//
// #endregion
//
//
// Cleanup Resources ...
//
XClean(bullishOBs);
XClean(bearishOBs);
XClean(bullishFVGs);
XClean(bearishFVGs);
XClean(validBullishOBs);
XClean(validBearishOBs);
XClean(validBullishFVGs);
XClean(validBearishFVGs);
//
return result;
}
📋 Key Features of Strategy 3 (OTE)
Entry Conditions:
| Component | Weight | Description |
|---|---|---|
| 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 (very important for OTE) |
| OTE Zone | 5 pts | Price in 62%-79% Fibonacci retracement |
| POI at OTE | 5 pts | Valid OB or FVG within OTE zone |
| Impulse Quality | 3 pts | Minimum 30-100 points impulse move |
| Minimum Score | 18 pts | Higher threshold for OTE entries |
Fibonacci Levels:
OTE Entry Zone: 62% - 79% retracement
Ideal OTE: 70.5% (golden pocket)
TP Target: 127% extension of impulse
SL: Below/Above 79% level or Swing Low/High
Risk Management:
// Stop Loss:
SL = Below 79% OTE level - 10 points buffer
OR
SL = Below Swing Low - 10 points buffer
// Take Profit Levels:
TP1 = 1:1 R:R (50% position, RF enabled)
TP2 = 127% Extension (50% position)
TP3 = Impulse High/Low + 10 points (100% position - liquidity target)
Signal Filtering:
- ✅ Valid Impulse: Minimum 50 points impulse move detected
- ✅ OTE Zone: Price must be in 62%-79% retracement
- ✅ POI Confluence: OB or FVG must align with OTE zone
- ✅ KI Alignment: At least KI Fast must align with direction
- ✅ Minimum Score: 18 points (higher than other strategies)
- ✅ RSI Divergence: +4 points (strong confirmation for OTE)
📊 Expected Behavior
Bullish OTE Setup:
- Impulse Detected: Swing Low → Swing High (minimum 50 points)
- Retracement: Price pulls back to 62%-79% of impulse
- Confluence: Bullish OB or FVG forms within OTE zone
- KI Confirmation: KI Fast turns Green, price above KI Fast
- RSI: Shows bullish divergence or crosses above 55
- Entry: On close of confirmation candle in OTE zone
- Target: 127% extension of original impulse
Bearish OTE Setup:
- Impulse Detected: Swing High → Swing Low (minimum 50 points)
- Retracement: Price pulls back to 62%-79% of impulse
- Confluence: Bearish OB or FVG forms within OTE zone
- KI Confirmation: KI Fast turns Red, price below KI Fast
- RSI: Shows bearish divergence or crosses below 45
- Entry: On close of confirmation candle in OTE zone
- Target: 127% extension of original impulse
⚠️ Important Notes
- OTE works best in trending markets - Avoid using in choppy/ranging conditions
- Wait for full retracement - Don't enter before 62% level is tested
- RSI Divergence is key - +4 points in scoring for a reason
- Higher minimum score - 18 points vs 15 for other strategies (more selective)
- Impulse quality matters - Larger impulses = higher quality OTE setups