852 lines
18 KiB
Plaintext
852 lines
18 KiB
Plaintext
///////////////////////////////////////////////////////
|
|
//
|
|
// SaherElm IT Center MQL5 Helper Class Library
|
|
// ----------------------------------------------
|
|
// Name: XCX121XDELTASHelper
|
|
// 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_X121_XDELTA_BUFFERS
|
|
{
|
|
//
|
|
X121_XDELTA_VOLUME_LINE = 0,
|
|
X121_XDELTA_HISTOGRAM_LINE = 1,
|
|
X121_XDELTA_MA_LINE = 3,
|
|
X121_XDELTA_STATE_LINE = 5,
|
|
};
|
|
|
|
//
|
|
// Input Models ...
|
|
struct X121XDELTAInputs
|
|
{
|
|
//
|
|
// Props ...
|
|
|
|
//
|
|
// Makret ...
|
|
int maLength; // Moving Averge Length
|
|
ENUM_X_MA_METHOD maMethod; // Moving Averge Method
|
|
|
|
//
|
|
// Presentation ...
|
|
int startCalculationForLastBars; // Calculate Last n Bars
|
|
bool showDeltaLine; // Show Delta Line
|
|
bool showDeltaHistogram; // Show Delta Histogram
|
|
bool showDeltaMa; // Show Delta Moving Average
|
|
|
|
//
|
|
// Constructor(s) ...
|
|
X121XDELTAInputs()
|
|
{
|
|
Clean();
|
|
}
|
|
|
|
//
|
|
// Tools ...
|
|
|
|
/**
|
|
* Cleanup Model ...
|
|
*/
|
|
void Clean()
|
|
{
|
|
//
|
|
// Makret ...
|
|
maLength = 0; // Moving Averge Length
|
|
maMethod = X_MA_MODE_NONE; // Moving Averge Method
|
|
|
|
//
|
|
// Presentation ...
|
|
startCalculationForLastBars = 0; // Calculate Last n Bars
|
|
showDeltaLine = false; // Show Delta Line
|
|
showDeltaHistogram = false; // Show Delta Histogram
|
|
showDeltaMa = false; // Show Delta Moving Average
|
|
|
|
//
|
|
ZeroMemory(this);
|
|
}
|
|
|
|
/**
|
|
* Default Inputs ...
|
|
*/
|
|
void Default()
|
|
{
|
|
//
|
|
// Makret ...
|
|
maLength = 14; // Moving Averge Length
|
|
maMethod = X_MA_MODE_EMA; // Moving Averge Method
|
|
|
|
//
|
|
// Presentation ...
|
|
startCalculationForLastBars = 1000; // Calculate Last n Bars
|
|
showDeltaLine = true; // Show Delta Line
|
|
showDeltaHistogram = true; // Show Delta Histogram
|
|
showDeltaMa = true; // Show Delta Moving Average
|
|
}
|
|
|
|
/**
|
|
* Validate Model ...
|
|
*
|
|
* @return ( bool )
|
|
*/
|
|
bool IsValid()
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
result =
|
|
//
|
|
maLength > 0 &&
|
|
IsValid(maMethod)
|
|
//
|
|
;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Retrieve Max Input Length ...
|
|
*
|
|
* @return ( int )
|
|
*/
|
|
int Max()
|
|
{
|
|
//
|
|
int result = 0;
|
|
|
|
//
|
|
result = MathMax(result, maLength);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
};
|
|
|
|
//
|
|
// Conditions ...
|
|
struct X121XDELTAConditions
|
|
{
|
|
//
|
|
// Common ...
|
|
string symbol;
|
|
ENUM_TIMEFRAMES period;
|
|
datetime time;
|
|
|
|
//
|
|
// Buffers ...
|
|
double deltaBuffer[];
|
|
double deltaMaBuffer[];
|
|
double deltaStateBuffer[];
|
|
|
|
//
|
|
// Conditions ...
|
|
|
|
//
|
|
bool isDeltaBullish;
|
|
bool isDeltaBearish;
|
|
bool isDeltaSwitchedToBullish;
|
|
bool isDeltaSwitchedToBearish;
|
|
|
|
//
|
|
bool isDeltaOverMa;
|
|
bool isDeltaUnderMa;
|
|
bool isDeltaCrossedOverMa;
|
|
bool isDeltaCrossedUnderMa;
|
|
|
|
//
|
|
// Constructor ...
|
|
X121XDELTAConditions()
|
|
{
|
|
Clean();
|
|
}
|
|
|
|
//
|
|
// Tools ...
|
|
|
|
/**
|
|
* Cleaning Up ...
|
|
*/
|
|
void Clean()
|
|
{
|
|
//
|
|
// Commons ...
|
|
time = NULL;
|
|
symbol = NULL;
|
|
period = NULL;
|
|
|
|
//
|
|
// Buffers ...
|
|
|
|
//
|
|
Clean(deltaBuffer);
|
|
Clean(deltaMaBuffer);
|
|
Clean(deltaStateBuffer);
|
|
|
|
//
|
|
ArraySetAsSeries(deltaBuffer, true);
|
|
ArraySetAsSeries(deltaMaBuffer, true);
|
|
ArraySetAsSeries(deltaStateBuffer, true);
|
|
|
|
//
|
|
// Conditions ...
|
|
|
|
//
|
|
isDeltaBullish = false;
|
|
isDeltaBearish = false;
|
|
isDeltaSwitchedToBullish = false;
|
|
isDeltaSwitchedToBearish = false;
|
|
|
|
//
|
|
isDeltaOverMa = false;
|
|
isDeltaUnderMa = false;
|
|
isDeltaCrossedOverMa = false;
|
|
isDeltaCrossedUnderMa = false;
|
|
|
|
//
|
|
ZeroMemory(this);
|
|
}
|
|
|
|
/**
|
|
* Generate Conditions Scores ...
|
|
*
|
|
* @param bullishScore: Double, Directional Scores Reference ...
|
|
* @param bearishScore: Double, Directional Scores Reference ...
|
|
*/
|
|
void GenerateScore(
|
|
double &bullishScore,
|
|
double &bearishScore //
|
|
)
|
|
{
|
|
//
|
|
bullishScore = 0;
|
|
bearishScore = 0;
|
|
|
|
//
|
|
double score = 1;
|
|
double minScore = 0.5;
|
|
double highScore = 1.5;
|
|
|
|
//
|
|
if (isDeltaOverMa)
|
|
{
|
|
bullishScore += minScore;
|
|
}
|
|
if (isDeltaBullish)
|
|
{
|
|
bullishScore += minScore;
|
|
}
|
|
if (isDeltaCrossedOverMa)
|
|
{
|
|
bullishScore += score;
|
|
}
|
|
if (isDeltaSwitchedToBullish)
|
|
{
|
|
bullishScore += score;
|
|
}
|
|
|
|
if (isDeltaBearish)
|
|
{
|
|
bearishScore += minScore;
|
|
}
|
|
if (isDeltaUnderMa)
|
|
{
|
|
bearishScore += minScore;
|
|
}
|
|
if (isDeltaCrossedUnderMa)
|
|
{
|
|
bearishScore += score;
|
|
}
|
|
if (isDeltaSwitchedToBearish)
|
|
{
|
|
bearishScore += score;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Generate Summary String for Represent Conditions State ...
|
|
*
|
|
* @param onlyCommons: Boolean, Just Generate Only Commons Conditions ...
|
|
* @param onlyConditions: Boolean, Just Generate Only Conditions ...
|
|
* @param includeScores: Boolean, Attach Scores Representations on Result ...
|
|
* @param ignoreFalseConditions: Boolean, Ignore False Conditions on Result ...
|
|
* @param separator: String, Separate Lines ...
|
|
*
|
|
* @return ( string )
|
|
*/
|
|
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 =
|
|
//
|
|
"-----------------------" + separator +
|
|
"XDELTA: " + separator +
|
|
"-----------------------" + separator +
|
|
ToString("isDeltaBullish", isDeltaBullish, ignoreFalseConditions, separator) +
|
|
ToString("isDeltaBearish", isDeltaBearish, ignoreFalseConditions, separator) +
|
|
ToString("isDeltaSwitchedToBullish", isDeltaSwitchedToBullish, ignoreFalseConditions, separator) +
|
|
ToString("isDeltaSwitchedToBearish", isDeltaSwitchedToBearish, ignoreFalseConditions, separator) +
|
|
ToString("isDeltaOverMa", isDeltaOverMa, ignoreFalseConditions, separator) +
|
|
ToString("isDeltaUnderMa", isDeltaUnderMa, ignoreFalseConditions, separator) +
|
|
ToString("isDeltaCrossedOverMa", isDeltaCrossedOverMa, ignoreFalseConditions, separator) +
|
|
ToString("isDeltaCrossedUnderMa", isDeltaCrossedUnderMa, ignoreFalseConditions, separator) +
|
|
//
|
|
""
|
|
//
|
|
;
|
|
|
|
//
|
|
result =
|
|
//
|
|
"[" + GetTag() + "]" + separator +
|
|
(onlyConditions
|
|
? ""
|
|
: commonStr) +
|
|
(!includeScores
|
|
? ""
|
|
: scoresStr) +
|
|
" " + separator +
|
|
(onlyCommons
|
|
? ""
|
|
: conditionsStr) +
|
|
""
|
|
//
|
|
;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Retrieve nique Tag Identifier ...
|
|
*
|
|
* @return ( string )
|
|
*/
|
|
string GetTag()
|
|
{
|
|
return GetTypeName(this);
|
|
}
|
|
|
|
//
|
|
};
|
|
|
|
//
|
|
// Class ...
|
|
class XCX121XDELTAHelper : public XCBaseHelper
|
|
{
|
|
//
|
|
// Public ...
|
|
public:
|
|
//
|
|
// Props ...
|
|
|
|
//
|
|
// Constructors ...
|
|
XCX121XDELTAHelper()
|
|
: XCBaseHelper(_Symbol, _Period)
|
|
{
|
|
}
|
|
|
|
//
|
|
// Deconstructor ...
|
|
~XCX121XDELTAHelper()
|
|
{
|
|
//
|
|
mInputs.Clean();
|
|
|
|
//
|
|
Clean(deltaBuffer);
|
|
Clean(deltaMaBuffer);
|
|
Clean(deltaStateBuffer);
|
|
}
|
|
|
|
//
|
|
// Tools ...
|
|
bool Init(
|
|
string symbol, // Trading Symbol
|
|
ENUM_TIMEFRAMES period, // Trading Period
|
|
X121XDELTAInputs &inputs // Inputs
|
|
)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
mSymbol = symbol;
|
|
mPeriod = period;
|
|
|
|
//
|
|
result = inputs.IsValid();
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
ArraySetAsSeries(deltaBuffer, true);
|
|
ArraySetAsSeries(deltaMaBuffer, true);
|
|
ArraySetAsSeries(deltaStateBuffer, true);
|
|
|
|
//
|
|
mInputs = inputs;
|
|
|
|
//
|
|
mHandler = iCustom(
|
|
mSymbol,
|
|
mPeriod,
|
|
"x-saherelm.x121.xdelta",
|
|
//
|
|
// Inputs ...
|
|
//
|
|
// Market ...
|
|
"",
|
|
mInputs.maLength,
|
|
mInputs.maMethod,
|
|
//
|
|
// Presentation ...
|
|
"",
|
|
//
|
|
mInputs.startCalculationForLastBars,
|
|
mInputs.showDeltaLine,
|
|
mInputs.showDeltaHistogram,
|
|
mInputs.showDeltaMa
|
|
//
|
|
);
|
|
result = mHandler != INVALID_HANDLE;
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Inputs ...
|
|
|
|
//
|
|
X121XDELTAInputs GetInputs()
|
|
{
|
|
return mInputs;
|
|
}
|
|
|
|
//
|
|
bool SetInputs(
|
|
X121XDELTAInputs &inputs // Configs
|
|
)
|
|
{
|
|
//
|
|
return Init(
|
|
mSymbol,
|
|
mPeriod,
|
|
inputs
|
|
//
|
|
);
|
|
}
|
|
|
|
//
|
|
// Readers ...
|
|
|
|
//
|
|
// Bullish ...
|
|
|
|
//
|
|
double GetDelta(
|
|
int barIndex // Bar Index
|
|
)
|
|
{
|
|
//
|
|
if (barIndex < 0)
|
|
{
|
|
barIndex = 0;
|
|
}
|
|
|
|
//
|
|
Calculate();
|
|
|
|
//
|
|
int count = ArraySize(deltaBuffer);
|
|
if (barIndex >= count)
|
|
{
|
|
barIndex = count - 1;
|
|
}
|
|
|
|
//
|
|
return deltaBuffer[barIndex];
|
|
}
|
|
|
|
//
|
|
int CopyDelta(
|
|
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,
|
|
deltaBuffer,
|
|
buffer,
|
|
forceClean
|
|
//
|
|
);
|
|
}
|
|
|
|
//
|
|
double GetDeltaState(
|
|
int barIndex // Bar Index
|
|
)
|
|
{
|
|
//
|
|
if (barIndex < 0)
|
|
{
|
|
barIndex = 0;
|
|
}
|
|
|
|
//
|
|
Calculate();
|
|
|
|
//
|
|
int count = ArraySize(deltaStateBuffer);
|
|
if (barIndex >= count)
|
|
{
|
|
barIndex = count - 1;
|
|
}
|
|
|
|
//
|
|
return deltaStateBuffer[barIndex];
|
|
}
|
|
|
|
//
|
|
int CopyDeltaState(
|
|
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,
|
|
deltaStateBuffer,
|
|
buffer,
|
|
forceClean
|
|
//
|
|
);
|
|
}
|
|
|
|
//
|
|
double GetDeltaMa(
|
|
int barIndex // Bar Index
|
|
)
|
|
{
|
|
//
|
|
if (barIndex < 0)
|
|
{
|
|
barIndex = 0;
|
|
}
|
|
|
|
//
|
|
Calculate();
|
|
|
|
//
|
|
int count = ArraySize(deltaMaBuffer);
|
|
if (barIndex >= count)
|
|
{
|
|
barIndex = count - 1;
|
|
}
|
|
|
|
//
|
|
return deltaMaBuffer[barIndex];
|
|
}
|
|
|
|
//
|
|
int CopyDeltaMa(
|
|
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,
|
|
deltaMaBuffer,
|
|
buffer,
|
|
forceClean
|
|
//
|
|
);
|
|
}
|
|
|
|
//
|
|
void Free() override
|
|
{
|
|
Cleanup(10);
|
|
}
|
|
|
|
//
|
|
bool GetConditions(
|
|
X121XDELTAConditions &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 ...
|
|
|
|
//
|
|
CopyDelta(
|
|
zIndex,
|
|
loopback,
|
|
conditions.deltaBuffer //
|
|
);
|
|
|
|
//
|
|
CopyDeltaState(
|
|
zIndex,
|
|
loopback,
|
|
conditions.deltaStateBuffer //
|
|
);
|
|
|
|
//
|
|
CopyDeltaMa(
|
|
zIndex,
|
|
loopback,
|
|
conditions.deltaMaBuffer //
|
|
);
|
|
|
|
//
|
|
// Conditions ...
|
|
|
|
//
|
|
int cIDX = 1;
|
|
int pIDX = cIDX + 1;
|
|
int pPIDX = pIDX + 1;
|
|
|
|
//
|
|
bool isDeltaBullish = conditions.deltaBuffer[cIDX] > conditions.deltaBuffer[pIDX];
|
|
bool isDeltaBullishP = conditions.deltaBuffer[pIDX] > conditions.deltaBuffer[pPIDX];
|
|
bool isDeltaSwitchedToBullish = isDeltaBullish &&
|
|
!isDeltaBullishP;
|
|
|
|
//
|
|
bool isDeltaBearish = conditions.deltaBuffer[cIDX] < conditions.deltaBuffer[pIDX];
|
|
bool isDeltaBearishP = conditions.deltaBuffer[pIDX] < conditions.deltaBuffer[pPIDX];
|
|
bool isDeltaSwitchedToBearish = isDeltaBearish &&
|
|
!isDeltaBearishP;
|
|
|
|
//
|
|
bool isDeltaOverMa = conditions.deltaBuffer[cIDX] > conditions.deltaMaBuffer[cIDX];
|
|
bool isDeltaOverMaP = conditions.deltaBuffer[pIDX] > conditions.deltaMaBuffer[pIDX];
|
|
bool isDeltaCrossedOverMa = isDeltaOverMa &&
|
|
!isDeltaOverMaP;
|
|
|
|
//
|
|
bool isDeltaUnderMa = conditions.deltaBuffer[cIDX] < conditions.deltaMaBuffer[cIDX];
|
|
bool isDeltaUnderMaP = conditions.deltaBuffer[pIDX] < conditions.deltaMaBuffer[pIDX];
|
|
bool isDeltaCrossedUnderMa = isDeltaUnderMa &&
|
|
!isDeltaUnderMaP;
|
|
|
|
//
|
|
conditions.isDeltaBullish = isDeltaBullish;
|
|
conditions.isDeltaBearish = isDeltaBearish;
|
|
conditions.isDeltaSwitchedToBullish = isDeltaSwitchedToBullish;
|
|
conditions.isDeltaSwitchedToBearish = isDeltaSwitchedToBearish;
|
|
|
|
//
|
|
conditions.isDeltaOverMa = isDeltaOverMa;
|
|
conditions.isDeltaUnderMa = isDeltaUnderMa;
|
|
conditions.isDeltaCrossedOverMa = isDeltaCrossedOverMa;
|
|
conditions.isDeltaCrossedUnderMa = isDeltaCrossedUnderMa;
|
|
|
|
//
|
|
Cleanup();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Protected ...
|
|
protected:
|
|
//
|
|
// Private ...
|
|
private:
|
|
//
|
|
// Props ...
|
|
X121XDELTAInputs mInputs; // Inputs ...
|
|
|
|
//
|
|
// Buffers ...
|
|
double deltaBuffer[];
|
|
double deltaMaBuffer[];
|
|
double deltaStateBuffer[];
|
|
|
|
//
|
|
void Calculate(
|
|
int maxRequiredBars = 100 //
|
|
)
|
|
{
|
|
//
|
|
// Buffers ...
|
|
|
|
//
|
|
CopyBuffer(
|
|
mHandler,
|
|
X121_XDELTA_VOLUME_LINE,
|
|
0,
|
|
maxRequiredBars,
|
|
deltaBuffer
|
|
//
|
|
);
|
|
|
|
//
|
|
CopyBuffer(
|
|
mHandler,
|
|
X121_XDELTA_MA_LINE,
|
|
0,
|
|
maxRequiredBars,
|
|
deltaMaBuffer
|
|
//
|
|
);
|
|
|
|
//
|
|
CopyBuffer(
|
|
mHandler,
|
|
X121_XDELTA_STATE_LINE,
|
|
0,
|
|
maxRequiredBars,
|
|
deltaStateBuffer
|
|
//
|
|
);
|
|
}
|
|
|
|
//
|
|
void Cleanup(
|
|
int maxAllowed = 100 //
|
|
)
|
|
{
|
|
//
|
|
CleanupArray(
|
|
deltaBuffer,
|
|
maxAllowed //
|
|
);
|
|
|
|
//
|
|
CleanupArray(
|
|
deltaMaBuffer,
|
|
maxAllowed //
|
|
);
|
|
|
|
//
|
|
CleanupArray(
|
|
deltaStateBuffer,
|
|
maxAllowed //
|
|
);
|
|
}
|
|
|
|
//
|
|
};
|
|
|
|
//
|