From 8dfe216d8297050ce2f928049e0cb8b85b68d9a1 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Thu, 27 Jun 2024 19:03:17 +0330 Subject: [PATCH] last ... --- Classes/x-saherelm.x121.setup.xea.mq5 | 308 ++++++++++++++++++-------- Documents/BKP/usefull.mq5 | 238 ++++++++++++++++++++ Libraries/x-saherelm.xtrade.lib.mq5 | 49 ++++ 3 files changed, 504 insertions(+), 91 deletions(-) diff --git a/Classes/x-saherelm.x121.setup.xea.mq5 b/Classes/x-saherelm.x121.setup.xea.mq5 index 8bcbb9f1..342aa28e 100644 --- a/Classes/x-saherelm.x121.setup.xea.mq5 +++ b/Classes/x-saherelm.x121.setup.xea.mq5 @@ -4231,7 +4231,7 @@ public: ; // - bool forceState = mSetups[i].ForceState(); + bool forceState = mForce; // mSetups[i].ForceState(); // if (!forceState) @@ -4600,6 +4600,10 @@ public: eSignals // ); } + else + { + Alert("INNNValid Signal"); + } } // @@ -4625,7 +4629,7 @@ public: bool HandleStateManagement(XSignal &signals[]) override { // - bool result = false; + const bool result = false; // // Do all State Management here ... @@ -4637,10 +4641,10 @@ public: // Make State of Signalling to Force State for Recieving // Signal and Clear their Tp and SL for allowing hedges ... EQMHandleForceState(signals); - if (mForce) - { - result = true; - } + // if (mForce) + // { + // result = true; + // } // DoHedge(); @@ -5215,8 +5219,8 @@ private: if (result) { // + mForce = false; mTradeHandler.Sync(); - EQMClearAllForceStates(); } // @@ -5295,7 +5299,9 @@ private: // Validate Force Marigin ... double marigin = mTrader.mAccount.GetMargin(); double minFreeMarigin = MinFreeMargingForOpenTrades(); - double forceMargin = 3 * minFreeMarigin; // TODO: Make It Configurable + double forceMargin = minFreeMarigin * 2; // TODO: Make It Configurable + bool isMarginForce = marigin >= forceMargin; + bool isMarginHotForce = marigin >= forceMargin * 3; // // Retrieving Positions ... @@ -5305,15 +5311,9 @@ private: { // mForce = false; - - // - datetime cTime = TimeCurrent(); - string message = "Exit From Force State: " + ToString(cTime); - - // - // Log(message); return; } + bool isPositionsCountHotForce = positionsCount >= 10; // XPosition oldest; @@ -5324,21 +5324,13 @@ private: // // Check Force State ... - bool isForce = marigin >= forceMargin && oldestAge >= 288; + bool isForce = isMarginForce; // && oldestAge >= 288; if (!isForce) { // if (mForce) { - // mForce = false; - - // - datetime cTime = TimeCurrent(); - string message = "Exit From Force State: " + ToString(cTime); - - // - // Log(message); } return; } @@ -5379,6 +5371,181 @@ private: return; } + // + XSignal signals[]; + int forceSignalsCount = ArraySize(forceSignals); + if (IsValidSize(forceSignalsCount)) + { + // + // Check if Symbol Contains or Not ... + for (int i = 0; i < forceSignalsCount; i++) + { + // + XSignal iForceSignal = forceSignals[i]; + + // + int symbolIDX = -1; + bool isSymbolContains = IsContainsSymbol( + iForceSignal.symbol, + symbolIDX, + symbolPositions // + ); + if (!isSymbolContains) + { + // + AddRef( + iForceSignal, + signals // + ); + + // + continue; + } + else + { + // + ENUM_POSITION_TYPE iType = iForceSignal.type; + bool isLong = IsLong(iType); + + // + XSymbolPositions iSymbolPositions = symbolPositions[symbolIDX]; + if (isLong) + { + // + if (iSymbolPositions.longsProfit > 0) + { + // + if (!isMarginHotForce && !isPositionsCountHotForce) + { + // + AddRef( + iForceSignal, + signals // + ); + } + else + { + } + } + else + { + // + // In Drawdown ... + if (isMarginHotForce && !isPositionsCountHotForce) + { + } + else + { + } + } + } + else + { + // + if (iSymbolPositions.shortsProfit > 0) + { + // + if (!isMarginHotForce && !isPositionsCountHotForce) + { + // + AddRef( + iForceSignal, + signals // + ); + } + else + { + } + } + else + { + // + // In Drawdown ... + if (!isMarginHotForce && !isPositionsCountHotForce) + { + } + else + { + } + } + } + } + } + } + + // + int signalsCount = ArraySize(signals); + if (IsValidSize(signalsCount)) + { + EQMExecuteSignals(signals); + } + else + { + // + // Handle CLosing ... + if (!isMarginHotForce) + { + return; + } + + // + for (int i = 0; i < symbolPositionsCount; i++) + { + // + XSymbolPositions iSymbolPositions = symbolPositions[i]; + + // + if (!iSymbolPositions.HasChild()) + { + continue; + } + + // + if (iSymbolPositions.profit > 0) + { + continue; + } + + // + XPosition iOldest; + int oldestAge = GetOldest( + iOldest, + iSymbolPositions.positions // + ); + if (oldestAge <= 140) + { + continue; + } + + // + int maxInDPIDX = FindMaxDrawdownIndex( + iSymbolPositions.positions // + ); + if (!IsValidIndex(maxInDPIDX)) + { + continue; + } + + // + XPosition maxInDP = iSymbolPositions.positions[maxInDPIDX]; + + // + string comment = XEQMSupportToken + " Close due Max In D ..."; + bool isClosed = mTrader.Close( + maxInDP.ticket, + comment // + ); + if (isClosed) + { + // + string message = XEQMSupportToken + " Close (" + ToString(maxInDP.ticket) + ") due Max In D ..."; + + // + Alert(message); + } + } + } + // // Log("symbolPositionsCount: " + ToString(symbolPositionsCount)); } @@ -5393,92 +5560,68 @@ private: ) { // - // Check Force State ... - // if (!ForceState()) - // { - // return; - // } - - // - int setupIDX = FindSetupIndex(signal.symbol); - if (!IsValidIndex(setupIDX)) + if (!mForce) { return; } - // - bool isForce = mSetups[setupIDX].ForceState(); - if (!isForce) - { - return; - } - - // - // Check Delay Between Two Signals ... - bool isMarginPassed = IsMarginPassed(); - bool isDelayPassed = IsSignalDelayPassed(signal); - bool isNumberOfPositionsPassed = IsNumberOfPositionsPassed(); - bool canUseSignal = - isMarginPassed && - isDelayPassed && - isNumberOfPositionsPassed; - // XSignal signals[]; // - if (isValid) + if (isValid || pushers > 1) { // bool canSupport = // + mForce && EnableSupport() && SupportOnlyForces() // ; bool canGrid = // + mForce && EnableGrid() && GridOnlyForces() // ; bool canRecover = // + mForce && EnableRecovery() && RecoverOnlyForces() // ; // - if (canUseSignal) - { - // - AddRef( - signal, - signals // - ); + AddRef( + signal, + signals // + ); - // - // Add new Signal Info ... - mTradeHandler.Add( - signal, - pushers, - canGrid, - canRecover, - canSupport, - conditions[0], - conditions[1], - conditions[2], - conditions[3], - conditions[4] // - ); - } + // + // Add new Signal Info ... + mTradeHandler.Add( + signal, + pushers, + canGrid, + canRecover, + canSupport, + conditions[0], + conditions[1], + conditions[2], + conditions[3], + conditions[4] // + ); } // if (ArraySize(signals) > 0) { - EQMExecuteSignals(signals); + // + // EQMExecuteSignals(signals); + EQMHandleForceState(signals); } } @@ -5575,23 +5718,6 @@ private: } } - // - void EQMClearAllForceStates() - { - // - int setupsCount = CountSetups(); - if (!IsValidSize(setupsCount)) - { - return; - } - - // - for (int i = 0; i < setupsCount; i++) - { - mSetups[i].ForceState(false); - } - } - // }; diff --git a/Documents/BKP/usefull.mq5 b/Documents/BKP/usefull.mq5 index aa21b405..f330dcd4 100644 --- a/Documents/BKP/usefull.mq5 +++ b/Documents/BKP/usefull.mq5 @@ -5072,3 +5072,241 @@ int pivotsCount = ArraySize(pivots); // } + +///////////////////////////////////////////////////////////// + + // + // Handle Force State Management ... + bool mForce; + void EQMHandleForceState(XSignal &forceSignals[]) + { + // + // Validate Force Marigin ... + double marigin = mTrader.mAccount.GetMargin(); + double minFreeMarigin = MinFreeMargingForOpenTrades(); + double forceMargin = 2 * minFreeMarigin; // TODO: Make It Configurable + bool isMarginForce = marigin >= forceMargin; + + // + // Retrieving Positions ... + XPosition positions[]; + int positionsCount = mTrader.GetPositions(positions); + if (!IsValidSize(positionsCount)) + { + // + mForce = false; + + // + datetime cTime = TimeCurrent(); + string message = "Exit From Force State: " + ToString(cTime); + + // + // Log(message); + return; + } + + // + XPosition oldest; + int oldestAge = GetOldest( + oldest, + positions // + ); + + // + // Check Force State ... + bool isForce = isMarginForce; // && oldestAge >= 288; + if (!isForce) + { + // + if (mForce) + { + // + mForce = false; + + // + datetime cTime = TimeCurrent(); + string message = "Exit From Force State: " + ToString(cTime); + + // + // Log(message); + } + return; + } + else + { + mForce = true; + } + + // + // Prevent Moving Forward From Non Force State ... + if (!mForce) + { + return; + } + + // + int forceSignalsCount = ArraySize(forceSignals); + if (IsValidSize(forceSignalsCount)) + { + // + Log("Force Signals Count: " + ToString(forceSignalsCount)); + } + + // // + // double minProfitPerTrade = MinProfitPerTrade(); + // double minProfitPerVolumeFactor = MinProfitPerVolumeFactor(); + + // // + // double profit = SpecifiedCalculatePositionsProfit(positions); + // double requiredProfit = SpecifiedCalculateRequiredProfitForHedge( + // positions, + // minProfitPerTrade, + // minProfitPerVolumeFactor // + // ); + + // // + // XSymbolPositions symbolPositions[]; + // int symbolPositionsCount = ExtractSymbolPositions( + // positions, + // symbolPositions, + // minProfitPerTrade, + // minProfitPerVolumeFactor // + // ); + // if (!IsValidSize(symbolPositionsCount)) + // { + // return; + // } + + // + // Log("symbolPositionsCount: " + ToString(symbolPositionsCount)); + } + + +///////////////////////////////////////////////////////////////// + + // + // Handle any Invalid Signals ... + void EQMHandleInvalidSignal( + XSignal &signal, // Invalid Signal + int pushers, // Pushers + bool isValid, // IsValid Signal ... + X121SetupConditions &conditions[] // All Time Frames Conditions + ) + { + // + // Check Force State ... + // if (!ForceState()) + // { + // return; + // } + + // + int setupIDX = FindSetupIndex(signal.symbol); + if (!IsValidIndex(setupIDX)) + { + return; + } + + // + bool isForce = mForce; // mSetups[setupIDX].ForceState(); + if (!isForce) + { + return; + } + + // + // Check Delay Between Two Signals ... + // bool isMarginPassed = IsMarginPassed(); + // bool isDelayPassed = IsSignalDelayPassed(signal); + // bool isNumberOfPositionsPassed = IsNumberOfPositionsPassed(); + // bool canUseSignal = + // isMarginPassed && + // isDelayPassed && + // isNumberOfPositionsPassed; + + // + XSignal signals[]; + + // + if (isValid) + { + // + Log("IsValid Invalid Signal ..."); + + // + bool canSupport = + // + EnableSupport() && + SupportOnlyForces() + // + ; + bool canGrid = + // + EnableGrid() && + GridOnlyForces() + // + ; + bool canRecover = + // + EnableRecovery() && + RecoverOnlyForces() + // + ; + + // + if (canUseSignal) + { + // + AddRef( + signal, + signals // + ); + + // + // Add new Signal Info ... + mTradeHandler.Add( + signal, + pushers, + canGrid, + canRecover, + canSupport, + conditions[0], + conditions[1], + conditions[2], + conditions[3], + conditions[4] // + ); + } + } + else + { + Log("IsValid Invalid Signal INN ..."); + } + + // + // if (ArraySize(signals) > 0) + // { + // EQMExecuteSignals(signals); + // } + } + + +///////////////////////////////////////////////////////// + + + // + void EQMClearAllForceStates() + { + // + int setupsCount = CountSetups(); + if (!IsValidSize(setupsCount)) + { + return; + } + + // + for (int i = 0; i < setupsCount; i++) + { + mSetups[i].ForceState(false); + } + } diff --git a/Libraries/x-saherelm.xtrade.lib.mq5 b/Libraries/x-saherelm.xtrade.lib.mq5 index a53e7e16..2aff8c59 100644 --- a/Libraries/x-saherelm.xtrade.lib.mq5 +++ b/Libraries/x-saherelm.xtrade.lib.mq5 @@ -2797,6 +2797,9 @@ struct XSymbolPositions XPosition shorts[]; XPosition positions[]; + // + bool forceHedge; + // XSymbolPositions() { @@ -2862,6 +2865,7 @@ struct XSymbolPositions { // symbol = NULL; + forceHedge = false; // profit = 0; @@ -2928,6 +2932,51 @@ struct XSymbolPositions // }; +// +bool IsContainsSymbol( + string symbol, + int &_index, + XSymbolPositions &_items[] // +) +{ + // + bool result = false; + + // + _index = -1; + + // + result = IsValid(symbol); + if (!result) + { + return result; + } + + // + int itemsCount = ArraySize(_items); + result = IsValidSize(itemsCount); + if (!result) + { + return result; + } + + // + for (int i = 0; i < itemsCount; i++) + { + // + result = _items[i].symbol == symbol; + if (result) + { + // + _index = i; + break; + } + } + + // + return result; +} + // // TypeDefs ...