From d4b6e6df06d0482bf7123012861dd9f299613eb7 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Tue, 23 Jul 2024 12:07:06 +0330 Subject: [PATCH] last ... --- .../Strategies/x-saherelm.xtest.strategy.mq5 | 1047 +++++++++++++++++ Experts/x-test-strategies.ea.mq5 | 77 +- Strategies/x-saherelm.xdtd.strategy.mq5 | 875 ++++++++++++++ 3 files changed, 1988 insertions(+), 11 deletions(-) create mode 100644 Documents/BKP/Old/Strategies/x-saherelm.xtest.strategy.mq5 create mode 100644 Strategies/x-saherelm.xdtd.strategy.mq5 diff --git a/Documents/BKP/Old/Strategies/x-saherelm.xtest.strategy.mq5 b/Documents/BKP/Old/Strategies/x-saherelm.xtest.strategy.mq5 new file mode 100644 index 00000000..99defb20 --- /dev/null +++ b/Documents/BKP/Old/Strategies/x-saherelm.xtest.strategy.mq5 @@ -0,0 +1,1047 @@ +/////////////////////////////////////////////////////// +// +// SaherElm IT Center MQL5 Class Library +// -------------------------------------- +// Name: XSCXTestStrategy +// Description: provides all based classes for use ... +// +// +// 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 ... + +// +// Import(s) Helper(s) ... +#include "../Helpers/x-saherelm.xhk.helper.mq5" +#include "../Helpers/x-saherelm.xmc.helper.mq5" +#include "../Helpers/x-saherelm.xtd.helper.mq5" +#include "../Helpers/x-saherelm.xpv.helper.mq5" +#include "../Helpers/x-saherelm.xche.helper.mq5" +#include "../Helpers/x-saherelm.xvlm.helper.mq5" +#include "../Helpers/x-saherelm.xosc.helper.mq5" +#include "../Helpers/x-saherelm.xchma.helper.mq5" + +// +// Base Classes ... +#include "../Classes/x-saherelm.xstrategy.class.mq5" + +// +// Specific Identifier(s) of XStrategy ... +string XTestStartegyToken = "XTest"; + +// +// XStrategy Class Implementation(s) ... +class XSCXTestStrategy : public XSCBaseStrategy +{ + // + // Public ... +public: + // + // Constructor(s) ... + void XSCXTestStrategy( + // + // Base ... + string _symbol, // Trading Symbol + ENUM_TIMEFRAMES _period, // Trading TimeFrame + double _volume, // Voluem + double _r2r, // Risk/Reward Ratio + int _slippage, // Trader Slippage + long _magicNumber, // Trader Magic Number + bool _ignoreSignalExecution = false, // Ignore Signal Execution + bool _useTPSLAsPoint = false, // Converts TP and SL by Provided Points + bool _forceMaxTPSLAsPoint = false, // Force Max TP and SL by Provided Points + double _tpPoint = 0, // TP As Point + double _slPoint = 0, // SL As Point + bool _ignoreTP = false, // TP set to 0 + bool _ignoreSL = false, // SL set to 0 + bool _allowLong = true, + bool _allowShort = true, + int _maxAllowedLongs = 0, + int _maxAllowedShorts = 0 // + ) : XSCBaseStrategy(_symbol, + _period, + _volume, + _r2r, + _slippage, + _magicNumber, + _ignoreSignalExecution, + _useTPSLAsPoint, + _forceMaxTPSLAsPoint, + _tpPoint, + _slPoint, + _ignoreTP, + _ignoreSL, + _allowLong, + _allowShort, + _maxAllowedLongs, + _maxAllowedShorts // + ) + { + // + SetAlertPrefix(GetTag()); + + // + PrepareRequirements(); + } + + // + // Deconstructor ... + void ~XSCXTestStrategy() + { + // + delete mcHelper; + delete pvHelper; + delete tdHelper; + delete hkHelper; + delete oscHelper; + delete vlmHelper; + delete cheHelper; + delete chmaHelper; + } + + // + // Getter / Setter (s) ... + + // + // Overrides ... + + // + // Customize Strategy Identifier ... + string GetTag() override + { + return XTestStartegyToken; + } + + // + // Check Conditions For Signal ... + bool HasSignal( + XSignal &signal // + ) override + { + // + bool result = false; + + // + signal.Clean(); + + // + double sl = 0; + double tp = 0; + double risk = 0; + double entry = 0; + double reward = 0; + double r2r = R2R(); + ENUM_POSITION_TYPE type = POSITION_TYPE_BUY; + ENUM_X_ORDER_MODES mode = X_ORDER_MODE_MARKET; + + // + bool hasLong = false; + bool hasShort = false; + + // + // Retrieve Conditions ... + + // + // XMC ... + XMCConditions mcConditions; + result = mcHelper.GetConditions(mcConditions); + if (!result) + { + return result; + } + + // + // XPV ... + XPVConditions pvConditions; + result = pvHelper.GetConditions(pvConditions); + if (!result) + { + return result; + } + + // + // XTD ... + XTDConditions tdConditions; + result = tdHelper.GetConditions(tdConditions); + if (!result) + { + return result; + } + + // + // XHK ... + XHKConditions hkConditions; + result = hkHelper.GetConditions(hkConditions); + if (!result) + { + return result; + } + + // + // XVLM ... + XVLMConditions vlmConditions; + result = vlmHelper.GetConditions(vlmConditions); + if (!result) + { + return result; + } + + // + // XCHMA ... + XCHMAConditions chmaConditions; + result = chmaHelper.GetConditions(chmaConditions); + if (!result) + { + return result; + } + + // + // XCHE ... + XCHEConditions cheConditions; + result = cheHelper.GetConditions(cheConditions); + if (!result) + { + return result; + } + + // + double atr = oscHelper.GetATR(1); + + // + double rsis[]; + ArraySetAsSeries(rsis, true); + oscHelper.CopyRSI( + 0, + 3, + rsis // + ); + + // + // Start Calculations ... + + // + double points = GetPoints(mSymbol); + + // + int zIndex = 0; + int cIndex = zIndex + 1; + int pIndex = cIndex + 1; + int ppIndex = pIndex + 1; + + // + // Retrieve Bars ... + + // + // Last Bar ... + XOHCL cBar; + result = cBar.Init( + mSymbol, + mPeriod, + cIndex // + ); + if (!result) + { + return result; + } + + // + // Previous Bar of Last Bar ... + XOHCL pBar; + result = cBar.GetPreviousBar(pBar); + if (!result) + { + return result; + } + + // + // Last Swing Low Bar ... + XOHCL lastSwingLow; + result = cBar.FindLastSwingLow(lastSwingLow); + if (!result) + { + return result; + } + + // + // Last Swing High Bar ... + XOHCL lastSwingHigh; + result = cBar.FindLastSwingHigh(lastSwingHigh); + if (!result) + { + return result; + } + + // + // Retrieve Highest High(s) and Lowest Low(s) ... + + // + // Highest High ... + double hh5 = cBar.FindHighest( + 5, + MODE_HIGH // + ); + + // + // Lowest Low ... + double ll5 = cBar.FindLowest( + 5, + MODE_LOW // + ); + + // + // Check Support Exists or Find ... + bool hasSupport = support.IsValid(); + if (!hasSupport) + { + // + hasSupport = + // + cBar.HasSupport(support) + // + ; + + // + if (hasSupport) + { + // + DrawSupport( + support, + ChartID() // + ); + + // + supportTime = cBar.time; + } + } + + // + // Check Resistance Exists or Find ... + bool hasResistance = resistance.IsValid(); + if (!hasResistance) + { + // + hasResistance = + // + cBar.HasResistance(resistance) + // + ; + + // + if (hasResistance) + { + // + DrawResistance( + resistance, + ChartID() // + ); + + // + resistanceTime = cBar.time; + } + } + + // + // For Trend ... + XOHCL tmpHSW[]; + XOHCL tmpLSW[]; + + // + // Detect Bullish/Bearish Patterns and Trends ... + + // + // Bullish ... + bool hasBullishPattern = HasBullishPattern(cBar); + bool isBullishTrend = cBar.HasBullishTrend( + tmpHSW, + tmpLSW, + true // + ); + + // + // Bearish ... + bool hasBearishPattern = HasBearishPattern(cBar); + bool isBearishTrend = cBar.HasBearishTrend( + tmpHSW, + tmpLSW, + false // + ); + + // + // Detect XCHMA based Conditions ... + + // + // Bullish ... + + // + bool isHCrossedOver = + // + ( + // + chmaConditions.isHFastCrossedOverSlow && + chmaConditions.isLFastOverSlow && + chmaConditions.isMFastOverSlow && + chmaConditions.isSFastOverSlow + // + ) + // + ; + + // + bool isLCrossedOver = + // + ( + // + chmaConditions.isLFastCrossedOverSlow && + chmaConditions.isHFastOverSlow && + chmaConditions.isMFastOverSlow && + chmaConditions.isSFastOverSlow + // + ) + // + ; + + // + bool isMCrossedOver = + // + ( + // + chmaConditions.isMFastCrossedOverSlow && + chmaConditions.isHFastOverSlow && + chmaConditions.isLFastOverSlow && + chmaConditions.isSFastOverSlow + // + ) + // + ; + + // + bool isSCrossedOver = + // + ( + // + chmaConditions.isSFastCrossedOverSlow && + chmaConditions.isHFastOverSlow && + chmaConditions.isLFastOverSlow && + chmaConditions.isMFastOverSlow + // + ) + // + ; + + // + // Bullish XCHMA Conditions ... + bool isCrossedOver = + // + isHCrossedOver + // + || + // + isLCrossedOver + // + || + // + isMCrossedOver + // + || + // + isSCrossedOver + // + ; + + // + // Bearish ... + + // + bool isHCrossedUnder = + // + ( + // + chmaConditions.isHFastCrossedUnderSlow && + chmaConditions.isLFastUnderSlow && + chmaConditions.isMFastUnderSlow && + chmaConditions.isSFastUnderSlow + // + ) + // + ; + + // + bool isLCrossedUnder = + // + ( + // + chmaConditions.isLFastCrossedUnderSlow && + chmaConditions.isHFastUnderSlow && + chmaConditions.isMFastUnderSlow && + chmaConditions.isSFastUnderSlow + // + ) + // + ; + + // + bool isMCrossedUnder = + // + ( + // + chmaConditions.isMFastCrossedUnderSlow && + chmaConditions.isHFastUnderSlow && + chmaConditions.isLFastUnderSlow && + chmaConditions.isSFastUnderSlow + // + ) + // + ; + + // + bool isSCrossedUnder = + // + ( + // + chmaConditions.isSFastCrossedUnderSlow && + chmaConditions.isHFastUnderSlow && + chmaConditions.isLFastUnderSlow && + chmaConditions.isMFastUnderSlow + // + ) + // + ; + + // + // Bullish XCHMA Conditions ... + bool isCrossedUnder = + // + isHCrossedUnder + // + || + // + isLCrossedUnder + // + || + // + isMCrossedUnder + // + || + // + isSCrossedUnder + // + ; + + // + // XCHE Conditions ... + + // + bool isCHESwitchedToStrongBullish = cheConditions.isStrongSwitchedToBullish; + + // + bool isCHESwitchedToStrongBearish = cheConditions.isStrongSwitchedToBearish; + + // + // Signalling ... + // Looking for Signals Based on Conditions ... + + // + // Long ... + if (isCrossedOver) + { + // + hasLong = + // + (isBullishTrend || + hasBullishPattern) + // + && + // + chmaConditions.isFastOverSlow && + chmaConditions.isCLoseOverFast && + chmaConditions.isFastSupport && + chmaConditions.isSlowSupport && + cheConditions.isStrongBullish + // + ; + + // + if (hasLong) + { + // + type = POSITION_TYPE_BUY; + sl = cheConditions.long2[1]; + } + } + + // + // Long ... + if (isCHESwitchedToStrongBullish) + { + // + hasLong = + // + // isBullishTrend && + // cBar.IsBullish() && + hasBullishPattern && + hkConditions.isSMHKBullish && + chmaConditions.isFastOverSlow + // + ; + + // + if (hasLong) + { + // + type = POSITION_TYPE_BUY; + sl = cheConditions.long1[1]; + } + } + + // + // Short ... + if (isCrossedUnder) + { + // + hasShort = + // + (isBearishTrend || + hasBearishPattern) + // + && + // + chmaConditions.isFastUnderSlow && + chmaConditions.isCloseUnderFast && + chmaConditions.isFastResistance && + chmaConditions.isSlowResistance && + cheConditions.isStrongBearish + // + ; + + // + if (hasShort) + { + // + type = POSITION_TYPE_SELL; + sl = cheConditions.short2[1]; + } + } + + // + // Short ... + if (isCHESwitchedToStrongBearish) + { + // + hasShort = + // + // isBearishTrend && + // cBar.IsBearish() && + hasBearishPattern && + hkConditions.isSMHKBearish && + chmaConditions.isFastUnderSlow + // + ; + + // + if (hasLong) + { + // + type = POSITION_TYPE_BUY; + sl = cheConditions.long1[1]; + } + } + + // + // Preparing Signals for Execution ... + + // + result = hasLong || hasShort; + if (!result) + { + // + CheckSupportValidation(); + CheckResistanceValidation(); + return result; + } + + // + if (hasSupport) + { + // + supportTime = NULL; + isSupportTested = false; + RemoveSupport(support); + support.Clean(); + } + + // + if (hasResistance) + { + // + resistanceTime = NULL; + isResistanceTested = false; + RemoveResistance(resistance); + resistance.Clean(); + } + + // + // Retrieve Entry Price based on Position Type ... + entry = GetEntry(mSymbol, type); + + // + // Calculate TP and SL ... + // based on Strategy ... + CalculateTPSLByPrice( + sl, + tp, + type, + entry, + r2r // + ); + + // + // Preparing Signal ... + result = signal.Prepare( + mSymbol, + GetTag(), + mPeriod, + type, + mode, + entry, + mVolume, + sl, + tp // + ); + + // + return result; + } + + // + // Protected ... +protected: + // + // Props ... + + // + XOHCL support; + XOHCL resistance; + + // + // XMC ... + XMCInputs mcInputs; + XSCXMCHelper *mcHelper; + + // + // XTD ... + XTDInputs tdInputs; + XSCXTDHelper *tdHelper; + + // + // XPV ... + XPVInputs pvInputs; + XSCXPVHelper *pvHelper; + + // + // XOSC ... + XOSCInputs oscInputs; + XSCXOSCHelper *oscHelper; + + // + // XVLM ... + XVLMInputs vlmInputs; + XSCXVLMHelper *vlmHelper; + + // + // XHK ... + XHKInputs hkInputs; + XSCXHKHelper *hkHelper; + + // + // XCHMA ... + XCHMAInputs chmaInputs; + XSCXCHMAHelper *chmaHelper; + + // + // XCHE ... + XCHEInputs cheInputs; + XSCXCHEHelper *cheHelper; + + // + // Private ... +private: + // + // Props ... + + // + void PrepareRequirements() + { + // + support.Clean(); + supportTime = NULL; + isSupportTested = false; + + // + resistance.Clean(); + resistanceTime = NULL; + isResistanceTested = false; + + // + // Prepare XTD ... + tdInputs.Default(); + + // + tdHelper = new XSCXTDHelper(); + tdHelper.Init( + mSymbol, + mPeriod, + tdInputs // + ); + + // + // Preparing XHK ... + hkInputs.Default(); + hkInputs.smoothingLength = 14; + + // + hkInputs.drawHikenAshi = false; + hkInputs.drawSmoothedHikenAshi = true; + + // + hkHelper = new XSCXHKHelper(); + hkHelper.Init( + mSymbol, + mPeriod, + hkInputs // + ); + + // + // Preparing XMC ... + mcInputs.Default(); + + // + mcInputs.showSar = false; + mcInputs.showFastMa = false; + mcInputs.showSlowMa = false; + mcInputs.showVerifierMa = false; + + // + mcHelper = new XSCXMCHelper(); + mcHelper.Init( + mSymbol, + mPeriod, + mcInputs // + ); + + // + // Preparing XPV ... + pvInputs.Default(); + + // + pvInputs.showPeaksAndVales = false; + pvInputs.showLevels = false; + pvInputs.showConsolidations = false; + pvInputs.showFibo1Levels = false; + pvInputs.showFibo2Levels = false; + pvInputs.showFibo3Levels = false; + pvInputs.showFibo4Levels = false; + pvInputs.showFibo5Levels = false; + + // + pvHelper = new XSCXPVHelper(); + pvHelper.Init( + mSymbol, + mPeriod, + pvInputs // + ); + + // + // Preparing XOSC ... + oscInputs.Default(); + oscHelper = new XSCXOSCHelper(); + oscHelper.Init( + mSymbol, + mPeriod, + oscInputs // + ); + + // + // Preparing XVLM ... + vlmInputs.Default(); + vlmHelper = new XSCXVLMHelper(); + vlmHelper.Init( + mSymbol, + mPeriod, + vlmInputs // + ); + + // + chmaInputs.Default(); + + // + chmaInputs.showFast = true; + chmaInputs.showSlow = true; + chmaInputs.showCCFast = false; + chmaInputs.showCCSlow = false; + chmaInputs.showSCFast = false; + chmaInputs.showSCSlow = false; + chmaInputs.showMCFast = false; + chmaInputs.showMCSlow = false; + chmaInputs.showLCFast = false; + chmaInputs.showLCSlow = false; + chmaInputs.showHCFast = false; + chmaInputs.showHCSlow = false; + + // + chmaHelper = new XSCXCHMAHelper(); + chmaHelper.Init( + mSymbol, + mPeriod, + chmaInputs // + ); + + // + cheInputs.Default(); + + // + cheInputs.showLongExit1Line = true; + cheInputs.showShortExit1Line = true; + cheInputs.showLongExit2Line = true; + cheInputs.showShortExit2Line = true; + + // + cheHelper = new XSCXCHEHelper(); + cheHelper.Init( + mSymbol, + mPeriod, + cheInputs // + ); + } + + // + bool isSupportTested; + datetime supportTime; + void CheckSupportValidation() + { + // + if (!isSupportTested) + { + // + if (support.IsValid() && IsValid(supportTime)) + { + // + int age = (int)(((int)(TimeToSeconds(TimeCurrent()) - TimeToSeconds(supportTime))) / PeriodSeconds(mPeriod)); + if (age >= 50) + { + // + isSupportTested = false; + supportTime = NULL; + RemoveSupport(support); + support.Clean(); + } + } + return; + } + + // + XOHCL cBar; + bool isInited = cBar.Init( + mSymbol, + mPeriod, + 1 // + ); + if (!isInited) + { + return; + } + + // + double points = GetPoints(mSymbol); + + // + // Find Price Distance ... + double highDiff = MathAbs(cBar.high - support.high); + double lowDiff = MathAbs(cBar.low - support.high); + + // + bool isBreaked = MathMax(highDiff, lowDiff) >= 100 * points; + if (!isBreaked) + { + return; + } + + // + isSupportTested = false; + RemoveSupport(support); + supportTime = NULL; + support.Clean(); + } + + // + bool isResistanceTested; + datetime resistanceTime; + void CheckResistanceValidation() + { + // + if (!isResistanceTested) + { + // + if (resistance.IsValid() && IsValid(resistanceTime)) + { + // + int age = (int)(((int)(TimeToSeconds(TimeCurrent()) - TimeToSeconds(resistanceTime))) / PeriodSeconds(mPeriod)); + if (age >= 50) + { + // + isResistanceTested = false; + resistanceTime = NULL; + RemoveResistance(resistance); + resistance.Clean(); + } + } + return; + } + + // + XOHCL cBar; + bool isInited = cBar.Init( + mSymbol, + mPeriod, + 1 // + ); + if (!isInited) + { + return; + } + + // + double points = GetPoints(mSymbol); + + // + // Find Price Distance ... + double highDiff = MathAbs(cBar.high - resistance.low); + double lowDiff = MathAbs(cBar.low - resistance.low); + + // + bool isBreaked = MathMax(highDiff, lowDiff) >= 100 * points; + if (!isBreaked) + { + return; + } + + // + isResistanceTested = false; + RemoveResistance(resistance); + resistanceTime = NULL; + resistance.Clean(); + } +}; + +// \ No newline at end of file diff --git a/Experts/x-test-strategies.ea.mq5 b/Experts/x-test-strategies.ea.mq5 index bce27dc6..5fb61746 100644 --- a/Experts/x-test-strategies.ea.mq5 +++ b/Experts/x-test-strategies.ea.mq5 @@ -28,6 +28,7 @@ #include "../Helpers/x-saherelm.xcc.helper.mq5" // +#include "../Strategies/x-saherelm.xdtd.strategy.mq5" #include "../Strategies/x-saherelm.xtest.strategy.mq5" // @@ -45,8 +46,8 @@ int eaMaxAllowedLongs = 0; // Max Allowed Long Positions int eaMaxAllowedShorts = 0; // Max Allowed Short Positions // -double eaTPPoint = 200; // Static TP Point -double eaSLPoint = 100; // Static SL Point +input double eaTPPoint = 200; // Static TP Point +input double eaSLPoint = 100; // Static SL Point // bool eaIgnoreTP = false; // Ignore TP @@ -246,7 +247,7 @@ bool InitialEA() // // Configure Hedging ... - eaTradeHandler.AllowHedging(true); + eaTradeHandler.AllowHedging(false); eaTradeHandler.HedgeVolumeFactor(eaVolume); eaTradeHandler.HedgeMinProfitPerVolumeFactor(0.2); @@ -272,11 +273,8 @@ bool InitialEA() bool useXAUUSD = false; // - bool useXSRBR = false; - bool useXEMRNG = false; - bool useXOBVG = false; - bool useXPVFMC = false; - bool useXTest = true; + bool useXTest = false; + bool useXDTD = false; // // EURUSD ... @@ -287,6 +285,37 @@ bool InitialEA() // MAGIC NUMBER ... long magicNumber = eaMagicNumber + 1; + // + // XDTD ... + if (useXDTD) + { + // + XSCXDTDStrategy *eurusdXTDTStrategy; + eurusdXTDTStrategy = new XSCXDTDStrategy( + eurusdSymbol, + PERIOD_M15, + eaVolume, + eaR2R, // R2R ... + eaSlippage, + magicNumber, + true, // Ignore Signal Execution + true, // Use TP SL Point + false, // Use Max + eaTPPoint, // TP + eaSLPoint, // SL + eaIgnoreTP, + eaIgnoreSL, + eaAllowLong, + eaAllowShort, + eaMaxAllowedLongs, // Max Longs + eaMaxAllowedShorts // Max Shorts + ); + eurusdXTDTStrategy.SetAlertLogAlerts(true); + eurusdXTDTStrategy.SetAlertEnableAlerts(true); + eurusdXTDTStrategy.AddSignalEventHandler(OnSignalRecieved); + RegisterStrategy(eurusdXTDTStrategy); + } + // // XTest ... if (useXTest) @@ -300,9 +329,9 @@ bool InitialEA() eaR2R, // R2R ... eaSlippage, magicNumber, - true, // Ignore Signal Execution - true, // Use TP SL Point - false, // Use Max + true, // Ignore Signal Execution + true, // Use TP SL Point + false, // Use Max eaTPPoint, // TP eaSLPoint, // SL eaIgnoreTP, @@ -319,6 +348,32 @@ bool InitialEA() } } + // + XSCXDTDStrategy *xTDTStrategy; + xTDTStrategy = new XSCXDTDStrategy( + _Symbol, + _Period, + eaVolume, + eaR2R, // R2R ... + eaSlippage, + eaMagicNumber, + true, // Ignore Signal Execution + true, // Use TP SL Point + false, // Use Max + eaTPPoint, // TP + eaSLPoint, // SL + eaIgnoreTP, + eaIgnoreSL, + eaAllowLong, + eaAllowShort, + eaMaxAllowedLongs, // Max Longs + eaMaxAllowedShorts // Max Shorts + ); + xTDTStrategy.SetAlertLogAlerts(true); + xTDTStrategy.SetAlertEnableAlerts(true); + xTDTStrategy.AddSignalEventHandler(OnSignalRecieved); + RegisterStrategy(xTDTStrategy); + // return result; } diff --git a/Strategies/x-saherelm.xdtd.strategy.mq5 b/Strategies/x-saherelm.xdtd.strategy.mq5 new file mode 100644 index 00000000..e3793a4b --- /dev/null +++ b/Strategies/x-saherelm.xdtd.strategy.mq5 @@ -0,0 +1,875 @@ +/////////////////////////////////////////////////////// +// +// SaherElm IT Center MQL5 Class Library +// -------------------------------------- +// Name: XSCXDTDStrategy +// Description: provides all based classes for use ... +// +// +// 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 ... + +// +// Import(s) Helper(s) ... +#include "../Helpers/x-saherelm.xhk.helper.mq5" +#include "../Helpers/x-saherelm.xpv.helper.mq5" +#include "../Helpers/x-saherelm.xtd.helper.mq5" +#include "../Helpers/x-saherelm.xdon.helper.mq5" +#include "../Helpers/x-saherelm.xvlm.helper.mq5" +#include "../Helpers/x-saherelm.xosc.helper.mq5" + +// +// Base Classes ... +#include "../Classes/x-saherelm.xstrategy.class.mq5" + +// +// Specific Identifier(s) of XStrategy ... +string XDTDStartegyToken = "XDTD"; + +// +// XStrategy Class Implementation(s) ... +class XSCXDTDStrategy : public XSCBaseStrategy +{ + // + // Public ... +public: + // + // Constructor(s) ... + void XSCXDTDStrategy( + // + // Base ... + string _symbol, // Trading Symbol + ENUM_TIMEFRAMES _period, // Trading TimeFrame + double _volume, // Voluem + double _r2r, // Risk/Reward Ratio + int _slippage, // Trader Slippage + long _magicNumber, // Trader Magic Number + bool _ignoreSignalExecution = false, // Ignore Signal Execution + bool _useTPSLAsPoint = false, // Converts TP and SL by Provided Points + bool _forceMaxTPSLAsPoint = false, // Force Max TP and SL by Provided Points + double _tpPoint = 0, // TP As Point + double _slPoint = 0, // SL As Point + bool _ignoreTP = false, // TP set to 0 + bool _ignoreSL = false, // SL set to 0 + bool _allowLong = true, + bool _allowShort = true, + int _maxAllowedLongs = 0, + int _maxAllowedShorts = 0 // + ) : XSCBaseStrategy(_symbol, + _period, + _volume, + _r2r, + _slippage, + _magicNumber, + _ignoreSignalExecution, + _useTPSLAsPoint, + _forceMaxTPSLAsPoint, + _tpPoint, + _slPoint, + _ignoreTP, + _ignoreSL, + _allowLong, + _allowShort, + _maxAllowedLongs, + _maxAllowedShorts // + ) + { + // + SetAlertPrefix(GetTag()); + + // + PrepareRequirements(); + } + + // + // Deconstructor ... + void ~XSCXDTDStrategy() + { + // + delete pvHelper; + delete tdHelper; + delete hkHelper; + delete oscHelper; + delete vlmHelper; + delete donHelper; + } + + // + // Getter / Setter (s) ... + + // + // Overrides ... + + // + // Customize Strategy Identifier ... + string GetTag() override + { + return XDTDStartegyToken; + } + + // + // Check Conditions For Signal ... + bool HasSignal( + XSignal &signal // + ) override + { + // + bool result = false; + + // + signal.Clean(); + + // + double sl = 0; + double tp = 0; + double risk = 0; + double entry = 0; + double reward = 0; + double r2r = R2R(); + ENUM_POSITION_TYPE type = POSITION_TYPE_BUY; + ENUM_X_ORDER_MODES mode = X_ORDER_MODE_MARKET; + + // + bool hasLong = false; + bool hasShort = false; + + // + // Retrieve Conditions ... + + // + // XPV ... + XPVConditions pvConditions; + result = pvHelper.GetConditions(pvConditions); + if (!result) + { + return result; + } + + // + // XTD ... + XTDConditions tdConditions; + result = tdHelper.GetConditions(tdConditions); + if (!result) + { + return result; + } + + // + // XHK ... + XHKConditions hkConditions; + result = hkHelper.GetConditions(hkConditions); + if (!result) + { + return result; + } + + // + // XVLM ... + XVLMConditions vlmConditions; + result = vlmHelper.GetConditions(vlmConditions); + if (!result) + { + return result; + } + + // + // XDON ... + + // + // Open ... + double upperOs[]; + double lowerOs[]; + ArraySetAsSeries(upperOs, true); + ArraySetAsSeries(lowerOs, true); + donHelper.CopyUpperO( + 0, + 5, + upperOs // + ); + donHelper.CopyLowerO( + 0, + 5, + lowerOs // + ); + + // + // Close ... + double upperCs[]; + double lowerCs[]; + ArraySetAsSeries(upperCs, true); + ArraySetAsSeries(lowerCs, true); + donHelper.CopyUpperC( + 0, + 5, + upperCs // + ); + donHelper.CopyLowerC( + 0, + 5, + lowerCs // + ); + + // + // High ... + double upperHs[]; + double lowerHs[]; + ArraySetAsSeries(upperHs, true); + ArraySetAsSeries(lowerHs, true); + donHelper.CopyUpperH( + 0, + 5, + upperHs // + ); + donHelper.CopyLowerH( + 0, + 5, + lowerHs // + ); + + // + // Low ... + double upperLs[]; + double lowerLs[]; + ArraySetAsSeries(upperLs, true); + ArraySetAsSeries(lowerLs, true); + donHelper.CopyUpperL( + 0, + 5, + upperLs // + ); + donHelper.CopyLowerL( + 0, + 5, + lowerLs // + ); + + // + double atr = oscHelper.GetATR(1); + + // + double rsis[]; + ArraySetAsSeries(rsis, true); + oscHelper.CopyRSI( + 0, + 3, + rsis // + ); + + // + // Start Calculations ... + + // + double points = GetPoints(mSymbol); + + // + int zIndex = 0; + int cIndex = zIndex + 1; + int pIndex = cIndex + 1; + int ppIndex = pIndex + 1; + + // + // Retrieve Bars ... + + // + // Last Bar ... + XOHCL cBar; + result = cBar.Init( + mSymbol, + mPeriod, + cIndex // + ); + if (!result) + { + return result; + } + + // + // Previous Bar of Last Bar ... + XOHCL pBar; + result = cBar.GetPreviousBar(pBar); + if (!result) + { + return result; + } + + // + // Last Swing Low Bar ... + XOHCL lastSwingLow; + result = cBar.FindLastSwingLow(lastSwingLow); + if (!result) + { + return result; + } + + // + // Last Swing High Bar ... + XOHCL lastSwingHigh; + result = cBar.FindLastSwingHigh(lastSwingHigh); + if (!result) + { + return result; + } + + // + // Retrieve Highest High(s) and Lowest Low(s) ... + + // + // Highest High ... + double hh3 = cBar.FindHighest( + 3, + MODE_HIGH // + ); + + // + // Lowest Low ... + double ll3 = cBar.FindLowest( + 3, + MODE_LOW // + ); + + // + // Check Support Exists or Find ... + bool hasSupport = support.IsValid(); + if (!hasSupport) + { + // + hasSupport = + // + cBar.HasSupport(support) + // + ; + + // + if (hasSupport) + { + // + DrawSupport( + support, + ChartID() // + ); + + // + supportTime = cBar.time; + } + } + + // + // Check Resistance Exists or Find ... + bool hasResistance = resistance.IsValid(); + if (!hasResistance) + { + // + hasResistance = + // + cBar.HasResistance(resistance) + // + ; + + // + if (hasResistance) + { + // + DrawResistance( + resistance, + ChartID() // + ); + + // + resistanceTime = cBar.time; + } + } + + // + // For Trend ... + XOHCL tmpHSW[]; + XOHCL tmpLSW[]; + + // + // Detect Bullish/Bearish Patterns and Trends ... + + // + // Bullish ... + bool hasBullishPattern = HasBullishPattern(cBar); + bool isBullishTrend = cBar.HasBullishTrend( + tmpHSW, + tmpLSW, + true // + ); + + // + // Bearish ... + bool hasBearishPattern = HasBearishPattern(cBar); + bool isBearishTrend = cBar.HasBearishTrend( + tmpHSW, + tmpLSW, + false // + ); + + // + // Detect Conditions ... + // Prepare Required Conditions ... + + // + // High Upper => tpp top + // Low Upper => top bottom + + // + // High Lower => bottom top + // Low Lower => bottom bottom + + // + bool isUpperBandToched = IsValid(upperTouchTime); + if (!isUpperBandToched) + { + // + isUpperBandToched = + // + (cBar.high > upperLs[cIndex]) + // + ; + } + + // + bool isLowerBandToched = IsValid(lowerTouchTime); + if (!isLowerBandToched) + { + // + isLowerBandToched = + // + (cBar.low < lowerHs[cIndex]) + // + ; + } + + // + // Signalling ... + // Looking for Signals Based on Conditions ... + + // + if (isLowerBandToched) + { + // + hasLong = + // + hasBullishPattern + // + ; + + // + if (hasLong) + { + // + type = POSITION_TYPE_BUY; + Print(""); + } + } + + // + if (isUpperBandToched) + { + // + hasShort = + // + hasBearishPattern + // + ; + + // + if (hasShort) + { + // + type = POSITION_TYPE_SELL; + Print(""); + } + } + + // + // Preparing Signals for Execution ... + + // + result = hasLong || hasShort; + if (!result) + { + // + CheckSupportValidation(); + CheckResistanceValidation(); + + // + CheckUpperTouchTimeValidation(); + CheckLowerTouchTimeValidation(); + + // + return result; + } + + // + if (hasSupport) + { + // + supportTime = NULL; + isSupportTested = false; + RemoveSupport(support); + support.Clean(); + } + + // + if (hasResistance) + { + // + resistanceTime = NULL; + isResistanceTested = false; + RemoveResistance(resistance); + resistance.Clean(); + } + + // + // Retrieve Entry Price based on Position Type ... + entry = GetEntry(mSymbol, type); + + // + // Calculate TP and SL ... + // based on Strategy ... + CalculateTPSLByPrice( + sl, + tp, + type, + entry, + r2r // + ); + + // + // Preparing Signal ... + result = signal.Prepare( + mSymbol, + GetTag(), + mPeriod, + type, + mode, + entry, + mVolume, + sl, + tp // + ); + + // + return result; + } + + // + // Protected ... +protected: + // + // Props ... + + // + XOHCL support; + XOHCL resistance; + + // + // XTD ... + XTDInputs tdInputs; + XSCXTDHelper *tdHelper; + + // + // XOSC ... + XOSCInputs oscInputs; + XSCXOSCHelper *oscHelper; + + // + // XVLM ... + XVLMInputs vlmInputs; + XSCXVLMHelper *vlmHelper; + + // + // XHK ... + XHKInputs hkInputs; + XSCXHKHelper *hkHelper; + + // + // XPV ... + XPVInputs pvInputs; + XSCXPVHelper *pvHelper; + + // + // XDON ... + XDONInputs donInputs; + XSCXDONHelper *donHelper; + + // + // Private ... +private: + // + // Props ... + + // + void PrepareRequirements() + { + // + support.Clean(); + supportTime = NULL; + isSupportTested = false; + + // + resistance.Clean(); + resistanceTime = NULL; + isResistanceTested = false; + + // + // Prepare XTD ... + tdInputs.Default(); + + // + tdHelper = new XSCXTDHelper(); + tdHelper.Init( + mSymbol, + mPeriod, + tdInputs // + ); + + // + // Preparing XHK ... + hkInputs.Default(); + hkInputs.smoothingLength = 14; + + // + hkInputs.drawHikenAshi = false; + hkInputs.drawSmoothedHikenAshi = true; + + // + hkHelper = new XSCXHKHelper(); + hkHelper.Init( + mSymbol, + mPeriod, + hkInputs // + ); + + // + // Preparing XPV ... + pvInputs.Default(); + + // + pvInputs.showPeaksAndVales = true; + pvInputs.showLevels = false; + pvInputs.showConsolidations = false; + pvInputs.showFibo1Levels = false; + pvInputs.showFibo2Levels = false; + pvInputs.showFibo3Levels = true; + pvInputs.showFibo4Levels = false; + pvInputs.showFibo5Levels = false; + + // + pvHelper = new XSCXPVHelper(); + pvHelper.Init( + mSymbol, + mPeriod, + pvInputs // + ); + + // + // Preparing XOSC ... + oscInputs.Default(); + oscHelper = new XSCXOSCHelper(); + oscHelper.Init( + mSymbol, + mPeriod, + oscInputs // + ); + + // + // Preparing XVLM ... + vlmInputs.Default(); + vlmHelper = new XSCXVLMHelper(); + vlmHelper.Init( + mSymbol, + mPeriod, + vlmInputs // + ); + + // + // XDON ... + + // + donInputs.Default(); + + // + donInputs.showLow = true; + donInputs.showHigh = true; + donInputs.showOpen = false; + donInputs.showClose = false; + + // + donHelper = new XSCXDONHelper(); + donHelper.Init( + mSymbol, + mPeriod, + donInputs // + ); + } + + // + bool isSupportTested; + datetime supportTime; + void CheckSupportValidation() + { + // + if (!isSupportTested) + { + // + if (support.IsValid() && IsValid(supportTime)) + { + // + int age = (int)(((int)(TimeToSeconds(TimeCurrent()) - TimeToSeconds(supportTime))) / PeriodSeconds(mPeriod)); + if (age >= 50) + { + // + isSupportTested = false; + supportTime = NULL; + RemoveSupport(support); + support.Clean(); + } + } + return; + } + + // + XOHCL cBar; + bool isInited = cBar.Init( + mSymbol, + mPeriod, + 1 // + ); + if (!isInited) + { + return; + } + + // + double points = GetPoints(mSymbol); + + // + // Find Price Distance ... + double highDiff = MathAbs(cBar.high - support.high); + double lowDiff = MathAbs(cBar.low - support.high); + + // + bool isBreaked = MathMax(highDiff, lowDiff) >= 100 * points; + if (!isBreaked) + { + return; + } + + // + isSupportTested = false; + RemoveSupport(support); + supportTime = NULL; + support.Clean(); + } + + // + bool isResistanceTested; + datetime resistanceTime; + void CheckResistanceValidation() + { + // + if (!isResistanceTested) + { + // + if (resistance.IsValid() && IsValid(resistanceTime)) + { + // + int age = (int)(((int)(TimeToSeconds(TimeCurrent()) - TimeToSeconds(resistanceTime))) / PeriodSeconds(mPeriod)); + if (age >= 50) + { + // + isResistanceTested = false; + resistanceTime = NULL; + RemoveResistance(resistance); + resistance.Clean(); + } + } + return; + } + + // + XOHCL cBar; + bool isInited = cBar.Init( + mSymbol, + mPeriod, + 1 // + ); + if (!isInited) + { + return; + } + + // + double points = GetPoints(mSymbol); + + // + // Find Price Distance ... + double highDiff = MathAbs(cBar.high - resistance.low); + double lowDiff = MathAbs(cBar.low - resistance.low); + + // + bool isBreaked = MathMax(highDiff, lowDiff) >= 100 * points; + if (!isBreaked) + { + return; + } + + // + isResistanceTested = false; + RemoveResistance(resistance); + resistanceTime = NULL; + resistance.Clean(); + } + + // + datetime lowerTouchTime; + void CheckLowerTouchTimeValidation() { + // + XOHCL cBar; + bool isInited = cBar.Init( + mSymbol, + mPeriod, + 1 // + ); + if (!isInited) + { + return; + } + + // + } + + // + datetime upperTouchTime; + void CheckUpperTouchTimeValidation() { + // + XOHCL cBar; + bool isInited = cBar.Init( + mSymbol, + mPeriod, + 1 // + ); + if (!isInited) + { + return; + } + } +}; + +// \ No newline at end of file