Files
xMQL5/BKPS/Projects/XTF-EA/Libraries/x-saherelm.xtf.provider.indicators.lib.mq5
T
2024-01-25 04:09:42 +03:30

1040 lines
22 KiB
Plaintext

///////////////////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 XTF Signal Provider Indicators Library
// ----------------------------------------------------------------
//
// 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 Provider Inputs ...
#include "x-saherelm.xtf.provider.inputs.lib.mq5";
//
// START Define Enums ...
//
enum ENUM_XTF_SIGNALER_TYPES
{
X_XTF_SIGNALER_TYPE_1,
};
struct XTF_SIGNAL
{
double tp;
double sl;
double riskFree;
};
struct XTF_MARKET_CONDITIONS
{
//
// XTM ...
bool isXTMPeak;
bool isXTMVale;
bool isXTMStartBullish;
bool isXTMStartBullishAfterNeutural;
bool isXTMBullish;
bool isXTMEndBullish;
bool isXTMEndBullishByNeutural;
bool isXTMStartBearish;
bool isXTMStartBearishAfterNeutural;
bool isXTMBearish;
bool isXTMEndBearish;
bool isXTMEndBearishByNeutural;
bool isXTMNeutural;
//
// XLH ...
bool isXLHHighAttachedHH;
bool isXLHLowAttachedLL;
bool isXLHPriceCrossedOverSignal;
bool isXLHPriceOverSignal;
bool isXLHPriceCrossedUnderSignal;
bool isXLHPriceUnderSignal;
bool isXLHPriceInSignalRange;
//
// XTF ...
bool isXTFFastCrossedOverSlow;
bool isXTFFastOverSlow;
bool isXTFFastCrossedUnderSlow;
bool isXTFFastUnderSlow;
bool isXTFTrendsUp;
bool isXTFTrendsDown;
};
//
// END Define Enums ...
//
//
// Imports all Indicators Helper here ...
input group "XTF Provider Indicators";
//
#include "x-saherelm.xtm.indicator.helper.lib.mq5";
#include "x-saherelm.xlh.indicator.helper.lib.mq5";
#include "x-saherelm.xobd.indicator.helper.lib.mq5";
#include "x-saherelm.xtf.oscillator.helper.lib.mq5";
//
int xTFMaxLengthOfInputs = 0;
int xTFCalculatedBars = 0;
//
// Here we Define some Global Market Variables to Analyse MArket Conditions
// using them at each time ...
//
// XTM ...
datetime xTMPeakTimes[];
datetime xTMValeTimes[];
datetime xTMEndBullishByNeuturalTimes[];
datetime xTMEndBearishByNeuturalTimes[];
datetime xTMStartBullishAfterNeuturalTimes[];
datetime xTMStartBearishAfterNeuturalTimes[];
//
// XTF ...
datetime xTFFastCrossedOverSlowTimes[];
datetime xTFFastCrossedUnderSlowTimes[];
double xTFMaxTrend;
datetime xTFMaxTrendTime;
//
// XLH ...
datetime xLHLowAttachedLLTimes[];
datetime xLHHighAttachedHHTimes[];
datetime xLHPriceInSignalRangeTimes[];
datetime xLHPriceCrossedOverSignalTimes[];
datetime xLHPriceCrossedUnderSignalTimes[];
//
XTF_MARKET_CONDITIONS conditions = {};
//
// Init Indicators ...
bool XTFInitIndicators()
{
//
bool result = false;
//
// Check and Calculate Max Length in Inputs ...
//
//
// XTM ...
xTFMaxLengthOfInputs = MathMax(
xTMMaPeriod,
xTFNumberOfItemsPerTick);
//
// XOBD ...
xTFMaxLengthOfInputs = MathMax(
xOBDLength,
xTFMaxLengthOfInputs);
//
// XLH ...
xTFMaxLengthOfInputs = MathMax(
xLHLength,
xTFMaxLengthOfInputs);
//
// XTF ...
xTFMaxLengthOfInputs = MathMax(
xTFFastLength,
xTFMaxLengthOfInputs);
xTFMaxLengthOfInputs = MathMax(
xTFSlowLength,
xTFMaxLengthOfInputs);
//
// Initialize Indicator/Oscillator Handlers ...
ResetLastError();
//
// XTM Init ...
result = XTMInitHandlers();
if (!result)
{
return result;
}
//
// XLH Init ...
result = XLHInitHandlers();
if (!result)
{
return result;
}
//
// XTF Init ...
result = XTFInitHandlers();
if (!result)
{
return result;
}
//
// XOBD Init ...
result = XOBDInitHandlers();
if (!result)
{
return result;
}
//
ArraySetAsSeries(xTMPeakTimes, true);
ArraySetAsSeries(xTMValeTimes, true);
ArraySetAsSeries(xTMEndBullishByNeuturalTimes, true);
ArraySetAsSeries(xTMEndBearishByNeuturalTimes, true);
ArraySetAsSeries(xTMStartBullishAfterNeuturalTimes, true);
ArraySetAsSeries(xTMStartBearishAfterNeuturalTimes, true);
ArraySetAsSeries(xTFFastCrossedOverSlowTimes, true);
ArraySetAsSeries(xTFFastCrossedUnderSlowTimes, true);
ArraySetAsSeries(xLHLowAttachedLLTimes, true);
ArraySetAsSeries(xLHHighAttachedHHTimes, true);
ArraySetAsSeries(xLHPriceInSignalRangeTimes, true);
ArraySetAsSeries(xLHPriceCrossedOverSignalTimes, true);
ArraySetAsSeries(xLHPriceCrossedUnderSignalTimes, true);
//
result = true;
//
return result;
}
//
// Release Indicators ...
void XTFReleaseIndicators()
{
//
// Release Handlers ...
XTMReleaseHandlers();
XTFReleaseHandlers();
XLHReleaseHandlers();
XOBDReleaseHandlers();
}
//
// Handle Reading Buffers ...
void XTFHandleReadingBuffers()
{
//
// Reading Calculated Bars for a Simple Handler ...
xTFCalculatedBars = BarsCalculated(xTMHandler);
//
// Read Indicators Buffers ...
XTMReadBuffers(xTFNumberOfItemsPerTick);
XTFReadBuffers(xTFNumberOfItemsPerTick);
XLHReadBuffers(xTFNumberOfItemsPerTick);
XOBDReadBuffers(xTFNumberOfItemsPerTick);
//
XTFReadConditions();
}
//
//
// START Combination Functions ...
//
//
//
// Can Open Long Trade ...
// this is a Function will Check all market Conditions and get all
// Strategy requirement Verifications for Long Signals ...
bool XTFCanOpenLongTrade(
double &providedSignals, // Recieve Which Number of Providers has Conditions here, used for Volume Increasing
XTF_SIGNAL &info // Retrieve Signal Additional Info on this Object
)
{
//
bool result = false;
//
bool hasSignal = XTFHasLongSignal(
providedSignals,
info);
//
result =
//
hasSignal
//
;
//
return result;
}
//
// Can Close Long Trade ...
// this is a Function will Check all market Conditions and get all
// Strategy requirement Verifications for Long Signals ...
bool XTFCanCloseLongTrade()
{
//
bool result = false;
//
// TODO: Check Market Conditions for Close Long Trades ...
result = false;
// conditions.isXLHHighAttachedHH;
//
return result;
}
//
// Can Open Short Trade ...
// this is a Function will Check all market Conditions and get all
// Strategy requirement Verifications for Short Signals ...
bool XTFCanOpenShortTrade(
double &providedSignals, // Recieve Which Number of Providers has Conditions here, used for Volume Increasing
XTF_SIGNAL &info // Retrieve Signal Additional Info on this Object
)
{
//
bool result = false;
//
bool hasSignal = XTFHasShortSignal(
providedSignals,
info);
//
result =
//
hasSignal
//
;
//
return result;
}
//
// Can Close Short Trade ...
// this is a Function will Check all market Conditions and get all
// Strategy requirement Verifications for Short Signals ...
bool XTFCanCloseShortTrade()
{
//
bool result = false;
//
// TODO: Check Market Conditions for Close Short Trades ...
//
return result;
}
//
//
// END Combination Functions ...
//
//
//
//
// START Combination Signaling ...
//
//
//
// Read All Globally Required Conditions ...
void XTFReadConditions()
{
//
XTFClearConditions();
//
// XTM ...
conditions.isXTMPeak = XTMIsPeak();
conditions.isXTMVale = XTMIsVale();
conditions.isXTMStartBullish = XTMIsStartBullish(1);
conditions.isXTMStartBullishAfterNeutural = XTMIsStartBullishAfterNeutural(1);
conditions.isXTMBullish = XTMIsBullish(1);
conditions.isXTMEndBullish = XTMIsEndBullish(1);
conditions.isXTMEndBullishByNeutural = XTMIsEndBullishByNeutural(1);
conditions.isXTMStartBearish = XTMIsStartBearish(1);
conditions.isXTMStartBearishAfterNeutural = XTMIsStartBearishAfterNeutural(1);
conditions.isXTMBearish = XTMIsBearish(1);
conditions.isXTMEndBearish = XTMIsEndBearish(1);
conditions.isXTMEndBearishByNeutural = XTMIsEndBearishByNeutural(1);
conditions.isXTMNeutural = XTMIsNeutural(1);
//
// XLH ...
conditions.isXLHHighAttachedHH = XLHIsHighAttachedHH(1);
conditions.isXLHLowAttachedLL = XLHIsLowAttachedLL(1);
conditions.isXLHPriceCrossedOverSignal = XLHIsPriceCrossedOverSignal(1);
conditions.isXLHPriceOverSignal = XLHIsPriceOverSignal(1);
conditions.isXLHPriceCrossedUnderSignal = XLHIsPriceCrossedUnderSignal(1);
conditions.isXLHPriceUnderSignal = XLHIsPriceUnderSignal(1);
conditions.isXLHPriceInSignalRange = XLHIsPriceInSignalRange(1);
//
// XTF ...
conditions.isXTFFastCrossedOverSlow = XTFIsFastCrossedOverSlow(1);
conditions.isXTFFastOverSlow = XTFIsFastOverSlow(1);
conditions.isXTFFastCrossedUnderSlow = XTFIsFastCrossedUnderSlow(1);
conditions.isXTFFastUnderSlow = XTFIsFastUnderSlow(1);
conditions.isXTFTrendsUp = XTFIsTrendsUp(1);
conditions.isXTFTrendsDown = XTFIsTrendsDown(1);
//
datetime time = iTime(
_Symbol,
_Period,
1);
//
// XTM ...
//
if (conditions.isXTMPeak)
{
//
Add(
time,
xTMPeakTimes);
}
//
if (conditions.isXTMVale)
{
//
Add(
time,
xTMValeTimes);
}
//
if (conditions.isXTMEndBullishByNeutural)
{
//
Add(
time,
xTMEndBullishByNeuturalTimes);
}
//
if (conditions.isXTMEndBearishByNeutural)
{
//
Add(
time,
xTMEndBearishByNeuturalTimes);
}
//
if (conditions.isXTMStartBullishAfterNeutural)
{
//
Add(
time,
xTMStartBullishAfterNeuturalTimes);
}
//
if (conditions.isXTMStartBearishAfterNeutural)
{
//
Add(
time,
xTMStartBearishAfterNeuturalTimes);
}
//
// XTF ...
//
if (conditions.isXTFFastCrossedOverSlow)
{
//
Add(
time,
xTFFastCrossedOverSlowTimes);
}
//
if (conditions.isXTFFastCrossedUnderSlow)
{
//
Add(
time,
xTFFastCrossedUnderSlowTimes);
}
//
// XLH ...
//
if (conditions.isXLHHighAttachedHH)
{
//
Add(
time,
xLHHighAttachedHHTimes);
}
//
if (conditions.isXLHLowAttachedLL)
{
//
Add(
time,
xLHLowAttachedLLTimes);
}
//
if (conditions.isXLHPriceCrossedOverSignal)
{
//
Add(
time,
xLHPriceCrossedOverSignalTimes);
//
xTFMaxTrendTime = time;
xTFMaxTrend = XTFGetTrendPower(1);
}
//
if (conditions.isXLHPriceCrossedUnderSignal)
{
//
Add(
time,
xLHPriceCrossedUnderSignalTimes);
//
xTFMaxTrendTime = time;
xTFMaxTrend = XTFGetTrendPower(1);
}
//
if (conditions.isXLHPriceInSignalRange)
{
//
Add(
time,
xLHPriceInSignalRangeTimes);
}
//
if (
//
conditions.isXTFFastOverSlow ||
conditions.isXTFFastUnderSlow
//
)
{
//
double trendPower = XTFGetTrendPower(1);
if (trendPower > xTFMaxTrend)
{
//
xTFMaxTrend = trendPower;
xTFMaxTrendTime = time;
}
}
}
//
// Cleanup Conditions ...
void XTFClearConditions()
{
//
XTF_MARKET_CONDITIONS mConditions = {};
//
conditions = mConditions;
}
//
// Converts Market Conditions to Specific String Representation ...
string XTFGetConditionsString(
string separator = ",", // Specify Separator
bool ignoreFalses = true // Ignore False Conditions
)
{
//
string result = "\n";
//
// XTM ...
result +=
"| XTM |" + separator;
//
if (
!ignoreFalses ||
(ignoreFalses && conditions.isXTMPeak))
{
result += "isXTMPeak: " + (string)conditions.isXTMPeak + separator;
}
//
if (
!ignoreFalses ||
(ignoreFalses && conditions.isXTMVale))
{
result += "isXTMVale: " + (string)conditions.isXTMVale + separator;
}
//
if (
!ignoreFalses ||
(ignoreFalses && conditions.isXTMStartBullish))
{
result += "isXTMStartBullish: " + (string)conditions.isXTMStartBullish + separator;
}
//
if (
!ignoreFalses ||
(ignoreFalses && conditions.isXTMStartBullishAfterNeutural))
{
result += "isXTMStartBullishAfterNeutural: " + (string)conditions.isXTMStartBullishAfterNeutural + separator;
}
//
if (
!ignoreFalses ||
(ignoreFalses && conditions.isXTMBullish))
{
result += "isXTMBullish: " + (string)conditions.isXTMBullish + separator;
}
//
if (
!ignoreFalses ||
(ignoreFalses && conditions.isXTMEndBullish))
{
result += "isXTMEndBullish: " + (string)conditions.isXTMEndBullish + separator;
}
//
if (
!ignoreFalses ||
(ignoreFalses && conditions.isXTMEndBullishByNeutural))
{
result += "isXTMEndBullishByNeutural: " + (string)conditions.isXTMEndBullishByNeutural + separator;
}
//
if (
!ignoreFalses ||
(ignoreFalses && conditions.isXTMStartBearish))
{
result += "isXTMStartBearish: " + (string)conditions.isXTMStartBearish + separator;
}
//
if (
!ignoreFalses ||
(ignoreFalses && conditions.isXTMStartBearishAfterNeutural))
{
result += "isXTMStartBearishAfterNeutural: " + (string)conditions.isXTMStartBearishAfterNeutural + separator;
}
//
if (
!ignoreFalses ||
(ignoreFalses && conditions.isXTMBearish))
{
result += "isXTMBearish: " + (string)conditions.isXTMBearish + separator;
}
//
if (
!ignoreFalses ||
(ignoreFalses && conditions.isXTMEndBearish))
{
result += "isXTMEndBearish: " + (string)conditions.isXTMEndBearish + separator;
}
//
if (
!ignoreFalses ||
(ignoreFalses && conditions.isXTMEndBearishByNeutural))
{
result += "isXTMEndBearishByNeutural: " + (string)conditions.isXTMEndBearishByNeutural + separator;
}
//
if (
!ignoreFalses ||
(ignoreFalses && conditions.isXTMNeutural))
{
result += "isXTMNeutural: " + (string)conditions.isXTMNeutural + separator;
}
//
// Separator ...
result += separator;
//
// XTF ...
result +=
"| XTF |" + separator;
//
if (
!ignoreFalses ||
(ignoreFalses && conditions.isXTFFastCrossedOverSlow))
{
result += "isXTFFastCrossedOverSlow: " + (string)conditions.isXTFFastCrossedOverSlow + separator;
}
//
if (
!ignoreFalses ||
(ignoreFalses && conditions.isXTFFastOverSlow))
{
result += "isXTFFastOverSlow: " + (string)conditions.isXTFFastOverSlow + separator;
}
//
if (
!ignoreFalses ||
(ignoreFalses && conditions.isXTFFastCrossedUnderSlow))
{
result += "isXTFFastCrossedUnderSlow: " + (string)conditions.isXTFFastCrossedUnderSlow + separator;
}
//
if (
!ignoreFalses ||
(ignoreFalses && conditions.isXTFFastUnderSlow))
{
result += "isXTFFastUnderSlow: " + (string)conditions.isXTFFastUnderSlow + separator;
}
//
if (
!ignoreFalses ||
(ignoreFalses && conditions.isXTFTrendsUp))
{
result += "isXTFTrendsUp: " + (string)conditions.isXTFTrendsUp + separator;
}
//
if (
!ignoreFalses ||
(ignoreFalses && conditions.isXTFTrendsDown))
{
result += "isXTFTrendsDown: " + (string)conditions.isXTFTrendsDown + separator;
}
//
// Separator ...
result += separator;
//
// XLH ...
result +=
"| XLH |" + separator;
//
if (
!ignoreFalses ||
(ignoreFalses && conditions.isXLHHighAttachedHH))
{
result += "isXLHHighAttachedHH: " + (string)conditions.isXLHHighAttachedHH + separator;
}
//
if (
!ignoreFalses ||
(ignoreFalses && conditions.isXLHLowAttachedLL))
{
result += "isXLHLowAttachedLL: " + (string)conditions.isXLHLowAttachedLL + separator;
}
//
if (
!ignoreFalses ||
(ignoreFalses && conditions.isXLHPriceCrossedOverSignal))
{
result += "isXLHPriceCrossedOverSignal: " + (string)conditions.isXLHPriceCrossedOverSignal + separator;
}
//
if (
!ignoreFalses ||
(ignoreFalses && conditions.isXLHPriceOverSignal))
{
result += "isXLHPriceOverSignal: " + (string)conditions.isXLHPriceOverSignal + separator;
}
//
if (
!ignoreFalses ||
(ignoreFalses && conditions.isXLHPriceCrossedUnderSignal))
{
result += "isXLHPriceCrossedUnderSignal: " + (string)conditions.isXLHPriceCrossedUnderSignal + separator;
}
//
if (
!ignoreFalses ||
(ignoreFalses && conditions.isXLHPriceUnderSignal))
{
result += "isXLHPriceUnderSignal: " + (string)conditions.isXLHPriceUnderSignal + separator;
}
//
if (
!ignoreFalses ||
(ignoreFalses && conditions.isXLHPriceInSignalRange))
{
result += "isXLHPriceInSignalRange: " + (string)conditions.isXLHPriceInSignalRange + separator;
}
//
// Separator ...
result += separator;
//
return result;
}
//
// Check Market Conditions by Combination of Power of All tools for LONGs ...
bool XTFHasLongSignal(
double &providedSignals, // Recieve Which Number of Providers has Conditions here, used for Volume Increasing
XTF_SIGNAL &info // Retrieve Signal Additional Info on this Object
)
{
//
providedSignals = 0;
//
bool result = false;
//
// START Custom Mechanism ...
//
//
// Required Data ...
double entry = GetEntry(X_SIGNAL_LONG);
double nearHH = GetHighestHigh(3, 1);
double nearLL = GetLowestLow(3, 1);
XOHCL candle1 = GetCandle(1);
//
bool isSharpChanged = IsSharpChanged(
5,
1,
xTMMaBuffer
//
);
//
bool isCandleSharpChanged = IsSharpChanged(
5,
1,
PRICE_CLOSE
//
);
//
double xTFTrendPower1 = XTFGetTrendPower(1);
bool isXTFMaxTrend = xTFTrendPower1 == xTFMaxTrend;
//
bool customMethodSignal =
//
// Method 1 ...
conditions.isXLHLowAttachedLL
//
&&
//
conditions.isXTFFastOverSlow
//
&&
//
conditions.isXTFTrendsDown
//
// &&
// //
// isXTFMaxTrend
//
&&
//
!isCandleSharpChanged
//
// &&
// //
// (
// //
// conditions.isXTMBearish
// //
// ?
// //
// (
// //
// !conditions.isXTFTrendsUp
// //
// )
// //
// :
// //
// true
// //
// )
//
// Method 2 ...
// conditions.isXTFFastCrossedOverSlow
//
;
//
if (customMethodSignal)
{
//
providedSignals++;
providedSignals++;
//
// double spread = GetSpread();
// double entry = GetEntry(X_SIGNAL_LONG);
// double tpPrice = xLHHHBuffer[1] - entry;
// double slPrice = entry - (xLHLLBuffer[1] - (2 * spread));
double tp = 0; // tpPrice;
double sl = 0; // slPrice;
//
// Fill Signal additional Info ...
info.tp = tp;
info.sl = sl;
}
else
{
//
// Clear Signal additional Info ...
info.tp = 0;
info.sl = 0;
}
//
// END Custom Mechanism ...
//
//
result =
//
customMethodSignal
//
;
//
// Logging Conditions ...
if (result)
{
//
string msg = XTFGetConditionsString("\n");
LogMessage(msg);
}
//
return result;
}
//
// Check Market Conditions by Combination of Power of All tools for SHORTs ...
bool XTFHasShortSignal(
double &providedSignals, // Recieve Which Number of Providers has Conditions here, used for Volume Increasing
XTF_SIGNAL &info // Retrieve Signal Additional Info on this Object
)
{
//
providedSignals = 0;
//
bool result = false;
//
// START Custom Mechanism ...
//
//
// Required Data ...
double entry = GetEntry(X_SIGNAL_SHORT);
double nearHH = GetHighestHigh(3, 1);
double nearLL = GetLowestLow(3, 1);
XOHCL candle1 = GetCandle(1);
//
bool isSharpChanged = IsSharpChanged(
5,
1,
xTMMaBuffer
//
);
//
bool isCandleSharpChanged = IsSharpChanged(
5,
1,
PRICE_CLOSE
//
);
// //
// if () {
// providedSignals++;
// }
//
// END Custom Mechanism ...
//
//
// Fill Result ...
result =
//
false
//
;
//
// Logging Conditions ...
if (result)
{
//
string msg = XTFGetConditionsString("\n");
LogMessage(msg);
}
//
return result;
}
//
//
// END Combination Signaling ...
//
//