Files
MQL5Data/X121SMCEA/Libraries/x-121.smc.lib.mq5
T
2025-01-07 02:57:14 +03:30

1056 lines
22 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Common Library
// --------------------------------------
// Name: X121SMCLib
// Description: provide all commonly used functions ...
//
//
// Maintainer:
// ------------
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
//
//////////////////////////////////////////////////////
//
// Global Properties ...
#property library
#property copyright "Copyright 2023, SaherElm IT Center"
#property link "https://www.saherelm.ir"
#property version "1.00"
#property strict
//
// Imports ...
#include "../../Classes/x-saherelm.base.class.mq5"
#include "../../Classes/x-saherelm.x-poi.drawer.class.mq5"
#include "../../Helpers/x-saherelm.x121.helper.mq5"
#include "../../Libraries/x-saherelm.common.lib.mq5"
#include "../../Libraries/x-saherelm.x-poi.lib.mq5"
#include "../../Libraries/x-saherelm.x-trade.lib.mq5"
//
// Definitions ...
//
const string X121SMCStrategyToken = "X121SMC";
//
const string X121SMCXTestToken = "X121XTest";
const string X121SMCXTWPVToken = "X121XTWPV";
//
// X121 SMC Providers ...
enum ENUM_X_121_SMC_PROVIDERS
{
//
X_121_SMC_PROVIDER_NONE,
X_121_SMC_PROVIDER_XTEST,
X_121_SMC_PROVIDER_XTWPV,
};
//
// Each Market Cycle (Time Frame) Conditions
// Model as this Structure ...
struct X121SMCCycleConditions
{
//
string symbol;
ENUM_TIMEFRAMES period;
datetime time;
//
X121Conditions x121Conditions;
//
XPOIState state;
//
void X121SMCStrategyConditions()
{
Clean();
}
//
// Tools ...
/**
* Cleanup ...
*/
void Clean()
{
//
symbol = NULL;
period = NULL;
time = NULL;
//
state.Clean();
x121Conditions.Clean();
}
/**
* Validate ...
*
* @return ( bool )
*/
bool IsValid()
{
//
bool result = false;
//
result = IsSpecifiedValid(symbol) &&
IsSpecifiedValid(period) &&
IsSpecifiedValid(time);
//
;
//
return result;
}
//
// Reporter Functions ...
/**
* Generate Provided Scores ...
*
* @param bullishScore: Integer, reference ...
* @param bearishScore: Integer, referenceF ...
*/
void GenerateScore(
double &bullishScore,
double &bearishScore //
)
{
//
bullishScore = 0;
bearishScore = 0;
//
if (!IsValid())
{
return;
}
//
double bullScore = 0;
double bearScore = 0;
}
/**
* Generate Summary ...
*
* @param onlyCommons: Boolean ...
* @param onlyConditions: Boolean ...
* @param includeScores: Boolean ...
* @param ignoreFalseConditions: Boolean ...
* @param separator: String ...
*
* @return ( string )
*/
string GenerateSummary(
bool onlyCommons = false,
bool onlyConditions = false,
bool includeScores = true,
bool ignoreFalseConditions = true,
string separator = "\n" //
)
{
//
string result = NULL;
//
double bullishScore = 0;
double bearishScore = 0;
GenerateScore(
bullishScore,
bearishScore //
);
//
string commonStr = GenerateSpecifiedCommonSummary(
this,
separator,
includeScores,
true //
);
//
string conditionsStr =
//
""
//
;
//
result =
//
"[" + GetTag() + "]" + separator +
"----------" + separator +
(onlyConditions
? ""
: commonStr) +
" " + separator +
(onlyCommons
? ""
: conditionsStr) +
""
//
;
//
return result;
}
/**
* Get Unique Identifier ...
*
* @param forObject: Boolean ...
*
* @return ( string )
*/
string GetTag(
bool forObject = false //
)
{
//
string result = NULL;
//
string typeName = GetTypeName(this);
//
if (!forObject)
{
result = typeName;
}
else
{
//
result = typeName + "_" +
symbol + "_" + ToString(period) + "_" +
ToMD5(time);
}
//
return result;
}
//
};
//
// Model Signalling Conditions ...
struct X121SMCStrategyConditions
{
//
string symbol;
ENUM_TIMEFRAMES period;
datetime time;
//
double sl;
double tp;
double target;
string provider;
ENUM_X_DIRECTION signalDir;
//
X121Conditions conditions;
// XPriceZones priceZones;
// XPriceZones decisionZones;
// XMarketStructure marketStructure;
//
void X121SMCStrategyConditions()
{
Clean();
}
//
// Tools ...
/**
* Cleanup ...
*/
void Clean()
{
//
symbol = NULL;
period = NULL;
time = NULL;
//
sl = 0;
tp = 0;
provider = NULL;
signalDir = X_DIRECTION_NONE;
//
conditions.Clean();
// priceZones.Clean();
// decisionZones.Clean();
// marketStructure.Clean();
}
/**
* Validate ...
*
* @return ( bool )
*/
bool IsValid()
{
//
bool result = false;
//
result = IsSpecifiedValid(symbol) &&
IsSpecifiedValid(period) &&
IsSpecifiedValid(time);
//
return result;
}
/**
* Check Conditions Has Valid Bullish Signal ...
*
* @return ( bool )
*/
bool HasBullishSignal()
{
//
bool result = false;
//
result =
(sl > 0 ||
tp > 0) &&
IsValid() &&
HasDirection(signalDir);
if (!result)
{
return result;
}
//
result =
IsBullish(signalDir);
//
return result;
}
/**
* Check Conditions Has Valid Bearish Signal ...
*
* @return ( bool )
*/
bool HasBearishSignal()
{
//
bool result = false;
//
result =
(sl > 0 ||
tp > 0) &&
IsValid() &&
HasDirection(signalDir);
if (!result)
{
return result;
}
//
result =
IsBearish(signalDir);
//
return result;
}
//
// Reporter Functions ...
/**
* Generate Provided Scores ...
*
* @param bullishScore: Integer, reference ...
* @param bearishScore: Integer, referenceF ...
*/
void GenerateScore(
double &bullishScore,
double &bearishScore //
)
{
//
bullishScore = 0;
bearishScore = 0;
//
if (!IsValid())
{
return;
}
//
double bullScore = 0;
double bearScore = 0;
}
/**
* Generate Summary ...
*
* @param onlyCommons: Boolean ...
* @param onlyConditions: Boolean ...
* @param includeScores: Boolean ...
* @param ignoreFalseConditions: Boolean ...
* @param separator: String ...
*
* @return ( string )
*/
string GenerateSummary(
bool onlyCommons = false,
bool onlyConditions = false,
bool includeScores = true,
bool ignoreFalseConditions = true,
string separator = "\n" //
)
{
//
string result = NULL;
//
double bullishScore = 0;
double bearishScore = 0;
GenerateScore(
bullishScore,
bearishScore //
);
//
string commonStr = GenerateSpecifiedCommonSummary(
this,
separator,
includeScores,
true //
);
//
string conditionsStr =
//
""
//
;
//
result =
//
"[" + GetTag() + "]" + separator +
"----------" + separator +
(onlyConditions
? ""
: commonStr) +
" " + separator +
(onlyCommons
? ""
: conditionsStr) +
""
//
;
//
return result;
}
/**
* Get Unique Identifier ...
*
* @param forObject: Boolean ...
*
* @return ( string )
*/
string GetTag(
bool forObject = false //
)
{
//
string result = NULL;
//
string typeName = GetTypeName(this);
//
if (!forObject)
{
result = typeName;
}
else
{
//
result = typeName + "_" +
symbol + "_" + ToString(period) + "_" +
ToMD5(time);
}
//
return result;
}
//
};
/**
* Draw X121 SMC Conditions ...
*
* @param conditions: X121SMCStrategyConditions instance Reference ...
* @param drawMarketStructure: Boolean ...
* @param drawPriceZones: Boolean ...
* @param drawDecisionZones: Boolean ...
*
* @return ( int )
*/
int DrawX121SMCStrategyConditions(
X121SMCStrategyConditions &conditions,
XCBaseObject *&drawnObjects[],
XCPOIDrawer *drawer,
bool drawMarketStructure = true,
bool drawPriceZones = true,
bool drawDecisionZones = true,
bool drawOnlyPriceZonesBoundary = true //
)
{
//
int result = 0;
//
Clean(drawnObjects);
//
if (drawer == NULL ||
!conditions.IsValid() ||
!HasDirection(conditions.signalDir))
{
return result;
}
//
bool isBullish = IsBullish(conditions.signalDir);
// //
// bool hasPriceZones = conditions.priceZones.IsValid();
// if (hasPriceZones &&
// drawPriceZones)
// {
// //
// XCBaseObject *objects[];
// int objectsCount = drawer.DrawPriceZone(
// conditions.priceZones,
// objects,
// conditions.signalDir,
// drawOnlyPriceZonesBoundary //
// );
// if (IsValidSize(objectsCount))
// {
// //
// Copy(
// objects,
// drawnObjects,
// false //
// );
// }
// }
// //
// bool hasDecisionZones = conditions.decisionZones.IsValid();
// if (hasDecisionZones &&
// drawDecisionZones)
// {
// //
// XCBaseObject *objects[];
// int objectsCount = drawer.DrawPriceZone(
// conditions.decisionZones,
// objects,
// conditions.signalDir,
// drawOnlyPriceZonesBoundary //
// );
// if (IsValidSize(objectsCount))
// {
// //
// Copy(
// objects,
// drawnObjects,
// false //
// );
// }
// }
// //
// bool hasMarketStructure = conditions.marketStructure.IsValid();
// if (hasMarketStructure &&
// drawMarketStructure)
// {
// //
// XCBaseObject *objects[];
// int objectsCount = drawer.DrawMarketStructure(
// conditions.marketStructure,
// objects //
// );
// if (IsValidSize(objectsCount))
// {
// //
// Copy(
// objects,
// drawnObjects,
// false //
// );
// }
// }
//
result = ArraySize(drawnObjects);
//
return result;
}
//
//
//
typedef void (*TOnX121SMCSignal)(
XSignal &signal,
X121SMCStrategyConditions &conditions //
);
//
// Extentions ...
/**
* Validate a Signal Provider ...
*
* @param value: ENUM_X_121_SMC_PROVIDERS member ...
*
* @return ( bool )
*/
bool IsValid(ENUM_X_121_SMC_PROVIDERS value)
{
//
bool result = false;
//
result = value != X_121_SMC_PROVIDER_NONE;
//
return result;
}
/**
* Converts a Signal Provider to String ...
*
* @param value: ENUM_X_121_SMC_PROVIDERS member ...
*
* @return ( string )
*/
string ToString(ENUM_X_121_SMC_PROVIDERS value)
{
//
string result = NULL;
//
if (!IsValid(value))
{
return result;
}
//
switch (value)
{
//
case X_121_SMC_PROVIDER_XTEST:
result = X121SMCXTestToken;
break;
//
case X_121_SMC_PROVIDER_XTWPV:
result = X121SMCXTWPVToken;
break;
}
//
return result;
}
/**
* Parse Specified String as a Signal Provider ...
*
* @param value: String ...
*
* @return ( ENUM_X_121_SMC_PROVIDERS )
*/
ENUM_X_121_SMC_PROVIDERS ParseProvider(string value)
{
//
ENUM_X_121_SMC_PROVIDERS result = X_121_SMC_PROVIDER_NONE;
//
if (!IsValid(value))
{
return result;
}
//
if (Contains(X121SMCXTestToken, value))
{
result = X_121_SMC_PROVIDER_XTEST;
}
else if (Contains(X121SMCXTWPVToken, value))
{
result = X_121_SMC_PROVIDER_XTWPV;
}
//
return result;
}
//
//
bool TestPOIState(
XCPOIDrawer *drawer,
ENUM_X_DIRECTION &forDir, // Test For Specified Direction ...
XFVGOrderBlock &bullishOrderBlocks[], // Bullish Order Blocks ...
XFVGOrderBlock &bearishOrderBlocks[], // Bearish Order Blocks ...
XPOIState &orderBlockState, // Order Block Detection TF POI(s) ...
XPOIState &fairValueGapState, // Fair Value Gap Detection TF POI(s) ...
XPOIState &triggerState, // Trigger TF POI(s) ...
int barIndex = 0 // Bar Index ...
)
{
//
bool result = false;
//
if (barIndex < 0)
{
barIndex = 0;
}
//
forDir = X_DIRECTION_NONE;
//
// Validate Args ...
result =
drawer != NULL &&
orderBlockState.HasChild() &&
fairValueGapState.HasChild() &&
triggerState.HasChild();
if (!result)
{
return result;
}
//
string symbol = orderBlockState.symbol;
ENUM_TIMEFRAMES period = orderBlockState.period;
//
// Retrieve Bars ...
XOHCL triggerCBar;
result = triggerCBar.Init(
symbol,
period,
barIndex //
);
//
// Select Order Blocks Which Has Big Changes ...
int count = 0;
datetime cTime = TimeCurrent();
//
// Bullish ...
int bullishOrderBlocksCount = orderBlockState.CountBullishOrderBlocks();
count = bullishOrderBlocksCount;
if (IsValidSize(count))
{
//
for (int i = count - 1; i >= 0; i--)
{
//
XCFVG *iFVG = NULL;
XCOrderBlock *iOb = orderBlockState.bullishOrderBlocks[i];
iOb.To(cTime);
//
// Find Exit Bar of Bullish OrderBlock Upper ...
XOHCL breakerBar;
bool hasBreakerBar = iOb.DetectBreakerBar(breakerBar);
if (!hasBreakerBar)
{
continue;
}
//
// Detect Order Block is Hunted Or Not ...
XOHCL huntedBar;
bool isHunted = iOb.IsHunted(huntedBar);
if (isHunted)
{
continue;
}
//
// Check Validation of Order Block ...
bool isValid = iOb.IsValidOrderBlock();
if (!isValid)
{
continue;
}
//
// Check Order Block Has Fair Value Gap ...
int fvgsCount = fairValueGapState.CountBullishFairValueGaps();
bool hasFVG = IsValidSize(fvgsCount);
if (!hasFVG)
{
continue;
}
//
for (int j = fvgsCount - 1; j >= 0; j--)
{
//
XCFVG *jFVG = fairValueGapState.bullishFairValueGaps[j];
//
bool isValid =
//
jFVG.IsBullish()
// &&
// jFVG.From() > iOb.From() &&
// ((jFVG.Upper() > iOb.Upper() &&
// jFVG.Lower() <= iOb.Upper()) ||
// (jFVG.Upper() <= iOb.Upper() &&
// jFVG.Lower() >= iOb.Lower()))
//
;
if (isValid)
{
//
jFVG.To(cTime);
iFVG = jFVG;
break;
}
}
//
hasFVG = iFVG != NULL;
result = hasFVG;
if (!result)
{
return result;
}
//
XFVGOrderBlock iModel;
iModel.orderBlock = iOb;
iModel.fairValueGap = iFVG;
//
AddRef(
iModel,
bullishOrderBlocks //
);
//
XCBullishOrderBlockObject *iOBObject;
bool isInited = drawer.CreateBullishOrderBlock(
iOb,
iOBObject //
);
if (isInited)
{
}
//
XCOHCLObject *iBarObj;
isInited = drawer.CreateBar(
breakerBar,
iBarObj,
cTime //
);
if (isInited)
{
}
}
}
//
// Bearish ...
int bearishOrderBlocksCount = orderBlockState.CountBearishOrderBlocks();
count = bearishOrderBlocksCount;
if (IsValidSize(count))
{
//
for (int i = count - 1; i >= 0; i--)
{
//
XCFVG *iFVG = NULL;
XCOrderBlock *iOb = orderBlockState.bearishOrderBlocks[i];
iOb.To(cTime);
//
// Find Exit Bar of Bullish OrderBlock Upper ...
XOHCL breakerBar;
bool hasBreakerBar = iOb.DetectBreakerBar(breakerBar);
if (!hasBreakerBar)
{
continue;
}
//
// Detect Order Block is Hunted Or Not ...
XOHCL huntedBar;
bool isHunted = iOb.IsHunted(huntedBar);
if (isHunted)
{
continue;
}
//
// Check Validation of Order Block ...
bool isValid = iOb.IsValidOrderBlock();
if (!isValid)
{
continue;
}
//
// Check Order Block Has Fair Value Gap ...
int fvgsCount = fairValueGapState.CountBearishFairValueGaps();
bool hasFVG = IsValidSize(fvgsCount);
if (!hasFVG)
{
continue;
}
//
for (int j = fvgsCount - 1; j >= 0; j--)
{
//
XCFVG *jFVG = fairValueGapState.bearishFairValueGaps[j];
//
bool isValid =
//
jFVG.IsBearish()
// &&
// jFVG.From() > iOb.From() &&
// jFVG.Upper() > iOb.Lower() &&
// jFVG.Lower() <= iOb.Lower()
//
;
if (isValid)
{
//
jFVG.To(cTime);
iFVG = jFVG;
break;
}
}
//
hasFVG = iFVG != NULL;
result = hasFVG;
if (!result)
{
return result;
}
//
XFVGOrderBlock iModel;
iModel.orderBlock = iOb;
iModel.fairValueGap = iFVG;
//
AddRef(
iModel,
bullishOrderBlocks //
);
//
XCBearishOrderBlockObject *iOBObject;
bool isInited = drawer.CreateBearishOrderBlock(
iOb,
iOBObject //
);
if (isInited)
{
}
//
XCOHCLObject *iBarObj;
isInited = drawer.CreateBar(
breakerBar,
iBarObj,
cTime //
);
if (isInited)
{
}
}
}
//
// Check Order Blocks ...
bullishOrderBlocksCount = ArraySize(bullishOrderBlocks);
bool hasBullishOrderBlocks = IsValidSize(bullishOrderBlocksCount);
//
// Check Order Blocks ...
bearishOrderBlocksCount = ArraySize(bearishOrderBlocks);
bool hasBearishOrderBlocks = IsValidSize(bearishOrderBlocksCount);
//
result =
//
hasBullishOrderBlocks ||
hasBearishOrderBlocks
//
;
if (!result)
{
return result;
}
//
bool isBullish = hasBullishOrderBlocks && !hasBearishOrderBlocks;
bool isBearish = hasBearishOrderBlocks && !hasBullishOrderBlocks;
bool isAll = hasBullishOrderBlocks && hasBearishOrderBlocks;
//
forDir =
isBullish
? X_DIRECTION_BULLISH
: isBearish
? X_DIRECTION_BEARISH
: isAll
? X_DIRECTION_ALL
: X_DIRECTION_NONE;
//
result = forDir != X_DIRECTION_NONE;
if (!result)
{
return result;
}
//
return result;
}
//