1014 lines
18 KiB
Plaintext
1014 lines
18 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;
|
|
}
|
|
};
|
|
|
|
//
|
|
// 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;
|
|
}
|
|
|
|
//
|
|
// 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 ... |