Files
MQL5Data/Libraries/x-saherelm.x-poi.lib.mq5
T
2025-10-07 18:09:38 +03:30

1573 lines
27 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Seriallize Library
// ---------------------------------------
// Name: XPOILib
// Description: All models related to POIs ...
//
//
// 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 "../Libraries/x-saherelm.common.lib.mq5"
#include <Arrays/ArrayObj.mqh>
//
// Definitions ...
/**
* All Available Market Patterns ...
**/
enum ENUM_X_BAR_PATTERN
{
X_BAR_PATTERN_NONE = 0,
X_BAR_PATTERN_HIGH = 1,
X_BAR_PATTERN_LOW = 2,
X_BAR_PATTERN_MOMENTUM = 3,
X_BAR_PATTERN_ENGULFED = 4,
X_BAR_PATTERN_TRUE_GAPED = 5,
X_BAR_PATTERN_PINNED = 6,
X_BAR_PATTERN_STAR = 7,
X_BAR_PATTERN_PIERCING = 8,
X_BAR_PATTERN_RISING = 9,
X_BAR_PATTERN_FLAG = 10,
X_BAR_PATTERN_SIGNALKEY = 11,
X_BAR_PATTERN_OB = 12,
X_BAR_PATTERN_FVG = 13,
X_BAR_PATTERN_SUPPORT = 14,
X_BAR_PATTERN_RESISTANCE = 15,
};
/**
* Validate an ENUM member ...
*
* @param value: ENUM_X_BAR_PATTERN ...
*
* @return ( bool )
*/
bool IsXValid(ENUM_X_BAR_PATTERN value)
{
return value != X_BAR_PATTERN_NONE;
}
/**
* Converts a Pattern ENUM to String Representation ...
*
* @param value: ENUM_X_BAR_PATTERN, member ...
*
* @return ( string )
*/
string ToXString(ENUM_X_BAR_PATTERN value)
{
//
string result = NULL;
//
switch (value)
{
//
case X_BAR_PATTERN_NONE:
result = "X_NONE";
break;
//
case X_BAR_PATTERN_HIGH:
result = "XHIGH";
break;
//
case X_BAR_PATTERN_LOW:
result = "XLOW";
break;
//
case X_BAR_PATTERN_MOMENTUM:
result = "XMOMENTUM";
break;
//
case X_BAR_PATTERN_ENGULFED:
result = "XENGULFED";
break;
//
case X_BAR_PATTERN_TRUE_GAPED:
result = "XGAPED";
break;
//
case X_BAR_PATTERN_PINNED:
result = "XPINNED";
break;
//
case X_BAR_PATTERN_STAR:
result = "XSTAR";
break;
//
case X_BAR_PATTERN_PIERCING:
result = "XPIERCING";
break;
//
case X_BAR_PATTERN_RISING:
result = "XRISING";
break;
//
case X_BAR_PATTERN_FLAG:
result = "XFLAG";
break;
//
case X_BAR_PATTERN_SIGNALKEY:
result = "XSIGNALKEY";
break;
//
case X_BAR_PATTERN_OB:
result = "XOB";
break;
//
case X_BAR_PATTERN_FVG:
result = "XFVG";
break;
//
case X_BAR_PATTERN_SUPPORT:
result = "XSUPPORT";
break;
//
case X_BAR_PATTERN_RESISTANCE:
result = "XRESISTANCE";
break;
}
//
return result;
}
/**
* Parse an String Representation to it's Corresponding Bar Pattern ENUM ...
*
* @param value: string, provided pattern's string ...
*
* @return ( ENUM_X_BAR_PATTERN )
*/
ENUM_X_BAR_PATTERN ToBarPattern(string value)
{
//
ENUM_X_BAR_PATTERN result = X_BAR_PATTERN_NONE;
//
// Validate ...
if (!IsXValid(value))
{
return result;
}
//
if (value == ToXString(X_BAR_PATTERN_NONE))
{
result = X_BAR_PATTERN_NONE;
}
else if (value == ToXString(X_BAR_PATTERN_HIGH))
{
result = X_BAR_PATTERN_HIGH;
}
else if (value == ToXString(X_BAR_PATTERN_LOW))
{
result = X_BAR_PATTERN_LOW;
}
else if (value == ToXString(X_BAR_PATTERN_MOMENTUM))
{
result = X_BAR_PATTERN_MOMENTUM;
}
else if (value == ToXString(X_BAR_PATTERN_ENGULFED))
{
result = X_BAR_PATTERN_ENGULFED;
}
else if (value == ToXString(X_BAR_PATTERN_TRUE_GAPED))
{
result = X_BAR_PATTERN_TRUE_GAPED;
}
else if (value == ToXString(X_BAR_PATTERN_PINNED))
{
result = X_BAR_PATTERN_PINNED;
}
else if (value == ToXString(X_BAR_PATTERN_STAR))
{
result = X_BAR_PATTERN_STAR;
}
else if (value == ToXString(X_BAR_PATTERN_PIERCING))
{
result = X_BAR_PATTERN_PIERCING;
}
else if (value == ToXString(X_BAR_PATTERN_RISING))
{
result = X_BAR_PATTERN_RISING;
}
else if (value == ToXString(X_BAR_PATTERN_FLAG))
{
result = X_BAR_PATTERN_FLAG;
}
else if (value == ToXString(X_BAR_PATTERN_SIGNALKEY))
{
result = X_BAR_PATTERN_SIGNALKEY;
}
else if (value == ToXString(X_BAR_PATTERN_OB))
{
result = X_BAR_PATTERN_OB;
}
else if (value == ToXString(X_BAR_PATTERN_FVG))
{
result = X_BAR_PATTERN_FVG;
}
else if (value == ToXString(X_BAR_PATTERN_SUPPORT))
{
result = X_BAR_PATTERN_SUPPORT;
}
else if (value == ToXString(X_BAR_PATTERN_RESISTANCE))
{
result = X_BAR_PATTERN_RESISTANCE;
}
//
return result;
}
/**
* Get All Available Valid Bar Patterns ...
*
* @param dest: ENUM_X_BAR_PATTERN, reference collection to fill ...
*
* @return ( int )
*/
int GetAllBarPatters(ENUM_X_BAR_PATTERN &dest[])
{
//
XClean(dest);
//
Add(X_BAR_PATTERN_HIGH, dest);
Add(X_BAR_PATTERN_LOW, dest);
Add(X_BAR_PATTERN_MOMENTUM, dest);
Add(X_BAR_PATTERN_ENGULFED, dest);
Add(X_BAR_PATTERN_TRUE_GAPED, dest);
Add(X_BAR_PATTERN_PINNED, dest);
Add(X_BAR_PATTERN_STAR, dest);
Add(X_BAR_PATTERN_PIERCING, dest);
Add(X_BAR_PATTERN_RISING, dest);
Add(X_BAR_PATTERN_FLAG, dest);
Add(X_BAR_PATTERN_SIGNALKEY, dest);
Add(X_BAR_PATTERN_OB, dest);
Add(X_BAR_PATTERN_FVG, dest);
Add(X_BAR_PATTERN_SUPPORT, dest);
Add(X_BAR_PATTERN_RESISTANCE, dest);
//
return ArraySize(dest);
}
//
// Structures ...
/**
* Represent a Pivot ...
**/
struct XPivot
{
//
// Props ...
double value; // Value
datetime time; // Time
string symbol; // Symbol
ENUM_X_DIRECTION dir; // Direction
ENUM_TIMEFRAMES period; // Period
ENUM_X_PIVOT_TYPE type; // Type
//
string prefix; // Object Prefix
//
// Constructor ...
XPivot()
{
Clean();
}
//
// Init ...
/**
* Initialize Structure ...
*
* @param _value: double, value of point ...
* @param _time: datetime, time of point ...
* @param _symbol: string, Symbol ...
* @param _dir: ENUM_X_DIRECTION, direction of point ...
* @param _period: ENUM_TIMEFRAMES, period ...
* @param _type: ENUM_X_PIVOT_TYPE, point type ...
*
* @return ( bool )
*/
bool Init(
double _value,
datetime _time,
string _symbol,
ENUM_X_DIRECTION _dir,
ENUM_TIMEFRAMES _period,
ENUM_X_PIVOT_TYPE _type //
)
{
//
bool result = false;
//
dir = _dir;
time = _time;
type = _type;
value = _value;
symbol = _symbol;
period = _period;
//
result = IsValid();
if (!result)
{
Clean();
}
//
return result;
}
//
// Tools ...
/**
* Cleanup Model ...
*/
void Clean()
{
//
value = 0;
time = NULL;
symbol = NULL;
period = NULL;
prefix = NULL;
dir = X_DIRECTION_NONE;
type = X_PIVOT_TYPE_NONE;
//
ZeroMemory(this);
}
/**
* Validate Model ...
*
* @return ( bool )
*/
bool IsValid()
{
//
bool result = false;
//
result =
value > 0 &&
IsXValid(type) &&
HasDirection(dir) &&
IsSpecifiedValid(time) &&
IsSpecifiedValid(symbol) &&
IsSpecifiedValid(period);
//
return result;
}
/**
* Check Model is Peak or not ...
*
* @return ( bool )
*/
bool IsPeak()
{
//
bool result = false;
//
result =
IsValid() &&
IsXPeak(type);
//
return result;
}
/**
* Check Model is Vale or not ...
*
* @return ( bool )
*/
bool IsVale()
{
//
bool result = false;
//
result =
IsValid() &&
IsXVale(type);
//
return result;
}
/**
* Check Point is Bullish or not ...
*
* @return ( bool )
*/
bool IsBullish()
{
//
bool result = false;
//
result = IsValid() &&
IsSpecifiedBullish(dir);
//
return result;
}
/**
* Check Point is Bearish or not ...
*
* @return ( bool )
*/
bool IsBearish()
{
//
bool result = false;
//
result = IsValid() &&
IsSpecifiedBearish(dir);
//
return result;
}
/**
* Check two Struct are Same or not ...
*
* @param item: XPivot ...
*
* @return ( bool )
*/
bool IsSameAs(XPivot &item)
{
//
bool result = false;
//
result =
IsValid() &&
item.IsValid() &&
dir == item.dir &&
type == item.type &&
time == item.time &&
value == item.value &&
symbol == item.symbol &&
period == item.period;
//
return result;
}
/**
* Calculate Pivot Age ...
*
* @return ( int )
*/
int GetAge()
{
//
int result = -1;
//
if (!IsValid())
{
return result;
}
//
result = GetBarIndex(
symbol,
period,
time //
);
//
return result;
}
/**
* Get Pivot Time Bar ...
*
* @param bar: XOHCL, reference to hold result ...
*
* @return ( bool )
*/
bool GetBar(XOHCL &bar)
{
//
bool result = false;
//
// Prepare ...
bar.Clean();
//
// Validate ...
result = IsValid();
if (!result)
{
return result;
}
//
// Initial Bar ...
result = bar.Init(
symbol,
period,
time //
);
//
if (!result)
{
bar.Clean();
}
//
return result;
}
/**
* Check to model Has Same Symbol ...
*
* @param item: XPivot, dest model for Checking ...
*
* @return ( bool )
*/
bool IsSameSymbolAs(XPivot &item)
{
//
bool result = false;
//
result =
IsValid() &&
item.IsValid() &&
symbol == item.symbol;
//
return result;
}
/**
* Check to model Has Same Period ...
*
* @param item: XPivot, dest model for Checking ...
*
* @return ( bool )
*/
bool IsSamePeriodAs(XPivot &item)
{
//
bool result = false;
//
result =
IsValid() &&
item.IsValid() &&
period == item.period;
//
return result;
}
/**
* Check to model Has Same Symbol/Period ...
*
* @param item: XPivot, dest model for Checking ...
*
* @return ( bool )
*/
bool IsSameMarketAs(XPivot &item)
{
//
bool result = false;
//
result =
IsValid() &&
item.IsValid() &&
IsSameSymbolAs(item) &&
IsSamePeriodAs(item);
//
return result;
}
/**
* Check to model Has Same Type ...
*
* @param item: XPivot, dest model for Checking ...
*
* @return ( bool )
*/
bool IsSameTypeAs(XPivot &item)
{
//
bool result = false;
//
result =
IsValid() &&
item.IsValid() &&
type == item.type;
//
return result;
}
/**
* Check to model Has Same Direction ...
*
* @param item: XPivot, dest model for Checking ...
*
* @return ( bool )
*/
bool IsSameDirectionAs(XPivot &item)
{
//
bool result = false;
//
result =
IsValid() &&
item.IsValid() &&
dir == item.dir;
//
return result;
}
//
// Logging Tools ...
/**
* Extract Specified Token of Struct ...
*
* @return ( string )
*/
string GetTag()
{
//
string result = NULL;
//
string token = GetToken(this);
//
result =
(IsSpecifiedValid(prefix)
? prefix + "_"
: "") +
token;
//
return result;
}
/**
* Represent an String Unique Identifier for Chart Objects ...
*
* @return ( string )
*/
string GetObjectID()
{
//
string result = "";
//
if (!IsValid())
{
return result;
}
//
result = GetTag() + "_" + symbol + "_" + ToXString(period) + "_" + ToXString(dir) + "_" + ToXString(type) + "_" + ToXString(TimeToSeconds(time));
//
return result;
}
/**
* Represent a Point as String ...
*
* @param spacer: string, How to Represent Spaces ...
*
* @return ( string )
*/
string GetMessage(string spacer = NULL)
{
//
string result = "";
//
// Normalize Spacer ...
if (!IsSpecifiedValid(spacer))
{
spacer = " ";
}
//
// Validate ...
if (!IsValid())
{
return result;
}
//
result =
ToXString(dir) + spacer + GetTag() + ":" + spacer +
"Symbol: " + symbol + spacer +
"Period: " + ToXString(period) + spacer +
"Type: " + ToXString(type) + spacer +
"Direction: " + ToXString(dir) + spacer +
"Value: " + ToXString(value) + spacer +
"Time: " + ToXString(time) + spacer;
//
return result;
}
};
/**
* Model a Box ...
**/
struct XBoxZone
{
//
// Props ...
string symbol;
ENUM_TIMEFRAMES period;
//
double upper;
double lower;
//
datetime at;
datetime from;
datetime to;
//
string type;
ENUM_X_DIRECTION dir;
//
// Special Props ...
datetime breakAt;
datetime tests[];
datetime acteds[];
datetime breaks[];
datetime insides[];
datetime fakeBreaks[];
//
// Constructor ...
XBoxZone()
{
Clean();
}
//
// Tools ...
/**
* Cleanup Model ...
*/
void Clean()
{
//
upper = 0;
lower = 0;
to = NULL;
at = NULL;
from = NULL;
symbol = NULL;
period = NULL;
dir = X_DIRECTION_NONE;
//
SpecialClean();
//
ZeroMemory(this);
}
/**
* Clean Special Props ...
*/
void SpecialClean()
{
//
breakAt = NULL;
XClean(tests);
XClean(acteds);
XClean(breaks);
XClean(insides);
XClean(fakeBreaks);
}
/**
* Validate Model ...
*
* @return ( bool )
*/
bool IsValid()
{
//
bool result = false;
//
result =
//
upper > 0 &&
lower > 0 &&
from < to &&
IsSpecifiedValid(to) &&
upper >= lower &&
IsSpecifiedValid(from) &&
IsSpecifiedValid(symbol) &&
IsSpecifiedValid(period)
//
;
//
return result;
}
/**
* Check Box is Bullish or not ...
*
* @return ( bool )
*/
bool IsBullish()
{
return IsValid() &&
IsSpecifiedBullish(dir);
}
/**
* Check Box is Bearish or not ...
*
* @return ( bool )
*/
bool IsBearish()
{
return IsValid() &&
IsSpecifiedBearish(dir);
}
/**
* Clone Box by Provided Source ...
*
* @return ( bool )
*/
bool Clone(XBoxZone &source)
{
//
bool result = false;
//
Clean();
result = source.IsValid();
if (!result)
{
return result;
}
//
// Filling Props ...
to = source.to;
dir = source.dir;
from = source.from;
type = source.type;
upper = source.upper;
lower = source.lower;
symbol = source.symbol;
period = source.period;
//
result = IsValid();
if (!result)
{
Clean();
}
//
return result;
}
/**
* Check Box is Same as Other ...
*
* @param item: XBoxZone instance Resource ...
*
* @return ( bool )
*/
bool IsSameAs(XBoxZone &item)
{
//
return IsValid() &&
item.IsValid() &&
dir == item.dir &&
from == item.from &&
type == item.type &&
lower == item.lower &&
upper == item.upper &&
symbol == item.symbol &&
period == item.period;
}
/**
* Retrieve Box Age ...
*
* @param forPeriod: ENUM_TIMEFRAMES member ...
*
* @return ( int )
*/
int GetAge(
ENUM_TIMEFRAMES forPeriod = NULL //
)
{
//
int result = 0;
//
if (!IsValid())
{
return result;
}
//
if (!IsSpecifiedValid(forPeriod))
{
forPeriod = period;
}
//
result = iBarShift(
symbol,
forPeriod,
from //
);
//
return result;
}
/**
* Calculate Box Middle Price ...
*
* @return ( double )
*/
double GetMid()
{
//
double result = 0;
//
if (!IsValid())
{
return result;
}
//
double diffAvg = (upper - lower) / 2;
result = lower + diffAvg;
result = NormalizePrice(result, symbol);
//
return result;
}
/**
* Calculate Box Leg N Price ...
* N default is 2
*
* @param leg: int ...
*
* @return ( double )
*/
double GetLeg(int leg = 2)
{
//
double result = 0;
//
leg = NormalizeInt(leg, 2);
//
if (!IsValid())
{
return result;
}
//
double range = (upper - lower) * leg;
result =
IsBullish()
? upper + range
: lower - range;
result = NormalizePrice(result, symbol);
//
return result;
}
/**
* Calculate Box Range ...
*
* @return ( double )
*/
double GetRange()
{
//
double result = 0;
//
if (!IsValid())
{
return result;
}
//
result = (upper - lower);
result = NormalizePrice(result, symbol);
//
return result;
}
/**
* Retrieve Box Applied Price ...
*
* @return ( double )
*/
double GetDirectionalPrice()
{
//
double result = 0;
//
if (!IsValid())
{
return result;
}
//
result =
IsBullish()
? upper
: lower;
//
return result;
}
/**
* Retrieve Box Breaked Price ...
*
* @return ( double )
*/
double GetInDirectionalPrice()
{
//
double result = 0;
//
if (!IsValid())
{
return result;
}
//
result =
IsBullish()
? lower
: upper;
//
return result;
}
/**
* To Bar Index ...
*
* @return ( int )
*/
int ToIndex(
ENUM_TIMEFRAMES forPeriod = NULL //
)
{
//
int result = -1;
//
if (!IsValid())
{
return result;
}
//
if (!IsSpecifiedValid(forPeriod))
{
forPeriod = period;
}
//
result = GetBarIndex(
symbol,
forPeriod,
to //
);
//
return result;
}
/**
* Retrieve To Bar ...
*
* @return ( bool )
*/
bool ToBar(XOHCL &bar)
{
//
bool result = false;
//
bar.Clean();
//
int idx = ToIndex();
//
result =
IsValid() &&
IsValidIndex(idx);
if (!result)
{
return result;
}
//
result = bar.Init(
symbol,
period,
idx //
);
//
return result;
}
/**
* At Bar Index ...
*
* @return ( int )
*/
int AtIndex(
ENUM_TIMEFRAMES forPeriod = NULL //
)
{
//
int result = -1;
//
if (!IsValid() || !IsXValid(at))
{
return result;
}
//
if (!IsSpecifiedValid(forPeriod))
{
forPeriod = period;
}
//
result = GetBarIndex(
symbol,
forPeriod,
at //
);
//
return result;
}
/**
* Retrieve At Bar ...
*
* @return ( bool )
*/
bool AtBar(XOHCL &bar)
{
//
bool result = false;
//
bar.Clean();
//
int idx = AtIndex();
//
result =
IsValid() &&
IsValidIndex(idx);
if (!result)
{
return result;
}
//
result = bar.Init(
symbol,
period,
idx //
);
//
return result;
}
/**
* From Bar Index ...
*
* @return ( int )
*/
int FromIndex(
ENUM_TIMEFRAMES forPeriod = NULL //
)
{
//
int result = -1;
//
if (!IsValid())
{
return result;
}
//
if (!IsSpecifiedValid(forPeriod))
{
forPeriod = period;
}
//
result = GetBarIndex(
symbol,
forPeriod,
from //
);
//
return result;
}
/**
* Retrieve From Bar ...
*
* @return ( bool )
*/
bool FromBar(XOHCL &bar)
{
//
bool result = false;
//
bar.Clean();
//
int idx = FromIndex();
//
result =
IsValid() &&
IsValidIndex(idx);
if (!result)
{
return result;
}
//
result = bar.Init(
symbol,
period,
idx //
);
//
return result;
}
/**
* Retrieve Unique Identifier ...
*
* @return ( string )
*/
string GetTag()
{
//
int digits = GetDigits(symbol);
double nUpper = NormalizeDouble(NormalizePrice(upper, symbol), digits);
double nLower = NormalizeDouble(NormalizePrice(lower, symbol), digits);
//
string typeStr = IsSpecifiedValid(type)
? type
: "XBOX";
string result =
//
typeStr + "_" +
ToString(dir) + "_" +
symbol + "_" +
ToXString(period) + "_" +
ToXString(nUpper) + "_" +
ToXString(nLower)
//
;
//
return result;
}
/**
* Retrieve Unique Middle Identifier ...
*
* @return ( string )
*/
string GetMiddleTag()
{
//
int digits = GetDigits(symbol);
double nMid = NormalizeDouble(NormalizePrice(GetMid(), symbol), digits);
//
string typeStr = IsSpecifiedValid(type)
? type
: "XBOX";
string result =
//
typeStr + "_" +
"Mid_" +
ToString(dir) + "_" +
symbol + "_" +
ToXString(period) + "_" +
ToXString(nMid)
//
;
//
return result;
}
};
/**
* Model a Direction Shift ...
**/
struct XDirectionShift
{
//
// Props ...
string type;
datetime at;
double after;
double before;
string symbol;
ENUM_TIMEFRAMES period;
ENUM_X_DIRECTION shiftTo;
//
// Constructor ...
XDirectionShift()
{
Clean();
}
//
// Tools ...
//
// Cleanup ...
void Clean()
{
//
at = NULL;
after = 0;
before = 0;
type = NULL;
symbol = NULL;
period = NULL;
shiftTo = X_DIRECTION_NONE;
//
ZeroMemory(this);
}
//
// Validate ...
bool IsValid()
{
//
bool result = false;
//
result =
after > 0 &&
IsSpecifiedValid(at) &&
IsSpecifiedValid(type) &&
after != before &&
IsSpecifiedValid(symbol) &&
IsSpecifiedValid(period) &&
HasDirection(shiftTo);
//
return result;
}
//
// Get Bar Index of Direction Shift ...
int Index()
{
//
int result = -1;
//
// Validate ...
if (!IsValid())
{
return result;
}
//
// Retrieve Bar Index ...
result = GetBarIndex(
symbol,
period,
at //
);
//
return result;
}
//
// Get At Bar ...
bool AtBar(XOHCL &bar)
{
//
bool result = false;
//
// Normalize ...
bar.Clean();
//
// Validate ...
result = IsValid();
if (!result)
{
return result;
}
//
int idx = Index();
if (IsValidIndex(idx))
{
//
result = bar.Init(
symbol,
period,
idx //
);
}
//
result = bar.IsValid();
//
return result;
}
//
// Check Same ...
bool IsSameAs(XDirectionShift &item)
{
//
bool result = false;
//
result = IsValid() &&
item.IsValid();
if (!result)
{
return result;
}
//
result =
//
at == item.at &&
type == item.type &&
after == item.after &&
before == item.before &&
symbol == item.symbol &&
period == item.period &&
shiftTo == item.shiftTo
//
;
//
return result;
}
};
//