diff --git a/Classes/x-saherelm.x121.setup.xea.mq5 b/Classes/x-saherelm.x121.setup.xea.mq5 index 0bf9cdf5..8bcbb9f1 100644 --- a/Classes/x-saherelm.x121.setup.xea.mq5 +++ b/Classes/x-saherelm.x121.setup.xea.mq5 @@ -204,53 +204,6 @@ struct XTradeInfo return result; } - // - bool IsOwn(XSignal &signal) - { - // - return IsOwn( - signal.symbol, - signal.provider, - signal.time, - signal.period, - signal.type // - ); - } - - // - bool IsOwn( - string _symbol, - string _provider, - datetime _time, - ENUM_TIMEFRAMES _period, - ENUM_POSITION_TYPE _type // - ) - { - // - bool result = false; - - // - ENUM_X_POSITION_TYPES iType = ToPositionType(_type); - - // - result = - // - IsValid(_symbol) && - IsValid(_provider) && - IsValid(_period) && - // - symbol == _symbol && - provider == _provider && - period == _period && - type == iType && - time == _time - // - ; - - // - return result; - } - // // Data Collector ... @@ -594,7 +547,6 @@ struct XTradeInfo // result = IsValid() && - allowGrid && distance > 0 && volumeMultiplier > 0; if (!result) @@ -813,7 +765,6 @@ struct XTradeInfo entry > 0 && volume > 0 && distance > 0 && - allowRecover && volumeMultiplier > 0 // ; @@ -2075,77 +2026,6 @@ public: Finish(infoIDX); } - // - bool AllowToGrid( - XSignal &signal // - ) - { - // - bool result = false; - - // - int count = Count(); - result = IsValidSize(count); - if (!result) - { - return result; - } - - // - int infoIDX = FindIndex(signal); - result = IsValidIndex(infoIDX); - if (!result) - { - return result; - } - - // - data[infoIDX].allowGrid = true; - data[infoIDX].PrepareNextGrid( - GridDistance(), - GridVolumeMultiplier() // - ); - - // - return result; - } - - // - bool AllowToRecovery( - XSignal &signal // - ) - { - // - bool result = false; - - // - int count = Count(); - result = IsValidSize(count); - if (!result) - { - return result; - } - - // - int infoIDX = FindIndex(signal); - result = IsValidIndex(infoIDX); - if (!result) - { - return result; - } - - // - data[infoIDX].allowRecover = true; - data[infoIDX].PrepareNextRecovery( - GridDistance(), - GridVolumeMultiplier(), - signal.type // - ); - - // - return result; - } - // // Sync ... void Sync() @@ -4351,7 +4231,7 @@ public: ; // - bool forceState = mForce; // mSetups[i].ForceState(); + bool forceState = mSetups[i].ForceState(); // if (!forceState) @@ -4745,7 +4625,7 @@ public: bool HandleStateManagement(XSignal &signals[]) override { // - const bool result = false; + bool result = false; // // Do all State Management here ... @@ -4756,11 +4636,11 @@ public: // Youngest Age of Positions longest as enough // 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; - // } + EQMHandleForceState(signals); + if (mForce) + { + result = true; + } // DoHedge(); @@ -4947,6 +4827,23 @@ protected: // Alert(message); + // + EQMHandleForceState(signals); + + // + return; + } + + // + bool isNumberOfPositionsPassed = IsNumberOfPositionsPassed(); + if (!isNumberOfPositionsPassed) + { + // + string message = "Max Allowed Signals Reached ..."; + + // + Alert(message); + // return; } @@ -5053,6 +4950,39 @@ protected: 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; + + // + result = true; + + // + return result; + } + // // Private ... private: @@ -5285,8 +5215,8 @@ private: if (result) { // - mForce = false; mTradeHandler.Sync(); + EQMClearAllForceStates(); } // @@ -5362,6 +5292,95 @@ private: void EQMHandleForceState(XSignal &forceSignals[]) { // + // Validate Force Marigin ... + double marigin = mTrader.mAccount.GetMargin(); + double minFreeMarigin = MinFreeMargingForOpenTrades(); + double forceMargin = 3 * minFreeMarigin; // TODO: Make It Configurable + + // + // 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 = marigin >= forceMargin && 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; + } + + // + 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)); } // @@ -5374,11 +5393,36 @@ private: ) { // - if (!mForce) + // Check Force State ... + // if (!ForceState()) + // { + // return; + // } + + // + int setupIDX = FindSetupIndex(signal.symbol); + if (!IsValidIndex(setupIDX)) { 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[]; @@ -5388,46 +5432,47 @@ private: // bool canSupport = // - mForce && EnableSupport() && SupportOnlyForces() // ; bool canGrid = // - mForce && EnableGrid() && GridOnlyForces() // ; bool canRecover = // - mForce && EnableRecovery() && RecoverOnlyForces() // ; // - AddRef( - signal, - signals // - ); + 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] // - ); + // + // Add new Signal Info ... + mTradeHandler.Add( + signal, + pushers, + canGrid, + canRecover, + canSupport, + conditions[0], + conditions[1], + conditions[2], + conditions[3], + conditions[4] // + ); + } } // @@ -5530,6 +5575,23 @@ private: } } + // + void EQMClearAllForceStates() + { + // + int setupsCount = CountSetups(); + if (!IsValidSize(setupsCount)) + { + return; + } + + // + for (int i = 0; i < setupsCount; i++) + { + mSetups[i].ForceState(false); + } + } + // };