Files
xMQL5/X121-EA/V1.0/Helpers/x-saherelm.xchswings.helper.mq5
T
2024-10-11 19:46:23 +03:30

1423 lines
26 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Helper Class Library
// ----------------------------------------------
// Name: XSCXCHSwingsHelper
// 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.xhelper.class.mq5"
//
// Definitions ...
//
enum ENUM_XCHSwings_BUFFERS
{
//
// Current ...
XCHSwings_C_SH_LINE = 0,
XCHSwings_C_SL_LINE = 1,
//
// Short ...
XCHSwings_S_SH_LINE = 2,
XCHSwings_S_SL_LINE = 3,
//
// Medium ...
XCHSwings_M_SH_LINE = 4,
XCHSwings_M_SL_LINE = 5,
//
// Long ...
XCHSwings_L_SH_LINE = 6,
XCHSwings_L_SL_LINE = 7,
//
// Hind ...
XCHSwings_H_SH_LINE = 8,
XCHSwings_H_SL_LINE = 9,
};
//
// Input Models ...
struct XCHSwingsInputs
{
//
// Props ...
//
// Market ...
int length; // Length
//
// Cycles ...
//
// Short ...
ENUM_X_PERIOD_METHOD scMethod; // How to Find Period
ENUM_TIMEFRAMES scPeriod; // Time Period
//
// Medium ...
ENUM_X_PERIOD_METHOD mcMethod; // How to Find Period
ENUM_TIMEFRAMES mcPeriod; // Time Period
//
// Long ...
ENUM_X_PERIOD_METHOD lcMethod; // How to Find Period
ENUM_TIMEFRAMES lcPeriod; // Time Period
//
// Hind ...
ENUM_X_PERIOD_METHOD hcMethod; // How to Find Period
ENUM_TIMEFRAMES hcPeriod; // Time Period
//
// Presentation ...
bool showSwingHighs; // Show Swing Highs
bool showSwingLows; // Show Swing Lows
bool showCurrent; // Show Current Cycle
bool showShort; // Show Short Cycle
bool showMedium; // Show Medium Cycle
bool showLong; // Show Long Cycle
bool showHind; // Show Hind Cycle
//
// Constructor(s) ...
XCHSwingsInputs()
{
//
Clean();
}
//
// Tools ...
//
// Clean ...
void Clean()
{
//
length = 0;
//
scMethod = X_PERIOD_AUTO;
scPeriod = NULL;
mcMethod = X_PERIOD_AUTO;
mcPeriod = NULL;
lcMethod = X_PERIOD_AUTO;
lcPeriod = NULL;
hcMethod = X_PERIOD_AUTO;
hcPeriod = NULL;
//
showSwingHighs = false;
showSwingLows = false;
showCurrent = false;
showShort = false;
showMedium = false;
showLong = false;
showHind = false;
}
//
// Default ...
void Default()
{
//
length = 14;
//
scMethod = X_PERIOD_AUTO;
scPeriod = NULL;
mcMethod = X_PERIOD_AUTO;
mcPeriod = NULL;
lcMethod = X_PERIOD_AUTO;
lcPeriod = NULL;
hcMethod = X_PERIOD_AUTO;
hcPeriod = NULL;
//
showSwingHighs = true;
showSwingLows = true;
showCurrent = true;
showShort = true;
showMedium = true;
showLong = true;
showHind = true;
}
//
// Validate ...
bool IsValid()
{
//
bool result = false;
//
result =
//
length > 2 &&
//
(IsValid(scMethod, scPeriod) &&
IsValid(mcMethod, mcPeriod) &&
IsValid(lcMethod, lcPeriod) &&
IsValid(hcMethod, hcPeriod))
//
;
//
return result;
}
//
// Retrieve MAx Length ...
int Max()
{
//
int result = 0;
//
result =
//
MathMax(0, length)
//
;
//
return result;
}
};
//
// Define Conditions ...
struct XCHSwingsConditions
{
//
// Common ...
string symbol;
ENUM_TIMEFRAMES period;
datetime time;
//
// Buffers ...
//
// Current ...
double cSH[];
double cSL[];
//
// Short ...
double sSH[];
double sSL[];
//
// Medium ...
double mSH[];
double mSL[];
//
// Long ...
double lSH[];
double lSL[];
//
// Hind ...
double hSH[];
double hSL[];
//
// Conditions ...
//
// Current ...
bool isCSwingHigh;
bool isCSwingLow;
//
// Short ...
bool isSSwingHigh;
bool isSSwingLow;
//
// Medium ...
bool isMSwingHigh;
bool isMSwingLow;
//
// Long ...
bool isLSwingHigh;
bool isLSwingLow;
//
// Hind ...
bool isHSwingHigh;
bool isHSwingLow;
//
void Clean()
{
//
Clean(cSH);
Clean(cSL);
Clean(sSH);
Clean(sSL);
Clean(mSH);
Clean(mSL);
Clean(lSH);
Clean(lSL);
Clean(hSH);
Clean(hSL);
//
ArraySetAsSeries(cSH, true);
ArraySetAsSeries(cSL, true);
ArraySetAsSeries(sSH, true);
ArraySetAsSeries(sSL, true);
ArraySetAsSeries(mSH, true);
ArraySetAsSeries(mSL, true);
ArraySetAsSeries(lSH, true);
ArraySetAsSeries(lSL, true);
ArraySetAsSeries(hSH, true);
ArraySetAsSeries(hSL, true);
//
// Conditions ...
//
// Current ...
isCSwingHigh = false;
isCSwingLow = false;
//
// Short ...
isSSwingHigh = false;
isSSwingLow = false;
//
// Medium ...
isMSwingHigh = false;
isMSwingLow = false;
//
// Long ...
isLSwingHigh = false;
isLSwingLow = false;
//
// Hind ...
isHSwingHigh = false;
isHSwingLow = 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 ...
//
// Conditions ...
//
// Current ...
//
// Short ...
//
// Medium ...
//
// Long ...
//
// Hind ...
}
//
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 =
//
// Current ...
ToString("isCSwingHigh", isCSwingHigh, ignoreFalseConditions, separator) +
ToString("isCSwingLow", isCSwingLow, ignoreFalseConditions, separator) +
//
// Short ...
ToString("isSSwingHigh", isSSwingHigh, ignoreFalseConditions, separator) +
ToString("isSSwingLow", isSSwingLow, ignoreFalseConditions, separator) +
//
// Medium ...
ToString("isMSwingHigh", isMSwingHigh, ignoreFalseConditions, separator) +
ToString("isMSwingLow", isMSwingLow, ignoreFalseConditions, separator) +
//
// Long ...
ToString("isLSwingHigh", isLSwingHigh, ignoreFalseConditions, separator) +
ToString("isLSwingLow", isLSwingLow, ignoreFalseConditions, separator) +
//
// Hind ...
ToString("isHSwingHigh", isHSwingHigh, ignoreFalseConditions, separator) +
ToString("isHSwingLow", isHSwingLow, ignoreFalseConditions, separator) +
//
""
//
;
//
result =
//
"[" + GetTag() + "]" + separator +
(onlyConditions
? ""
: commonStr) +
(!includeScores
? ""
: scoresStr) +
" " + separator +
(onlyCommons
? ""
: conditionsStr) +
""
//
;
//
return result;
}
//
string GetTag()
{
return "XCHSwings";
}
};
//
// Class ...
class XSCXCHSwingsHelper : public XSCBaseHelper
{
//
// Public ...
public:
//
// Props ...
//
// Constructors ...
XSCXCHSwingsHelper() : XSCBaseHelper(_Symbol, _Period)
{
}
//
// Deconstructor ...
~XSCXCHSwingsHelper()
{
//
Clean(cSHBuffer);
Clean(cSLBuffer);
Clean(sSHBuffer);
Clean(sSLBuffer);
Clean(mSHBuffer);
Clean(mSLBuffer);
Clean(lSHBuffer);
Clean(lSLBuffer);
Clean(hSHBuffer);
Clean(hSLBuffer);
}
//
// Tools ...
bool Init(
string symbol, // Trading Symbol
ENUM_TIMEFRAMES period, // Trading Period
XCHSwingsInputs &inputs // Inputs
)
{
//
bool result = false;
//
mSymbol = symbol;
mPeriod = period;
//
result = inputs.IsValid();
if (!result)
{
return result;
}
//
mInputs = inputs;
//
ArraySetAsSeries(cSHBuffer, true);
ArraySetAsSeries(cSLBuffer, true);
ArraySetAsSeries(sSHBuffer, true);
ArraySetAsSeries(sSLBuffer, true);
ArraySetAsSeries(mSHBuffer, true);
ArraySetAsSeries(mSLBuffer, true);
ArraySetAsSeries(lSHBuffer, true);
ArraySetAsSeries(lSLBuffer, true);
ArraySetAsSeries(hSHBuffer, true);
ArraySetAsSeries(hSLBuffer, true);
//
mHandler = iCustom(
mSymbol,
mPeriod,
"x-saherelm.xchswings",
//
// Inputs ...
//
// Market ...
"",
mInputs.length, // Length
//
// Cycles ...
"",
//
// Short ...
"",
mInputs.scMethod,
mInputs.scPeriod,
//
// Medium ...
"",
mInputs.mcMethod,
mInputs.mcPeriod,
//
// Long ...
"",
mInputs.lcMethod,
mInputs.lcPeriod,
//
// Hind ...
"",
mInputs.hcMethod,
mInputs.hcPeriod,
//
// Presentation ...
"",
mInputs.showSwingHighs,
mInputs.showSwingLows,
mInputs.showCurrent,
mInputs.showShort,
mInputs.showMedium,
mInputs.showLong,
mInputs.showHind
//
);
result = mHandler != INVALID_HANDLE;
if (!result)
{
return result;
}
//
return result;
}
//
// Inputs ...
//
XCHSwingsInputs GetInputs()
{
return mInputs;
}
//
bool SetInputs(
XCHSwingsInputs &inputs // Configs
)
{
//
return Init(
mSymbol,
mPeriod,
inputs
//
);
}
//
// Buffers ...
//
// Current ...
//
double GetCSH(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
return cSHBuffer[barIndex];
}
//
// Copy Required Buffer ...
int CopyCSH(
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,
cSHBuffer,
buffer,
forceClean
//
);
}
//
double GetCSL(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
return cSLBuffer[barIndex];
}
//
// Copy Required Buffer ...
int CopyCSL(
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,
cSLBuffer,
buffer,
forceClean
//
);
}
//
// Short ...
//
double GetSSH(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
return sSHBuffer[barIndex];
}
//
// Copy Required Buffer ...
int CopySSH(
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,
sSHBuffer,
buffer,
forceClean
//
);
}
//
double GetSSL(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
return sSLBuffer[barIndex];
}
//
// Copy Required Buffer ...
int CopySSL(
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,
sSLBuffer,
buffer,
forceClean
//
);
}
//
// Medium ...
//
double GetMSH(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
return mSHBuffer[barIndex];
}
//
// Copy Required Buffer ...
int CopyMSH(
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,
mSHBuffer,
buffer,
forceClean
//
);
}
//
double GetMSL(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
return mSLBuffer[barIndex];
}
//
// Copy Required Buffer ...
int CopyMSL(
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,
mSLBuffer,
buffer,
forceClean
//
);
}
//
// Long ...
//
double GetLSH(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
return lSHBuffer[barIndex];
}
//
// Copy Required Buffer ...
int CopyLSH(
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,
lSHBuffer,
buffer,
forceClean
//
);
}
//
double GetLSL(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
return lSLBuffer[barIndex];
}
//
// Copy Required Buffer ...
int CopyLSL(
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,
lSLBuffer,
buffer,
forceClean
//
);
}
//
// Hind ...
//
double GetHSH(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
return hSHBuffer[barIndex];
}
//
// Copy Required Buffer ...
int CopyHSH(
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,
hSHBuffer,
buffer,
forceClean
//
);
}
//
double GetHSL(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
return hSLBuffer[barIndex];
}
//
// Copy Required Buffer ...
int CopyHSL(
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,
hSLBuffer,
buffer,
forceClean
//
);
}
//
// Tools ...
//
// Conditions ...
//
bool GetConditions(
XCHSwingsConditions &conditions, //
int barIndex = 0, //
int loopback = 5 //
)
{
//
bool result = true;
//
if (loopback < 5)
{
loopback = 5;
}
//
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 ...
//
// Current ...
CopyCSH(
zIndex,
loopback,
conditions.cSH //
);
CopyCSL(
zIndex,
loopback,
conditions.cSL //
);
//
// Short ...
CopySSH(
zIndex,
loopback,
conditions.sSH //
);
CopySSL(
zIndex,
loopback,
conditions.sSL //
);
//
// Medium ...
CopyMSH(
zIndex,
loopback,
conditions.mSH //
);
CopyMSL(
zIndex,
loopback,
conditions.mSL //
);
//
// Long ...
CopyLSH(
zIndex,
loopback,
conditions.lSH //
);
CopyLSL(
zIndex,
loopback,
conditions.lSL //
);
//
// Hind ...
CopyHSH(
zIndex,
loopback,
conditions.hSH //
);
CopyHSL(
zIndex,
loopback,
conditions.hSL //
);
//
// Conditions ...
//
XOHCL cBar;
result = cBar.Init(
mSymbol,
mPeriod,
cIndex //
);
if (!result)
{
return result;
}
//
XOHCL pBar;
result = pBar.Init(
mSymbol,
mPeriod,
pIndex //
);
if (!result)
{
return result;
}
//
// Current ...
//
bool isCSwingHigh =
cSHBuffer[cIndex] != 0 &&
cSHBuffer[cIndex] != EMPTY_VALUE;
//
bool isCSwingLow =
cSLBuffer[cIndex] != 0 &&
cSLBuffer[cIndex] != EMPTY_VALUE;
//
conditions.isCSwingHigh = isCSwingHigh;
conditions.isCSwingLow = isCSwingLow;
//
// Short ...
//
bool isSSwingHigh =
sSHBuffer[cIndex] != 0 &&
sSHBuffer[cIndex] != EMPTY_VALUE;
//
bool isSSwingLow =
sSLBuffer[cIndex] != 0 &&
sSLBuffer[cIndex] != EMPTY_VALUE;
//
conditions.isSSwingHigh = isSSwingHigh;
conditions.isSSwingLow = isSSwingLow;
//
// Medium ...
//
bool isMSwingHigh =
mSHBuffer[cIndex] != 0 &&
mSHBuffer[cIndex] != EMPTY_VALUE;
//
bool isMSwingLow =
mSLBuffer[cIndex] != 0 &&
mSLBuffer[cIndex] != EMPTY_VALUE;
//
conditions.isMSwingHigh = isMSwingHigh;
conditions.isMSwingLow = isMSwingLow;
//
// Long ...
//
bool isLSwingHigh =
lSHBuffer[cIndex] != 0 &&
lSHBuffer[cIndex] != EMPTY_VALUE;
//
bool isLSwingLow =
lSLBuffer[cIndex] != 0 &&
lSLBuffer[cIndex] != EMPTY_VALUE;
//
conditions.isLSwingHigh = isLSwingHigh;
conditions.isLSwingLow = isLSwingLow;
//
// Hind ...
//
bool isHSwingHigh =
hSHBuffer[cIndex] != 0 &&
hSHBuffer[cIndex] != EMPTY_VALUE;
//
bool isHSwingLow =
hSLBuffer[cIndex] != 0 &&
hSLBuffer[cIndex] != EMPTY_VALUE;
//
conditions.isHSwingHigh = isHSwingHigh;
conditions.isHSwingLow = isHSwingLow;
//
return result;
}
//
// Protected ...
protected:
//
// Private ...
private:
//
// Props ...
XCHSwingsInputs mInputs; // Inputs ...
//
// Buffers ...
//
// Current ...
double cSHBuffer[];
double cSLBuffer[];
//
double sSHBuffer[];
double sSLBuffer[];
// Short ...
//
// Medium ...
double mSHBuffer[];
double mSLBuffer[];
//
// Long ...
double lSHBuffer[];
double lSLBuffer[];
//
// Hind ...
double hSHBuffer[];
double hSLBuffer[];
//
// Tools ...
//
// Calculate Values Until Now ...
void Calculate()
{
//
int totalBars = CountBars();
if (totalBars > 1000)
{
totalBars = 1000;
}
//
// Current ...
CopyBuffer(
mHandler,
XCHSwings_C_SH_LINE,
0,
totalBars,
cSHBuffer
//
);
CopyBuffer(
mHandler,
XCHSwings_C_SL_LINE,
0,
totalBars,
cSLBuffer
//
);
//
// Short ...
CopyBuffer(
mHandler,
XCHSwings_S_SH_LINE,
0,
totalBars,
sSHBuffer
//
);
CopyBuffer(
mHandler,
XCHSwings_S_SL_LINE,
0,
totalBars,
sSLBuffer
//
);
//
// Medium ...
CopyBuffer(
mHandler,
XCHSwings_M_SH_LINE,
0,
totalBars,
mSHBuffer
//
);
CopyBuffer(
mHandler,
XCHSwings_M_SL_LINE,
0,
totalBars,
mSLBuffer
//
);
//
// Long ...
CopyBuffer(
mHandler,
XCHSwings_L_SH_LINE,
0,
totalBars,
lSHBuffer
//
);
CopyBuffer(
mHandler,
XCHSwings_L_SL_LINE,
0,
totalBars,
lSLBuffer
//
);
//
// Hind ...
CopyBuffer(
mHandler,
XCHSwings_H_SH_LINE,
0,
totalBars,
hSHBuffer
//
);
CopyBuffer(
mHandler,
XCHSwings_H_SL_LINE,
0,
totalBars,
hSLBuffer
//
);
};
};
//