638 lines
11 KiB
Plaintext
638 lines
11 KiB
Plaintext
///////////////////////////////////////////////////////
|
|
//
|
|
// SaherElm IT Center MQL5 Helper Class Library
|
|
// ----------------------------------------------
|
|
// Name: XSCXSLNHelper
|
|
// 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_XSLN_STATE
|
|
{
|
|
XSLN_STATE_BULLISH = 1,
|
|
XSLN_STATE_BEARISH = -1,
|
|
};
|
|
|
|
//
|
|
enum ENUM_XSLN_BUFFERS
|
|
{
|
|
XSLN_MAIN_LINE = 0,
|
|
XSLN_STATE_LINE = 2,
|
|
};
|
|
|
|
//
|
|
// Input Models ...
|
|
struct XSLNInputs
|
|
{
|
|
//
|
|
// Props ...
|
|
|
|
//
|
|
// Presentation ...
|
|
bool show; // Show
|
|
|
|
//
|
|
// Constructor(s) ...
|
|
XSLNInputs()
|
|
{
|
|
//
|
|
Clean();
|
|
}
|
|
|
|
//
|
|
// Tools ...
|
|
|
|
//
|
|
// Clean ...
|
|
void Clean()
|
|
{
|
|
//
|
|
show = false;
|
|
}
|
|
|
|
//
|
|
// Default ...
|
|
void Default()
|
|
{
|
|
//
|
|
show = true;
|
|
}
|
|
|
|
//
|
|
// Validate ...
|
|
bool IsValid()
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
result =
|
|
//
|
|
true
|
|
//
|
|
;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Retrieve MAx Length ...
|
|
int Max()
|
|
{
|
|
//
|
|
int result = 0;
|
|
|
|
//
|
|
result = 0;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
};
|
|
|
|
//
|
|
// Define Conditions ...
|
|
struct XSLNConditions
|
|
{
|
|
//
|
|
// Common ...
|
|
string symbol;
|
|
ENUM_TIMEFRAMES period;
|
|
datetime time;
|
|
|
|
//
|
|
// Buffers ...
|
|
double sln[];
|
|
double state[];
|
|
|
|
//
|
|
// Conditions ...
|
|
|
|
//
|
|
bool isBullish;
|
|
bool isBearish;
|
|
|
|
//
|
|
bool isSwitchedToBullish;
|
|
bool isSwitchedToBearish;
|
|
|
|
//
|
|
void Clean()
|
|
{
|
|
//
|
|
Clean(sln);
|
|
Clean(state);
|
|
|
|
//
|
|
ArraySetAsSeries(sln, true);
|
|
ArraySetAsSeries(state, true);
|
|
|
|
//
|
|
isBullish = false;
|
|
isBearish = false;
|
|
isSwitchedToBullish = false;
|
|
isSwitchedToBearish = false;
|
|
}
|
|
|
|
//
|
|
void GenerateScore(
|
|
double &bullishScore,
|
|
double &bearishScore //
|
|
)
|
|
{
|
|
//
|
|
bullishScore = 0;
|
|
bearishScore = 0;
|
|
|
|
//
|
|
if (isBullish)
|
|
{
|
|
bullishScore++;
|
|
}
|
|
if (isSwitchedToBullish)
|
|
{
|
|
bullishScore++;
|
|
}
|
|
|
|
if (isBearish)
|
|
{
|
|
bearishScore++;
|
|
}
|
|
if (isSwitchedToBearish)
|
|
{
|
|
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("isSwitchedToBullish", isSwitchedToBullish, ignoreFalseConditions, separator) +
|
|
ToString("isSwitchedToBearish", isSwitchedToBearish, ignoreFalseConditions, separator) +
|
|
""
|
|
//
|
|
;
|
|
|
|
//
|
|
result =
|
|
//
|
|
"[" + GetTag() + "]" + separator +
|
|
(onlyConditions
|
|
? ""
|
|
: commonStr) +
|
|
(!includeScores
|
|
? ""
|
|
: scoresStr) +
|
|
" " + separator +
|
|
(onlyCommons
|
|
? ""
|
|
: conditionsStr) +
|
|
""
|
|
//
|
|
;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
string GetTag()
|
|
{
|
|
return "XSLN";
|
|
}
|
|
};
|
|
|
|
//
|
|
// Class ...
|
|
class XSCXSLNHelper : public XSCBaseHelper
|
|
{
|
|
//
|
|
// Public ...
|
|
public:
|
|
//
|
|
// Props ...
|
|
|
|
//
|
|
// Constructors ...
|
|
XSCXSLNHelper() : XSCBaseHelper(_Symbol, _Period)
|
|
{
|
|
}
|
|
|
|
//
|
|
// Deconstructor ...
|
|
~XSCXSLNHelper() {}
|
|
|
|
//
|
|
// Tools ...
|
|
bool Init(
|
|
string symbol, // Trading Symbol
|
|
ENUM_TIMEFRAMES period, // Trading Period
|
|
XSLNInputs &inputs // Inputs
|
|
)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
mSymbol = symbol;
|
|
mPeriod = period;
|
|
|
|
//
|
|
result = inputs.IsValid();
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
mInputs = inputs;
|
|
|
|
//
|
|
ArraySetAsSeries(slnBuffer, true);
|
|
ArraySetAsSeries(stateBuffer, true);
|
|
mHandler = iCustom(
|
|
mSymbol,
|
|
mPeriod,
|
|
"x-saherelm.xsln",
|
|
//
|
|
// Inputs ...
|
|
//
|
|
// Presentation ...
|
|
"",
|
|
mInputs.show
|
|
//
|
|
);
|
|
result = mHandler != INVALID_HANDLE;
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Inputs ...
|
|
|
|
//
|
|
XSLNInputs GetInputs()
|
|
{
|
|
return mInputs;
|
|
}
|
|
|
|
//
|
|
bool SetInputs(
|
|
XSLNInputs &inputs // Configs
|
|
)
|
|
{
|
|
//
|
|
return Init(
|
|
mSymbol,
|
|
mPeriod,
|
|
inputs
|
|
//
|
|
);
|
|
}
|
|
|
|
//
|
|
// Buffers ...
|
|
|
|
//
|
|
double GetSLN(
|
|
int barIndex // Bar Index
|
|
)
|
|
{
|
|
//
|
|
if (barIndex < 0)
|
|
{
|
|
barIndex = 0;
|
|
}
|
|
|
|
//
|
|
Calculate();
|
|
|
|
//
|
|
int count = ArraySize(slnBuffer);
|
|
if (barIndex >= count)
|
|
{
|
|
barIndex = count - 1;
|
|
}
|
|
|
|
//
|
|
return slnBuffer[barIndex];
|
|
}
|
|
|
|
//
|
|
// Copy Required Items ...
|
|
int CopySLN(
|
|
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,
|
|
slnBuffer,
|
|
buffer,
|
|
forceClean
|
|
//
|
|
);
|
|
}
|
|
|
|
//
|
|
double GetState(
|
|
int barIndex // Bar Index
|
|
)
|
|
{
|
|
//
|
|
if (barIndex < 0)
|
|
{
|
|
barIndex = 0;
|
|
}
|
|
|
|
//
|
|
Calculate();
|
|
|
|
//
|
|
int count = ArraySize(stateBuffer);
|
|
if (barIndex >= count)
|
|
{
|
|
barIndex = count - 1;
|
|
}
|
|
|
|
//
|
|
return stateBuffer[barIndex];
|
|
}
|
|
|
|
//
|
|
// Copy Required Items ...
|
|
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
|
|
//
|
|
);
|
|
}
|
|
|
|
//
|
|
ENUM_XSLN_STATE ToState(double value)
|
|
{
|
|
return (ENUM_XSLN_STATE)value;
|
|
}
|
|
|
|
//
|
|
bool IsBullish(double state)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
result = state == 1 || state == -1;
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
ENUM_XSLN_STATE eState = ToState(state);
|
|
result = eState == XSLN_STATE_BULLISH;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
bool IsBearish(double state)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
result = state == 1 || state == -1;
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
ENUM_XSLN_STATE eState = ToState(state);
|
|
result = eState == XSLN_STATE_BEARISH;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
bool GetConditions(
|
|
XSLNConditions &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 ...
|
|
|
|
//
|
|
CopySLN(
|
|
zIndex,
|
|
loopback,
|
|
conditions.sln //
|
|
);
|
|
|
|
//
|
|
CopyState(
|
|
zIndex,
|
|
loopback,
|
|
conditions.state //
|
|
);
|
|
|
|
//
|
|
// Conditions ...
|
|
|
|
//
|
|
bool isBullish = IsBullish(conditions.state[cIndex]);
|
|
bool isBullishPrev = IsBullish(conditions.state[pIndex]);
|
|
|
|
//
|
|
bool isBearish = IsBearish(conditions.state[cIndex]);
|
|
bool isBearishPrev = IsBearish(conditions.state[pIndex]);
|
|
|
|
//
|
|
bool isSwitchedToBullish = isBullish &&
|
|
!isBullishPrev;
|
|
bool isSwitchedToBearish = isBearish &&
|
|
!isBearishPrev;
|
|
|
|
//
|
|
conditions.isBullish = isBullish;
|
|
conditions.isBearish = isBearish;
|
|
conditions.isSwitchedToBullish = isSwitchedToBullish;
|
|
conditions.isSwitchedToBearish = isSwitchedToBearish;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Protected ...
|
|
protected:
|
|
//
|
|
// Private ...
|
|
private:
|
|
//
|
|
// Props ...
|
|
XSLNInputs mInputs; // Inputs ...
|
|
|
|
//
|
|
// Buffers ...
|
|
double slnBuffer[];
|
|
double stateBuffer[];
|
|
|
|
//
|
|
// Tools ...
|
|
|
|
//
|
|
// Calculate Values Until Now ...
|
|
void Calculate()
|
|
{
|
|
//
|
|
int totalBars = CountBars();
|
|
if (totalBars > 1000)
|
|
{
|
|
totalBars = 1000;
|
|
}
|
|
|
|
//
|
|
// Longs Buffer ...
|
|
CopyBuffer(
|
|
mHandler,
|
|
XSLN_MAIN_LINE,
|
|
0,
|
|
totalBars,
|
|
slnBuffer
|
|
//
|
|
);
|
|
|
|
//
|
|
// Longs Buffer ...
|
|
CopyBuffer(
|
|
mHandler,
|
|
XSLN_STATE_LINE,
|
|
0,
|
|
totalBars,
|
|
stateBuffer
|
|
//
|
|
);
|
|
}
|
|
};
|
|
|
|
// |