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

404 lines
7.1 KiB
Plaintext

///////////////////////////////////////////////////////
//
// 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 ...
};
//
string GetTitle(ENUM_XSCMID_BUFFERS bufferLine)
{
//
string result = NULL;
//
switch (bufferLine)
{
//
case XSCMID_MAIN_LINE:
result = "XMID";
break;
//
case XSCMID_STATE_LINE:
result = "XSTATE";
break;
}
//
return result;
}
//
// XSCMid Indicator Inputs ...
struct XSCMidInputs
{
//
// Props ...
string version;
//
// Constructor ...
XSCMidInputs()
{
Clean();
}
//
// Tools ...
//
// Initial Inputs ...
bool Init()
{
//
bool result = false;
//
result = IsValid();
//
return result;
}
//
// Cleanup ...
void Clean()
{
}
//
// Default ...
void Default()
{
}
//
// Validate ...
bool IsValid()
{
//
bool result = false;
//
result =
//
true
//
;
//
return result;
}
//
// Retrieve Max Length ...
int Max()
{
//
int result = 0;
//
result = 0;
//
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)
{
//
mSymbol = symbol;
mPeriod = period;
//
mCalculatedBars = 0;
}
//
// 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;
string mTitle = GetTitle(XSCMID_MAIN_LINE);
result = main.Init(
mTitle,
mSymbol,
mPeriod //
);
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 limit = CountLimit();
int totalBars = CountBars();
int prevCalculated = GetCalculatedBars();
//
// Prepare Buffers Index ...
int mainIDX = GetBufferIndexByLine(XSCMID_MAIN_LINE);
//
// Print("Total: ", totalBars, ", prevCalculated: ", prevCalculated, ", Limit: ", limit);
//
// Only Calculate Last Item ...
bool onlyLast = false;
if (prevCalculated == totalBars - 1)
{
onlyLast = true;
}
//
int start = totalBars - prevCalculated - 1;
for (int i = start; i >= 0; i--)
{
//
int barIndex = i - 1;
// Print("barIndex: ", barIndex, ", onlyLast: ", onlyLast);
if (barIndex < 0 && !onlyLast)
{
continue;
}
//
if (onlyLast)
{
barIndex = 0;
}
//
datetime iBarTime = iTime(
mSymbol,
mPeriod,
barIndex - 1
//
);
int iBarIndex = iBarShift(
mSymbol,
mPeriod,
iBarTime //
);
//
double iValue = iClose(mSymbol, mPeriod, iBarIndex);
//
Print("Bar: ", barIndex, ", Bar Time: ", iBarTime, ", Bar Index: ", iBarIndex, ", iValue: ", iValue);
if (iValue != 0.0)
{
//
// Print("Bar: ", barIndex, ", Bar Time: ", iBarTime, ", Bar Index: ", iBarIndex, ", iValue: ", iValue);
mBuffers[mainIDX].Add(barIndex, iValue);
}
}
//
// Prevent Double Calculation Same Bars ...
mCalculatedBars = totalBars - 1;
}
//
// De Initialize Class ...
void DeInit(int reason)
{
}
//
// Tools ...
//
int GetBufferIndexByLine(
ENUM_XSCMID_BUFFERS bufferLine // Specified Buffer Line
)
{
//
string mTitle = GetTitle(bufferLine);
return GetBufferIndex(mTitle);
}
//
bool GetBufferByLine(
ENUM_XSCMID_BUFFERS bufferLine, // Specified Buffer Line
XBuffer &buffer // Hold Result
)
{
//
string mTitle = GetTitle(bufferLine);
return GetBuffer(mTitle, buffer);
}
// //
// double GetBufferValueByLine(
// ENUM_XSCMID_BUFFERS bufferLine, // Specified Buffer Line
// datetime time = NULL // Time
// )
// {
// //
// string mTitle = GetTitle(bufferLine);
// return GetBufferValue(mTitle, time);
// }
//
// Protected ...
protected:
//
// Private ...
private:
//
// Props ...
//
// Inputs ...
XSCMidInputs mInputs;
};