Files
MQL5Data/Helpers/x-saherelm.x121.xtrend.helper.mq5
T
2025-04-06 20:46:15 +03:30

1309 lines
25 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Helper Class Library
// ----------------------------------------------
// Name: XCX121XTRENDHelper
// Description: provides all Indicator
// Helper requirements ...
//
//
// 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.x-helper.class.mq5"
//
// Definitions ...
//
// Definitions ...
enum ENUM_X121_XTREND_BUFFERS
{
//
X121_XTREND_UP_LINE = 0,
X121_XTREND_DOWN_LINE = 1,
X121_XTREND_MA_LINE = 2,
X121_XTREND_ADX_LINE = 3,
X121_XTREND_ADX_P_LINE = 4,
X121_XTREND_ADX_M_LINE = 5,
X121_XTREND_UP_T_LINE = 6,
X121_XTREND_DOWN_T_LINE = 7,
};
//
// Input Models ...
struct X121XTRENDInputs
{
//
// Props ...
//
// Market ...
ENUM_APPLIED_PRICE trendPriceType; // Detect Trend Price Type
//
// MA Detection ...
int maLength; // MA Length
ENUM_MA_METHOD maMethod; // MA Method
ENUM_APPLIED_PRICE maAppliedTo; // MA Applied To
//
// ADX Detection ...
int adxLength; // ADX Length
double adxThreshold; // ADX Threshold for Strong Trends
//
// Presentation ...
//
int startCalculationForLastBars; // Calculate Last n Bars
int upTrendArrowCode; // Up Trend Arrow Code
int downTrendArrowCode; // Down Trend Arrow Code
//
bool showOnlyTrendStart; // Show Only Trend Start
bool showUpTrend; // Show Up Trend
bool showDownTrend; // Show Down Trend
//
// Constructor(s) ...
X121XTRENDInputs()
{
Clean();
}
//
// Tools ...
//
// Clean ...
void Clean()
{
//
trendPriceType = PRICE_CLOSE;
maLength = 50;
maMethod = MODE_EMA;
maAppliedTo = PRICE_CLOSE;
adxLength = 14;
adxThreshold = 25;
startCalculationForLastBars = 1000;
upTrendArrowCode = 233;
downTrendArrowCode = 234;
showOnlyTrendStart = false;
showUpTrend = false;
showDownTrend = false;
//
ZeroMemory(this);
}
//
// Default ...
void Default()
{
//
trendPriceType = PRICE_CLOSE;
maLength = 50;
maMethod = MODE_EMA;
maAppliedTo = PRICE_CLOSE;
adxLength = 14;
adxThreshold = 25;
startCalculationForLastBars = 1000;
upTrendArrowCode = 233;
downTrendArrowCode = 234;
showOnlyTrendStart = true;
showUpTrend = true;
showDownTrend = true;
}
//
// Validate ...
bool IsValid()
{
//
bool result = false;
//
result =
//
// MA ...
maLength > 0 &&
//
// ADX ...
adxLength > 0 &&
adxThreshold > 0
//
;
//
return result;
}
//
// Retrieve MAx Length ...
int Max()
{
//
int result = 0;
//
result = MathMax(maLength, adxLength);
//
return result;
}
};
//
// Conditions ...
struct X121XTRENDConditions
{
//
// Common ...
string symbol;
ENUM_TIMEFRAMES period;
datetime time;
//
// Buffers ...
double upTrendBuffer[];
double downTrendBuffer[];
double maBuffer[];
double adxBuffer[];
double adxPBuffer[];
double adxMBuffer[];
double upTBuffer[];
double downTBuffer[];
//
// Conditions ...
//
bool isTrendBullish;
bool isTrendBearish;
//
bool isTrendSwitchedToBullish;
bool isTrendSwitchedToBearish;
//
// bool isCloseLower
//
// Constructor ...
X121XTRENDConditions()
{
Clean();
}
//
// Tools ...
/**
* Cleaning Up ...
*/
void Clean()
{
//
// Commons ...
symbol = NULL;
period = NULL;
time = NULL;
//
// Buffers ...
//
Clean(upTrendBuffer);
Clean(downTrendBuffer);
Clean(maBuffer);
Clean(adxBuffer);
Clean(adxPBuffer);
Clean(adxMBuffer);
Clean(upTBuffer);
Clean(downTBuffer);
//
ArraySetAsSeries(upTrendBuffer, true);
ArraySetAsSeries(downTrendBuffer, true);
ArraySetAsSeries(maBuffer, true);
ArraySetAsSeries(adxBuffer, true);
ArraySetAsSeries(adxPBuffer, true);
ArraySetAsSeries(adxMBuffer, true);
ArraySetAsSeries(upTBuffer, true);
ArraySetAsSeries(downTBuffer, true);
//
// Conditions ...
//
isTrendBullish = false;
isTrendBearish = false;
//
isTrendSwitchedToBullish = false;
isTrendSwitchedToBearish = false;
//
ZeroMemory(this);
}
/**
* Generate Conditions Scores ...
*
* @param bullishScore: Double, Directional Scores Reference ...
* @param bearishScore: Double, Directional Scores Reference ...
*/
void GenerateScore(
double &bullishScore,
double &bearishScore //
)
{
//
bullishScore = 0;
bearishScore = 0;
//
double score = 1;
double minScore = 0.5;
double highScore = 1.5;
//
if (isTrendBullish)
{
bullishScore += minScore;
}
if (isTrendSwitchedToBullish)
{
bullishScore += score;
}
//
if (isTrendBearish)
{
bearishScore += minScore;
}
if (isTrendSwitchedToBearish)
{
bearishScore += score;
}
}
/**
* Generate Summary String for Represent Conditions State ...
*
* @param onlyCommons: Boolean, Just Generate Only Commons Conditions ...
* @param onlyConditions: Boolean, Just Generate Only Conditions ...
* @param includeScores: Boolean, Attach Scores Representations on Result ...
* @param ignoreFalseConditions: Boolean, Ignore False Conditions on Result ...
* @param separator: String, Separate Lines ...
*
* @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 scoresStr =
//
"Scores: " + separator +
"---------------" + separator +
"Bullish: " + ToString(bullishScore) + separator +
"Bearish: " + ToString(bearishScore) + separator +
""
//
;
//
string commonStr = GenerateSpecifiedCommonSummary(
this,
separator,
includeScores //
);
//
string conditionsStr =
//
"-----------------------" + separator +
"XTREND: " + separator +
"-----------------------" + separator +
ToString("isTrendBullish", isTrendBullish, ignoreFalseConditions, separator) +
ToString("isTrendBearish", isTrendBearish, ignoreFalseConditions, separator) +
ToString("isTrendSwitchedToBullish", isTrendSwitchedToBullish, ignoreFalseConditions, separator) +
ToString("isTrendSwitchedToBearish", isTrendSwitchedToBearish, ignoreFalseConditions, separator) +
//
""
//
;
//
result =
//
"[" + GetTag() + "]" + separator +
(onlyConditions
? ""
: commonStr) +
(!includeScores
? ""
: scoresStr) +
" " + separator +
(onlyCommons
? ""
: conditionsStr) +
""
//
;
//
return result;
}
/**
* Retrieve nique Tag Identifier ...
*
* @return ( string )
*/
string GetTag()
{
return GetTypeName(this);
}
//
};
//
// Class ...
class XCX121XTRENDHelper : public XCBaseHelper
{
//
// Public ...
public:
//
// Props ...
//
// Constructors ...
XCX121XTRENDHelper()
: XCBaseHelper(_Symbol, _Period)
{
}
//
// Deconstructor ...
~XCX121XTRENDHelper()
{
//
mInputs.Clean();
//
Clean(upTrendBuffer);
Clean(downTrendBuffer);
Clean(maBuffer);
Clean(adxBuffer);
Clean(adxPBuffer);
Clean(adxMBuffer);
Clean(upTBuffer);
Clean(downTBuffer);
}
//
// Tools ...
bool Init(
string symbol, // Trading Symbol
ENUM_TIMEFRAMES period, // Trading Period
X121XTRENDInputs &inputs // Inputs
)
{
//
bool result = false;
//
mSymbol = symbol;
mPeriod = period;
//
result = inputs.IsValid();
if (!result)
{
return result;
}
//
ArraySetAsSeries(upTrendBuffer, true);
ArraySetAsSeries(downTrendBuffer, true);
ArraySetAsSeries(maBuffer, true);
ArraySetAsSeries(adxBuffer, true);
ArraySetAsSeries(adxPBuffer, true);
ArraySetAsSeries(adxMBuffer, true);
ArraySetAsSeries(upTBuffer, true);
ArraySetAsSeries(downTBuffer, true);
//
mInputs = inputs;
//
mHandler = iCustom(
mSymbol,
mPeriod,
"x-saherelm.x121.xtrend",
//
// Inputs ...
//
// Market ...
"",
mInputs.trendPriceType,
//
// MA Detection ...
"",
mInputs.maLength,
mInputs.maMethod,
mInputs.maAppliedTo,
//
// ADX Detection ...
"",
mInputs.adxLength,
mInputs.adxThreshold,
//
// Presentation ...
"",
mInputs.startCalculationForLastBars,
mInputs.upTrendArrowCode,
mInputs.downTrendArrowCode,
mInputs.showOnlyTrendStart,
mInputs.showUpTrend,
mInputs.showDownTrend
//
);
result = mHandler != INVALID_HANDLE;
if (!result)
{
return result;
}
//
return result;
}
//
// Inputs ...
//
X121XTRENDInputs GetInputs()
{
return mInputs;
}
//
bool SetInputs(
X121XTRENDInputs &inputs // Configs
)
{
//
return Init(
mSymbol,
mPeriod,
inputs
//
);
}
//
// Readers ...
//
// Up Trend ...
//
double GetUpTrend(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate(barIndex);
//
int count = ArraySize(upTrendBuffer);
if (barIndex >= count)
{
barIndex = count - 1;
}
//
return upTrendBuffer[barIndex];
}
//
int CopyUpTrend(
int start, // Start
int count, // Number of Items for read
double &buffer[], // Dest Buffer
bool forceClean = true // Force To Clean buffer
)
{
//
// Update Calculations ...
Calculate(start);
//
// Copy Items ...
return Copy(
0,
count,
upTrendBuffer,
buffer,
forceClean
//
);
}
//
// Down Trend ...
//
double GetDownTrend(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate(barIndex);
//
int count = ArraySize(downTrendBuffer);
if (barIndex >= count)
{
barIndex = count - 1;
}
//
return downTrendBuffer[barIndex];
}
//
int CopyDownTrend(
int start, // Start
int count, // Number of Items for read
double &buffer[], // Dest Buffer
bool forceClean = true // Force To Clean buffer
)
{
//
// Update Calculations ...
Calculate(start);
//
// Copy Items ...
return Copy(
0,
count,
downTrendBuffer,
buffer,
forceClean
//
);
}
//
// Ma ...
//
double GetMA(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate(barIndex);
//
int count = ArraySize(downTrendBuffer);
if (barIndex >= count)
{
barIndex = count - 1;
}
//
return downTrendBuffer[barIndex];
}
//
int CopyMA(
int start, // Start
int count, // Number of Items for read
double &buffer[], // Dest Buffer
bool forceClean = true // Force To Clean buffer
)
{
//
// Update Calculations ...
Calculate(start);
//
// Copy Items ...
return Copy(
0,
count,
downTrendBuffer,
buffer,
forceClean
//
);
}
//
// ADX ...
//
double GetADX(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate(barIndex);
//
int count = ArraySize(adxBuffer);
if (barIndex >= count)
{
barIndex = count - 1;
}
//
return adxBuffer[barIndex];
}
//
int CopyADX(
int start, // Start
int count, // Number of Items for read
double &buffer[], // Dest Buffer
bool forceClean = true // Force To Clean buffer
)
{
//
// Update Calculations ...
Calculate(start);
//
// Copy Items ...
return Copy(
0,
count,
adxBuffer,
buffer,
forceClean
//
);
}
//
// ADX P...
//
double GetADXP(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate(barIndex);
//
int count = ArraySize(adxPBuffer);
if (barIndex >= count)
{
barIndex = count - 1;
}
//
return adxPBuffer[barIndex];
}
//
int CopyADXP(
int start, // Start
int count, // Number of Items for read
double &buffer[], // Dest Buffer
bool forceClean = true // Force To Clean buffer
)
{
//
// Update Calculations ...
Calculate(start);
//
// Copy Items ...
return Copy(
0,
count,
adxPBuffer,
buffer,
forceClean
//
);
}
//
// ADX M ...
//
double GetADXM(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate(barIndex);
//
int count = ArraySize(adxMBuffer);
if (barIndex >= count)
{
barIndex = count - 1;
}
//
return adxMBuffer[barIndex];
}
//
int CopyADXM(
int start, // Start
int count, // Number of Items for read
double &buffer[], // Dest Buffer
bool forceClean = true // Force To Clean buffer
)
{
//
// Update Calculations ...
Calculate(start);
//
// Copy Items ...
return Copy(
0,
count,
adxMBuffer,
buffer,
forceClean
//
);
}
//
// Up Trend ...
//
double GetUpT(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate(barIndex);
//
int count = ArraySize(upTBuffer);
if (barIndex >= count)
{
barIndex = count - 1;
}
//
return upTBuffer[barIndex];
}
//
int CopyUpT(
int start, // Start
int count, // Number of Items for read
double &buffer[], // Dest Buffer
bool forceClean = true // Force To Clean buffer
)
{
//
// Update Calculations ...
Calculate(start);
//
// Copy Items ...
return Copy(
0,
count,
upTBuffer,
buffer,
forceClean
//
);
}
//
// Down Trend ...
//
double GetDownT(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate(barIndex);
//
int count = ArraySize(downTBuffer);
if (barIndex >= count)
{
barIndex = count - 1;
}
//
return downTBuffer[barIndex];
}
//
int CopyDownT(
int start, // Start
int count, // Number of Items for read
double &buffer[], // Dest Buffer
bool forceClean = true // Force To Clean buffer
)
{
//
// Update Calculations ...
Calculate(start);
//
// Copy Items ...
return Copy(
0,
count,
downTBuffer,
buffer,
forceClean
//
);
}
//
void Free() override
{
Cleanup(10);
}
//
bool GetConditions(
X121XTRENDConditions &conditions, //
int barIndex = 0, //
int loopback = 5 //
)
{
//
bool result = true;
//
if (loopback < 5)
{
loopback = 5;
}
//
conditions.Clean();
//
conditions.symbol = mSymbol;
conditions.period = mPeriod;
conditions.time = TimeCurrent();
//
int zIndex = barIndex;
int cIndex = zIndex + 1;
int pIndex = cIndex + 1;
int ppIndex = pIndex + 1;
//
XOHCL zBar;
result = zBar.Init(
mSymbol,
mPeriod,
zIndex //
);
if (!result)
{
return result;
}
//
XOHCL cBar;
result = cBar.Init(
mSymbol,
mPeriod,
cIndex //
);
if (!result)
{
return result;
}
//
XOHCL pBar;
result = pBar.Init(
mSymbol,
mPeriod,
pIndex //
);
if (!result)
{
return result;
}
//
// Buffers ...
//
CopyUpTrend(
zIndex,
loopback,
conditions.upTrendBuffer //
);
//
CopyDownTrend(
zIndex,
loopback,
conditions.downTrendBuffer //
);
//
CopyMA(
zIndex,
loopback,
conditions.maBuffer //
);
//
CopyADX(
zIndex,
loopback,
conditions.adxBuffer //
);
//
CopyADXP(
zIndex,
loopback,
conditions.adxPBuffer //
);
//
CopyADXM(
zIndex,
loopback,
conditions.adxMBuffer //
);
//
CopyUpT(
zIndex,
loopback,
conditions.upTBuffer //
);
//
CopyDownT(
zIndex,
loopback,
conditions.downTBuffer //
);
//
// Conditions ...
//
int cIDX = 1;
int pIDX = cIDX + 1;
//
bool isTrendBullish = upTBuffer[cIDX] != 0;
bool isTrendBullishPrev = upTBuffer[pIDX] != 0;
//
bool isTrendBearish = downTBuffer[cIDX] != 0;
bool isTrendBearishPrev = downTBuffer[pIDX] != 0;
//
bool isTrendSwitchedToBullish = isTrendBullish &&
!isTrendBullishPrev;
bool isTrendSwitchedToBearish = isTrendBearish &&
!isTrendBearishPrev;
//
conditions.isTrendBullish = isTrendBullish;
conditions.isTrendBearish = isTrendBearish;
conditions.isTrendSwitchedToBullish = isTrendSwitchedToBullish;
conditions.isTrendSwitchedToBearish = isTrendSwitchedToBearish;
//
Cleanup();
//
zBar.Clean();
cBar.Clean();
pBar.Clean();
//
return result;
}
//
// Protected ...
protected:
//
// Private ...
private:
//
// Props ...
X121XTRENDInputs mInputs; // Inputs ...
//
// Buffers ...
double upTrendBuffer[];
double downTrendBuffer[];
double maBuffer[];
double adxBuffer[];
double adxPBuffer[];
double adxMBuffer[];
double upTBuffer[];
double downTBuffer[];
//
void Calculate(
int barIndex = 0,
int maxRequiredBars = 100 //
)
{
//
// Buffers ...
if (barIndex < 0)
{
barIndex = 0;
}
//
// Up Trend ...
CopyBuffer(
mHandler,
X121_XTREND_UP_LINE,
barIndex,
maxRequiredBars,
upTrendBuffer
//
);
//
// Down Trend ...
CopyBuffer(
mHandler,
X121_XTREND_DOWN_LINE,
barIndex,
maxRequiredBars,
downTrendBuffer
//
);
//
// MA ...
CopyBuffer(
mHandler,
X121_XTREND_MA_LINE,
barIndex,
maxRequiredBars,
maBuffer
//
);
//
// ADX ...
CopyBuffer(
mHandler,
X121_XTREND_ADX_LINE,
barIndex,
maxRequiredBars,
adxBuffer
//
);
//
// ADX P ...
CopyBuffer(
mHandler,
X121_XTREND_ADX_P_LINE,
barIndex,
maxRequiredBars,
adxPBuffer
//
);
//
// ADX M ...
CopyBuffer(
mHandler,
X121_XTREND_ADX_M_LINE,
barIndex,
maxRequiredBars,
adxMBuffer
//
);
//
// Up T ...
CopyBuffer(
mHandler,
X121_XTREND_UP_T_LINE,
barIndex,
maxRequiredBars,
upTBuffer
//
);
//
// Down T ...
CopyBuffer(
mHandler,
X121_XTREND_DOWN_T_LINE,
barIndex,
maxRequiredBars,
downTBuffer
//
);
}
//
void Cleanup(
int maxAllowed = 100 //
)
{
//
CleanupArray(
upTrendBuffer,
maxAllowed //
);
//
CleanupArray(
downTrendBuffer,
maxAllowed //
);
//
CleanupArray(
maBuffer,
maxAllowed //
);
//
CleanupArray(
adxBuffer,
maxAllowed //
);
//
CleanupArray(
adxPBuffer,
maxAllowed //
);
//
CleanupArray(
adxMBuffer,
maxAllowed //
);
//
CleanupArray(
upTBuffer,
maxAllowed //
);
//
CleanupArray(
downTBuffer,
maxAllowed //
);
//
}
//
};
//
// Tools ...