526 lines
8.4 KiB
Plaintext
526 lines
8.4 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
|
|
double values[]; // Buffer Values
|
|
bool asSeries; // As Series Flag
|
|
|
|
//
|
|
// Constructor ...
|
|
XBuffer()
|
|
{
|
|
Clean();
|
|
}
|
|
|
|
//
|
|
// Tools ...
|
|
bool Init(
|
|
string mTitle, // Title
|
|
bool mAsSeries = true // As Series Flag
|
|
)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
result = IsValid(mTitle);
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
Clean();
|
|
|
|
//
|
|
result = StringToLower(mTitle);
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
title = mTitle;
|
|
|
|
//
|
|
result = IsValid();
|
|
|
|
//
|
|
asSeries = mAsSeries;
|
|
ArraySetAsSeries(values, mAsSeries);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Cleanup ...
|
|
void Clean()
|
|
{
|
|
//
|
|
Clean(values);
|
|
|
|
//
|
|
title = NULL;
|
|
}
|
|
|
|
//
|
|
// Validate ...
|
|
bool IsValid()
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
result =
|
|
//
|
|
IsValid(title)
|
|
//
|
|
;
|
|
|
|
//
|
|
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;
|
|
}
|
|
|
|
//
|
|
ArraySetAsSeries(values, false);
|
|
|
|
//
|
|
int count = Count();
|
|
ArrayResize(
|
|
values,
|
|
count + 1 //
|
|
);
|
|
|
|
//
|
|
values[count] = value;
|
|
|
|
//
|
|
result = Count();
|
|
|
|
//
|
|
ArraySetAsSeries(values, asSeries);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Get Specified Index Value ...
|
|
double Get(
|
|
int index, // Specified Index
|
|
bool reverse = false // Retrieve Reversal Index Value
|
|
)
|
|
{
|
|
//
|
|
double result = EMPTY_VALUE;
|
|
|
|
//
|
|
NormalizeIndex(
|
|
index,
|
|
values //
|
|
);
|
|
|
|
//
|
|
if (reverse)
|
|
{
|
|
ArraySetAsSeries(values, !asSeries);
|
|
}
|
|
|
|
//
|
|
result = values[index];
|
|
|
|
//
|
|
if (reverse)
|
|
{
|
|
ArraySetAsSeries(values, asSeries);
|
|
}
|
|
|
|
//
|
|
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;
|
|
}
|
|
};
|
|
|
|
//
|
|
// Base Indicator Class ...
|
|
class XSCBaseInd : public XSCBaseAlert
|
|
{
|
|
//
|
|
// Public ...
|
|
public:
|
|
//
|
|
// Props ...
|
|
|
|
//
|
|
// Constructor(s) ...
|
|
void XSCBaseInd() : XSCBaseAlert()
|
|
{
|
|
}
|
|
|
|
//
|
|
// Deconstructor ...
|
|
void ~XSCBaseInd() {}
|
|
|
|
//
|
|
// Overrides ...
|
|
|
|
//
|
|
virtual string GetTag();
|
|
|
|
//
|
|
virtual string GetToken();
|
|
|
|
//
|
|
// Indicator Initializitation
|
|
virtual bool Init(
|
|
string symbol, // Trading Symbol
|
|
ENUM_TIMEFRAMES period // Trading TimeFrame
|
|
)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
result =
|
|
//
|
|
IsSpecifiedValid(symbol) &&
|
|
IsSpecifiedValid(period)
|
|
//
|
|
;
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
mSymbol = symbol;
|
|
mPeriod = period;
|
|
|
|
//
|
|
mCalculatedBars = 0;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
virtual bool DeInit(); // Indicator DeInitialization
|
|
|
|
//
|
|
// Validate Inputs ...
|
|
virtual bool IsValidInputs()
|
|
{
|
|
return false;
|
|
}
|
|
|
|
//
|
|
// Calculate ...
|
|
|
|
//
|
|
// Tools ...
|
|
|
|
//
|
|
// Count available Bars ...
|
|
int CountBars()
|
|
{
|
|
//
|
|
return iBars(
|
|
mSymbol,
|
|
mPeriod //
|
|
);
|
|
}
|
|
|
|
//
|
|
// Validate Indicator States ...
|
|
bool IsValid()
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
result = IsValidInputs();
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// TODO: Implement this ...
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Protected ...
|
|
protected:
|
|
//
|
|
// Props ...
|
|
|
|
//
|
|
string mSymbol; // Trading Symbol
|
|
ENUM_TIMEFRAMES mPeriod; // Trading TimeFrame
|
|
|
|
//
|
|
int mCalculatedBars; // Calculated Bars
|
|
|
|
//
|
|
// Tools ...
|
|
|
|
//
|
|
// 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 (!IsValid(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;
|
|
}
|
|
|
|
//
|
|
// Private ...
|
|
private:
|
|
//
|
|
// Props ...
|
|
|
|
//
|
|
// Indicators Buffers ...
|
|
XBuffer mBuffers[];
|
|
} |