Files
MQL5Data/Libraries/x-saherelm.x-pivot.analysis.lib.mq5
T

1086 lines
20 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 2025, SaherElm IT Center"
#property link "https://www.saherelm.ir"
#property version "1.00"
#property strict
//
// Imports ...
#include "../Libraries/x-saherelm.common.lib.mq5"
#include "../Libraries/x-saherelm.x-poi.extensions.lib.mq5"
#include "../Libraries/x-saherelm.x-poi.lib.mq5"
/**
* Model Specified Pivots Analysis ...
**/
struct XPivotAnalysis
{
//
// Props ...
string symbol; // Symbol ...
datetime time; // Time ...
XPivot pivots[]; // Pivots ...
ENUM_TIMEFRAMES period; // Period ...
//
// Linear Regression Parameters ...
double peakSlope;
double valeSlope;
double peakIntercept;
double valeIntercept;
//
// Analysed Results ...
//
// Directions ...
ENUM_X_DIRECTION dir; // Trending Direction ...
ENUM_X_DIRECTION peakDir;
ENUM_X_DIRECTION valeDir;
//
// Predicted Values ...
double peakTo;
double valeTo;
double peakFrom;
double valeFrom;
//
// Time Analysis ...
datetime toTime;
datetime fromTime;
//
// Support and Resistances ...
double minorSupport;
double majorSupport;
double minorResistance;
double majorResistance;
//
// Constructor ...
XPivotAnalysis()
{
Clean();
}
//
// Tools ...
//
// Cleaners ...
/**
* Cleanup Model ...
*/
void Clean()
{
//
time = NULL;
symbol = NULL;
period = NULL;
SpecifiedClean(pivots);
//
CleanAnalysis();
CleanLinearRegressionParameters();
//
ZeroMemory(this);
}
/**
* Clean Analysis Props ...
*/
void CleanAnalysis()
{
//
toTime = NULL;
fromTime = NULL;
peakTo = EMPTY_VALUE;
valeTo = EMPTY_VALUE;
peakFrom = EMPTY_VALUE;
valeFrom = EMPTY_VALUE;
dir = X_DIRECTION_NONE;
peakDir = X_DIRECTION_NONE;
valeDir = X_DIRECTION_NONE;
minorSupport = EMPTY_VALUE;
majorSupport = EMPTY_VALUE;
minorResistance = EMPTY_VALUE;
majorResistance = EMPTY_VALUE;
}
/**
* Clean Linear Regression Parameters ...
*/
void CleanLinearRegressionParameters()
{
//
peakSlope = EMPTY_VALUE;
valeSlope = EMPTY_VALUE;
peakIntercept = EMPTY_VALUE;
valeIntercept = EMPTY_VALUE;
}
//
// Initializer / Actions (s) ...
/**
* Initialize Model ...
*
* @param _symbol: Symbol ...
* @param _period: Period ...
* @param _pivots: Provided Pivots ...
*
* @return ( bool )
*/
bool Init(
string _symbol,
ENUM_TIMEFRAMES _period,
XPivot &_pivots[] //
)
{
//
bool result = false;
//
// Validate Args ...
result =
HasChild(_pivots) &&
IsSpecifiedValid(_symbol) &&
IsSpecifiedValid(_period);
if (!result)
{
//
Clean();
return result;
}
//
symbol = _symbol;
period = _period;
Copy(
_pivots,
pivots //
);
time = TimeCurrent();
//
result = IsValid();
if (!result)
{
Clean();
}
//
// Do Calculations if Model is Valid ...
if (result)
{
Update();
}
//
return result;
}
/**
* Fully Update Analysis based on Pivots ...
*/
void Update()
{
//
bool has = false;
//
// Prepare ...
CleanAnalysis();
CleanLinearRegressionParameters();
//
// Validate ...
has = IsValid() &&
HasPivots();
if (!has)
{
return;
}
//
// Calculate Linear Regression Parameters ...
has = CalculateLinearRegressionParameters();
if (!has)
{
return;
}
//
// Analyse ...
has = Analyse();
if (!has)
{
return;
}
}
/**
* Fully Update Analysis based on Pivots ...
*
* @param _pivots: new Pivots Collection for Update ...
*/
void Update(XPivot &_pivots[])
{
//
bool has =
IsValid() &&
HasChild(_pivots);
if (!has)
{
return;
}
//
SpecifiedClean(pivots);
Copy(
_pivots,
pivots //
);
//
Update();
}
/**
* Re Analyse Pivots ...
*
* @return ( bool )
*/
bool Analyse()
{
//
bool result = false;
//
// Prepare ...
CleanAnalysis();
//
// Validate ...
result = IsValid() &&
HasPivots() &&
HasLinearRegressionParameters();
if (!result)
{
return result;
}
//
// Do Analyse ...
//
bool has = false;
//
// Specified To and From Time ...
//
// To Time ...
int toIDX = GetYoungest(pivots);
if (IsValidIndex(toIDX))
{
//
toTime = pivots[toIDX].time;
toIDX = GetBarIndex(
symbol,
period,
toTime //
);
}
//
// From Time ...
int fromIDX = GetOldest(pivots);
if (IsValidIndex(fromIDX))
{
//
fromTime = pivots[fromIDX].time;
fromIDX = GetBarIndex(
symbol,
period,
fromTime //
);
}
//
// Validate To From times ...
result =
IsValidIndex(toIDX) &&
IsValidIndex(fromIDX) &&
IsSpecifiedValid(toTime) &&
IsSpecifiedValid(fromTime);
if (!result)
{
return result;
}
//
// Predict Values ...
//
// Peaks ...
//
// Check Slope and Intercept Exists ...
has = NotEmpty(peakSlope) &&
NotEmpty(peakIntercept);
if (has)
{
//
// To ...
peakTo = PredictValue(
peakSlope,
peakIntercept,
toIDX //
);
//
// From ...
peakFrom = PredictValue(
peakSlope,
peakIntercept,
fromIDX //
);
}
//
// Vales ...
//
// Check Slope and Intercept Exists ...
has = NotEmpty(valeSlope) &&
NotEmpty(valeIntercept);
if (has)
{
//
// To ...
valeTo = PredictValue(
valeSlope,
valeIntercept,
toIDX //
);
//
// From ...
valeFrom = PredictValue(
valeSlope,
valeIntercept,
fromIDX //
);
}
//
// Check Directions ...
//
// Peak ...
has = IsPeakAnalysed();
if (has)
{
//
peakDir =
peakFrom < peakTo
? X_DIRECTION_BULLISH
: peakFrom > peakTo
? X_DIRECTION_BEARISH
: X_DIRECTION_NONE;
}
//
// Vale ...
has = IsValeAnalysed();
if (has)
{
//
valeDir =
valeFrom < valeTo
? X_DIRECTION_BULLISH
: valeFrom > valeTo
? X_DIRECTION_BEARISH
: X_DIRECTION_NONE;
}
//
// Check For Strong Type Trending ...
has =
IsPeakAnalysed() &&
IsValeAnalysed() &&
peakDir == valeDir;
if (has)
{
dir = peakDir;
}
//
// Calculate Support and Resistances ...
//
// Support ...
ExtractSupport(
minorSupport,
majorSupport,
pivots //
);
//
// Resistance ...
ExtractResistances(
minorResistance,
majorResistance,
pivots //
);
//
result = IsAnalysed();
//
// Cleanup Resources ...
//
return result;
}
/**
* Re Calculate Linear Regression Parameters ...
*
* @return ( bool )
*/
bool CalculateLinearRegressionParameters()
{
//
bool result = false;
//
// Prepare ...
CleanLinearRegressionParameters();
//
// Validate ...
result = IsValid() &&
HasPivots();
if (!result)
{
return result;
}
//
// Make a Copy of Pivots ...
XPivot tmpPivots[];
Copy(
pivots,
tmpPivots //
);
//
// Sort Pivots ...
Sort(
tmpPivots,
X_SORT_BY_DATE,
X_DIRECTION_BEARISH //
);
//
// Do Calculations ...
//
double yData[];
double xData[];
bool has = false;
//
// Peaks ...
//
XPivot peaks[];
int peaksCount = ExtractPivots(
peaks,
tmpPivots,
X_PIVOT_TYPE_PEAK //
);
has = IsValidSize(peaksCount);
if (has)
{
//
// Extract X and Y axis Data ...
ExtractPivotsData(
xData,
yData,
peaks //
);
//
// Calculating Slope and Intercept ...
CalculateSlopeAndIntercept(
peakSlope,
peakIntercept,
xData,
yData //
);
}
//
// Vales ...
//
XPivot vales[];
int valesCount = ExtractPivots(
vales,
tmpPivots,
X_PIVOT_TYPE_VALE //
);
has = IsValidSize(valesCount);
if (has)
{
//
// Extract X and Y axis Data ...
ExtractPivotsData(
xData,
yData,
vales //
);
//
// Calculating Slope and Intercept ...
CalculateSlopeAndIntercept(
valeSlope,
valeIntercept,
xData,
yData //
);
}
//
// Validate Result ...
result =
IsValid() &&
HasLinearRegressionParameters();
//
// Cleanup Resources ...
SpecifiedClean(yData);
SpecifiedClean(xData);
SpecifiedClean(peaks);
SpecifiedClean(vales);
SpecifiedClean(tmpPivots);
//
return result;
}
//
// Validator / Checker (s) ...
/**
* Validate Model ...
*
* @return ( bool )
*/
bool IsValid()
{
//
bool result = false;
//
result =
HasPivots() &&
IsSpecifiedValid(time) &&
IsSpecifiedValid(symbol) &&
IsSpecifiedValid(period);
//
return result;
}
/**
* Check Model has Pivots or not ...
*
* @return ( bool )
*/
bool HasPivots()
{
//
bool result = false;
//
result = HasChild(pivots);
//
return result;
}
/**
* Check Model is Analysed or not ...
*
* @return ( bool )
*/
bool IsAnalysed()
{
//
bool result = false;
//
result =
IsPeakAnalysed() ||
IsValeAnalysed();
//
return result;
}
/**
* Check Peak Analysed or not ...
*
* @return ( bool )
*/
bool IsPeakAnalysed()
{
//
bool result = false;
//
result =
IsValid() &&
HasLinearRegressionParameters() &&
(
//
NotEmpty(peakTo) &&
NotEmpty(peakFrom) &&
HasDirection(peakDir) &&
IsSpecifiedValid(toTime) &&
IsSpecifiedValid(fromTime)
//
);
//
return result;
}
/**
* Check Vale Analysed or not ...
*
* @return ( bool )
*/
bool IsValeAnalysed()
{
//
bool result = false;
//
result =
IsValid() &&
HasLinearRegressionParameters() &&
(
//
NotEmpty(valeTo) &&
NotEmpty(valeFrom) &&
HasDirection(valeDir) &&
IsSpecifiedValid(toTime) &&
IsSpecifiedValid(fromTime)
//
);
//
return result;
}
/**
* Check Models Linear Regression Parameters Calculated or not ...
*
* @return ( bool )
*/
bool HasLinearRegressionParameters()
{
//
bool result = false;
//
result =
(NotEmpty(peakSlope) &&
NotEmpty(peakIntercept)) ||
(NotEmpty(valeSlope) ||
NotEmpty(valeIntercept));
//
return result;
}
/**
* Check Analysis Has Trend or not ...
*
* @return ( bool )
*/
bool HasTrend()
{
//
bool result = false;
//
result = IsAnalysed() &&
HasDirection(dir);
//
return result;
}
/**
* Check Analysis Has Bullish Trend or not ...
*
* @return ( bool )
*/
bool HasBullishTrend()
{
//
bool result = false;
//
result = HasTrend() &&
IsSpecifiedBullish(dir);
//
return result;
}
/**
* Check Analysis Has Bearish Trend or not ...
*
* @return ( bool )
*/
bool HasBearishTrend()
{
//
bool result = false;
//
result = HasTrend() &&
IsSpecifiedBearish(dir);
//
return result;
}
/**
* Check Market Has Support ...
*
* @return ( bool )
*/
bool HasSupport()
{
//
bool result = false;
//
result =
IsValid() &&
HasPivots() &&
IsAnalysed() &&
NotEmpty(minorSupport) &&
NotEmpty(majorSupport);
//
return result;
}
/**
* Check Market Has Resistance ...
*
* @return ( bool )
*/
bool HasResistance()
{
//
bool result = false;
//
result =
IsValid() &&
HasPivots() &&
IsAnalysed() &&
NotEmpty(minorResistance) &&
NotEmpty(majorResistance);
//
return result;
}
//
// Pivots ...
/**
* Extract all Peaks from Exists Pivots ...
*
* @param dest: a Collection refrence to Hold Extracted Pivots ...
*
* @return ( int )
*/
int ExtractPeaks(XPivot &dest[])
{
//
int result = -1;
//
// Prepare ...
SpecifiedClean(dest);
//
// Do Extraction ...
result = ExtractPivots(
dest,
pivots,
X_PIVOT_TYPE_PEAK //
);
//
return result;
}
/**
* Extract all Vales from Exists Pivots ...
*
* @param dest: a Collection refrence to Hold Extracted Pivots ...
*
* @return ( int )
*/
int ExtractVales(XPivot &dest[])
{
//
int result = -1;
//
// Prepare ...
SpecifiedClean(dest);
//
// Do Extraction ...
result = ExtractPivots(
dest,
pivots,
X_PIVOT_TYPE_VALE //
);
//
return result;
}
/**
* Sort Pivots ...
*
* @param sortBy: ENUM_X_SORT_BY, Specified Sort Param ...
* @param sortDir: ENUM_X_DIRECTION, Specified Sort Direction ...
*/
void SortPivots(
ENUM_X_SORT_BY sortBy,
ENUM_X_DIRECTION sortDir //
)
{
//
// Validate ...
bool has = IsValid() &&
HasPivots();
if (!has)
{
return;
}
//
// Apply Sorting ...
Sort(
pivots,
sortBy,
sortDir //
);
}
//
// Detecting ...
/**
* Check Two Model is Same or not ...
*
* @param item: Specified Model for Compare ...
*
* @return ( bool )
*/
bool IsSameAs(XPivot &item)
{
//
bool result = false;
//
result = IsValid() &&
item.IsValid() &&
IsSameMarketAs(item) &&
time == item.time;
//
return result;
}
/**
* Check Two Model Has Same Symbol or not ...
*
* @param item: Specified Model for Compare ...
*
* @return ( bool )
*/
bool IsSameSymbolAs(XPivot &item)
{
//
bool result = false;
//
result = IsValid() &&
item.IsValid() &&
symbol == item.symbol;
//
return result;
}
/**
* Check Two Model Has Same Period or not ...
*
* @param item: Specified Model for Compare ...
*
* @return ( bool )
*/
bool IsSamePeriodAs(XPivot &item)
{
//
bool result = false;
//
result = IsValid() &&
item.IsValid() &&
period == item.period;
//
return result;
}
/**
* Check Two Model Has Same Symbol/Period or not ...
*
* @param item: Specified Model for Compare ...
*
* @return ( bool )
*/
bool IsSameMarketAs(XPivot &item)
{
//
bool result = false;
//
result = IsValid() &&
item.IsValid() &&
IsSameSymbolAs(item) &&
IsSamePeriodAs(item);
//
return result;
}
//
// Logging ...
/**
* Extract Specified Token of Struct ...
*
* @return ( string )
*/
string GetTag()
{
return GetToken(this);
}
/**
* Represent an String Unique Identifier for Chart Objects ...
*
* @return ( string )
*/
string GetObjectID()
{
//
string result = "";
//
if (!IsValid())
{
return result;
}
//
result = GetTag() + "_" + symbol + "_" + ToXString(period) + "_" + ToXString(TimeToSeconds(time));
//
return result;
}
/**
* Represent 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 =
"==========================" + spacer +
GetTag() + ":" + spacer +
"==========================" + spacer +
"Symbol: " + symbol + spacer +
"Period: " + ToXString(period) + spacer +
(!HasTrend()
? ""
: "Direction: " + ToXString(dir) + spacer) +
"Time: " + ToXString(time) + spacer;
//
return result;
}
//
};
//