Files
xMQL5/MQLTestWorkspace/BKPS/14030322/Helpers/x-saherelm.xhk.helper.mq5
T

1293 lines
24 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Helper Class Library
// ----------------------------------------------
// Name: XSCXHKHelper
// 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_XHK_BUFFERS
{
//
// Hiken Ashi ...
XHK_OPEN_LINE = 0,
XHK_HIGHLINE = 1,
XHK_LOW_LINE = 2,
XHK_CLOSE_LINE = 3,
//
// Smoothed Hiken Ashi ...
XHK_SM_OPEN_LINE = 5,
XHK_SM_HIGHLINE = 6,
XHK_SM_LOW_LINE = 7,
XHK_SM_CLOSE_LINE = 8,
};
//
// Input Models ...
struct XHKInputs
{
//
// Props ...
//
// Market ...
int smoothingLength; // Smoothing Length
//
// Presentation ...
bool drawHikenAshi; // Draw Hiken Ashi
bool drawSmoothedHikenAshi; // Draw Smoothed Hiken Ashi
//
// Constructor(s) ...
XHKInputs()
{
//
Clean();
}
//
// Tools ...
//
// Clean ...
void Clean()
{
//
// Market ...
smoothingLength = 0;
//
// Presentation ...
drawHikenAshi = false;
drawSmoothedHikenAshi = false;
}
//
// Default ...
void Default()
{
//
// Market ...
smoothingLength = 17;
//
// Presentation ...
drawHikenAshi = true;
drawSmoothedHikenAshi = true;
}
//
// Validate ...
bool IsValid()
{
//
bool result = false;
//
result =
//
smoothingLength > 2
//
;
//
return result;
}
//
// Retrieve MAx Length ...
int Max()
{
//
int result = 0;
//
result = MathMax(0, smoothingLength);
//
return result;
}
};
//
// Define Conditions ...
struct XHKConditions
{
//
// Common ...
string symbol;
ENUM_TIMEFRAMES period;
datetime time;
//
// Buffers ...
XOHCL hkBar[];
XOHCL smHKBar[];
//
// Conditions ...
//
bool isHKBullish;
bool isHKBearish;
bool isHKSwitchedToBullish;
bool isHKSwitchedToBearish;
//
bool isSMHKBullish;
bool isSMHKBearish;
bool isSMHKSwitchedToBullish;
bool isSMHKSwitchedToBearish;
//
void Clean()
{
//
Clean(hkBar);
Clean(smHKBar);
//
ArraySetAsSeries(hkBar, true);
ArraySetAsSeries(smHKBar, true);
//
isHKBullish = false;
isHKBearish = false;
isHKSwitchedToBullish = false;
isHKSwitchedToBearish = false;
//
isSMHKBullish = false;
isSMHKBearish = false;
isSMHKSwitchedToBullish = false;
isSMHKSwitchedToBearish = false;
}
//
void GenerateScore(
double &bullishScore,
double &bearishScore //
)
{
//
bullishScore = 0;
bearishScore = 0;
//
if (isHKBullish)
{
bullishScore++;
}
if (isHKSwitchedToBullish)
{
bullishScore++;
}
//
if (isHKBearish)
{
bearishScore++;
}
if (isHKSwitchedToBearish)
{
bearishScore++;
}
//
if (isSMHKBullish)
{
bullishScore++;
}
if (isSMHKSwitchedToBullish)
{
bullishScore++;
}
//
if (isSMHKBearish)
{
bearishScore++;
}
if (isSMHKSwitchedToBearish)
{
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("isHKBullish", isHKBullish, ignoreFalseConditions, separator) +
ToString("isHKSwitchedToBullish", isHKSwitchedToBullish, ignoreFalseConditions, separator) +
ToString("isHKBearish", isHKBearish, ignoreFalseConditions, separator) +
ToString("isHKSwitchedToBearish", isHKSwitchedToBearish, ignoreFalseConditions, separator) +
//
ToString("isSMHKBullish", isSMHKBullish, ignoreFalseConditions, separator) +
ToString("isSMHKSwitchedToBullish", isSMHKSwitchedToBullish, ignoreFalseConditions, separator) +
ToString("isSMHKBearish", isSMHKBearish, ignoreFalseConditions, separator) +
ToString("isSMHKSwitchedToBearish", isSMHKSwitchedToBearish, ignoreFalseConditions, separator) +
""
//
;
//
result =
//
"[" + GetTag() + "]" + separator +
(onlyConditions
? ""
: commonStr) +
(!includeScores
? ""
: scoresStr) +
" " + separator +
(onlyCommons
? ""
: conditionsStr) +
""
//
;
//
return result;
}
//
string GetTag()
{
return "XHK";
}
};
//
// Class ...
class XSCXHKHelper : public XSCBaseHelper
{
//
// Public ...
public:
//
// Props ...
//
// Constructors ...
XSCXHKHelper() : XSCBaseHelper(_Symbol, _Period)
{
}
//
// Deconstructor ...
~XSCXHKHelper() {}
//
// Tools ...
bool Init(
string symbol, // Trading Symbol
ENUM_TIMEFRAMES period, // Trading Period
XHKInputs &inputs // Inputs
)
{
//
bool result = false;
//
mSymbol = symbol;
mPeriod = period;
//
result = inputs.IsValid();
if (!result)
{
return result;
}
//
mInputs = inputs;
//
ArraySetAsSeries(openBuffer, true);
ArraySetAsSeries(highBuffer, true);
ArraySetAsSeries(lowBuffer, true);
ArraySetAsSeries(closeBuffer, true);
ArraySetAsSeries(smOpenBuffer, true);
ArraySetAsSeries(smHighBuffer, true);
ArraySetAsSeries(smLowBuffer, true);
ArraySetAsSeries(smCloseBuffer, true);
//
mHandler = iCustom(
mSymbol,
mPeriod,
"x-saherelm.xhk",
//
// Inputs ...
//
// Market ...
"",
mInputs.smoothingLength, // Smoothing Length
//
// Presentation ...
"",
mInputs.drawHikenAshi, // Draw Hiken Ashi
mInputs.drawSmoothedHikenAshi // Draw Smoothed Hiken Ashi
//
);
result = mHandler != INVALID_HANDLE;
if (!result)
{
return result;
}
//
return result;
}
//
// Inputs ...
//
XHKInputs GetInputs()
{
return mInputs;
}
//
bool SetInputs(
XHKInputs &inputs // Configs
)
{
//
return Init(
mSymbol,
mPeriod,
inputs
//
);
}
//
// Buffers ...
//
double GetOpen(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
int count = ArraySize(openBuffer);
if (barIndex >= count)
{
barIndex = count - 1;
}
//
return openBuffer[barIndex];
}
//
// Copy Required Items ...
int CopyOpen(
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,
openBuffer,
buffer,
forceClean
//
);
}
//
double GetHigh(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
int count = ArraySize(highBuffer);
if (barIndex >= count)
{
barIndex = count - 1;
}
//
return highBuffer[barIndex];
}
//
// Copy Required Items ...
int CopyHigh(
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,
highBuffer,
buffer,
forceClean
//
);
}
//
double GetLow(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
int count = ArraySize(lowBuffer);
if (barIndex >= count)
{
barIndex = count - 1;
}
//
return lowBuffer[barIndex];
}
//
// Copy Required Items ...
int CopyLow(
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,
lowBuffer,
buffer,
forceClean
//
);
}
//
double GetClose(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
int count = ArraySize(closeBuffer);
if (barIndex >= count)
{
barIndex = count - 1;
}
//
return closeBuffer[barIndex];
}
//
// Copy Required Items ...
int CopyClose(
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,
closeBuffer,
buffer,
forceClean
//
);
}
//
// Smoothed ...
//
double GetSMOpen(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
int count = ArraySize(smOpenBuffer);
if (barIndex >= count)
{
barIndex = count - 1;
}
//
return smOpenBuffer[barIndex];
}
//
// Copy Required Items ...
int CopySMOpen(
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,
smOpenBuffer,
buffer,
forceClean
//
);
}
//
double GetSMHigh(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
int count = ArraySize(smHighBuffer);
if (barIndex >= count)
{
barIndex = count - 1;
}
//
return smHighBuffer[barIndex];
}
//
// Copy Required Items ...
int CopySMHigh(
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,
smHighBuffer,
buffer,
forceClean
//
);
}
//
double GetSMLow(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
int count = ArraySize(smLowBuffer);
if (barIndex >= count)
{
barIndex = count - 1;
}
//
return smLowBuffer[barIndex];
}
//
// Copy Required Items ...
int CopySMLow(
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,
smLowBuffer,
buffer,
forceClean
//
);
}
//
double GetSMClose(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
int count = ArraySize(smCloseBuffer);
if (barIndex >= count)
{
barIndex = count - 1;
}
//
return smCloseBuffer[barIndex];
}
//
// Copy Required Items ...
int CopySMClose(
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,
smCloseBuffer,
buffer,
forceClean
//
);
}
//
// XOHCL ...
//
// Retrieve Hiken Ashi Info as XOHCL model ...
bool AsOHCL(
XOHCL &bar, // Holds Result
int barIndex // Bar Index
)
{
//
bool result = false;
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
int count = ArraySize(openBuffer);
if (barIndex >= count)
{
barIndex = count - 1;
}
//
double open = GetOpen(barIndex);
double high = GetHigh(barIndex);
double close = GetClose(barIndex);
double low = GetLow(barIndex);
//
result = bar.Init(
mSymbol,
mPeriod,
open,
high,
close,
low,
barIndex
//
);
//
return result;
}
//
// Copy Required Items ...
int CopyAsOHCL(
int start, // Start
int count, // Number of Items for read
XOHCL &buffer[], // Dest Buffer
bool forceClean = true // Force To Clean buffer
)
{
//
int result = 0;
//
// Update Calculations ...
Calculate();
//
if (forceClean)
{
Clean(buffer);
}
//
int before = ArraySize(buffer);
//
if (start < 0)
{
start = 0;
}
//
if (start >= ArraySize(openBuffer))
{
start = ArraySize(openBuffer) - 1;
}
//
bool asSeriesBuffer = ArrayGetAsSeries(buffer);
ArraySetAsSeries(buffer, true);
//
for (int i = start; i < start + count; i++)
{
//
XOHCL iBar;
bool isInit = AsOHCL(iBar, i);
//
if (isInit)
{
//
AddRef(
iBar,
buffer
//
);
}
}
//
ArraySetAsSeries(buffer, asSeriesBuffer);
//
int after = ArraySize(buffer);
//
result = after - before;
//
return result;
}
//
// Retrieve Smoothed Hiken Ashi Info as XOHCL model ...
bool SMAsOHCL(
XOHCL &bar, // Holds Result
int barIndex // Bar Index
)
{
//
bool result = false;
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
int count = ArraySize(openBuffer);
if (barIndex >= count)
{
barIndex = count - 1;
}
//
double open = GetSMOpen(barIndex);
double high = GetSMHigh(barIndex);
double close = GetSMClose(barIndex);
double low = GetSMLow(barIndex);
//
result = bar.Init(
mSymbol,
mPeriod,
open,
high,
close,
low,
barIndex
//
);
//
return result;
}
//
// Copy Required Items ...
int CopySMAsOHCL(
int start, // Start
int count, // Number of Items for read
XOHCL &buffer[], // Dest Buffer
bool forceClean = true // Force To Clean buffer
)
{
//
int result = 0;
//
// Update Calculations ...
Calculate();
//
if (forceClean)
{
Clean(buffer);
}
//
int before = ArraySize(buffer);
//
if (start < 0)
{
start = 0;
}
//
if (start >= ArraySize(openBuffer))
{
start = ArraySize(openBuffer) - 1;
}
//
bool asSeriesBuffer = ArrayGetAsSeries(buffer);
ArraySetAsSeries(buffer, true);
//
for (int i = start; i < start + count; i++)
{
//
XOHCL iBar;
bool isInit = SMAsOHCL(iBar, i);
//
if (isInit)
{
//
AddRef(
iBar,
buffer
//
);
}
}
//
ArraySetAsSeries(buffer, asSeriesBuffer);
//
int after = ArraySize(buffer);
//
result = after - before;
//
return result;
}
//
bool GetConditions(
XHKConditions &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;
//
CopyAsOHCL(
zIndex,
loopback,
conditions.hkBar //
);
CopySMAsOHCL(
zIndex,
loopback,
conditions.smHKBar //
);
//
// Calculate Conditions ...
//
bool isHKBullish = conditions.hkBar[cIndex].IsBullish();
bool isHKBullishPrev = conditions.hkBar[pIndex].IsBullish();
//
bool isHKSwitchedToBullish = isHKBullish &&
!isHKBullishPrev;
//
bool isHKBearish = conditions.hkBar[cIndex].IsBearish();
bool isHKBearishPrev = conditions.hkBar[pIndex].IsBearish();
//
bool isHKSwitchedToBearish = isHKBearish &&
!isHKBearishPrev;
//
bool isSMHKBullish = conditions.smHKBar[cIndex].IsBullish();
bool isSMHKBullishPrev = conditions.smHKBar[pIndex].IsBullish();
//
bool isSMHKSwitchedToBullish = isSMHKBullish &&
!isSMHKBullishPrev;
//
bool isSMHKBearish = conditions.smHKBar[cIndex].IsBearish();
bool isSMHKBearishPrev = conditions.smHKBar[pIndex].IsBearish();
//
bool isSMHKSwitchedToBearish = isSMHKBearish &&
!isSMHKBearishPrev;
//
conditions.isHKBullish = isHKBullish;
conditions.isHKSwitchedToBullish = isHKSwitchedToBullish;
conditions.isHKBearish = isHKBearish;
conditions.isHKSwitchedToBearish = isHKSwitchedToBearish;
conditions.isSMHKBullish = isSMHKBullish;
conditions.isSMHKSwitchedToBullish = isSMHKSwitchedToBullish;
conditions.isSMHKBearish = isSMHKBearish;
conditions.isSMHKSwitchedToBearish = isSMHKSwitchedToBearish;
//
return result;
}
//
// Protected ...
protected:
//
// Private ...
private:
//
// Props ...
XHKInputs mInputs; // Inputs ...
//
// Buffers ...
double openBuffer[];
double highBuffer[];
double lowBuffer[];
double closeBuffer[];
double smOpenBuffer[];
double smHighBuffer[];
double smLowBuffer[];
double smCloseBuffer[];
//
// Tools ...
//
// Calculate Values Until Now ...
void Calculate()
{
//
int totalBars = CountBars();
//
// Open Buffer ...
CopyBuffer(
mHandler,
XHK_OPEN_LINE,
0,
totalBars,
openBuffer
//
);
//
// High Buffer ...
CopyBuffer(
mHandler,
XHK_HIGHLINE,
0,
totalBars,
highBuffer
//
);
//
// Low Buffer ...
CopyBuffer(
mHandler,
XHK_LOW_LINE,
0,
totalBars,
lowBuffer
//
);
//
// Close Buffer ...
CopyBuffer(
mHandler,
XHK_CLOSE_LINE,
0,
totalBars,
closeBuffer
//
);
//
// Smoothed ...
//
// Open Buffer ...
CopyBuffer(
mHandler,
XHK_SM_OPEN_LINE,
0,
totalBars,
smOpenBuffer
//
);
//
// High Buffer ...
CopyBuffer(
mHandler,
XHK_SM_HIGHLINE,
0,
totalBars,
smHighBuffer
//
);
//
// Low Buffer ...
CopyBuffer(
mHandler,
XHK_SM_LOW_LINE,
0,
totalBars,
smLowBuffer
//
);
//
// Close Buffer ...
CopyBuffer(
mHandler,
XHK_SM_CLOSE_LINE,
0,
totalBars,
smCloseBuffer
//
);
}
};
//
// Tools ...