684 lines
11 KiB
Plaintext
684 lines
11 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
|
|
)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
result = IsValid(mTitle);
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
Clean();
|
|
|
|
//
|
|
result = StringToLower(mTitle);
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
title = mTitle;
|
|
// ArraySetAsSeries(values, false);
|
|
|
|
//
|
|
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(double value)
|
|
{
|
|
//
|
|
int result = -1;
|
|
|
|
//
|
|
if (!IsValid())
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
datetime time = GetPeriodStartTime(
|
|
this.symbol,
|
|
this.period);
|
|
|
|
//
|
|
// Check Date Exists before ...
|
|
int timeIDX = FindIndex(
|
|
time,
|
|
times //
|
|
);
|
|
if (timeIDX >= 0)
|
|
{
|
|
//
|
|
// Item Exists ...
|
|
// Update it ...
|
|
|
|
//
|
|
values[timeIDX] = value;
|
|
}
|
|
else
|
|
{
|
|
//
|
|
// New Item ...
|
|
// Add It ...
|
|
|
|
//
|
|
int count = Count();
|
|
ArrayResize(
|
|
values,
|
|
count + 1 //
|
|
);
|
|
ArrayResize(
|
|
times,
|
|
count + 1 //
|
|
);
|
|
|
|
//
|
|
times[count] = time;
|
|
values[count] = 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;
|
|
}
|
|
|
|
//
|
|
double Get(
|
|
datetime time = NULL //
|
|
)
|
|
{
|
|
//
|
|
double result = EMPTY_VALUE;
|
|
|
|
//
|
|
NormalizeTime(time);
|
|
|
|
//
|
|
datetime iTime = GetPeriodStartTime(
|
|
this.symbol,
|
|
this.period,
|
|
time //
|
|
);
|
|
|
|
//
|
|
int timeIDX = FindIndex(
|
|
iTime,
|
|
this.times //
|
|
);
|
|
if (timeIDX < 0)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
result = Get(timeIDX);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
bool Remove(
|
|
datetime time = NULL //
|
|
)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
NormalizeTime(time);
|
|
|
|
//
|
|
datetime iTime = GetPeriodStartTime(
|
|
this.symbol,
|
|
this.period,
|
|
time //
|
|
);
|
|
|
|
//
|
|
int timeIDX = FindIndex(
|
|
iTime,
|
|
this.times //
|
|
);
|
|
if (timeIDX < 0)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
result = Remove(timeIDX);
|
|
|
|
//
|
|
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 ...
|
|
|
|
//
|
|
// 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;
|
|
}
|
|
|
|
//
|
|
// 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;
|
|
}
|
|
|
|
//
|
|
double GetBufferValue(
|
|
string mTitle, // Required Buffer Title
|
|
datetime time = NULL // Time
|
|
)
|
|
{
|
|
//
|
|
NormalizeTime(time);
|
|
double result = EMPTY_VALUE;
|
|
|
|
//
|
|
// Validate State ...
|
|
if (!IsValid())
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
int bufferIDX = GetBufferIndex(mTitle);
|
|
if (bufferIDX < 0)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
Calculate();
|
|
|
|
//
|
|
result = mBuffers[bufferIDX].Get(time);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Protected ...
|
|
protected:
|
|
//
|
|
// Props ...
|
|
|
|
//
|
|
string mSymbol; // Trading Symbol
|
|
ENUM_TIMEFRAMES mPeriod; // Trading TimeFrame
|
|
|
|
//
|
|
int mCalculatedBars; // Calculated Bars
|
|
|
|
//
|
|
// Tools ...
|
|
|
|
//
|
|
// Private ...
|
|
private:
|
|
//
|
|
// Props ...
|
|
};
|