Complete Fair Value Gaps Detetion ...

This commit is contained in:
2024-11-14 15:34:51 +03:30
parent 849fc73ee2
commit 53f3f94b75
7 changed files with 14382 additions and 126 deletions
+247 -87
View File
@@ -223,6 +223,141 @@ class XCBarAnalyser : public XCBase
return result;
}
/**
* Detect Same Bars ...
*
* @param bar: XOHCL instance, Start Bar ...
* @param dir: ENUM_X_DIRECTION member, Holds Result Direction ...
* @param index: Integer, Holds Result Index ...
* @param sameBars: Iteger, Required Same Bar ...
*
* @return ( bool )
*/
bool HasSameBars(
XOHCL &bar,
ENUM_X_DIRECTION &dir,
int &index,
int sameBars = 3 //
)
{
//
bool result = false;
//
dir = X_DIRECTION_NONE;
//
if (sameBars < 1)
{
sameBars = 1;
}
//
result = bar.IsValid();
if (!result)
{
return result;
}
//
index = bar.Index();
bool canContinue = true;
int sameBullishBarsFounded = 0;
int sameBearishBarsFounded = 0;
bool canContinueForBullish = true;
bool canContinueForBearish = true;
while (canContinue)
{
//
XOHCL iBar;
result = iBar.Init(
bar.symbol,
bar.period,
index //
);
if (!result)
{
break;
}
//
bool isBullish = iBar.IsBullish();
bool isBearish = iBar.IsBearish();
result = isBullish ||
isBearish;
if (!result)
{
break;
}
//
if (isBullish)
{
//
sameBullishBarsFounded++;
canContinueForBearish = false;
}
else
{
//
sameBearishBarsFounded++;
canContinueForBullish = false;
}
//
result = canContinueForBullish ||
canContinueForBearish;
if (!result)
{
break;
}
//
canContinueForBullish =
canContinueForBullish &&
sameBullishBarsFounded > 0;
//
canContinueForBearish =
canContinueForBearish &&
sameBearishBarsFounded > 0;
//
canContinue =
canContinueForBullish ||
canContinueForBearish;
if (canContinue)
{
index++;
}
}
//
bool hasBullishSameBars =
sameBullishBarsFounded >= sameBars;
//
bool hasBearishSameBars =
sameBearishBarsFounded >= sameBars;
//
result = hasBullishSameBars ||
hasBearishSameBars;
if (!result)
{
return result;
}
//
dir =
hasBullishSameBars
? X_DIRECTION_BULLISH
: X_DIRECTION_BEARISH;
//
return result;
}
/**
* Detect Consolidation Range ...
*
@@ -1041,89 +1176,13 @@ class XCBarAnalyser : public XCBase
}
//
int index = bar.Index();
bool canContinue = true;
int sameBullishBarsFounded = 0;
int sameBearishBarsFounded = 0;
bool canContinueForBullish = true;
bool canContinueForBearish = true;
while (canContinue)
{
//
XOHCL iBar;
result = iBar.Init(
bar.symbol,
bar.period,
index //
);
if (!result)
{
break;
}
//
bool isBullish = iBar.IsBullish();
bool isBearish = iBar.IsBearish();
result = isBullish ||
isBearish;
if (!result)
{
break;
}
//
if (isBullish)
{
//
sameBullishBarsFounded++;
canContinueForBearish = false;
}
else
{
//
sameBearishBarsFounded++;
canContinueForBullish = false;
}
//
result = canContinueForBullish ||
canContinueForBearish;
if (!result)
{
break;
}
//
canContinueForBullish =
canContinueForBullish &&
sameBullishBarsFounded > 0;
//
canContinueForBearish =
canContinueForBearish &&
sameBearishBarsFounded > 0;
//
canContinue =
canContinueForBullish ||
canContinueForBearish;
if (canContinue)
{
index++;
}
}
//
bool hasBullishOB =
sameBullishBarsFounded >= sameBars;
//
bool hasBearishOB =
sameBearishBarsFounded >= sameBars;
//
result = hasBullishOB ||
hasBearishOB;
int index = 0;
result = HasSameBars(
bar,
dir,
index,
sameBars //
);
if (!result)
{
return result;
@@ -1142,10 +1201,111 @@ class XCBarAnalyser : public XCBase
}
//
dir =
hasBullishOB
? X_DIRECTION_BULLISH
: X_DIRECTION_BEARISH;
return result;
}
//
// Fair Value Gaps ...
/**
* Detect a Fair Value Gap Based on Given Bar ...
*
* @param bar: XOHCL instance, Specified Start Bar ...
* @param dir: ENUM_X_DIRECTION member, Specified Founded Order Block Direction ...
* @param sameBars: Integer, Specified Min Same Bars for Detecting Order Block ...
*
* @return ( bool )
*/
bool HasFairValueGap(
XOHCL &bar,
XOHCL &prevBar,
XOHCL &nextBar,
ENUM_X_DIRECTION &dir,
int sameBars = 3 //
)
{
//
bool result = false;
//
prevBar.Clean();
nextBar.Clean();
dir = X_DIRECTION_NONE;
//
if (sameBars < 1)
{
sameBars = 1;
}
//
result = bar.IsValid();
if (!result)
{
return result;
}
//
int index = 0;
result = HasSameBars(
bar,
dir,
index,
sameBars //
);
if (!result)
{
return result;
}
//
bool isBullish = IsBullish(dir);
//
int to = bar.Index() + 1;
int from = index + 1;
for (int i = from; i > to; i--)
{
//
result = prevBar.Init(
bar.symbol,
bar.period,
i //
);
if (!result)
{
break;
}
//
result = nextBar.Init(
bar.symbol,
bar.period,
i - 2 //
);
if (!result)
{
break;
}
//
result = isBullish
? prevBar.high < nextBar.low
: prevBar.low > nextBar.high;
if (result)
{
break;
}
}
//
if (!result)
{
//
prevBar.Clean();
nextBar.Clean();
dir = X_DIRECTION_NONE;
}
//
return result;
+138 -2
View File
@@ -885,12 +885,16 @@ class XCZoneObject : public XCBaseObject
//
bool result = false;
//
bool isValidFrom = zone.IsValidFrom();
bool isValidBoundary = zone.IsValidBoundary();
//
result =
//
IsValid(name) &&
zone.IsValidFrom() &&
zone.IsValidBoundary()
isValidFrom &&
isValidBoundary
//
;
if (!result)
@@ -1396,4 +1400,136 @@ class XCBearishOrderBlockObject : public XCZoneObject
}
};
//
// Bullish Fair Value Gaps ...
class XCBullishFairValueGapObject : public XCZoneObject
{
//
public:
//
/**
* Creator ...
*
* @param chart_id: Long, Specified Chart ID ...
* @param window: Integer, Specified Chart Window ID ...
* @param zone: XCOrderBlock instance ...
* @param prefix: String ...
*
* @return ( bool )
*/
bool Create(
long chart_id,
int window,
XCFVG &zone,
string prefix = NULL //
)
{
//
bool result = false;
//
result = zone.IsValid() &&
zone.IsBullish();
if (!result)
{
return result;
}
//
string name = zone.GetTag();
//
result = CreateByZone(
chart_id,
name,
window,
zone,
prefix //
);
//
return result;
}
//
// Virtual ...
/**
* Retrieve Object Type ...
*
* @return ( ENUM_XCHARTOBJECTS )
*/
virtual ENUM_XCHARTOBJECTS Type()
{
return X_BULLISH_FVG_OBJ;
}
};
//
// Bearish Fair Value Gaps ...
class XCBearishFairValueGapObject : public XCZoneObject
{
//
public:
//
/**
* Creator ...
*
* @param chart_id: Long, Specified Chart ID ...
* @param window: Integer, Specified Chart Window ID ...
* @param zone: XCOrderBlock instance ...
* @param prefix: String ...
*
* @return ( bool )
*/
bool Create(
long chart_id,
int window,
XCFVG &zone,
string prefix = NULL //
)
{
//
bool result = false;
//
result = zone.IsValid() &&
zone.IsBearish();
if (!result)
{
return result;
}
//
string name = zone.GetTag();
//
result = CreateByZone(
chart_id,
name,
window,
zone,
prefix //
);
//
return result;
}
//
// Virtual ...
/**
* Retrieve Object Type ...
*
* @return ( ENUM_XCHARTOBJECTS )
*/
virtual ENUM_XCHARTOBJECTS Type()
{
return X_BEARISH_FVG_OBJ;
}
};
//
+450
View File
@@ -407,6 +407,36 @@ class XCPOIDetector : public XCBaseAlert
mOrderBlocksSameBars = value;
}
//
// Fair Value Gaps ...
/**
* Get Fair Value Gaps Detection Same Bars ...
*
* @return ( int )
*/
int FairValueGapsSameBars()
{
return mFairValueGapsSameBars;
}
/**
* Set Fair Value Gaps Detection Same Bars ...
*
* @param value: Integer ...
*/
void FairValueGapsSameBars(int value)
{
//
if (value < 0)
{
value = 0;
}
//
mFairValueGapsSameBars = value;
}
//
// Actions ...
@@ -860,6 +890,68 @@ class XCPOIDetector : public XCBaseAlert
}
}
//
// Fair Value Gaps ...
int fvgSameBars = FairValueGapsSameBars();
if (fvgSameBars > 0)
{
//
// Bullish Fair Value Gap ...
isDetected = DetectBullishFairValueGap(cBar);
if (isDetected)
{
//
ENUM_XPOI_EVENTS event = X_BULLISH_FVG_DETECTED;
XCFVG *param;
bool hasItem = GetLastItem(
param,
mBullishFairValueGaps //
);
if (hasItem)
{
//
NotifyEventListeners(
event,
param //
);
}
//
Add(
event,
state //
);
}
//
// Bearish Fair Value Gap ...
isDetected = DetectBearishFairValueGap(cBar);
if (isDetected)
{
//
ENUM_XPOI_EVENTS event = X_BEARISH_FVG_DETECTED;
XCFVG *param;
bool hasItem = GetLastItem(
param,
mBearishFairValueGaps //
);
if (hasItem)
{
//
NotifyEventListeners(
event,
param //
);
}
//
Add(
event,
state //
);
}
}
//
ValidatePOIs(cBar);
@@ -959,6 +1051,10 @@ class XCPOIDetector : public XCBaseAlert
//
Clean(mBullishOrderBlocks);
Clean(mBearishOrderBlocks);
//
Clean(mBullishFairValueGaps);
Clean(mBearishFairValueGaps);
}
/**
@@ -1010,6 +1106,9 @@ class XCPOIDetector : public XCBaseAlert
//
OrderBlocksSameBars(3);
//
FairValueGapsSameBars(3);
}
//
@@ -1086,6 +1185,10 @@ class XCPOIDetector : public XCBaseAlert
int bullishOrderBlocksCount = 0;
int bearishOrderBlocksCount = 0;
//
int bullishFairValueGapsCount = 0;
int bearishFairValueGapsCount = 0;
//
bool canContinue = true;
@@ -1192,6 +1295,28 @@ class XCPOIDetector : public XCBaseAlert
}
}
//
// Fair Value Gaps ...
int fvgSameBars = FairValueGapsSameBars();
if (fvgSameBars > 0)
{
//
// Bullish Fair Value Gaps ...
bullishFairValueGapsCount = DetectRequiredBullishFairValueGaps(barIndex);
if (bullishFairValueGapsCount == requiredPOIs)
{
ArrayReverse(mBullishFairValueGaps);
}
//
// Bearish Fair Value Gaps ...
bearishFairValueGapsCount = DetectRequiredBearishFairValueGaps(barIndex);
if (bearishFairValueGapsCount == requiredPOIs)
{
ArrayReverse(mBearishFairValueGaps);
}
}
//
canContinue =
//
@@ -1253,6 +1378,16 @@ class XCPOIDetector : public XCBaseAlert
// Bearish Order Blocks ...
bearishOrderBlocksCount < requiredPOIs
//
||
//
// Bullish Fair Value Gaps ...
bullishFairValueGapsCount < requiredPOIs
//
||
//
// Bearish Fair Value Gaps ...
bearishFairValueGapsCount < requiredPOIs
//
;
if (canContinue)
{
@@ -3439,12 +3574,327 @@ class XCPOIDetector : public XCBaseAlert
//
// Fair Value Gaps ...
int mFairValueGapsSameBars; // Fair Value Gaps Detection Same Bars ...
//
// Bullish FVG ...
XCFVG *mBullishFairValueGaps[];
/**
* Detect Bullish Fair Value Gap ...
*
* @param bar: XOHCL instance ...
*
* @return ( bool )
*/
bool DetectBullishFairValueGap(XOHCL &bar)
{
//
bool result = false;
//
result = bar.IsValid();
if (!result)
{
return result;
}
//
int sameBars = FairValueGapsSameBars();
if (sameBars < 0)
{
return result;
}
//
XOHCL prevBar;
XOHCL nextBar;
ENUM_X_DIRECTION dir;
result = mBarAnalyser
.HasFairValueGap(
bar,
prevBar,
nextBar,
dir,
sameBars //
);
result =
result &&
dir == X_DIRECTION_BULLISH;
if (!result)
{
return result;
}
//
if (result)
{
//
XCFVG *fvg;
fvg = new XCFVG();
result = fvg.Init(
prevBar,
nextBar,
dir //
);
//
if (result)
{
//
datetime cTime = TimeCurrent();
fvg.To(cTime);
//
Add(
fvg,
mBullishFairValueGaps //
);
// //
// XCBullishFairValueGapObject *iObj;
// bool isCreated = mDrawer.CreateBullishFairValueGap(
// fvg,
// iObj //
// );
// if (isCreated)
// {
// AddObjectIfNotExists(iObj);
// }
}
}
//
return result;
}
/**
* Detect Required Bullish Fair Value Gaps ...
*
* @param barIndex: Integer Start bar index ...
* 0 is Default ...
*
* @return ( int )
*/
int DetectRequiredBullishFairValueGaps(int barIndex)
{
//
int result = 0;
//
if (barIndex < 0)
{
barIndex = 0;
}
//
int required = MaxNumberOfRequiredPOIs();
if (required <= 0)
{
return result;
}
//
int count = ArraySize(mBullishFairValueGaps);
if (count >= required)
{
//
result = count;
return result;
}
//
int sameBars = FairValueGapsSameBars();
if (sameBars < 0)
{
return result;
}
//
string symbol = GetSymbol();
ENUM_TIMEFRAMES period = GetPeriod();
//
XOHCL bar;
bool isInited = bar.Init(
symbol,
period,
barIndex //
);
if (!isInited)
{
return result;
}
//
DetectBullishFairValueGap(bar);
//
result = ArraySize(mBullishFairValueGaps);
//
return result;
}
//
// Bearish FVG ...
XCFVG *mBearishFairValueGaps[];
/**
* Detect Bearish Fair Value Gap ...
*
* @param bar: XOHCL instance ...
*
* @return ( bool )
*/
bool DetectBearishFairValueGap(XOHCL &bar)
{
//
bool result = false;
//
result = bar.IsValid();
if (!result)
{
return result;
}
//
int sameBars = FairValueGapsSameBars();
if (sameBars < 0)
{
return result;
}
//
XOHCL prevBar;
XOHCL nextBar;
ENUM_X_DIRECTION dir;
result = mBarAnalyser
.HasFairValueGap(
bar,
prevBar,
nextBar,
dir,
sameBars //
);
result =
result &&
dir == X_DIRECTION_BEARISH;
if (!result)
{
return result;
}
//
if (result)
{
//
XCFVG *fvg;
fvg = new XCFVG();
result = fvg.Init(
prevBar,
nextBar,
dir //
);
//
if (result)
{
//
datetime cTime = TimeCurrent();
fvg.To(cTime);
//
Add(
fvg,
mBearishFairValueGaps //
);
// //
// XCBearishFairValueGapObject *iObj;
// bool isCreated = mDrawer.CreateBearishFairValueGap(
// fvg,
// iObj //
// );
// if (isCreated)
// {
// AddObjectIfNotExists(iObj);
// }
}
}
//
return result;
}
/**
* Detect Required Bearish Fair Value Gaps ...
*
* @param barIndex: Integer Start bar index ...
* 0 is Default ...
*
* @return ( int )
*/
int DetectRequiredBearishFairValueGaps(int barIndex)
{
//
int result = 0;
//
if (barIndex < 0)
{
barIndex = 0;
}
//
int required = MaxNumberOfRequiredPOIs();
if (required <= 0)
{
return result;
}
//
int count = ArraySize(mBearishFairValueGaps);
if (count >= required)
{
//
result = count;
return result;
}
//
int sameBars = FairValueGapsSameBars();
if (sameBars < 0)
{
return result;
}
//
string symbol = GetSymbol();
ENUM_TIMEFRAMES period = GetPeriod();
//
XOHCL bar;
bool isInited = bar.Init(
symbol,
period,
barIndex //
);
if (!isInited)
{
return result;
}
//
DetectBearishFairValueGap(bar);
//
result = ArraySize(mBearishFairValueGaps);
//
return result;
}
//
// Liquidity Zones ...
+129 -3
View File
@@ -1109,7 +1109,7 @@ class XCPOIDrawer : public XCBase
*/
void BullishFVGFill(bool value)
{
mBearishFVGFill = value;
mBullishFVGFill = value;
}
/**
@@ -1352,13 +1352,13 @@ class XCPOIDrawer : public XCBase
BearishOrderBlockColor(clrRed);
//
BullishFVGFill(true);
BullishFVGFill(false);
BullishFVGWidth(1);
BullishFVGStyle(STYLE_DOT);
BullishFVGColor(clrLime);
//
BearishFVGFill(true);
BearishFVGFill(false);
BearishFVGWidth(1);
BearishFVGStyle(STYLE_DOT);
BearishFVGColor(clrRed);
@@ -2014,6 +2014,9 @@ class XCPOIDrawer : public XCBase
return result;
}
//
// Order Blocks ...
/**
* Create Bullish Order Block Object ...
*
@@ -2142,6 +2145,129 @@ class XCPOIDrawer : public XCBase
return result;
}
//
// Fair Value Gaps ...
/**
* Create Bullish Fair Value Gap Object ...
*
* @param zone: XCFVG instance ...
* @param object: XCBullishOrderBlockObject pointer ...
*
* @return ( bool )
*/
bool CreateBullishFairValueGap(
XCFVG &zone,
XCBullishFairValueGapObject *&object //
)
{
//
bool result = false;
//
result = zone.IsValid() &&
zone.IsBullish();
if (!result)
{
return result;
}
//
string prefix = Prefix();
long chartID = ChartIdentification();
int window = SubWindowIdentification();
//
bool fill = BullishFVGFill();
int width = BullishFVGWidth();
color crl = BullishFVGColor();
ENUM_LINE_STYLE style = BullishFVGStyle();
//
object = new XCBullishFairValueGapObject();
result = object.Create(
chartID,
window,
zone,
prefix //
);
//
if (!result)
{
return result;
}
//
object.ZoneFill(fill);
object.ZoneColor(crl);
object.ZoneWidth(width);
object.ZoneStyle(style);
//
return result;
}
/**
* Create Bearish Fair Value Gap Object ...
*
* @param zone: XCFVG instance ...
* @param object: XCBearishOrderBlockObject pointer ...
*
* @return ( bool )
*/
bool CreateBearishFairValueGap(
XCFVG &zone,
XCBearishFairValueGapObject *&object //
)
{
//
bool result = false;
//
result = zone.IsValid() &&
zone.IsBearish();
if (!result)
{
return result;
}
//
string prefix = Prefix();
long chartID = ChartIdentification();
int window = SubWindowIdentification();
//
bool fill = BearishFVGFill();
int width = BearishFVGWidth();
color crl = BearishFVGColor();
ENUM_LINE_STYLE style = BearishFVGStyle();
//
object = new XCBearishFairValueGapObject();
result = object.Create(
chartID,
window,
zone,
prefix //
);
//
if (!result)
{
return result;
}
//
object.ZoneFill(fill);
object.ZoneColor(crl);
object.ZoneWidth(width);
object.ZoneStyle(style);
//
return result;
}
//
protected:
//