Files
xMQL5/BKPS/14030505/Documents/BKP/Old/x-saherelm.xadxtd.helper.mq5
T

678 lines
12 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Class Library
// ----------------------------------------------
// Name: XADXTD
// Description: provides Indicator implementation
// 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.base.class.mq5"
//
// Indicator Buffers ...
enum ENUM_XADXTD_BUFFERS
{
XADXTD_STRENGTH_LINE = 0, // Strenght Buffer ...
XADXTD_BULL_LINE = 1, // Bull Buffer ...
XADXTD_BEAR_LINE = 2, // Bear Buffer ...
};
//
string GetTitle(ENUM_XADXTD_BUFFERS bufferLine)
{
//
string result = NULL;
//
switch (bufferLine)
{
//
case XADXTD_STRENGTH_LINE:
result = "XStrength";
break;
//
case XADXTD_BULL_LINE:
result = "XBull";
break;
//
case XADXTD_BEAR_LINE:
result = "XBear";
break;
}
//
return result;
}
//
// XADXTD Indicator Inputs ...
struct XADXTDInputs
{
//
// Props ...
string version;
//
// Market ...
//
int length; // Length
//
// Constructor ...
XADXTDInputs()
{
Clean();
}
//
// Tools ...
//
// Initial Inputs ...
bool Init(
int _length = 21 // Length
)
{
//
bool result = false;
//
this.length = _length;
//
result = IsValid();
//
return result;
}
//
// Cleanup ...
void Clean()
{
//
length = 0;
}
//
// Default ...
void Default()
{
//
length = 21;
}
//
// Validate ...
bool IsValid()
{
//
bool result = false;
//
result =
//
length > 0
//
;
//
return result;
}
//
// Retrieve Max Length ...
int Max()
{
//
int result = 0;
//
int values[1] = {
length //
};
//
result = GetMax(values);
//
return result;
}
};
//
// Define Conditions ...
struct XADXTDConditions
{
//
// Common ...
string symbol;
ENUM_TIMEFRAMES period;
datetime time;
//
// Buffers ...
double strength[];
double bullp[];
double bearp[];
//
// Conditions ...
//
bool isStrong;
bool isBullish;
bool isBearish;
bool isSwitchedToBullish;
bool isSwitchedToBearish;
bool isStrongSwitchedToBullish;
bool isStrongSwitchedToBearish;
//
void Clean()
{
//
Clean(strength);
Clean(bullp);
Clean(bearp);
//
ArraySetAsSeries(strength, true);
ArraySetAsSeries(bullp, true);
ArraySetAsSeries(bearp, true);
//
isStrong = false;
isBullish = false;
isBearish = false;
isSwitchedToBullish = false;
isSwitchedToBearish = false;
isStrongSwitchedToBullish = false;
isStrongSwitchedToBearish = false;
}
//
void GenerateScore(
double &bullishScore,
double &bearishScore //
)
{
//
bullishScore = 0;
bearishScore = 0;
//
if (isBullish)
{
//
bullishScore++;
if (isStrong)
{
bullishScore++;
}
}
if (isBearish)
{
bearishScore++;
if (isStrong)
{
bearishScore++;
}
}
if (isSwitchedToBullish)
{
bullishScore++;
if (isStrong)
{
bullishScore++;
}
}
if (isSwitchedToBearish)
{
bearishScore++;
if (isStrong)
{
bearishScore++;
}
}
if (isStrongSwitchedToBullish)
{
bullishScore++;
}
if (isStrongSwitchedToBearish)
{
bearishScore++;
}
}
//
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 =
//
ToString("isStrong", isStrong, ignoreFalseConditions, separator) +
ToString("isBullish", isBullish, ignoreFalseConditions, separator) +
ToString("isBearish", isBearish, ignoreFalseConditions, separator) +
ToString("isSwitchedToBullish", isSwitchedToBullish, ignoreFalseConditions, separator) +
ToString("isSwitchedToBearish", isSwitchedToBearish, ignoreFalseConditions, separator) +
ToString("isStrongSwitchedToBullish", isStrongSwitchedToBullish, ignoreFalseConditions, separator) +
ToString("isStrongSwitchedToBearish", isStrongSwitchedToBearish, ignoreFalseConditions, separator) +
""
//
;
//
result =
//
"[" + GetTag() + "]" + separator +
(onlyConditions
? ""
: commonStr) +
(!includeScores
? ""
: scoresStr) +
" " + separator +
(onlyCommons
? ""
: conditionsStr) +
""
//
;
//
return result;
}
//
string GetTag()
{
return "XADXTD";
}
};
//
// Indicator Class ...
class XSCXADXTDHelper : public XSCBase
{
//
// Public ...
public:
//
// Props ...
//
// Constructor(s) ...
void XSCXADXTDHelper(
string symbol, // Trading Symbol
ENUM_TIMEFRAMES period // Trading TimeFrame
)
{
//
mSymbol = symbol;
mPeriod = period;
}
//
// Deconstructor ...
void ~XSCXADXTDHelper() {}
//
// Initialize Indicator ...
bool Init(
XADXTDInputs &inputs // Indicator Properties
)
{
//
bool result = false;
//
result = inputs.IsValid();
if (!result)
{
return result;
}
//
// Set Inputs ...
this.mInputs = inputs;
//
// Validate Indicator State ...
result = this.IsValid();
if (!result)
{
return result;
}
//
result = DefineBuffers();
if (!result)
{
return result;
}
//
handler = iADX(
mSymbol,
mPeriod,
mInputs.length //
);
result = handler != INVALID_HANDLE;
//
return result;
}
//
XADXTDInputs GetInputs()
{
return mInputs;
}
//
bool SetInputs(
XADXTDInputs &inputs // Indicator Properties
)
{
return Init(inputs);
}
//
// Get Tag ...
string GetTag()
{
//
string result = NULL;
//
result = GetToken();
//
return result;
}
//
// Get Token ...
string GetToken()
{
//
string result = NULL;
//
result = GetSpecificToken(this);
//
return result;
}
//
string GetSymbol()
{
return mSymbol;
}
//
ENUM_TIMEFRAMES GetPeriod()
{
return mPeriod;
}
//
// Validate ...
bool IsValid()
{
//
bool result = false;
//
result =
//
mInputs.IsValid() &&
IsSpecifiedValid(mPeriod) &&
IsSpecifiedValid(mSymbol)
//
;
//
return result;
}
//
// De Initialize Class ...
void DeInit(int reason)
{
//
IndicatorRelease(handler);
}
//
// Tools ...
int CopyData(
ENUM_XADXTD_BUFFERS line,
double &dest[],
int start = 0,
int count = 1 //
)
{
//
int result = 0;
//
int mLine = -1;
switch (line)
{
//
case XADXTD_STRENGTH_LINE:
mLine = MAIN_LINE;
break;
//
case XADXTD_BULL_LINE:
mLine = PLUSDI_LINE;
break;
//
case XADXTD_BEAR_LINE:
mLine = MINUSDI_LINE;
break;
}
//
if (mLine == -1 || handler == INVALID_HANDLE)
{
return result;
}
//
result = CopyBuffer(
handler,
mLine,
start,
count,
dest //
);
//
return result;
}
//
bool GetConditions(
XADXTDConditions &conditions, //
int barIndex = 0, //
int loopback = 3 //
)
{
//
bool result = true;
//
if (loopback < 3)
{
loopback = 3;
}
//
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;
//
CopyData(
XADXTD_STRENGTH_LINE,
conditions.strength,
zIndex,
loopback //
);
CopyData(
XADXTD_BULL_LINE,
conditions.bullp,
zIndex,
loopback //
);
CopyData(
XADXTD_BEAR_LINE,
conditions.bearp,
zIndex,
loopback //
);
//
// Calculate Conditions ...
//
bool isStrong = conditions.strength[cIndex] >= 20;
//
bool isBullish = conditions.bullp[cIndex] > conditions.bearp[cIndex];
bool isBearish = conditions.bearp[cIndex] > conditions.bullp[cIndex];
//
bool isBullishPrev = conditions.bullp[pIndex] > conditions.bearp[pIndex];
bool isBearishPrev = conditions.bearp[pIndex] > conditions.bullp[pIndex];
//
bool isSwitchedToBullish = isBullish &&
!isBullishPrev;
bool isSwitchedToBearish = isBearish &&
!isBearishPrev;
//
bool isStrongSwitchedToBullish = isStrong &&
isSwitchedToBullish;
bool isStrongSwitchedToBearish = isStrong &&
isSwitchedToBearish;
//
conditions.isStrong = isStrong;
conditions.isBullish = isBullish;
conditions.isBearish = isBearish;
conditions.isSwitchedToBullish = isSwitchedToBullish;
conditions.isSwitchedToBearish = isSwitchedToBearish;
conditions.isStrongSwitchedToBullish = isStrongSwitchedToBullish;
conditions.isStrongSwitchedToBearish = isStrongSwitchedToBearish;
//
return result;
}
//
// Protected ...
protected:
//
// Props ...
int handler;
//
// Tools ...
bool DefineBuffers()
{
//
bool result = false;
//
result = true;
//
return result;
}
//
// Private ...
private:
//
// Props ...
//
string mSymbol; // Symbol
ENUM_TIMEFRAMES mPeriod; // TimeFrame
//
XADXTDInputs mInputs; // Properties
};
//