From cf4dba082dc546554bb385922848cdd3079b09b8 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Thu, 1 Aug 2024 03:31:05 +0330 Subject: [PATCH] apply fix on Hedging ... add support for ignore recovery Close when hedging is enabled ... --- Classes/x-saherelm.xtrade.class.mq5 | 93 ++++++++++++++++++++++++++--- 1 file changed, 84 insertions(+), 9 deletions(-) diff --git a/Classes/x-saherelm.xtrade.class.mq5 b/Classes/x-saherelm.xtrade.class.mq5 index 07627788..5f5da01d 100644 --- a/Classes/x-saherelm.xtrade.class.mq5 +++ b/Classes/x-saherelm.xtrade.class.mq5 @@ -4553,6 +4553,9 @@ public: return drawdownPercent; } + // + // Trade Handling Functions ... + // // First Step of Trade Handling ... // Since Must Call when a Signal Executed ... @@ -4813,19 +4816,37 @@ public: if (mAllowHedging) { // - // Check Values Validation ... - if ((mMinTradesForHedge <= 0 - ? count > 1 - : count >= mMinTradesForHedge) && + // Retrieve Positions ... + XPosition positions[]; + int positionsCount = mTrader.GetPositions( + positions // + ); + + // + bool isParamsValid = mHedgeVolumeFactor > 0 && - mHedgeMinProfitPerVolumeFactor > 0) + mHedgeMinProfitPerVolumeFactor > 0; + + // + bool isCountValid = mMinTradesForHedge <= 0 + ? positionsCount >= 1 + : positionsCount >= mMinTradesForHedge; + + // + // Validate State ... + if (isParamsValid && isCountValid) { // + double swaps = 0; double profits = 0; + double volumes = 0; double commissions = 0; double requiredProfitsForHedge = 0; CalculateHedgeData( + positions, + swaps, profits, + volumes, commissions, requiredProfitsForHedge // ); @@ -4838,8 +4859,6 @@ public: string comment = "Hedging ..."; // - XPosition positions[]; - int positionsCount = mTrader.GetPositions(positions); if (IsValidSize(positionsCount)) { // @@ -4932,7 +4951,7 @@ public: } // - // Handle Recovery Break Event ... + // Handle Recovery Break Event if Hedging is Disabled ... if (mData[i].allowRecover) { // @@ -4965,7 +4984,7 @@ public: else { // - if (recoveriesCount > 0) + if (recoveriesCount > 0 && !mAllowHedging) { // XPosition mustClose[]; @@ -5940,6 +5959,62 @@ private: requiredProfitsForHedge -= commissions; } + // + // Calculate Required Data for Hedging ... + void CalculateHedgeData( + XPosition &positions[], + double &swaps, + double &profits, + double &volumes, + double &commissions, + double &requiredProfitsForHedge // + ) + { + // + // Reset Values ... + swaps = 0; + profits = 0; + volumes = 0; + commissions = 0; + requiredProfitsForHedge = 0; + + // + // Validate Configurations ... + if (mHedgeVolumeFactor <= 0 || + mHedgeMinProfitPerVolumeFactor <= 0) + { + return; + } + + // + // Check Positions Exists For Hedging ... + int count = ArraySize(positions); + if (!IsValidSize(count)) + { + return; + } + + // + for (int i = 0; i < count; i++) + { + // + XPosition iPosition = positions[i]; + double iCommission = mTrader.GetPositionCommission(iPosition.ticket); + + // + swaps += iPosition.swap; + profits += iPosition.profit; + volumes += iPosition.volume; + commissions += MathAbs(iCommission); + } + + // + // Calculate required Profits for Hedging ... + requiredProfitsForHedge = ((volumes / mHedgeVolumeFactor) * mHedgeMinProfitPerVolumeFactor); + requiredProfitsForHedge += commissions; + requiredProfitsForHedge += -1 * (swaps); + } + // bool FixRecoveredPositionTPSL(XPosition &position) {