Files
MQL5Data/Helpers/x-saherelm.xchsar.helper.mq5
T

1267 lines
25 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Helper Class Library
// ----------------------------------------------
// Name: XCXCHSARHelper
// 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_XCHSAR_BUFFERS
{
//
// Current ...
XCHSAR_C_LINE = 0,
//
// Short ...
XCHSAR_S_LINE = 1,
//
// Medium ...
XCHSAR_M_LINE = 2,
//
// Long ...
XCHSAR_L_LINE = 3,
//
// Hind ...
XCHSAR_H_LINE = 4,
};
//
// Input Models ...
struct XCHSARInputs
{
//
// Props ...
//
// Market ...
double sarStep; // Step
double sarMax; // Maximum
//
// 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 ...
//
int cArrowCode; // Current Cycle Arrow Code
int sArrowCode; // Short Cycle Arrow Code
int mArrowCode; // Medium Cycle Arrow Code
int lArrowCode; // Long Cycle Arrow Code
int hArrowCode; // Hind Cycle Arrow Code
//
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) ...
XCHSARInputs()
{
//
Clean();
}
//
// Tools ...
//
// Clean ...
void Clean()
{
//
sarStep = 0.02;
sarMax = 0.2;
//
scMethod = X_PERIOD_AUTO;
scPeriod = NULL;
mcMethod = X_PERIOD_AUTO;
mcPeriod = NULL;
lcMethod = X_PERIOD_AUTO;
lcPeriod = NULL;
hcMethod = X_PERIOD_AUTO;
hcPeriod = NULL;
//
cArrowCode = 255;
sArrowCode = 255;
mArrowCode = 255;
lArrowCode = 255;
hArrowCode = 255;
//
showCurrent = true;
showShort = true;
showMedium = true;
showLong = true;
showHind = true;
}
//
// Default ...
void Default()
{
//
sarStep = 0.02;
sarMax = 0.2;
//
scMethod = X_PERIOD_AUTO;
scPeriod = NULL;
mcMethod = X_PERIOD_AUTO;
mcPeriod = NULL;
lcMethod = X_PERIOD_AUTO;
lcPeriod = NULL;
hcMethod = X_PERIOD_AUTO;
hcPeriod = NULL;
//
cArrowCode = 255;
sArrowCode = 255;
mArrowCode = 255;
lArrowCode = 255;
hArrowCode = 255;
//
showCurrent = true;
showShort = true;
showMedium = true;
showLong = true;
showHind = true;
}
//
// Validate ...
bool IsValid()
{
//
bool result = false;
//
result =
//
sarStep > 0 &&
sarMax > sarStep &&
//
(IsValid(scMethod, scPeriod) &&
IsValid(mcMethod, mcPeriod) &&
IsValid(lcMethod, lcPeriod) &&
IsValid(hcMethod, hcPeriod))
//
;
//
return result;
}
//
// Retrieve MAx Length ...
int Max()
{
//
int result = 0;
//
return result;
}
};
//
// Define Conditions ...
struct XCHSARConditions
{
//
// Common ...
string symbol;
ENUM_TIMEFRAMES period;
datetime time;
//
// Buffers ...
//
double cSars[];
double sSars[];
double mSars[];
double lSars[];
double hSars[];
//
// Conditions ...
//
// Current ...
//
bool isCBullish;
bool isCBearish;
//
bool isCSwitchedBullish;
bool isCSwitchedBearish;
//
// Short ...
//
bool isSBullish;
bool isSBearish;
//
bool isSSwitchedBullish;
bool isSSwitchedBearish;
//
// Medium ...
//
bool isMBullish;
bool isMBearish;
//
bool isMSwitchedBullish;
bool isMSwitchedBearish;
//
// Long ...
//
bool isLBullish;
bool isLBearish;
//
bool isLSwitchedBullish;
bool isLSwitchedBearish;
//
// Hind ...
//
bool isHBullish;
bool isHBearish;
//
bool isHSwitchedBullish;
bool isHSwitchedBearish;
//
void Clean()
{
//
Clean(cSars);
Clean(sSars);
Clean(mSars);
Clean(lSars);
Clean(hSars);
//
ArraySetAsSeries(cSars, true);
ArraySetAsSeries(sSars, true);
ArraySetAsSeries(mSars, true);
ArraySetAsSeries(lSars, true);
ArraySetAsSeries(hSars, true);
//
isCBullish = false;
isCBearish = false;
isCSwitchedBullish = false;
isCSwitchedBearish = false;
isSBullish = false;
isSBearish = false;
isSSwitchedBullish = false;
isSSwitchedBearish = false;
isMBullish = false;
isMBearish = false;
isMSwitchedBullish = false;
isMSwitchedBearish = false;
isLBullish = false;
isLBearish = false;
isLSwitchedBullish = false;
isLSwitchedBearish = false;
isHBullish = false;
isHBearish = false;
isHSwitchedBullish = false;
isHSwitchedBearish = false;
}
//
void GenerateScore(
double &bullishScore,
double &bearishScore //
)
{
//
bullishScore = 0;
bearishScore = 0;
//
// Current ...
if (isCBullish)
{
bullishScore++;
}
if (isCSwitchedBullish)
{
bullishScore++;
}
if (isCBearish)
{
bearishScore++;
}
if (isCSwitchedBearish)
{
bearishScore++;
}
//
// Short ...
if (isSBullish)
{
bullishScore++;
}
if (isSSwitchedBullish)
{
bullishScore++;
}
if (isSBearish)
{
bearishScore++;
}
if (isSSwitchedBearish)
{
bearishScore++;
}
//
// Medium ...
if (isMBullish)
{
bullishScore++;
}
if (isMSwitchedBullish)
{
bullishScore++;
}
if (isMBearish)
{
bearishScore++;
}
if (isMSwitchedBearish)
{
bearishScore++;
}
//
// Long ...
if (isLBullish)
{
bullishScore++;
}
if (isLSwitchedBullish)
{
bullishScore++;
}
if (isLBearish)
{
bearishScore++;
}
if (isLSwitchedBearish)
{
bearishScore++;
}
//
// Hind ...
if (isHBullish)
{
bullishScore++;
}
if (isHSwitchedBullish)
{
bullishScore++;
}
if (isHBearish)
{
bearishScore++;
}
if (isHSwitchedBearish)
{
bearishScore++;
}
}
//
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("isCBullish", isCBullish, ignoreFalseConditions, separator) +
ToString("isCBearish", isCBearish, ignoreFalseConditions, separator) +
ToString("isCSwitchedBullish", isCSwitchedBullish, ignoreFalseConditions, separator) +
ToString("isCSwitchedBearish", isCSwitchedBearish, ignoreFalseConditions, separator) +
//
// Short ...
ToString("isSBullish", isSBullish, ignoreFalseConditions, separator) +
ToString("isSBearish", isSBearish, ignoreFalseConditions, separator) +
ToString("isSSwitchedBullish", isSSwitchedBullish, ignoreFalseConditions, separator) +
ToString("isSSwitchedBearish", isSSwitchedBearish, ignoreFalseConditions, separator) +
//
// Medium ...
ToString("isMBullish", isMBullish, ignoreFalseConditions, separator) +
ToString("isMBearish", isMBearish, ignoreFalseConditions, separator) +
ToString("isMSwitchedBullish", isMSwitchedBullish, ignoreFalseConditions, separator) +
ToString("isMSwitchedBearish", isMSwitchedBearish, ignoreFalseConditions, separator) +
//
// Long ...
ToString("isLBullish", isLBullish, ignoreFalseConditions, separator) +
ToString("isLBearish", isLBearish, ignoreFalseConditions, separator) +
ToString("isLSwitchedBullish", isLSwitchedBullish, ignoreFalseConditions, separator) +
ToString("isLSwitchedBearish", isLSwitchedBearish, ignoreFalseConditions, separator) +
//
// Hind ...
ToString("isHBullish", isHBullish, ignoreFalseConditions, separator) +
ToString("isHBearish", isHBearish, ignoreFalseConditions, separator) +
ToString("isHSwitchedBullish", isHSwitchedBullish, ignoreFalseConditions, separator) +
ToString("isHSwitchedBearish", isHSwitchedBearish, ignoreFalseConditions, separator) +
//
""
//
;
//
result =
//
"[" + GetTag() + "]" + separator +
(onlyConditions
? ""
: commonStr) +
(!includeScores
? ""
: scoresStr) +
" " + separator +
(onlyCommons
? ""
: conditionsStr) +
""
//
;
//
return result;
}
//
string GetTag()
{
return "XCHSAR";
}
};
//
// Class ...
class XCXCHSARHelper : public XCBaseHelper
{
//
// Public ...
public:
//
// Props ...
//
// Constructors ...
XCXCHSARHelper()
: XCBaseHelper(_Symbol, _Period)
{
}
//
// Deconstructor ...
~XCXCHSARHelper()
{
}
//
// Tools ...
bool Init(
string symbol, // Trading Symbol
ENUM_TIMEFRAMES period, // Trading Period
XCHSARInputs &inputs // Inputs
)
{
//
bool result = false;
//
mSymbol = symbol;
mPeriod = period;
//
result = inputs.IsValid();
if (!result)
{
return result;
}
//
mInputs = inputs;
//
ArraySetAsSeries(cBuffer, true);
ArraySetAsSeries(sBuffer, true);
ArraySetAsSeries(mBuffer, true);
ArraySetAsSeries(lBuffer, true);
ArraySetAsSeries(hBuffer, true);
//
mHandler = iCustom(
mSymbol,
mPeriod,
"x-saherelm.xchsar",
//
// Inputs ...
//
// Market ...
"",
mInputs.sarStep,
mInputs.sarMax,
//
// Cycles ...
"",
//
// Short ...
"",
mInputs.scMethod,
mInputs.scPeriod,
//
// Medium ...
"",
mInputs.mcMethod,
mInputs.mcPeriod,
//
// Long ...
"",
mInputs.lcMethod,
mInputs.lcPeriod,
//
// Hind ...
"",
mInputs.hcMethod,
mInputs.hcPeriod,
//
// Presentation ...
"",
mInputs.cArrowCode,
mInputs.sArrowCode,
mInputs.mArrowCode,
mInputs.lArrowCode,
mInputs.hArrowCode,
mInputs.showCurrent,
mInputs.showShort,
mInputs.showMedium,
mInputs.showLong,
mInputs.showHind
//
);
result = mHandler != INVALID_HANDLE;
if (!result)
{
return result;
}
//
return result;
}
//
// Inputs ...
//
XCHSARInputs GetInputs()
{
return mInputs;
}
//
bool SetInputs(
XCHSARInputs &inputs // Configs
)
{
//
return Init(
mSymbol,
mPeriod,
inputs
//
);
}
//
// Buffers ...
//
// Current ...
//
double GetC(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
int count = ArraySize(cBuffer);
if (barIndex >= count)
{
barIndex = count - 1;
}
//
return cBuffer[barIndex];
}
//
// Copy Required Items ...
int CopyC(
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,
cBuffer,
buffer,
forceClean
//
);
}
//
// Short ...
//
double GetS(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
int count = ArraySize(sBuffer);
if (barIndex >= count)
{
barIndex = count - 1;
}
//
return sBuffer[barIndex];
}
//
// Copy Required Items ...
int CopyS(
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,
sBuffer,
buffer,
forceClean
//
);
}
//
// Medium ...
//
double GetM(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
int count = ArraySize(mBuffer);
if (barIndex >= count)
{
barIndex = count - 1;
}
//
return mBuffer[barIndex];
}
//
// Copy Required Items ...
int CopyM(
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,
mBuffer,
buffer,
forceClean
//
);
}
//
// Long ...
//
double GetL(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
int count = ArraySize(lBuffer);
if (barIndex >= count)
{
barIndex = count - 1;
}
//
return lBuffer[barIndex];
}
//
// Copy Required Items ...
int CopyL(
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,
lBuffer,
buffer,
forceClean
//
);
}
//
// Hind ...
//
double GetH(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
int count = ArraySize(hBuffer);
if (barIndex >= count)
{
barIndex = count - 1;
}
//
return hBuffer[barIndex];
}
//
// Copy Required Items ...
int CopyH(
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,
hBuffer,
buffer,
forceClean
//
);
}
//
bool GetConditions(
XCHSARConditions &conditions, //
int barIndex = 0, //
int loopback = 3 //
)
{
//
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 ...
//
// Current ...
CopyC(
zIndex,
loopback,
conditions.cSars //
);
//
// Short ...
CopyS(
zIndex,
loopback,
conditions.sSars //
);
//
// Medium ...
CopyM(
zIndex,
loopback,
conditions.mSars //
);
//
// Long ...
CopyL(
zIndex,
loopback,
conditions.lSars //
);
//
// Hind ...
CopyH(
zIndex,
loopback,
conditions.hSars //
);
//
// 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;
}
double hhValue = pBar.FindHighest(
loopback,
MODE_HIGH //
);
double llValue = pBar.FindLowest(
loopback,
MODE_LOW //
);
//
// Current ...
//
bool isCBullish = conditions.cSars[cIndex] < cBar.low;
bool isCBullishPrev = conditions.cSars[pIndex] < pBar.low;
//
bool isCBearish = conditions.cSars[cIndex] > cBar.high;
bool isCBearishPrev = conditions.cSars[pIndex] > pBar.high;
//
bool isCSwitchedBullish = isCBullish &&
!isCBullishPrev;
bool isCSwitchedBearish = isCBearish &&
!isCBearishPrev;
//
conditions.isCBullish = isCBullish;
conditions.isCBearish = isCBearish;
conditions.isCSwitchedBullish = isCSwitchedBullish;
conditions.isCSwitchedBearish = isCSwitchedBearish;
//
// Short ...
//
bool isSBullish = conditions.sSars[cIndex] < cBar.low;
bool isSBullishPrev = conditions.sSars[pIndex] < pBar.low;
//
bool isSBearish = conditions.sSars[cIndex] > cBar.high;
bool isSBearishPrev = conditions.sSars[pIndex] > pBar.high;
//
bool isSSwitchedBullish = isSBullish &&
!isSBullishPrev;
bool isSSwitchedBearish = isSBearish &&
!isSBearishPrev;
//
conditions.isSBullish = isSBullish;
conditions.isSBearish = isSBearish;
conditions.isSSwitchedBullish = isSSwitchedBullish;
conditions.isSSwitchedBearish = isSSwitchedBearish;
//
// Medium ...
//
bool isMBullish = conditions.mSars[cIndex] < cBar.low;
bool isMBullishPrev = conditions.mSars[pIndex] < pBar.low;
//
bool isMBearish = conditions.mSars[cIndex] > cBar.high;
bool isMBearishPrev = conditions.mSars[pIndex] > pBar.high;
//
bool isMSwitchedBullish = isMBullish &&
!isMBullishPrev;
bool isMSwitchedBearish = isMBearish &&
!isMBearishPrev;
//
conditions.isMBullish = isMBullish;
conditions.isMBearish = isMBearish;
conditions.isMSwitchedBullish = isMSwitchedBullish;
conditions.isMSwitchedBearish = isMSwitchedBearish;
//
// Long ...
//
bool isLBullish = conditions.lSars[cIndex] < cBar.low;
bool isLBullishPrev = conditions.lSars[pIndex] < pBar.low;
//
bool isLBearish = conditions.lSars[cIndex] > cBar.high;
bool isLBearishPrev = conditions.lSars[pIndex] > pBar.high;
//
bool isLSwitchedBullish = isLBullish &&
!isLBullishPrev;
bool isLSwitchedBearish = isLBearish &&
!isLBearishPrev;
//
conditions.isLBullish = isLBullish;
conditions.isLBearish = isLBearish;
conditions.isLSwitchedBullish = isLSwitchedBullish;
conditions.isLSwitchedBearish = isLSwitchedBearish;
//
// Hind ...
//
bool isHBullish = conditions.hSars[cIndex] < cBar.low;
bool isHBullishPrev = conditions.hSars[pIndex] < pBar.low;
//
bool isHBearish = conditions.hSars[cIndex] > cBar.high;
bool isHBearishPrev = conditions.hSars[pIndex] > pBar.high;
//
bool isHSwitchedBullish = isHBullish &&
!isHBullishPrev;
bool isHSwitchedBearish = isHBearish &&
!isHBearishPrev;
//
conditions.isHBullish = isHBullish;
conditions.isHBearish = isHBearish;
conditions.isHSwitchedBullish = isHSwitchedBullish;
conditions.isHSwitchedBearish = isHSwitchedBearish;
//
return result;
}
//
// Protected ...
protected:
//
// Private ...
private:
//
// Props ...
XCHSARInputs mInputs; // Inputs ...
//
// Buffers ...
//
double cBuffer[];
double sBuffer[];
double mBuffer[];
double lBuffer[];
double hBuffer[];
//
// Tools ...
//
// Calculate Values Until Now ...
void Calculate()
{
//
int totalBars = CountBars();
if (totalBars > 1000)
{
totalBars = 1000;
}
//
// Current ...
CopyBuffer(
mHandler,
XCHSAR_C_LINE,
0,
totalBars,
cBuffer
//
);
//
// Short ...
CopyBuffer(
mHandler,
XCHSAR_S_LINE,
0,
totalBars,
sBuffer
//
);
//
// Medium ...
CopyBuffer(
mHandler,
XCHSAR_S_LINE,
0,
totalBars,
mBuffer
//
);
//
// Long ...
CopyBuffer(
mHandler,
XCHSAR_S_LINE,
0,
totalBars,
lBuffer
//
);
//
// Hind ...
CopyBuffer(
mHandler,
XCHSAR_S_LINE,
0,
totalBars,
hBuffer
//
);
}
};
//