This commit is contained in:
2024-05-30 06:35:09 +03:30
parent 34cfc745f5
commit 7fc6f52c51
6 changed files with 1020 additions and 56 deletions
+367
View File
@@ -0,0 +1,367 @@
///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Class Library
// ----------------------------------------------
// Name: XSCBaseInd
// Description: provides Indicator implementation
// 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.xind.class.mq5"
//
// XSCMid Indicator Buffers ...
enum ENUM_XSCMID_BUFFERS
{
XSCMID_MAIN_LINE = 0, // Main Buffer ...
XSCMID_STATE_LINE = 1, // State Buffer ...
};
//
// XSCMid Indicator Inputs ...
struct XSCMidInputs
{
//
// Props ...
//
int length; // Market Length ...
//
// Constructor ...
XSCMidInputs()
{
Clean();
}
//
// Tools ...
//
// Initial Inputs ...
bool Init(
int mLength // Market Length
)
{
//
bool result = false;
//
result =
//
mLength >= 2
//
;
if (!result)
{
return result;
}
//
this.length = mLength;
//
result = IsValid();
//
return result;
}
//
// Cleanup ...
void Clean()
{
//
this.length = 0;
}
//
// Default ...
void Default()
{
//
this.length = 2;
}
//
// Validate ...
bool IsValid()
{
//
bool result = false;
//
result =
//
length > 1
//
;
//
return result;
}
//
// Retrieve Max Length ...
int Max()
{
//
int result = 0;
//
result = MathMax(this.length, this.length);
//
return result;
}
};
//
// a Simple Indicator ...
class XSCMid : public XSCBaseInd
{
//
// Public ...
public:
//
// Props ...
//
// Constructor(s) ...
void XSCMid(
string symbol, // Trading Symbol
ENUM_TIMEFRAMES period // Trading TimeFrame
) : XSCBaseInd(symbol, period)
{
//
string token = GetTag();
//
SetAlertPrefix(token);
}
//
// Deconstructor ...
void ~XSCMid() {}
//
// Initialize Indicator ...
bool Init(
XSCMidInputs &inputs // Indicator Inputs ...
)
{
//
bool result = false;
//
result = inputs.IsValid();
if (!result)
{
return result;
}
//
// Set Inputs ...
this.mInputs = inputs;
//
// Validate Indicator State ...
result = this.IsValid();
if (!result)
{
return result;
}
//
// Register Required Buffers ...
XBuffer main;
result = main.Init(
"XSCMid",
false //
);
if (!result)
{
return result;
}
//
result = SetBuffer(main);
if (!result)
{
return result;
}
//
// Calculate First Time ...
Calculate();
//
return result;
}
//
// Property Getter(s) / Setter(s) ...
//
XSCMidInputs GetInputs()
{
return mInputs;
}
//
bool SetInputs(
XSCMidInputs &inputs // Indicator Inputs
)
{
return Init(inputs);
}
//
// Override(s) ...
//
// Get Tag ...
string GetTag() override
{
//
string result = NULL;
//
result = GetToken();
//
return result;
}
//
// Get Token ...
string GetToken() override
{
//
string result = NULL;
//
result = GetSpecificToken(this);
//
return result;
}
//
// Validate Inputs ...
bool IsValidInputs() override
{
return mInputs.IsValid();
}
//
// Calculate Buffers ...
void Calculate() override
{
//
int max = mInputs.Max();
int totalBars = CountBars();
int prevCalculated = GetCalculatedBars();
//
// Prepare Buffers Index ...
int mainIDX = GetBufferIndex("XSCMid");
//
int start = 0;
//
if (prevCalculated == 0)
{
//
if (max > 0)
{
for (int i = 0; i < max; i++)
{
//
for (int j = 0; j < CountBuffers(); j++)
{
mBuffers[j].Add(0);
}
}
}
}
else
{
start = prevCalculated - 1;
}
//
// Loop through Bars ...
for (int i = start; i < totalBars - 1 && !IsStopped(); i++)
{
//
XOHCL iBar;
iBar.Init(
mSymbol,
mPeriod,
i //
);
//
double iValue = iBar.close;
//
Print("BarIndex: " + ToString(i), ", iValue: ", iValue);
//
if (i > ArraySize(mBuffers[mainIDX].values) - 1)
{
ArrayResize(mBuffers[mainIDX].values, i + 1);
}
//
mBuffers[mainIDX].values[i] = iValue;
}
//
// Prevent Double Calculation Same Bars ...
mCalculatedBars = totalBars;
}
//
// De Initialize Class ...
void DeInit(int reason)
{
}
//
// Tools ...
//
// Reading Values ...
//
// Protected ...
protected:
//
// Private ...
private:
//
// Props ...
//
// Inputs ...
XSCMidInputs mInputs;
};
+2
View File
@@ -86,3 +86,5 @@ protected:
// Private ...
private:
};
//
@@ -164,7 +164,7 @@ struct XBuffer
}
//
ArraySetAsSeries(values, false);
ArraySetAsSeries(values, true);
//
int count = Count();
@@ -255,6 +255,39 @@ struct XBuffer
}
};
//
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
@@ -265,16 +298,44 @@ public:
//
// Props ...
//
// Indicators Buffers ...
XBuffer mBuffers[];
//
// Constructor(s) ...
void XSCBaseInd() : XSCBaseAlert()
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 ...
@@ -285,40 +346,7 @@ public:
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
virtual void DeInit(int reason); // Indicator DeInitialization
//
// Validate Inputs ...
@@ -329,10 +357,18 @@ public:
//
// Calculate ...
virtual void Calculate() {}
//
// Tools ...
//
// Retrieve Calculate Bars ...
int GetCalculatedBars()
{
return mCalculatedBars;
}
//
// Count available Bars ...
int CountBars()
@@ -359,27 +395,33 @@ public:
}
//
// TODO: Implement this ...
result =
//
IsSpecifiedValid(mSymbol) &&
IsSpecifiedValid(mPeriod)
//
;
//
return result;
}
//
// Protected ...
protected:
//
// Props ...
// Calculate Limit ...
// this means Required Bars for Calculation ...
int CalculateLimit()
{
//
int result = 0;
//
string mSymbol; // Trading Symbol
ENUM_TIMEFRAMES mPeriod; // Trading TimeFrame
//
int totalBars = CountBars();
int calculatedBars = GetCalculatedBars();
result = MathAbs(totalBars - calculatedBars);
//
int mCalculatedBars; // Calculated Bars
//
// Tools ...
//
return result;
}
//
// Buffer Registrations ...
@@ -401,7 +443,7 @@ protected:
int result = -1;
//
if (!IsValid(title))
if (!IsSpecifiedValid(title))
{
return result;
}
@@ -515,12 +557,277 @@ protected:
}
//
// Private ...
private:
int CopyBuffer(
string mTitle, // Required Buffer Title
double &buffer[], // Buffer Dest
int start = 0, // Start
int count = 0 // Count, if 0 Whole
)
{
//
int result = 0;
//
// Validate State ...
if (!IsValid())
{
return result;
}
//
// Clean(buffer);
//
int bufferIDX = GetBufferIndex(mTitle);
if (bufferIDX < 0)
{
return result;
}
//
Calculate();
//
for (int i = start; i < start + count - 1; i++)
{
//
result++;
buffer[i] = mBuffers[bufferIDX].values[i];
}
//
return result;
}
//
// Protected ...
protected:
//
// Props ...
//
// Indicators Buffers ...
XBuffer mBuffers[];
}
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++;
}
//