Files
xMQL5/BKPS/14030505/Helpers/x-saherelm.xstr.helper.mq5
T

650 lines
12 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Helper Class Library
// ----------------------------------------------
// Name: XSCXSTRHelper
// 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_XSTR_BUFFERS
{
X_STR_TREND_LINE = 0,
X_STR_STATE_LINE = 5,
};
//
enum ENUM_XSTR_TREND_STATES
{
X_STR_BULLISH = 1, // Bullish
X_STR_BEARISH = -1, // Bearish
};
//
// Input Models ...
struct XSTRInputs
{
//
// Props ...
//
// Market ...
int length; // Length
double multiplier; // Multiplier
ENUM_APPLIED_PRICE appliedTo; // Applied To
//
// Presentation ...
bool showTrends; // Show Trends
bool fillTrends; // Fill Trends
//
// Constructor(s) ...
XSTRInputs()
{
//
Clean();
}
//
// Tools ...
//
// Clean ...
void Clean()
{
//
length = 0;
multiplier = 0;
appliedTo = PRICE_CLOSE;
//
showTrends = false;
fillTrends = false;
}
//
// Default ...
void Default()
{
//
length = 14;
multiplier = 3.5;
appliedTo = PRICE_MEDIAN;
//
showTrends = true;
fillTrends = false;
}
//
// Validate ...
bool IsValid()
{
//
bool result = false;
//
result =
//
length > 0 &&
multiplier > 0
//
;
//
return result;
}
//
// Retrieve MAx Length ...
int Max()
{
//
int result = 0;
//
result = length;
//
return result;
}
};
//
// Define Conditions ...
struct XSTRConditions
{
//
// Common ...
string symbol;
ENUM_TIMEFRAMES period;
datetime time;
//
// Buffers ...
double trend[];
double state[];
//
// Conditions ...
//
bool isTrendBullish;
bool isTrendBearish;
bool isTrendSwitchedToBullish;
bool isTrendSwitchedToBearish;
//
void Clean()
{
//
Clean(trend);
Clean(state);
//
ArraySetAsSeries(trend, true);
ArraySetAsSeries(state, true);
//
isTrendBullish = false;
isTrendBearish = false;
isTrendSwitchedToBullish = false;
isTrendSwitchedToBearish = false;
}
//
void GenerateScore(
double &bullishScore,
double &bearishScore //
)
{
//
bullishScore = 0;
bearishScore = 0;
//
if (isTrendBullish)
{
bullishScore++;
}
if (isTrendSwitchedToBullish)
{
bullishScore++;
}
//
if (isTrendBearish)
{
bearishScore++;
}
if (isTrendSwitchedToBearish)
{
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("isTrendBullish", isTrendBullish, ignoreFalseConditions, separator) +
ToString("isTrendBearish", isTrendBearish, ignoreFalseConditions, separator) +
ToString("isTrendSwitchedToBullish", isTrendSwitchedToBullish, ignoreFalseConditions, separator) +
ToString("isTrendSwitchedToBearish", isTrendSwitchedToBearish, ignoreFalseConditions, separator) +
""
//
;
//
result =
//
"[" + GetTag() + "]" + separator +
(onlyConditions
? ""
: commonStr) +
(!includeScores
? ""
: scoresStr) +
" " + separator +
(onlyCommons
? ""
: conditionsStr) +
""
//
;
//
return result;
}
//
string GetTag()
{
return "XSTR";
}
};
//
// Class ...
class XSCXSTRHelper : public XSCBaseHelper
{
//
// Public ...
public:
//
// Props ...
//
// Constructors ...
XSCXSTRHelper() : XSCBaseHelper(_Symbol, _Period)
{
}
//
// Deconstructor ...
~XSCXSTRHelper()
{
//
Clean(trendBuffer);
Clean(stateBuffer);
}
//
// Tools ...
bool Init(
string symbol, // Trading Symbol
ENUM_TIMEFRAMES period, // Trading Period
XSTRInputs &inputs // Inputs
)
{
//
bool result = false;
//
mSymbol = symbol;
mPeriod = period;
//
result = inputs.IsValid();
if (!result)
{
return result;
}
//
mInputs = inputs;
//
ArraySetAsSeries(trendBuffer, true);
ArraySetAsSeries(stateBuffer, true);
//
mHandler = iCustom(
mSymbol,
mPeriod,
"x-saherelm.xstr",
//
// Inputs ...
//
// ATR ...
"",
mInputs.length, // Length
mInputs.multiplier, // Multiplier
mInputs.appliedTo, // Applied To
//
// Presentation ...
"",
mInputs.showTrends, // Show Trends
mInputs.fillTrends // Fill Trends
//
);
result = mHandler != INVALID_HANDLE;
if (!result)
{
return result;
}
//
return result;
}
//
// Inputs ...
//
XSTRInputs GetInputs()
{
return mInputs;
}
//
bool SetInputs(
XSTRInputs &inputs // Configs
)
{
//
return Init(
mSymbol,
mPeriod,
inputs);
}
//
// Buffers ...
//
// Trend ...
//
double GetTrend(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
return trendBuffer[barIndex];
}
//
// Copy Required Buffer ...
int CopyTrend(
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,
trendBuffer,
buffer,
forceClean
//
);
}
//
// State ...
//
ENUM_XSTR_TREND_STATES GetState(
int barIndex // Bar Index
)
{
//
if (barIndex < 0)
{
barIndex = 0;
}
//
Calculate();
//
return (ENUM_XSTR_TREND_STATES)((int)stateBuffer[barIndex]);
}
//
// Copy Required Buffer ...
int CopyState(
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,
stateBuffer,
buffer,
forceClean
//
);
}
//
// Tools ...
//
ENUM_XSTR_TREND_STATES ToState(double state)
{
return (ENUM_XSTR_TREND_STATES)((int)state);
}
//
bool IsBullish(double state)
{
//
bool result = false;
//
result =
//
ToState(state) == X_STR_BULLISH;
//
;
//
return result;
}
//
bool IsBearish(double state)
{
//
bool result = false;
//
result =
//
ToState(state) == X_STR_BEARISH;
//
;
//
return result;
}
//
bool GetConditions(
XSTRConditions &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;
//
CopyTrend(
zIndex,
loopback,
conditions.trend //
);
CopyState(
zIndex,
loopback,
conditions.state //
);
//
// Calculate Conditions ...
//
bool isTrendBullish = IsBullish(conditions.state[cIndex]);
bool isTrendBearish = IsBearish(conditions.state[cIndex]);
//
bool isTrendBullishPrev = IsBullish(conditions.state[pIndex]);
bool isTrendBearishPrev = IsBearish(conditions.state[pIndex]);
//
bool isTrendSwitchedToBullish = isTrendBullish &&
!isTrendBullishPrev;
bool isTrendSwitchedToBearish = isTrendBearish &&
!isTrendBearishPrev;
//
conditions.isTrendBullish = isTrendBullish;
conditions.isTrendBearish = isTrendBearish;
conditions.isTrendSwitchedToBullish = isTrendSwitchedToBullish;
conditions.isTrendSwitchedToBearish = isTrendSwitchedToBearish;
//
return result;
}
//
// Protected ...
protected:
//
// Private ...
private:
//
// Props ...
XSTRInputs mInputs; // Inputs ...
//
// Buffers ...
double trendBuffer[];
double stateBuffer[];
//
// Tools ...
//
// Calculate Values Until Now ...
void Calculate()
{
//
int totalBars = CountBars();
if (totalBars > 1000)
{
totalBars = 1000;
}
//
// Trend ...
CopyBuffer(
mHandler,
X_STR_TREND_LINE,
0,
totalBars,
trendBuffer
//
);
//
// State ...
CopyBuffer(
mHandler,
X_STR_STATE_LINE,
0,
totalBars,
stateBuffer
//
);
}
};
//
// Tools ...