From 4854b9f6a898164e5bbf17d9f86c7036859818f0 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Mon, 24 Jun 2024 09:37:03 +0330 Subject: [PATCH] last ... --- Classes/x-saherelm.x121.setup.xea.mq5 | 234 +++++++++++++++++++++----- Experts/x-saherelm.x121.ea.mq5 | 6 + 2 files changed, 194 insertions(+), 46 deletions(-) diff --git a/Classes/x-saherelm.x121.setup.xea.mq5 b/Classes/x-saherelm.x121.setup.xea.mq5 index e90c8cbf..b73d1ac4 100644 --- a/Classes/x-saherelm.x121.setup.xea.mq5 +++ b/Classes/x-saherelm.x121.setup.xea.mq5 @@ -3361,6 +3361,56 @@ public: mMaxPositionsForce = value; } + // + bool AllowSymbolHedgeOnForce() + { + return mAllowSymbolHedgeOnForce; + } + + // + void AllowSymbolHedgeOnForce(bool value) + { + mAllowSymbolHedgeOnForce = value; + } + + // + double ForceCloseOnEuityDiff() + { + return mForceCloseOnEuityDiff; + } + + // + void ForceCloseOnEuityDiff(double value) + { + // + if (value < 0) + { + value = 0; + } + + // + mForceCloseOnEuityDiff = value; + } + + // + double MinFreeMargingForHandleForce() + { + return mMinFreeMargingForHandleForce; + } + + // + void MinFreeMargingForHandleForce(double value) + { + // + if (value < 0) + { + value = 0; + } + + // + mMinFreeMargingForHandleForce = value; + } + // // // bool ForceState() // { @@ -4245,6 +4295,34 @@ protected: return; } + // + bool isMarginPassed = IsMarginPassed(); + if (!isMarginPassed) + { + // + string message = "No Margin For Execute Signals ..."; + + // + Alert(message); + + // + return; + } + + // + bool isNumberOfPositionsPassed = IsNumberOfPositionsPassed(); + if (!isNumberOfPositionsPassed) + { + // + string message = "Max Allowed Signals Reached ..."; + + // + Alert(message); + + // + return; + } + // ENUM_X_SIGNAL_EXECUTION_RESULT states[]; int executedSignalsCount = ExecuteSignals( @@ -4310,6 +4388,52 @@ protected: return result; } + // + bool IsMarginPassed() + { + // + double freeMargin = mTrader.mAccount.GetFreeMargin(); + double minFreeMargingForHandleForce = MinFreeMargingForHandleForce(); + + // + bool result = minFreeMargingForHandleForce <= 0 + ? true + : freeMargin >= minFreeMargingForHandleForce; + + // + return result; + } + + // + bool IsNumberOfPositionsPassed() + { + // + bool result = false; + + // + int maxAllowed = MaxPositionsForce(); + result = maxAllowed <= 0; + if (result) + { + return result; + } + + // + XPosition positions[]; + int positionsCount = mTrader.GetPositions(positions); + result = !IsValidSize(positionsCount); + if (result) + { + return result; + } + + // + result = positionsCount < maxAllowed; + + // + return result; + } + // // Private ... private: @@ -4349,9 +4473,12 @@ private: // - Increase Volume Size of in Profit Positions; // - etc ... // bool mForceState; // Determine in Force State or not - int mDelayBetweenTwoSignal; // Delay between two same signal - int mMaxPositionsForce; // Max Number of Positions For Guard Handling - double mMaxDrawdownForce; // Force Handling State on Drawdown for Guard + int mDelayBetweenTwoSignal; // Delay between two same signal + int mMaxPositionsForce; // Max Number of Positions For Guard Handling + double mMaxDrawdownForce; // Force Handling State on Drawdown for Guard + bool mAllowSymbolHedgeOnForce; // Allow Symbol Hedging on Force States + double mMinFreeMargingForHandleForce; // Min Free Margin for Force States + double mForceCloseOnEuityDiff; // Force Close On Equity Differential // XTradeHandler *mTradeHandler; // Handle all Event which related to Signals and Positions @@ -4802,62 +4929,66 @@ private: // // Check Symbol Hedging ... - for (int i = 0; i < setupsCount; i++) + bool allowSymbolHedge = AllowSymbolHedgeOnForce(); + if (allowSymbolHedge) { - // - string iSymbol = mSetups[i].GetSymbol(); - - // - XPosition iSymbolPositions[]; - int iSymbolPositionsCount = mTrader.GetPositions( - iSymbolPositions, - // - iSymbol, - NULL, // All Providers ... - NULL, // All Periods ... - NULL, // All Types ... - true // Filter By Magic ... - ); - if (!IsValidSize(iSymbolPositionsCount)) - { - continue; - } - - // - double profit = SpecifiedCalculatePositionsProfit(iSymbolPositions); - - // - double requiredProfit = SpecifiedCalculateRequiredProfitForHedge( - iSymbolPositions, - minRequiredProfitPerTrade, - minRequiredProfitPerVolumeFactor // - ); - - // - bool canHedge = - requiredProfit <= 0 - ? profit > 0 - : profit >= requiredProfit; - if (canHedge) + for (int i = 0; i < setupsCount; i++) { // - string comment = XEQMSupportToken + " Hedge Symbol ..."; + string iSymbol = mSetups[i].GetSymbol(); // - int closed = mTrader.Close( + XPosition iSymbolPositions[]; + int iSymbolPositionsCount = mTrader.GetPositions( iSymbolPositions, - comment // + // + iSymbol, + NULL, // All Providers ... + NULL, // All Periods ... + NULL, // All Types ... + true // Filter By Magic ... + ); + if (!IsValidSize(iSymbolPositionsCount)) + { + continue; + } + + // + double profit = SpecifiedCalculatePositionsProfit(iSymbolPositions); + + // + double requiredProfit = SpecifiedCalculateRequiredProfitForHedge( + iSymbolPositions, + minRequiredProfitPerTrade, + minRequiredProfitPerVolumeFactor // ); // - if (closed == iSymbolPositionsCount) + bool canHedge = + requiredProfit <= 0 + ? profit > 0 + : profit >= requiredProfit; + if (canHedge) { // - mSetups[i].ForceState(false); + string comment = XEQMSupportToken + " Hedge Symbol ..."; // - string message = XEQMSupportToken + " Hedge Symbol (" + iSymbol + "): " + ToString(profit); - Alert(message); + int closed = mTrader.Close( + iSymbolPositions, + comment // + ); + + // + if (closed == iSymbolPositionsCount) + { + // + mSetups[i].ForceState(false); + + // + string message = XEQMSupportToken + " Hedge Symbol (" + iSymbol + "): " + ToString(profit); + Alert(message); + } } } } @@ -4868,6 +4999,17 @@ private: // // Implement Other Senarios ... + // // + // double equity = mTrader.mAccount.GetEquity(); + // double balance = mTrader.mAccount.GetBalance(); + // double forceCloseOnEuityDiff = ForceCloseOnEuityDiff(); + + // // + // bool canForceClose = + // forceCloseOnEuityDiff <= 0 + // ? false + // : balance > + // } diff --git a/Experts/x-saherelm.x121.ea.mq5 b/Experts/x-saherelm.x121.ea.mq5 index 6c85be6b..79179e99 100644 --- a/Experts/x-saherelm.x121.ea.mq5 +++ b/Experts/x-saherelm.x121.ea.mq5 @@ -114,6 +114,9 @@ input int x121EARestingAfterHedge = 0; // Resting Seconds A input double x121EAForceCloseProfit = 15; // Close All Positions Regardless of Policies when Profit Reached input double x121EAMaxDrawdownForce = 5; // Max Drawdown Force State for Handling Guards input int x121EAMaxPositionsForce = 10; // Max Number of Positions Force State for Handling Guards +input double x121EAMinFreeMargingForHandleForce = 800; // Min Free Margin for Force States +input double x121EAForceCloseOnEuityDiff = 15; // Force Close On Equity Differential +input bool x121EAAllowSymbolHedgeOnForce = false; // Allow Symbol Hedging on Force States input double x121EAMinRequiredProfitPerTrade = 0.5; // Minimum Required Profit for Hedging input double x121EAMinRequiredProfitPerVolumeFactor = 0.01; // Minimum Required Profit for Hedging Per Volume @@ -379,6 +382,9 @@ bool InitialEA() x121EA.RestingAfterHedge(x121EARestingAfterHedge); x121EA.MaxDrawdownForce(x121EAMaxDrawdownForce); x121EA.MaxPositionsForce(x121EAMaxPositionsForce); + x121EA.AllowSymbolHedgeOnForce(x121EAAllowSymbolHedgeOnForce); + x121EA.ForceCloseOnEuityDiff(x121EAMinFreeMargingForHandleForce); + x121EA.MinFreeMargingForHandleForce(x121EAForceCloseOnEuityDiff); x121EA.MinRequiredProfitPerTrade(x121EAMinRequiredProfitPerTrade); x121EA.MinRequiredProfitPerVolumeFactor(x121EAMinRequiredProfitPerVolumeFactor);