Files
xMQL5/MQLTestWorkspace/BKPS/14030322/Classes/x-saherelm.xind.class.mq5
T

891 lines
14 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Class Library
// ----------------------------------------------
// Name: XSCBaseInd
// Description: provides all Indicator 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"
#include "../Libraries/x-saherelm.draw.lib.mq5"
#include "../Classes/x-saherelm.xalert.class.mq5"
//
// Definitions ...
//
struct XBuffer
{
//
string title; // Buffer Title
string symbol; // Buffer Symbol
double values[]; // Buffer Values
ENUM_TIMEFRAMES period; // Buffer Period
datetime times[]; // Buffer Values Time
//
// Constructor ...
XBuffer()
{
Clean();
}
//
// Tools ...
bool Init(
string mTitle, // Title
string mSymbol, // Symbol
ENUM_TIMEFRAMES mPeriod // TimeFrame
)
{
//
bool result = false;
//
result = IsValid(mTitle) &&
IsValid(mSymbol) &&
IsValid(mPeriod);
if (!result)
{
return result;
}
//
Clean();
//
this.symbol = mSymbol;
this.period = mPeriod;
//
result = StringToLower(mTitle);
if (!result)
{
return result;
}
//
this.title = mTitle;
//
result = IsValid();
//
return result;
}
//
// Cleanup ...
void Clean()
{
//
Clean(values);
Clean(times);
//
title = NULL;
symbol = NULL;
period = NULL;
}
//
// Validate ...
bool IsValid()
{
//
bool result = false;
//
result =
//
IsValid(title) &&
IsValid(period) &&
IsValid(symbol)
//
;
//
return result;
}
//
// Check Owwn ...
bool IsOwn(
string mTitle // Checking Title
)
{
//
bool result = false;
//
result = IsValid(mTitle);
if (!result)
{
return result;
}
//
result = StringToLower(mTitle);
if (!result)
{
return result;
}
//
result = this.title == mTitle;
//
return result;
}
//
// Count Size ...
int Count()
{
return ArraySize(values);
}
//
// Add Value to Buffer ...
int Add(
int index,
double value //
)
{
//
int result = -1;
//
if (!IsValid())
{
return result;
}
//
if (index > 0)
{
Add(value, values);
}
else
{
values[Count() - 1] = value;
}
//
result = Count();
//
return result;
}
//
// Get Specified Index Value ...
double Get(
int index // Specified Index
)
{
//
double result = EMPTY_VALUE;
//
NormalizeIndex(
index,
values //
);
//
result = values[index];
//
return result;
}
//
// Remove Specified Index Value ...
bool Remove(int index)
{
//
bool result = false;
//
int count = Count();
result = IsValidSize(count);
if (!result)
{
return result;
}
//
result = index > 0 && index < count - 1;
if (!result)
{
return result;
}
//
result = ArrayRemove(
values,
index,
1 //
);
//
return result;
}
};
//
class XSCBaseBufferClass : public XSCBaseAlert
{
//
// Public ...
public:
//
// Virtual ...
//
// Validate ...
virtual bool IsValid()
{
return false;
}
//
// Calculations ...
virtual void Calculate() {}
//
// Protected ...
protected:
//
// Props ...
//
// Indicators Buffers ...
XBuffer mBuffers[];
//
// Buffer Management ...
//
// Count Registered Buffers ...
int CountBuffers()
{
return ArraySize(mBuffers);
}
//
// Retrieve Specified Buffer Index ...
int GetBufferIndex(
string title // Specified Buffer Title
)
{
//
int result = -1;
//
if (!IsSpecifiedValid(title))
{
return result;
}
//
int buffersCount = CountBuffers();
if (!IsValidSize(buffersCount))
{
return result;
}
//
for (int i = 0; i < buffersCount; i++)
{
//
XBuffer iBuffer = mBuffers[i];
//
bool isOwn = iBuffer.IsOwn(title);
if (isOwn)
{
//
result = i;
break;
}
}
//
return result;
}
//
// Check Contains Specific Buffer or not ...
bool HasBuffer(
string title // Specified Buffer Title
)
{
//
bool result = false;
//
int bufferIndex = GetBufferIndex(title);
result = bufferIndex >= 0;
//
return result;
}
//
// Retrieve Specified Buffer ...
bool GetBuffer(
string title, // Specified Buffer Title
XBuffer &buffer // Hold Result
)
{
//
bool result = false;
//
int idx = GetBufferIndex(title);
result = idx >= 0;
if (!result)
{
return result;
}
//
buffer = mBuffers[idx];
//
result = buffer.IsValid();
//
return result;
}
//
// Register Specified Buffer ...
bool SetBuffer(
XBuffer &buffer // Specified Buffer for Registration
)
{
//
bool result = false;
//
// Validate buffer ...
result = buffer.IsValid();
if (!result)
{
return result;
}
//
// Check Buffer Doesn't Exists before ...
int idx = GetBufferIndex(buffer.title);
result = idx == -1;
if (!result)
{
return result;
}
//
AddRef(
buffer,
mBuffers //
);
//
return result;
}
//
double GetBufferValue(
string mTitle, // Required Buffer Title
int index // Bar Index
)
{
//
double result = EMPTY_VALUE;
//
// Validate State ...
if (!IsValid())
{
return result;
}
//
int bufferIDX = GetBufferIndex(mTitle);
if (bufferIDX < 0)
{
return result;
}
//
Calculate();
//
result = mBuffers[bufferIDX].Get(index);
//
return result;
}
//
int CopyBufferValue(
string mTitle,
double &buffer[],
int start,
int count //
)
{
//
int result = 0;
//
if (!IsValid())
{
return result;
}
//
int sourceIDX = GetBufferIndex(mTitle);
if (sourceIDX < 0)
{
return result;
}
//
Calculate();
//
result = ArrayCopy(
buffer,
mBuffers[sourceIDX].values,
start,
start,
count //
);
//
return result;
}
};
//
// Base Indicator Class ...
class XSCBaseInd : public XSCBaseAlert
{
//
// Public ...
public:
//
// Props ...
//
// Indicators Buffers ...
XBuffer mBuffers[];
//
// Constructor(s) ...
void XSCBaseInd(
string symbol, // Trading Symbol
ENUM_TIMEFRAMES period // Trading TimeFrame
) : XSCBaseAlert()
{
//
mSymbol = symbol;
mPeriod = period;
//
mCalculatedBars = 0;
}
//
// Deconstructor ...
void ~XSCBaseInd() {}
//
// Property Getter(s) / Setter(s) ...
//
string GetSymbol()
{
return mSymbol;
}
//
ENUM_TIMEFRAMES GetPeriod()
{
return mPeriod;
}
//
// Overrides ...
//
virtual string GetTag();
//
virtual string GetToken();
//
virtual void DeInit(int reason); // Indicator DeInitialization
//
// Validate Inputs ...
virtual bool IsValidInputs()
{
return false;
}
//
// Calculate ...
virtual void Calculate() {}
//
// Tools ...
//
datetime GetBarTime(
datetime time = NULL //
)
{
//
NormalizeTime(time);
//
datetime result = GetPeriodStartTime(
mSymbol,
mPeriod,
time //
);
//
return result;
}
//
int GetBarIndex(
datetime time = NULL //
)
{
//
datetime iBarTime = GetBarTime(time);
//
int result = iBarShift(
mSymbol,
mPeriod,
iBarTime //
);
//
return result;
}
//
// Retrieve Calculate Bars ...
int GetCalculatedBars()
{
return mCalculatedBars;
}
//
// Count available Bars ...
int CountBars()
{
//
return iBars(
mSymbol,
mPeriod //
);
}
//
// Validate Indicator States ...
bool IsValid()
{
//
bool result = false;
//
result = IsValidInputs();
if (!result)
{
return result;
}
//
result =
//
IsSpecifiedValid(mSymbol) &&
IsSpecifiedValid(mPeriod)
//
;
//
return result;
}
//
// Calculate Limit ...
// this means Required Bars for Calculation ...
int CountLimit()
{
//
int result = 0;
//
int totalBars = CountBars();
int calculatedBars = GetCalculatedBars();
result = MathAbs(totalBars - calculatedBars);
//
return result;
}
//
// Buffer Registrations ...
//
// Count Registered Buffers ...
int CountBuffers()
{
return ArraySize(mBuffers);
}
//
// Retrieve Specified Buffer Index ...
int GetBufferIndex(
string title // Specified Buffer Title
)
{
//
int result = -1;
//
if (!IsSpecifiedValid(title))
{
return result;
}
//
int buffersCount = CountBuffers();
if (!IsValidSize(buffersCount))
{
return result;
}
//
for (int i = 0; i < buffersCount; i++)
{
//
XBuffer iBuffer = mBuffers[i];
//
bool isOwn = iBuffer.IsOwn(title);
if (isOwn)
{
//
result = i;
break;
}
}
//
return result;
}
//
// Check Contains Specific Buffer or not ...
bool HasBuffer(
string title // Specified Buffer Title
)
{
//
bool result = false;
//
int bufferIndex = GetBufferIndex(title);
result = bufferIndex >= 0;
//
return result;
}
//
// Retrieve Specified Buffer ...
bool GetBuffer(
string title, // Specified Buffer Title
XBuffer &buffer // Hold Result
)
{
//
bool result = false;
//
int idx = GetBufferIndex(title);
result = idx >= 0;
if (!result)
{
return result;
}
//
buffer = mBuffers[idx];
//
result = buffer.IsValid();
//
return result;
}
//
// Register Specified Buffer ...
bool SetBuffer(
XBuffer &buffer // Specified Buffer for Registration
)
{
//
bool result = false;
//
// Validate buffer ...
result = buffer.IsValid();
if (!result)
{
return result;
}
//
// Check Buffer Doesn't Exists before ...
int idx = GetBufferIndex(buffer.title);
result = idx == -1;
if (!result)
{
return result;
}
//
AddRef(
buffer,
mBuffers //
);
//
return result;
}
//
double GetBufferValue(
string mTitle, // Required Buffer Title
int index // Bar Index
)
{
//
double result = EMPTY_VALUE;
//
// Validate State ...
if (!IsValid())
{
return result;
}
//
int bufferIDX = GetBufferIndex(mTitle);
if (bufferIDX < 0)
{
return result;
}
//
Calculate();
//
result = mBuffers[bufferIDX].Get(index);
//
return result;
}
//
int CopyBufferValue(
string mTitle,
double &buffer[],
int start,
int count //
)
{
//
int result = 0;
//
if (!IsValid())
{
return result;
}
//
int sourceIDX = GetBufferIndex(mTitle);
if (sourceIDX < 0)
{
return result;
}
//
Calculate();
//
result = ArrayCopy(
buffer,
mBuffers[sourceIDX].values,
start,
start,
count //
);
//
return result;
}
//
// Protected ...
protected:
//
// Props ...
//
string mSymbol; // Trading Symbol
ENUM_TIMEFRAMES mPeriod; // Trading TimeFrame
//
int mCalculatedBars; // Calculated Bars
//
// Tools ...
//
// Private ...
private:
//
// Props ...
};
//