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

806 lines
16 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Helper Class Library
// ----------------------------------------------
// Name: XSCXCHEHelper
// 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
//
#include "../Classes/x-saherelm.xhelper.class.mq5"
//
// Definitions ...
//
enum ENUM_XCHE_BUFFERS
{
X_CHE_LONG_EXIT_1_LINE = 0,
X_CHE_SHORT_EXIT_1_LINE = 1,
X_CHE_LONG_EXIT_2_LINE = 2,
X_CHE_SHORT_EXIT_2_LINE = 3,
};
//
// Input Models ...
struct XCHEInputs
{
//
// Props ...
//
// Market ...
int length; // Length
int loopback; // Loopback
double multiplier1; // 1st Multiplier
double multiplier2; // 2nd Multiplier
//
// Calculation ...
ENUM_APPLIED_PRICE upAppliedTo; // Up Zone Applied To
ENUM_APPLIED_PRICE downAppliedTo; // Down Zone Applied To
//
// Presentation ...
bool showLongExit1Line; // Show 1st Long Exit
bool showShortExit1Line; // Show 1st Short Exit
bool showLongExit2Line; // Show 2st Long Exit
bool showShortExit2Line; // Show 2st Short Exit
//
// Constructor(s) ...
XCHEInputs()
{
//
Clean();
}
//
// Tools ...
//
// Clean ...
void Clean()
{
//
length = 0;
loopback = 0;
multiplier1 = 0;
multiplier2 = 0;
//
upAppliedTo = PRICE_HIGH;
downAppliedTo = PRICE_LOW;
//
showLongExit1Line = false;
showShortExit1Line = false;
showLongExit2Line = false;
showShortExit2Line = false;
}
//
// Default ...
void Default()
{
//
length = 35;
loopback = 26;
multiplier1 = 3.0;
multiplier2 = 3.5;
//
upAppliedTo = PRICE_HIGH;
downAppliedTo = PRICE_LOW;
//
showLongExit1Line = true;
showShortExit1Line = true;
showLongExit2Line = true;
showShortExit2Line = true;
}
//
// Validate ...
bool IsValid()
{
//
bool result = false;
//
result =
//
length > 0 &&
loopback < length &&
multiplier1 > 0 &&
multiplier2 > multiplier1
//
;
//
return result;
}
//
// Retrieve MAx Length ...
int Max()
{
//
int result = 0;
//
result = MathMax(length, loopback);
//
return result;
}
};
//
// Define Conditions ...
struct XCHEConditions
{
//
// Common ...
string symbol;
ENUM_TIMEFRAMES period;
datetime time;
//
// Buffers ...
double long1[];
double long2[];
double short1[];
double short2[];
//
// Conditions ...
//
bool isBullish;
bool isBearish;
bool isStrongBullish;
bool isStrongBearish;
bool isSwitchedToBullish;
bool isSwitchedToBearish;
bool isStrongSwitchedToBullish;
bool isStrongSwitchedToBearish;
//
void Clean()
{
//
Clean(long1);
Clean(long2);
Clean(short1);
Clean(short2);
//
ArraySetAsSeries(long1, true);
ArraySetAsSeries(long2, true);
ArraySetAsSeries(short1, true);
ArraySetAsSeries(short2, true);
//
isBullish = false;
isBearish = false;
isStrongBullish = false;
isStrongBearish = false;
isSwitchedToBullish = false;
isSwitchedToBearish = false;
isStrongSwitchedToBullish = false;
isStrongSwitchedToBearish = false;
}
//
void GenerateScore(
double &bullishScore,
double &bearishScore //
)
{
//
bullishScore = 0;
bearishScore = 0;
//
if (isBullish)
{
bullishScore++;
}
if (isStrongBullish)
{
bullishScore++;
}
if (isSwitchedToBullish)
{
bullishScore++;
}
if (isStrongSwitchedToBullish)
{
bullishScore++;
}
//
if (isBearish)
{
bearishScore++;
}
if (isStrongBearish)
{
bearishScore++;
}
if (isSwitchedToBearish)
{
bearishScore++;
}
if (isStrongSwitchedToBearish)
{
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 =
//
ToString("isBullish", isBullish, ignoreFalseConditions, separator) +
ToString("isBearish", isBearish, ignoreFalseConditions, separator) +
ToString("isStrongBullish", isStrongBullish, ignoreFalseConditions, separator) +
ToString("isStrongBearish", isStrongBearish, ignoreFalseConditions, separator) +
ToString("isSwitchedToBullish", isSwitchedToBullish, ignoreFalseConditions, separator) +
ToString("isSwitchedToBearish", isSwitchedToBearish, ignoreFalseConditions, separator) +
ToString("isStrongSwitchedToBullish", isStrongSwitchedToBullish, ignoreFalseConditions, separator) +
ToString("isStrongSwitchedToBearish", isStrongSwitchedToBearish, ignoreFalseConditions, separator) +
""
//
;
//
result =
//
"[" + GetTag() + "]" + separator +
(onlyConditions
? ""
: commonStr) +
(!includeScores
? ""
: scoresStr) +
" " + separator +
(onlyCommons
? ""
: conditionsStr) +
""
//
;
//
return result;
}
//
string GetTag()
{
return "XCHE";
}
};
//
// Class ...
class XSCXCHEHelper : public XSCBaseHelper
{
//
// Public ...
public:
//
// Props ...
//
// Constructors ...
XSCXCHEHelper() : XSCBaseHelper(_Symbol, _Period)
{
}
//
// Deconstructor ...
~XSCXCHEHelper() {}
//
// Tools ...
bool Init(
string symbol, // Trading Symbol
ENUM_TIMEFRAMES period, // Trading Period
XCHEInputs &inputs // Inputs
)
{
//
bool result = false;
//
mSymbol = symbol;
mPeriod = period;
//
result = inputs.IsValid();
if (!result)
{
return result;
}
//
mInputs = inputs;
//
ArraySetAsSeries(longExit1Buffer, true);
ArraySetAsSeries(longExit2Buffer, true);
ArraySetAsSeries(shortExit1Buffer, true);
ArraySetAsSeries(shortExit2Buffer, true);
//
mHandler = iCustom(
mSymbol,
mPeriod,
"x-saherelm.xche",
//
// Inputs ...
//
// Market ...
"",
mInputs.length,
mInputs.loopback,
mInputs.multiplier1,
mInputs.multiplier2,
//
// Calculations ...
"",
mInputs.upAppliedTo,
mInputs.downAppliedTo,
//
// Presentation ...
"",
mInputs.showLongExit1Line, // Show 1st Long Exit,
mInputs.showShortExit1Line, // Show 1st Short Exit,
mInputs.showLongExit2Line, // Show 2st Long Exit,
mInputs.showShortExit2Line // Show 2st Short Exit
//
);
result = mHandler != INVALID_HANDLE;
if (!result)
{
return result;
}
//
return result;
}
//
// Inputs ...
//
XCHEInputs GetInputs()
{
return mInputs;
}
//
bool SetInputs(
XCHEInputs &inputs // Configs
)
{
//
return Init(
mSymbol,
mPeriod,
inputs);
}
//
// Buffers ...
//
double GetLongExit1(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
return longExit1Buffer[barIndex];
}
//
// Copy Required Long Exits 1 ...
int CopyLongExit1(
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,
longExit1Buffer,
buffer,
forceClean
//
);
}
//
double GetLongExit2(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
return longExit2Buffer[barIndex];
}
//
// Copy Required Long Exits 2 ...
int CopyLongExit2(
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,
longExit2Buffer,
buffer,
forceClean
//
);
}
//
double GetShortExit1(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
return shortExit1Buffer[barIndex];
}
//
// Copy Required Short Exits 1 ...
int CopyShortExit1(
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,
shortExit1Buffer,
buffer,
forceClean
//
);
}
//
double GetShortExit2(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
return shortExit2Buffer[barIndex];
}
//
// Copy Required Short Exits 2 ...
int CopyShortExit2(
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,
shortExit2Buffer,
buffer,
forceClean
//
);
}
//
bool GetConditions(
XCHEConditions &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 ...
//
CopyLongExit1(
zIndex,
loopback,
conditions.long1 //
);
CopyLongExit2(
zIndex,
loopback,
conditions.long2 //
);
CopyShortExit1(
zIndex,
loopback,
conditions.short1 //
);
CopyShortExit2(
zIndex,
loopback,
conditions.short2 //
);
//
// Conditions ...
//
bool isBullish = NotEmptyZero(conditions.long1[cIndex]);
bool isBearish = NotEmptyZero(conditions.short1[cIndex]);
//
bool isBullishPrev = NotEmptyZero(conditions.long1[pIndex]);
bool isBearishPrev = NotEmptyZero(conditions.short1[pIndex]);
//
bool isStrongBullish = isBullish &&
NotEmptyZero(conditions.long2[cIndex]);
bool isStrongBearish = isBearish &&
NotEmptyZero(conditions.short2[cIndex]);
//
bool isStrongBullishPrev = isBullishPrev &&
NotEmptyZero(conditions.long2[pIndex]);
bool isStrongBearishPrev = isBearishPrev &&
NotEmptyZero(conditions.short2[pIndex]);
//
bool isSwitchedToBullish = isBullish &&
!isBullishPrev;
bool isSwitchedToBearish = isBearish &&
!isBearishPrev;
//
bool isStrongSwitchedToBullish = isStrongBullish &&
!isStrongBullishPrev;
bool isStrongSwitchedToBearish = isStrongBearish &&
!isStrongBearishPrev;
//
conditions.isBullish = isBullish;
conditions.isBearish = isBearish;
conditions.isStrongBullish = isStrongBullish;
conditions.isStrongBearish = isStrongBearish;
conditions.isSwitchedToBullish = isSwitchedToBullish;
conditions.isSwitchedToBearish = isSwitchedToBearish;
conditions.isStrongSwitchedToBullish = isStrongSwitchedToBullish;
conditions.isStrongSwitchedToBearish = isStrongSwitchedToBearish;
//
return result;
}
//
// Protected ...
protected:
//
// Private ...
private:
//
// Props ...
XCHEInputs mInputs; // Inputs ...
//
// Buffers ...
//
double longExit1Buffer[];
double longExit2Buffer[];
double shortExit1Buffer[];
double shortExit2Buffer[];
//
// Tools ...
//
// Calculate Values Until Now ...
void Calculate()
{
//
int totalBars = CountBars();
if (totalBars > 1000) {
totalBars = 1000;
}
//
// Long Exit 1 ...
CopyBuffer(
mHandler,
X_CHE_LONG_EXIT_1_LINE,
0,
totalBars,
longExit1Buffer
//
);
//
// Long Exit 2 ...
CopyBuffer(
mHandler,
X_CHE_LONG_EXIT_2_LINE,
0,
totalBars,
longExit2Buffer
//
);
//
// Short Exit 1 ...
CopyBuffer(
mHandler,
X_CHE_SHORT_EXIT_1_LINE,
0,
totalBars,
shortExit1Buffer
//
);
//
// Short Exit 2 ...
CopyBuffer(
mHandler,
X_CHE_SHORT_EXIT_2_LINE,
0,
totalBars,
shortExit2Buffer
//
);
}
};
//
// Tools ...