812 lines
14 KiB
Plaintext
812 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
|
|
double values[]; // Buffer Values
|
|
|
|
//
|
|
// 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);
|
|
|
|
//
|
|
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;
|
|
}
|
|
|
|
//
|
|
int count = Count();
|
|
ArrayResize(
|
|
values,
|
|
count + 1 //
|
|
);
|
|
|
|
//
|
|
values[count] = value;
|
|
|
|
//
|
|
result = Count();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
void Set(
|
|
int index,
|
|
double value //
|
|
)
|
|
{
|
|
//
|
|
NormalizeIndex(index, values);
|
|
|
|
//
|
|
values[index] = value;
|
|
}
|
|
|
|
//
|
|
// 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;
|
|
}
|
|
};
|
|
|
|
//
|
|
struct XBufferPlotStyle
|
|
{
|
|
//
|
|
int width; // Width
|
|
color clr; // Color
|
|
uchar arrow; // Arrow Code
|
|
ENUM_DRAW_TYPE type; // Type
|
|
ENUM_LINE_STYLE style; // Style
|
|
|
|
//
|
|
// Constructor ...
|
|
XBufferPlotStyle()
|
|
{
|
|
Default();
|
|
}
|
|
|
|
//
|
|
// Tools ...
|
|
|
|
//
|
|
// Default ...
|
|
void Default()
|
|
{
|
|
//
|
|
width = 1;
|
|
arrow = 0;
|
|
clr = CLR_NONE;
|
|
type = DRAW_NONE;
|
|
style = STYLE_SOLID;
|
|
}
|
|
};
|
|
|
|
//
|
|
// 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;
|
|
}
|
|
|
|
//
|
|
// 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;
|
|
}
|
|
|
|
//
|
|
// Protected ...
|
|
protected:
|
|
//
|
|
// Props ...
|
|
|
|
//
|
|
string mSymbol; // Trading Symbol
|
|
ENUM_TIMEFRAMES mPeriod; // Trading TimeFrame
|
|
|
|
//
|
|
int mCalculatedBars; // Calculated Bars
|
|
|
|
//
|
|
// Tools ...
|
|
|
|
//
|
|
// Private ...
|
|
private:
|
|
//
|
|
// Props ...
|
|
};
|
|
|
|
//
|
|
// Tools ...
|
|
|
|
//
|
|
// Register a Buffer as Indexed Buffer ...
|
|
void SetIndexBuffer(
|
|
int &index, // the Index refrence for Buffer
|
|
double &buffer[], // Buffer to Set
|
|
XBufferPlotStyle &style, // Style to Apply to Buffer
|
|
double &colorBuffer[], // Color Buffer
|
|
string label = NULL, // Buffer Label
|
|
bool showData = true, // Show Data on Data Window
|
|
double empty = -1, // Set 0 or EMPTY_VALUE used for setting Empty Value
|
|
int drawBegin = -1 // if Set More Than -1 applied
|
|
)
|
|
{
|
|
//
|
|
// Set Buffer Index ...
|
|
SetIndexBuffer(index, buffer, INDICATOR_DATA);
|
|
|
|
//
|
|
// Show Date ...
|
|
PlotIndexSetInteger(index, PLOT_SHOW_DATA, showData);
|
|
|
|
//
|
|
// Set Buffer Label ...
|
|
if (IsValid(label))
|
|
{
|
|
PlotIndexSetString(index, PLOT_LABEL, label);
|
|
}
|
|
|
|
//
|
|
if (empty == 0 || empty == EMPTY_VALUE)
|
|
{
|
|
PlotIndexSetDouble(index, PLOT_EMPTY_VALUE, empty);
|
|
}
|
|
|
|
//
|
|
if (drawBegin > -1)
|
|
{
|
|
PlotIndexSetInteger(index, PLOT_DRAW_BEGIN, drawBegin);
|
|
}
|
|
|
|
//
|
|
if (style.type == DRAW_ARROW)
|
|
{
|
|
PlotIndexSetInteger(index, PLOT_ARROW, style.arrow);
|
|
}
|
|
|
|
//
|
|
PlotIndexSetInteger(index, PLOT_LINE_STYLE, style.style);
|
|
PlotIndexSetInteger(index, PLOT_LINE_WIDTH, style.width);
|
|
|
|
//
|
|
string drawTypeStr = EnumToString(style.type);
|
|
bool hasColorBuffer = Contains("COLOR", drawTypeStr);
|
|
|
|
//
|
|
if (!hasColorBuffer)
|
|
{
|
|
PlotIndexSetInteger(index, PLOT_LINE_COLOR, style.clr);
|
|
}
|
|
else
|
|
{
|
|
//
|
|
// Since Color Buffers must add as next Index of it's own Buffer
|
|
// here before applied Color Buffer increase index ...
|
|
index++;
|
|
|
|
//
|
|
SetIndexBuffer(index, colorBuffer, INDICATOR_COLOR_INDEX);
|
|
}
|
|
|
|
//
|
|
// Increase Index for Next Buffer ...
|
|
index++;
|
|
}
|
|
|
|
//
|
|
void SetIndexCandleBuffers(
|
|
int &index, // the Index refrence for Buffer
|
|
string openLabel, // Open Label
|
|
double &open[], // Open Buffer
|
|
string highLabel, // High Label
|
|
double &high[], // High Buffer
|
|
string lowLabel, // Low Label
|
|
double &low[], // Low Buffer
|
|
string closeLabel, // Close Label
|
|
double &close[], // Close Buffer
|
|
bool showData = true, // Show Data on Data Window
|
|
double empty = -1, // Set 0 or EMPTY_VALUE used for setting Empty Value
|
|
int drawBegin = -1 // if Set More Than -1 applied
|
|
)
|
|
{
|
|
//
|
|
bool hasLabel =
|
|
//
|
|
IsValid(openLabel) &&
|
|
IsValid(highLabel) &&
|
|
IsValid(lowLabel) &&
|
|
IsValid(closeLabel)
|
|
//
|
|
;
|
|
|
|
//
|
|
string label =
|
|
!hasLabel
|
|
? NULL
|
|
: openLabel + ";" + highLabel + ";" + lowLabel + ";" + closeLabel;
|
|
|
|
//
|
|
// Set Buffer Index ...
|
|
SetIndexBuffer(index, open, INDICATOR_DATA);
|
|
|
|
//
|
|
// Set Draw Candles Type ...
|
|
PlotIndexSetInteger(index, PLOT_LINE_STYLE, DRAW_CANDLES);
|
|
|
|
//
|
|
// Show Date ...
|
|
PlotIndexSetInteger(index, PLOT_SHOW_DATA, showData);
|
|
|
|
//
|
|
// Set Buffer Label ...
|
|
if (IsValid(label))
|
|
{
|
|
PlotIndexSetString(index, PLOT_LABEL, label);
|
|
}
|
|
|
|
//
|
|
if (empty == 0 || empty == EMPTY_VALUE)
|
|
{
|
|
PlotIndexSetDouble(index, PLOT_EMPTY_VALUE, empty);
|
|
}
|
|
|
|
//
|
|
if (drawBegin > -1)
|
|
{
|
|
PlotIndexSetInteger(index, PLOT_DRAW_BEGIN, drawBegin);
|
|
}
|
|
|
|
//
|
|
index++;
|
|
|
|
//
|
|
// Set Buffer Index ...
|
|
SetIndexBuffer(index, high, INDICATOR_DATA);
|
|
index++;
|
|
|
|
//
|
|
// Set Buffer Index ...
|
|
SetIndexBuffer(index, low, INDICATOR_DATA);
|
|
index++;
|
|
|
|
//
|
|
// Set Buffer Index ...
|
|
SetIndexBuffer(index, close, INDICATOR_DATA);
|
|
index++;
|
|
}
|
|
|
|
//
|
|
void SetIndexCandleBuffers(
|
|
int &index, // the Index refrence for Buffer
|
|
string openLabel, // Open Label
|
|
double &open[], // Open Buffer
|
|
string highLabel, // High Label
|
|
double &high[], // High Buffer
|
|
string lowLabel, // Low Label
|
|
double &low[], // Low Buffer
|
|
string closeLabel, // Close Label
|
|
double &close[], // Close Buffer
|
|
double &colorBuffer[], // Candle Color Buffer
|
|
bool showData = true, // Show Data on Data Window
|
|
double empty = -1, // Set 0 or EMPTY_VALUE used for setting Empty Value
|
|
int drawBegin = -1 // if Set More Than -1 applied
|
|
)
|
|
{
|
|
//
|
|
SetIndexCandleBuffers(
|
|
index,
|
|
openLabel,
|
|
open,
|
|
highLabel,
|
|
high,
|
|
lowLabel,
|
|
low,
|
|
closeLabel,
|
|
close,
|
|
showData,
|
|
empty,
|
|
drawBegin //
|
|
);
|
|
|
|
//
|
|
int mFirstIndex = index - 4;
|
|
|
|
//
|
|
// Set Draw Candles Type ...
|
|
PlotIndexSetInteger(mFirstIndex, PLOT_LINE_STYLE, DRAW_COLOR_CANDLES);
|
|
|
|
//
|
|
// Set Color Buffer ...
|
|
SetIndexBuffer(index, colorBuffer, INDICATOR_COLOR_INDEX);
|
|
|
|
//
|
|
index++;
|
|
}
|
|
|
|
// |