Files
MQL5Data/Helpers/x-saherelm.x121.xstr.helper.mq5
T

1137 lines
21 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Helper Class Library
// ----------------------------------------------
// Name: XCX121XSTRHelper
// 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_XSTR_BUFFERS
{
//
X121_XSTR_LINE = 0,
X121_XSTR_UP_LINE = 2,
X121_XSTR_DOWN_LINE = 3,
X121_XSTR_ATR_LINE = 4,
X121_XSTR_PRICE_LINE = 5,
X121_XSTR_STATE_LINE = 6,
};
//
// Input Models ...
struct X121XSTRInputs
{
//
// Props ...
int strLength; // Length
double strMultiplier; // Multiplier
ENUM_APPLIED_PRICE strAppliedTo; // Applied To
//
bool showStr; // Show Upper Zone
bool showStrUpper; // Show Str Upper
bool showStrLower; // Show Str Lower
//
// Constructor(s) ...
X121XSTRInputs()
{
//
Clean();
}
//
// Tools ...
//
// Clean ...
void Clean()
{
//
strLength = 0;
strMultiplier = 0.0;
strAppliedTo = PRICE_MEDIAN;
//
showStr = false;
showStrUpper = false;
showStrLower = false;
//
ZeroMemory(this);
}
//
// Default ...
void Default()
{
//
strLength = 14;
strMultiplier = 3.0;
strAppliedTo = PRICE_CLOSE;
//
showStr = true;
showStrUpper = true;
showStrLower = true;
}
//
// Validate ...
bool IsValid()
{
//
bool result = false;
//
result =
//
strLength > 0 &&
strMultiplier > 0
//
;
//
return result;
}
//
// Retrieve MAx Length ...
int Max()
{
//
int result = 0;
//
result = MathMax(1, strLength);
//
return result;
}
};
//
// Conditions ...
struct X121XSTRConditions
{
//
// Common ...
string symbol;
ENUM_TIMEFRAMES period;
datetime time;
//
// Buffers ...
double strBuffer[];
double atrBuffer[];
double strPriceBuffer[];
double strUpBuffer[];
double strDownBuffer[];
double strStateBuffer[];
//
// Conditions ...
//
bool isStrBullish;
bool isStrBearish;
//
bool isStrSwitchedToBullish;
bool isStrSwitchedToBearish;
//
// bool isCloseLower
//
// Constructor ...
X121XSTRConditions()
{
Clean();
}
//
// Tools ...
/**
* Cleaning Up ...
*/
void Clean()
{
//
// Commons ...
symbol = NULL;
period = NULL;
time = NULL;
//
// Buffers ...
//
Clean(strBuffer);
Clean(atrBuffer);
Clean(strPriceBuffer);
Clean(strUpBuffer);
Clean(strDownBuffer);
Clean(strStateBuffer);
//
ArraySetAsSeries(strBuffer, true);
ArraySetAsSeries(atrBuffer, true);
ArraySetAsSeries(strPriceBuffer, true);
ArraySetAsSeries(strUpBuffer, true);
ArraySetAsSeries(strDownBuffer, true);
ArraySetAsSeries(strStateBuffer, true);
//
// Conditions ...
//
isStrBullish = false;
isStrBearish = false;
//
isStrSwitchedToBullish = false;
isStrSwitchedToBearish = 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 (isStrBullish)
{
bullishScore += minScore;
}
if (isStrSwitchedToBullish)
{
bullishScore += score;
}
//
if (isStrBearish)
{
bearishScore += minScore;
}
if (isStrSwitchedToBearish)
{
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 +
"XSTR: " + separator +
"-----------------------" + separator +
ToString("isStrBullish", isStrBullish, ignoreFalseConditions, separator) +
ToString("isStrBearish", isStrBearish, ignoreFalseConditions, separator) +
ToString("isStrSwitchedToBullish", isStrSwitchedToBullish, ignoreFalseConditions, separator) +
ToString("isStrSwitchedToBearish", isStrSwitchedToBearish, 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 XCX121XSTRHelper : public XCBaseHelper
{
//
// Public ...
public:
//
// Props ...
//
// Constructors ...
XCX121XSTRHelper()
: XCBaseHelper(_Symbol, _Period)
{
}
//
// Deconstructor ...
~XCX121XSTRHelper()
{
//
mInputs.Clean();
//
Clean(strBuffer);
Clean(atrBuffer);
Clean(strPriceBuffer);
Clean(strUpBuffer);
Clean(strDownBuffer);
Clean(strStateBuffer);
}
//
// Tools ...
bool Init(
string symbol, // Trading Symbol
ENUM_TIMEFRAMES period, // Trading Period
X121XSTRInputs &inputs // Inputs
)
{
//
bool result = false;
//
mSymbol = symbol;
mPeriod = period;
//
result = inputs.IsValid();
if (!result)
{
return result;
}
//
ArraySetAsSeries(strBuffer, true);
ArraySetAsSeries(atrBuffer, true);
ArraySetAsSeries(strPriceBuffer, true);
ArraySetAsSeries(strUpBuffer, true);
ArraySetAsSeries(strDownBuffer, true);
ArraySetAsSeries(strStateBuffer, true);
//
mInputs = inputs;
//
mHandler = iCustom(
mSymbol,
mPeriod,
"x-saherelm.x121.xstr",
//
// Inputs ...
//
// Market ...
"",
mInputs.strLength,
mInputs.strMultiplier,
mInputs.strAppliedTo,
//
// Presentation ...
"",
mInputs.showStr,
mInputs.showStrUpper,
mInputs.showStrLower
//
);
result = mHandler != INVALID_HANDLE;
if (!result)
{
return result;
}
//
return result;
}
//
// Inputs ...
//
X121XSTRInputs GetInputs()
{
return mInputs;
}
//
bool SetInputs(
X121XSTRInputs &inputs // Configs
)
{
//
return Init(
mSymbol,
mPeriod,
inputs
//
);
}
//
// Readers ...
//
// ATR ...
//
double GetATR(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
int count = ArraySize(atrBuffer);
if (barIndex >= count)
{
barIndex = count - 1;
}
//
return atrBuffer[barIndex];
}
//
int CopyATR(
int start, // Start
int count, // Number of Items for read
double &buffer[], // Dest Buffer
bool forceClean = true // Force To Clean buffer
)
{
//
// Update Calculations ...
Calculate();
//
// Copy Items ...
return Copy(
start,
count,
atrBuffer,
buffer,
forceClean
//
);
}
//
double GetSTR(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
int count = ArraySize(strBuffer);
if (barIndex >= count)
{
barIndex = count - 1;
}
//
return strBuffer[barIndex];
}
//
int CopySTR(
int start, // Start
int count, // Number of Items for read
double &buffer[], // Dest Buffer
bool forceClean = true // Force To Clean buffer
)
{
//
// Update Calculations ...
Calculate();
//
// Copy Items ...
return Copy(
start,
count,
strBuffer,
buffer,
forceClean
//
);
}
//
double GetSTRState(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
int count = ArraySize(strStateBuffer);
if (barIndex >= count)
{
barIndex = count - 1;
}
//
return strStateBuffer[barIndex];
}
//
int CopySTRState(
int start, // Start
int count, // Number of Items for read
double &buffer[], // Dest Buffer
bool forceClean = true // Force To Clean buffer
)
{
//
// Update Calculations ...
Calculate();
//
// Copy Items ...
return Copy(
start,
count,
strStateBuffer,
buffer,
forceClean
//
);
}
//
double GetSTRPrice(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
int count = ArraySize(strPriceBuffer);
if (barIndex >= count)
{
barIndex = count - 1;
}
//
return strPriceBuffer[barIndex];
}
//
int CopySTRPrice(
int start, // Start
int count, // Number of Items for read
double &buffer[], // Dest Buffer
bool forceClean = true // Force To Clean buffer
)
{
//
// Update Calculations ...
Calculate();
//
// Copy Items ...
return Copy(
start,
count,
strPriceBuffer,
buffer,
forceClean
//
);
}
//
double GetSTRUp(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
int count = ArraySize(strUpBuffer);
if (barIndex >= count)
{
barIndex = count - 1;
}
//
return strUpBuffer[barIndex];
}
//
int CopySTRUp(
int start, // Start
int count, // Number of Items for read
double &buffer[], // Dest Buffer
bool forceClean = true // Force To Clean buffer
)
{
//
// Update Calculations ...
Calculate();
//
// Copy Items ...
return Copy(
start,
count,
strUpBuffer,
buffer,
forceClean
//
);
}
//
double GetSTRDown(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
int count = ArraySize(strDownBuffer);
if (barIndex >= count)
{
barIndex = count - 1;
}
//
return strDownBuffer[barIndex];
}
//
int CopySTRDown(
int start, // Start
int count, // Number of Items for read
double &buffer[], // Dest Buffer
bool forceClean = true // Force To Clean buffer
)
{
//
// Update Calculations ...
Calculate();
//
// Copy Items ...
return Copy(
start,
count,
strDownBuffer,
buffer,
forceClean
//
);
}
//
double GetLastSTR(
ENUM_X_DIRECTION dir,
int barIndex = 0 //
)
{
//
double result = 0;
//
if (barIndex < 0)
{
barIndex = 0;
}
//
if (!HasDirection(dir))
{
return result;
}
//
int index = barIndex;
bool isBullish = IsBullish(dir);
//
bool canContinue = true;
while (canContinue)
{
//
double iStr = GetSTR(index);
double iState = GetSTRState(index);
//
canContinue =
isBullish
? iState < 0
: iState > 0;
if (!canContinue)
{
//
result = iStr;
break;
}
//
index++;
}
//
return result;
}
//
void Free() override
{
Cleanup(10);
}
//
bool GetConditions(
X121XSTRConditions &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 ...
//
CopyATR(
zIndex,
loopback,
conditions.atrBuffer //
);
//
CopySTR(
zIndex,
loopback,
conditions.strBuffer //
);
//
CopySTRState(
zIndex,
loopback,
conditions.strStateBuffer //
);
//
CopySTRPrice(
zIndex,
loopback,
conditions.strPriceBuffer //
);
//
CopySTRUp(
zIndex,
loopback,
conditions.strUpBuffer //
);
//
CopySTRDown(
zIndex,
loopback,
conditions.strDownBuffer //
);
//
// Conditions ...
//
int cIDX = 1;
int pIDX = cIDX + 1;
//
bool isStrBullish = strStateBuffer[cIDX] > 0;
bool isStrBullishPrev = strStateBuffer[pIDX] > 0;
//
bool isStrBearish = strStateBuffer[cIDX] < 0;
bool isStrBearishPrev = strStateBuffer[pIDX] < 0;
//
bool isStrSwitchedToBullish = isStrBullish &&
!isStrBullishPrev;
bool isStrSwitchedToBearish = isStrBearish &&
!isStrBearishPrev;
//
conditions.isStrBullish = isStrBullish;
conditions.isStrBearish = isStrBearish;
conditions.isStrSwitchedToBullish = isStrSwitchedToBullish;
conditions.isStrSwitchedToBearish = isStrSwitchedToBearish;
//
Cleanup();
//
zBar.Clean();
cBar.Clean();
pBar.Clean();
//
return result;
}
//
// Protected ...
protected:
//
// Private ...
private:
//
// Props ...
X121XSTRInputs mInputs; // Inputs ...
//
// Buffers ...
double strBuffer[];
double atrBuffer[];
double strPriceBuffer[];
double strUpBuffer[];
double strDownBuffer[];
double strStateBuffer[];
//
void Calculate(
int maxRequiredBars = 100 //
)
{
//
// Buffers ...
//
// STR ...
CopyBuffer(
mHandler,
X121_XSTR_LINE,
0,
maxRequiredBars,
strBuffer
//
);
//
// State ...
CopyBuffer(
mHandler,
X121_XSTR_STATE_LINE,
0,
maxRequiredBars,
strStateBuffer
//
);
//
// Price ...
CopyBuffer(
mHandler,
X121_XSTR_PRICE_LINE,
0,
maxRequiredBars,
strPriceBuffer
//
);
//
// Up ...
CopyBuffer(
mHandler,
X121_XSTR_UP_LINE,
0,
maxRequiredBars,
strUpBuffer
//
);
//
// STR ...
CopyBuffer(
mHandler,
X121_XSTR_DOWN_LINE,
0,
maxRequiredBars,
strDownBuffer
//
);
//
// ATR ...
//
CopyBuffer(
mHandler,
X121_XSTR_ATR_LINE,
0,
maxRequiredBars,
atrBuffer
//
);
}
//
void Cleanup(
int maxAllowed = 100 //
)
{
//
CleanupArray(
strBuffer,
maxAllowed //
);
//
CleanupArray(
atrBuffer,
maxAllowed //
);
//
CleanupArray(
strPriceBuffer,
maxAllowed //
);
//
CleanupArray(
strUpBuffer,
maxAllowed //
);
//
CleanupArray(
strDownBuffer,
maxAllowed //
);
//
CleanupArray(
strStateBuffer,
maxAllowed //
);
//
}
//
};
//
// Tools ...