Files
MQL5Data/Helpers/x-saherelm.x3ma.helper.mq5
T
2024-11-17 06:46:34 +03:30

758 lines
14 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Helper Class Library
// ----------------------------------------------
// Name: XSCX3MAHelper
// Description: provides all Indicator
// Helper 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.x-helper.class.mq5"
//
// Definitions ...
//
enum ENUM_X3MA_BUFFERS
{
//
X3MA_FAST_LINE = 0,
X3MA_MID_LINE = 1,
X3MA_SLOW_LINE = 2,
};
//
// Input Models ...
struct X3MAInputs
{
//
// Props ...
//
// Market ...
int fastLength; // Fast Length
int midLength; // Mid Length
int slowLength; // Slow Length
int shiftLength; // Shift
ENUM_MA_METHOD method; // Mode
ENUM_APPLIED_PRICE appliedTo; // Applied To
//
// Presentation ...
//
bool showFast; // Show Fast
bool showMid; // Show Mid
bool showSlow; // Show Slow
//
// Constructor(s) ...
X3MAInputs()
{
//
Clean();
}
//
// Tools ...
//
// Clean ...
void Clean()
{
//
fastLength = 0;
midLength = 0;
slowLength = 0;
shiftLength = 0;
method = MODE_SMA;
appliedTo = PRICE_CLOSE;
//
showFast = false;
showMid = false;
showSlow = false;
}
//
// Default ...
void Default()
{
//
fastLength = 50;
midLength = 100;
slowLength = 200;
shiftLength = 0;
method = MODE_SMA;
appliedTo = PRICE_CLOSE;
//
showFast = true;
showMid = true;
showSlow = true;
}
//
// Validate ...
bool IsValid()
{
//
bool result = false;
//
result =
//
fastLength > 2 &&
midLength > fastLength &&
slowLength > midLength &&
//
shiftLength >= 0
//
;
//
return result;
}
//
// Retrieve MAx Length ...
int Max()
{
//
int result = 0;
//
result = MathMax(fastLength, midLength);
result = MathMax(result, slowLength);
//
return result;
}
};
//
// Define Conditions ...
struct X3MAConditions
{
//
// Common ...
string symbol;
ENUM_TIMEFRAMES period;
datetime time;
//
// Buffers ...
//
double fasts[];
double mids[];
double slows[];
//
// Conditions ...
//
bool isFastOverMid;
bool isMidOverSlow;
//
bool isFastUnderMid;
bool isMidUnderSlow;
//
bool isBullishOrdered;
bool isBearishOrdered;
//
bool isSwitchedToBullishOrdered;
bool isSwitchedToBearishOrdered;
//
void X3MAConditions()
{
Clean();
}
//
void Clean()
{
//
Clean(fasts);
Clean(mids);
Clean(slows);
//
ArraySetAsSeries(fasts, true);
ArraySetAsSeries(mids, true);
ArraySetAsSeries(slows, true);
//
isFastOverMid = false;
isMidOverSlow = false;
isFastUnderMid = false;
isMidUnderSlow = false;
isBullishOrdered = false;
isBearishOrdered = false;
isSwitchedToBullishOrdered = false;
isSwitchedToBearishOrdered = false;
}
//
void GenerateScore(
double &bullishScore,
double &bearishScore //
)
{
//
bullishScore = 0;
bearishScore = 0;
//
// Since We have to Use this
// Only for Filtering Signals ...
// We Can Ignore Scoring ...
// or Complete this later ...
}
//
string GenerateSummary(
bool onlyCommons = false,
bool onlyConditions = false,
bool includeScores = true,
bool ignoreFalseConditions = true,
string separator = "\n" //
)
{
//
string result = NULL;
//
double bullishScore = 0;
double bearishScore = 0;
GenerateScore(
bullishScore,
bearishScore //
);
//
string scoresStr =
//
"Scores: " + separator +
"---------------" + separator +
"Bullish: " + ToString(bullishScore) + separator +
"Bearish: " + ToString(bearishScore) + separator +
""
//
;
//
string commonStr = GenerateSpecifiedCommonSummary(
this,
separator,
includeScores //
);
//
string conditionsStr =
//
""
//
;
//
result =
//
"[" + GetTag() + "]" + separator +
(onlyConditions
? ""
: commonStr) +
(!includeScores
? ""
: scoresStr) +
" " + separator +
(onlyCommons
? ""
: conditionsStr) +
""
//
;
//
return result;
}
//
string GetTag()
{
return "X3MA";
}
};
//
// Class ...
class XCX3MAHelper : public XCBaseHelper
{
//
// Public ...
public:
//
// Props ...
//
// Constructors ...
XCX3MAHelper()
: XCBaseHelper(_Symbol, _Period)
{
}
//
// Deconstructor ...
~XCX3MAHelper()
{
//
Clean(fastBuffer);
Clean(midBuffer);
Clean(slowBuffer);
}
//
// Tools ...
bool Init(
string symbol, // Trading Symbol
ENUM_TIMEFRAMES period, // Trading Period
X3MAInputs &inputs // Inputs
)
{
//
bool result = false;
//
mSymbol = symbol;
mPeriod = period;
//
result = inputs.IsValid();
if (!result)
{
return result;
}
//
mInputs = inputs;
//
ArraySetAsSeries(fastBuffer, true);
ArraySetAsSeries(midBuffer, true);
ArraySetAsSeries(slowBuffer, true);
//
mHandler = iCustom(
mSymbol,
mPeriod,
"x-saherelm.x3ma",
//
// Inputs ...
//
// Market ...
"",
mInputs.fastLength,
mInputs.midLength,
mInputs.slowLength,
mInputs.shiftLength,
mInputs.method,
mInputs.appliedTo,
//
// Presentation ...
"",
//
mInputs.showFast,
mInputs.showMid,
mInputs.showSlow
//
);
result = mHandler != INVALID_HANDLE;
if (!result)
{
return result;
}
//
return result;
}
//
// Inputs ...
//
X3MAInputs GetInputs()
{
return mInputs;
}
//
bool SetInputs(
X3MAInputs &inputs // Configs
)
{
//
return Init(
mSymbol,
mPeriod,
inputs
//
);
}
//
// Buffers ...
//
double GetFast(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
int count = ArraySize(fastBuffer);
if (barIndex >= count)
{
barIndex = count - 1;
}
//
return fastBuffer[barIndex];
}
//
// Copy Required Items ...
int CopyFast(
int start, // Start
int count, // Number of Items for read
double &buffer[], // Dest Buffer
bool forceClean = true // Force To Clean buffer
)
{
//
// Update Calculations ...
Calculate();
//
// Copy Items ...
return Copy(
start,
count,
fastBuffer,
buffer,
forceClean
//
);
}
//
double GetMid(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
int count = ArraySize(midBuffer);
if (barIndex >= count)
{
barIndex = count - 1;
}
//
return midBuffer[barIndex];
}
//
// Copy Required Items ...
int CopyMid(
int start, // Start
int count, // Number of Items for read
double &buffer[], // Dest Buffer
bool forceClean = true // Force To Clean buffer
)
{
//
// Update Calculations ...
Calculate();
//
// Copy Items ...
return Copy(
start,
count,
midBuffer,
buffer,
forceClean
//
);
}
//
double GetSlow(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
int count = ArraySize(slowBuffer);
if (barIndex >= count)
{
barIndex = count - 1;
}
//
return slowBuffer[barIndex];
}
//
// Copy Required Items ...
int CopySlow(
int start, // Start
int count, // Number of Items for read
double &buffer[], // Dest Buffer
bool forceClean = true // Force To Clean buffer
)
{
//
// Update Calculations ...
Calculate();
//
// Copy Items ...
return Copy(
start,
count,
slowBuffer,
buffer,
forceClean
//
);
}
//
// Conditions ...
//
bool GetConditions(
X3MAConditions &conditions, //
int barIndex = 0, //
int loopback = 5 //
)
{
//
bool result = true;
//
if (loopback < 3)
{
loopback = 3;
}
//
conditions.Clean();
//
conditions.symbol = mSymbol;
conditions.period = mPeriod;
conditions.time = TimeCurrent();
//
int zIndex = barIndex;
int cIndex = zIndex + 1;
int pIndex = cIndex + 1;
int ppIndex = pIndex + 1;
//
// Buffers ...
//
// Fast ...
CopyFast(
zIndex,
loopback,
conditions.fasts //
);
//
// Mid ...
CopyMid(
zIndex,
loopback,
conditions.mids //
);
//
// Slow ...
CopySlow(
zIndex,
loopback,
conditions.slows //
);
//
// Conditions ...
//
bool isFastOverMid =
conditions.fasts[cIndex] > conditions.mids[cIndex];
bool isFastOverMidPrev =
conditions.fasts[pIndex] > conditions.mids[pIndex];
//
bool isMidOverSlow =
conditions.mids[cIndex] > conditions.slows[cIndex];
bool isMidOverSlowPrev =
conditions.mids[pIndex] > conditions.slows[pIndex];
//
bool isFastUnderMid =
conditions.fasts[cIndex] < conditions.mids[cIndex];
bool isFastUnderMidPrev =
conditions.fasts[pIndex] < conditions.mids[pIndex];
//
bool isMidUnderSlow =
conditions.mids[cIndex] < conditions.slows[cIndex];
bool isMidUnderSlowPrev =
conditions.mids[pIndex] < conditions.slows[pIndex];
//
bool isBullishOrdered = isFastOverMid &&
isMidOverSlow;
bool isBullishOrderedPrev = isFastOverMidPrev &&
isMidOverSlowPrev;
//
bool isBearishOrdered = isFastUnderMid &&
isMidUnderSlow;
bool isBearishOrderedPrev = isFastUnderMidPrev &&
isMidUnderSlowPrev;
//
bool isSwitchedToBullishOrdered = isBullishOrdered &&
!isBullishOrderedPrev;
bool isSwitchedToBearishOrdered = isBearishOrdered &&
!isBearishOrderedPrev;
//
conditions.isFastOverMid = isFastOverMid;
conditions.isMidOverSlow = isMidOverSlow;
conditions.isFastUnderMid = isFastUnderMid;
conditions.isMidUnderSlow = isMidUnderSlow;
conditions.isBullishOrdered = isBullishOrdered;
conditions.isBearishOrdered = isBearishOrdered;
conditions.isSwitchedToBullishOrdered = isSwitchedToBullishOrdered;
conditions.isSwitchedToBearishOrdered = isSwitchedToBearishOrdered;
//
return result;
}
//
// Protected ...
protected:
//
// Private ...
private:
//
// Props ...
X3MAInputs mInputs; // Inputs ...
//
// Buffers ...
double fastBuffer[];
double midBuffer[];
double slowBuffer[];
//
// Tools ...
//
// Calculate Values Until Now ...
void Calculate()
{
//
int totalBars = CountBars();
if (totalBars > 1000)
{
totalBars = 1000;
}
//
// Fast ...
CopyBuffer(
mHandler,
X3MA_FAST_LINE,
0,
totalBars,
fastBuffer
//
);
//
// Mid ...
CopyBuffer(
mHandler,
X3MA_MID_LINE,
0,
totalBars,
midBuffer
//
);
//
// Slow ...
CopyBuffer(
mHandler,
X3MA_SLOW_LINE,
0,
totalBars,
slowBuffer
//
);
}
};
//