From 94539b2b5d668d29e899c27b3cc9c527065ab992 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Thu, 11 Sep 2025 15:19:56 +0330 Subject: [PATCH] Attach CheckForGuard Event Handler for Signallers to Handle Guards ... --- Classes/x-saherelm.x-guard.class.mq5 | 197 +---------------- Classes/x-saherelm.x-trade-manager.class.mq5 | 7 + Experts/x-saherelm.xom1.ea.mq5 | 12 ++ Libraries/x-saherelm.x-guard.lib.mq5 | 214 +++++++++++++++++++ Libraries/x-saherelm.x-trade.lib.mq5 | 10 + XOM1EA/Classes/xom1ea.expert.class.mq5 | 51 +++++ XOM1EA/Classes/xom1ea.signaller.class.mq5 | 26 +++ 7 files changed, 321 insertions(+), 196 deletions(-) create mode 100644 Libraries/x-saherelm.x-guard.lib.mq5 diff --git a/Classes/x-saherelm.x-guard.class.mq5 b/Classes/x-saherelm.x-guard.class.mq5 index fb330229..d3861233 100644 --- a/Classes/x-saherelm.x-guard.class.mq5 +++ b/Classes/x-saherelm.x-guard.class.mq5 @@ -24,202 +24,7 @@ // Imports ... #include "../Classes/x-saherelm.x-alert.class.mq5" #include "../Classes/x-saherelm.x-trade.class.mq5" - -// -// Enumeration ... - -// -// Guard Actions ... -enum ENUM_X_GUARD_ACTIONS -{ - X_GUARD_ACTION_NONE, // Nothing to Do - X_GUARD_ACTION_CLOSE, // Close Specified Position - X_GUARD_ACTION_CLOSE_ALL, // Close All Positions - X_GUARD_ACTION_CLOSE_LONGS, // Close All Long Positions - X_GUARD_ACTION_CLOSE_SHORTS, // Close All Short Position - X_GUARD_ACTION_CLOSE_IN_LOSTS, // Close All In Lost Position - X_GUARD_ACTION_CLOSE_IN_PROFITS, // Close All In Profit Position - X_GUARD_ACTION_CLOSE_IN_LOST_LONGS, // Close All In Lost Long Position - X_GUARD_ACTION_CLOSE_IN_PROFIT_LONGS, // Close All In Profit Long Position - X_GUARD_ACTION_CLOSE_IN_LOST_SHORTS, // Close All In Lost Short Position - X_GUARD_ACTION_CLOSE_IN_PROFITS_SHORT, // Close All In Profit Short Position - X_GUARD_ACTION_PARTIAL_CLOSE, // Partial Close Specified Position - X_GUARD_ACTION_TRAIL_STOP, // Trail Stop of Specified Position - X_GUARD_ACTION_TRAIL_TARGET, // Trail Target of Specified Position - X_GUARD_ACTION_HEDGE, // Hedge Specified Positions -}; - -// -// Definitions ... - -// -// Model a Guard Action ... -struct XGuard -{ - // - // Props ... - ENUM_X_GUARD_ACTIONS action; - datetime time; - - // - string symbol; - string provider; - ENUM_TIMEFRAMES period; - - // - // Specified Position ... - ulong ticket; - - // - // Partial Close ... - double volumeMultiplier; - - // - // SL Trial ... - double sl; - - // - // TP Trial ... - double tp; - - // - bool force; - - // - // Constructor ... - XGuard() - { - Clean(); - } - - // - // Tools ... - - /** - * Cleanup ... - */ - void Clean() - { - // - time = NULL; - action = X_GUARD_ACTION_NONE; - - // - symbol = NULL; - period = NULL; - provider = NULL; - - // - ticket = 0; - - // - volumeMultiplier = 0; - - // - sl = 0; - tp = 0; - - // - force = false; - - // - ZeroMemory(this); - } - - /** - * Validate ... - * - * @return ( bool ) - */ - bool IsValid() - { - // - bool result = false; - - // - result = - // - IsSpecifiedValid(time) && - IsSpecifiedValid(symbol) && - IsSpecifiedValid(period) && - IsSpecifiedValid(action) && - IsSpecifiedValid(provider) - // - ; - - if (!result) - { - return result; - } - - // - // Validate Model Based On Specified Guard Actions ... - - // - return result; - } -}; - -// -// an Event Type for Notify Guard Triggered ... -typedef void (*TOnGuarded)( - ENUM_X_GUARD_ACTIONS guard, - XPosition &positions[]); - -// -// Extensions ... - -// -bool IsValid(ENUM_X_GUARD_ACTIONS item) -{ - // - bool result = false; - - // - result = item != X_GUARD_ACTION_NONE; - - // - return result; -} -bool IsSpecifiedValid(ENUM_X_GUARD_ACTIONS item) -{ - return IsValid(item); -} - -/** - * Add Guard to Collection ... - * - * @param guard: XGuard instance ... - * @param guards: XGuard instance Collection ... - * - * @return ( int ) - */ -int AddGuard( - XGuard &guard, - XGuard &guards[] // -) -{ - // - int result = 0; - - // - if (!guard.IsValid()) - { - return result; - } - - // - AddRef( - guard, - guards // - ); - - // - result = ArraySize(guards); - - // - return result; -} +#include "../Libraries/x-saherelm.x-guard.lib.mq5" // // Implementation ... diff --git a/Classes/x-saherelm.x-trade-manager.class.mq5 b/Classes/x-saherelm.x-trade-manager.class.mq5 index 9d017814..b1b3d3bd 100644 --- a/Classes/x-saherelm.x-trade-manager.class.mq5 +++ b/Classes/x-saherelm.x-trade-manager.class.mq5 @@ -47,6 +47,9 @@ class XCTradeManager : public XCBase // XCPOIDrawer *drawer; + // + TCheckForGuard checkForGuardEventListener; + // // Constructor ... XCTradeManager( @@ -1679,6 +1682,10 @@ class XCTradeManager : public XCBase // // Check For Guards ... + if (checkForGuardEventListener != NULL) + { + result = checkForGuardEventListener(dest); + } // result = ArraySize(dest); diff --git a/Experts/x-saherelm.xom1.ea.mq5 b/Experts/x-saherelm.xom1.ea.mq5 index 56342b6e..2dd7ba32 100644 --- a/Experts/x-saherelm.xom1.ea.mq5 +++ b/Experts/x-saherelm.xom1.ea.mq5 @@ -213,6 +213,7 @@ int OnInit() eaExpert.AddOnOrdersChangedEventHandler(OnOrdersChanged); eaExpert.canAnalyseEventHandler = CanAnalyseEventTriggered; eaExpert.AddOnTakeProfitEventHandler(OnTakeProfitTriggered); + eaExpert.checkForGuardEventHandler = CheckForGuardTriggered; eaExpert.AddOnModifyPositionEventHandler(OnPositionModified); eaExpert.AddOnPositionsChangedEventHandler(OnPositionsChanged); eaExpert.AddOnTradeStateChangedEventHandler(OnTradeStateChanged); @@ -317,6 +318,17 @@ bool CanAnalyseEventTriggered( ); } +/** + * Check For Guard Event Handler ... + * + * @param guards: XGuard[] + * + * @return ( int ) + */ +int CheckForGuardTriggered(XGuard &guards[]) { + return eaExpert.CheckForGuardTriggered(guards); +} + /** * Handle Guard Events ... * diff --git a/Libraries/x-saherelm.x-guard.lib.mq5 b/Libraries/x-saherelm.x-guard.lib.mq5 new file mode 100644 index 00000000..e0e4c05f --- /dev/null +++ b/Libraries/x-saherelm.x-guard.lib.mq5 @@ -0,0 +1,214 @@ +/////////////////////////////////////////////////////// +// +// SaherElm IT Center MQL5 Seriallize Library +// --------------------------------------- +// Name: XGuardLib +// Description: All models related to Guard ... +// +// +// 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 "../Libraries/x-saherelm.common.lib.mq5" + +// +// Enumeration ... + +// +// Guard Actions ... +enum ENUM_X_GUARD_ACTIONS +{ + X_GUARD_ACTION_NONE, // Nothing to Do + X_GUARD_ACTION_CLOSE, // Close Specified Position + X_GUARD_ACTION_CLOSE_ALL, // Close All Positions + X_GUARD_ACTION_CLOSE_LONGS, // Close All Long Positions + X_GUARD_ACTION_CLOSE_SHORTS, // Close All Short Position + X_GUARD_ACTION_CLOSE_IN_LOSTS, // Close All In Lost Position + X_GUARD_ACTION_CLOSE_IN_PROFITS, // Close All In Profit Position + X_GUARD_ACTION_CLOSE_IN_LOST_LONGS, // Close All In Lost Long Position + X_GUARD_ACTION_CLOSE_IN_PROFIT_LONGS, // Close All In Profit Long Position + X_GUARD_ACTION_CLOSE_IN_LOST_SHORTS, // Close All In Lost Short Position + X_GUARD_ACTION_CLOSE_IN_PROFITS_SHORT, // Close All In Profit Short Position + X_GUARD_ACTION_PARTIAL_CLOSE, // Partial Close Specified Position + X_GUARD_ACTION_TRAIL_STOP, // Trail Stop of Specified Position + X_GUARD_ACTION_TRAIL_TARGET, // Trail Target of Specified Position + X_GUARD_ACTION_HEDGE, // Hedge Specified Positions +}; + +// +// Definitions ... + +// +// Model a Guard Action ... +struct XGuard +{ + // + // Props ... + ENUM_X_GUARD_ACTIONS action; + datetime time; + + // + string symbol; + string provider; + ENUM_TIMEFRAMES period; + + // + // Specified Position ... + ulong ticket; + + // + // Partial Close ... + double volumeMultiplier; + + // + // SL Trial ... + double sl; + + // + // TP Trial ... + double tp; + + // + bool force; + + // + // Constructor ... + XGuard() + { + Clean(); + } + + // + // Tools ... + + /** + * Cleanup ... + */ + void Clean() + { + // + time = NULL; + action = X_GUARD_ACTION_NONE; + + // + symbol = NULL; + period = NULL; + provider = NULL; + + // + ticket = 0; + + // + volumeMultiplier = 0; + + // + sl = 0; + tp = 0; + + // + force = false; + + // + ZeroMemory(this); + } + + /** + * Validate ... + * + * @return ( bool ) + */ + bool IsValid() + { + // + bool result = false; + + // + result = + // + IsSpecifiedValid(time) && + IsSpecifiedValid(symbol) && + IsSpecifiedValid(period) && + IsSpecifiedValid(action) && + IsSpecifiedValid(provider) + // + ; + + if (!result) + { + return result; + } + + // + // Validate Model Based On Specified Guard Actions ... + + // + return result; + } +}; + +// +// Extensions ... + +// +bool IsValid(ENUM_X_GUARD_ACTIONS item) +{ + // + bool result = false; + + // + result = item != X_GUARD_ACTION_NONE; + + // + return result; +} +bool IsSpecifiedValid(ENUM_X_GUARD_ACTIONS item) +{ + return IsValid(item); +} + +/** + * Add Guard to Collection ... + * + * @param guard: XGuard instance ... + * @param guards: XGuard instance Collection ... + * + * @return ( int ) + */ +int AddGuard( + XGuard &guard, + XGuard &guards[] // +) +{ + // + int result = 0; + + // + if (!guard.IsValid()) + { + return result; + } + + // + AddRef( + guard, + guards // + ); + + // + result = ArraySize(guards); + + // + return result; +} diff --git a/Libraries/x-saherelm.x-trade.lib.mq5 b/Libraries/x-saherelm.x-trade.lib.mq5 index 1d117128..a90a11aa 100644 --- a/Libraries/x-saherelm.x-trade.lib.mq5 +++ b/Libraries/x-saherelm.x-trade.lib.mq5 @@ -22,6 +22,7 @@ // // Imports ... #include "../Libraries/x-saherelm.common.lib.mq5" +#include "../Libraries/x-saherelm.x-guard.lib.mq5" #include // @@ -3752,6 +3753,15 @@ typedef bool (*TCanAnalyse)( datetime _time // ); +// +// an Event Type for Notify Guard Triggered ... +typedef void (*TOnGuarded)( + ENUM_X_GUARD_ACTIONS guard, + XPosition &positions[]); + +// +typedef int (*TCheckForGuard)(XGuard &guards[]); + // typedef void (*TOnDealsChanged)(int count); diff --git a/XOM1EA/Classes/xom1ea.expert.class.mq5 b/XOM1EA/Classes/xom1ea.expert.class.mq5 index 58bd5286..cdfe58b0 100644 --- a/XOM1EA/Classes/xom1ea.expert.class.mq5 +++ b/XOM1EA/Classes/xom1ea.expert.class.mq5 @@ -116,6 +116,7 @@ class XCXOM1EAExpert : public XCBase // TOnGuarded onGuardedEventHandler; TCanAnalyse canAnalyseEventHandler; + TCheckForGuard checkForGuardEventHandler; // // Signal Event Handlers ... @@ -519,6 +520,10 @@ class XCXOM1EAExpert : public XCBase // // Configuring Trade Manager ... + // + // Attache Check For Guard Event Listener ... + eaTradeManager.checkForGuardEventListener = checkForGuardEventHandler; + // eaTradeManager.SaveWins(eaSaveWins); eaTradeManager.SaveLosts(eaSaveLosts); @@ -742,6 +747,52 @@ class XCXOM1EAExpert : public XCBase eaTradeManager.HandleSignal(signal); } + /** + * Check For Guard Event Handler ... + * + * @param guards: XGuard[] + * + * @return ( int ) + */ + int CheckForGuardTriggered(XGuard &guards[]) + { + // + int result = 0; + + // + if (!HasChild(eaSignallers)) + { + return result; + } + + // + int count = ArraySize(eaSignallers); + for (int i = 0; i < count; i++) + { + // + XGuard iGuards[]; + int iGuardsCount = eaSignallers[i].CheckForGuard(iGuards); + if (IsValidSize(iGuardsCount)) + { + // + Copy( + iGuards, + guards, + false // + ); + } + + // + SpecifiedClean(iGuards); + } + + // + result = ArraySize(guards); + + // + return result; + } + /** * Handle Guard Events ... * diff --git a/XOM1EA/Classes/xom1ea.signaller.class.mq5 b/XOM1EA/Classes/xom1ea.signaller.class.mq5 index 416be15d..fecf04dc 100644 --- a/XOM1EA/Classes/xom1ea.signaller.class.mq5 +++ b/XOM1EA/Classes/xom1ea.signaller.class.mq5 @@ -558,6 +558,32 @@ class XCXOM1SignallerBase iConditions.Clean(); } + /** + * Check Guards Based on MarketConditions ... + * + * @param guards: XGuard[] + * @param barIndex: int + * + * @return ( int ) + */ + int CheckForGuard( + XGuard &guards[], + int barIndex = 0 // + ) + { + // + int result = 0; + + // + SpecifiedClean(guards); + + // + result = ArraySize(guards); + + // + return result; + } + // // Virtuals ...