Files
MQL5Data/Helpers/x-saherelm.xhull.helper.mq5
T

680 lines
13 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Helper Class Library
// ----------------------------------------------
// Name: XSCXHULLHelper
// 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.xhelper.class.mq5"
//
// Definitions ...
//
enum ENUM_XHULLC_BUFFERS
{
XHULLC_UP_LINE = 0,
XHULLC_DOWN_LINE = 2,
};
//
// Input Models ...
struct XHULLInputs
{
//
// Props ...
//
// Market ...
int length; // Length
double divisor; // Divisor (Speed)
//
// Calculation ...
ENUM_APPLIED_PRICE upAppliedTo; // Up Zone Applied to
ENUM_APPLIED_PRICE downAppliedTo; // Down Zone Applied to
//
// Presentation ...
bool showUpZone; // Show Up Zone
bool showDownZone; // Show Down Zone
//
// Constructor(s) ...
XHULLInputs()
{
//
Clean();
}
//
// Tools ...
//
// Clean ...
void Clean()
{
//
// Market ...
length = 0;
divisor = 0;
//
// Calculation ...
upAppliedTo = PRICE_HIGH;
downAppliedTo = PRICE_LOW;
//
// Presentation ...
showUpZone = false;
showDownZone = false;
}
//
// Default ...
void Default()
{
//
// Market ...
length = 72;
divisor = 2.0;
//
// Calculation ...
upAppliedTo = PRICE_HIGH;
downAppliedTo = PRICE_LOW;
//
// Presentation ...
showUpZone = true;
showDownZone = true;
}
//
// Validate ...
bool IsValid()
{
//
bool result = false;
//
result =
//
length >= 9 &&
divisor >= 0.5
//
;
//
return result;
}
//
// Retrieve MAx Length ...
int Max()
{
//
int result = 0;
//
result = MathMax(0, length);
//
return result;
}
};
//
// Define Conditions ...
struct XHULLConditions
{
//
// Common ...
string symbol;
ENUM_TIMEFRAMES period;
datetime time;
//
// Buffers ...
double up[];
double down[];
//
// Conditions ...
//
bool isBullish;
bool isBearish;
bool isUpBullish;
bool isUpBearish;
bool isDownBullish;
bool isDownBearish;
bool isSwitchedToBullish;
bool isSwitchedToBearish;
//
void Clean()
{
//
Clean(up);
Clean(down);
//
ArraySetAsSeries(up, true);
ArraySetAsSeries(down, true);
//
isBullish = false;
isBearish = false;
isUpBullish = false;
isUpBearish = false;
isDownBullish = false;
isDownBearish = false;
isSwitchedToBullish = false;
isSwitchedToBearish = false;
}
//
void GenerateScore(
double &bullishScore,
double &bearishScore //
)
{
//
bullishScore = 0;
bearishScore = 0;
//
if (isBullish)
{
bullishScore++;
}
if (isUpBullish)
{
bullishScore++;
}
if (isDownBullish)
{
bullishScore++;
}
if (isSwitchedToBullish)
{
bullishScore++;
}
//
if (isBearish)
{
bearishScore++;
}
if (isUpBearish)
{
bearishScore++;
}
if (isDownBearish)
{
bearishScore++;
}
if (isSwitchedToBearish)
{
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("isBullish", isBullish, ignoreFalseConditions, separator) +
ToString("isBearish", isBearish, ignoreFalseConditions, separator) +
ToString("isUpBullish", isUpBullish, ignoreFalseConditions, separator) +
ToString("isUpBearish", isUpBearish, ignoreFalseConditions, separator) +
ToString("isDownBullish", isDownBullish, ignoreFalseConditions, separator) +
ToString("isDownBearish", isDownBearish, ignoreFalseConditions, separator) +
ToString("isSwitchedToBullish", isSwitchedToBullish, ignoreFalseConditions, separator) +
ToString("isSwitchedToBearish", isSwitchedToBearish, ignoreFalseConditions, separator) +
""
//
;
//
result =
//
"[" + GetTag() + "]" + separator +
(onlyConditions
? ""
: commonStr) +
(!includeScores
? ""
: scoresStr) +
" " + separator +
(onlyCommons
? ""
: conditionsStr) +
""
//
;
//
return result;
}
//
string GetTag()
{
return "XHULL";
}
};
//
// Class ...
class XSCXHULLHelper : public XSCBaseHelper
{
//
// Public ...
public:
//
// Props ...
//
// Constructors ...
XSCXHULLHelper() : XSCBaseHelper(_Symbol, _Period)
{
}
//
// Deconstructor ...
~XSCXHULLHelper()
{
//
Clean(upBuffer);
Clean(downBuffer);
}
//
// Tools ...
bool Init(
string symbol, // Trading Symbol
ENUM_TIMEFRAMES period, // Trading Period
XHULLInputs &inputs // Inputs
)
{
//
bool result = false;
//
mSymbol = symbol;
mPeriod = period;
//
result = inputs.IsValid();
if (!result)
{
return result;
}
//
mInputs = inputs;
//
// ArraySetAsSeries(mainBuffer, true);
mHandler = iCustom(
mSymbol,
mPeriod,
"x-saherelm.xhull",
//
// Inputs ...
//
// Market ...
"",
mInputs.length, // Length
mInputs.divisor, // Divisor (Speed)
//
// Calculation ...
"",
mInputs.upAppliedTo, // Up Zone Applied to
mInputs.downAppliedTo, // Down Zone Applied to
//
// Presentation ...
"",
mInputs.showUpZone, // Show Up Zone
mInputs.showDownZone // Show Down Zone
//
);
result = mHandler != INVALID_HANDLE;
if (!result)
{
return result;
}
//
return result;
}
//
// Inputs ...
//
XHULLInputs GetInputs()
{
return mInputs;
}
//
bool SetInputs(
XHULLInputs &inputs // Configs
)
{
//
return Init(
mSymbol,
mPeriod,
inputs
//
);
}
//
// Buffers ...
//
double GetUp(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
int count = ArraySize(upBuffer);
if (barIndex >= count)
{
barIndex = count - 1;
}
//
return upBuffer[barIndex];
}
//
// Copy Required Items ...
int CopyUp(
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,
upBuffer,
buffer,
forceClean
//
);
}
//
double GetDown(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
int count = ArraySize(downBuffer);
if (barIndex >= count)
{
barIndex = count - 1;
}
//
return downBuffer[barIndex];
}
//
// Copy Required Items ...
int CopyDown(
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,
downBuffer,
buffer,
forceClean
//
);
}
//
bool GetConditions(
XHULLConditions &conditions, //
int barIndex = 0, //
int loopback = 4 //
)
{
//
bool result = true;
//
if (loopback < 4)
{
loopback = 4;
}
//
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;
//
CopyUp(
zIndex,
loopback,
conditions.up //
);
CopyDown(
zIndex,
loopback,
conditions.down //
);
//
// Calculate Conditions ...
//
bool isUpBullish = conditions.up[cIndex] > conditions.up[pIndex];
bool isUpBearish = conditions.up[cIndex] < conditions.up[pIndex];
//
bool isUpBullishPrev = conditions.up[pIndex] > conditions.up[ppIndex];
bool isUpBearishPrev = conditions.up[pIndex] < conditions.up[ppIndex];
//
bool isDownBullish = conditions.down[cIndex] > conditions.down[pIndex];
bool isDownBearish = conditions.down[cIndex] < conditions.down[pIndex];
//
bool isDownBullishPrev = conditions.down[pIndex] > conditions.down[ppIndex];
bool isDownBearishPrev = conditions.down[pIndex] < conditions.down[ppIndex];
//
bool isBullish = isUpBullish &&
isDownBullish;
bool isBearish = isUpBearish &&
isDownBearish;
//
bool isBullishPrev = isUpBullishPrev &&
isDownBullishPrev;
bool isBearishPrev = isUpBearishPrev &&
isDownBearishPrev;
//
bool isSwitchedToBullish = isBullish &&
!isBullishPrev;
bool isSwitchedToBearish = isBearish &&
!isBearishPrev;
//
conditions.isBullish = isBullish;
conditions.isBearish = isBearish;
conditions.isUpBullish = isUpBullish;
conditions.isUpBearish = isUpBearish;
conditions.isDownBullish = isDownBullish;
conditions.isDownBearish = isDownBearish;
conditions.isSwitchedToBullish = isSwitchedToBullish;
conditions.isSwitchedToBearish = isSwitchedToBearish;
//
return result;
}
//
// Protected ...
protected:
//
// Private ...
private:
//
// Props ...
XHULLInputs mInputs; // Inputs ...
//
// Buffers ...
double upBuffer[];
double downBuffer[];
//
// Tools ...
//
// Calculate Values Until Now ...
void Calculate()
{
//
int totalBars = CountBars();
if (totalBars > 1000)
{
totalBars = 1000;
}
//
// Up Buffer ...
CopyBuffer(
mHandler,
XHULLC_UP_LINE,
0,
totalBars,
upBuffer
//
);
//
// Down Buffer ...
CopyBuffer(
mHandler,
XHULLC_DOWN_LINE,
0,
totalBars,
downBuffer
//
);
}
};
//
// Tools ...