From e54a991b6d0fd92395b95bcd3dc0c13e245c07d7 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Fri, 21 Jun 2024 09:18:04 +0330 Subject: [PATCH] last ... --- Classes/x-saherelm.x121.setup.class.mq5 | 277 ++++++------ Classes/x-saherelm.x121.setup.xea.mq5 | 566 ++++++++++++++++-------- Classes/x-saherelm.xea.class.mq5 | 44 +- Libraries/x-saherelm.xtrade.lib.mq5 | 2 +- 4 files changed, 554 insertions(+), 335 deletions(-) diff --git a/Classes/x-saherelm.x121.setup.class.mq5 b/Classes/x-saherelm.x121.setup.class.mq5 index fae874de..fcfffcd8 100644 --- a/Classes/x-saherelm.x121.setup.class.mq5 +++ b/Classes/x-saherelm.x121.setup.class.mq5 @@ -2810,13 +2810,13 @@ struct X121SignalGenerator // // Generate Support Signals ... - if (_useSupport) - { - // - GenerateSupportSignals( - signal // - ); - } + // if (_useSupport) + // { + // // + // GenerateSupportSignals( + // signal // + // ); + // } // return result; @@ -2827,11 +2827,52 @@ struct X121SignalGenerator XSignal &signal // Source Signal for Support Generating ... ) { + // + double pivots[]; + int pivotsCount = GenerateSupportPivots( + signal, + pivots // + ); + if (!IsValidSize(pivotsCount)) + { + return; + } + + // + XSignal supports[]; + int indirectionalsCount = + PrepareInDirectionalSupportSignals( + supports, + signal, + pivots // + ); + if (IsValidSize(indirectionalsCount)) + { + // + Copy( + supports, + signal.supports // + ); + + // + Clean(supports); + } + } + + // + int GenerateSupportPivots( + XSignal &signal, // Source Signal for Support Generating ... + double &pivots[] // + ) + { + // + int result = 0; + // // Validate Signal ... if (!signal.IsValid()) { - return; + return result; } // @@ -2875,6 +2916,7 @@ struct X121SignalGenerator ); // + Clean(pivots); double tmpPivots[]; // @@ -2999,27 +3041,44 @@ struct X121SignalGenerator } // - XSignal supports[]; - int indirectionalsCount = - PrepareInDirectionalSupportSignals( - supports, - signal, - tmpPivots // - ); - if (IsValidSize(indirectionalsCount)) + int tmpPivotsCount = ArraySize(tmpPivots); + if (!IsValidSize(tmpPivotsCount)) { - // - Copy( - supports, - signal.supports // - ); - - // - Clean(supports); + return result; } + // + for (int i = 0; i < tmpPivotsCount; i++) + { + // + double iPivot = tmpPivots[i]; + + // + bool isValidPivot = + isLong + ? signal.entry > iPivot + : signal.entry < iPivot; + if (isValidPivot) + { + // + Add( + iPivot, + pivots // + ); + } + } + + // + ArrayFree(tmpPivots); + // RemoveDraws(entryLineName); + + // + result = ArraySize(pivots); + + // + return result; } // @@ -3043,77 +3102,71 @@ struct X121SignalGenerator } // - bool isDirectional = support.provider == XEQMSupportToken + XDirectionalToken; + mResult = support.provider == XEQMSupportToken + XINDirectionalToken; + if (!mResult) + { + return mResult; + } // - if (!isDirectional) + // Indirectional Supports Updated Using Directional ... + + // + double entryPrice = GetEntry( + parent.symbol, + parent.type // + ); + + // + ENUM_X_ORDER_MODES rMode = X_ORDER_MODE_NOTHING; + bool isParentLong = IsLong(parent.type); + if (isParentLong) { // - // Indirectional Supports Updated Using Directional ... - - // - double entryPrice = GetEntry( - parent.symbol, - parent.type // - ); - - // - ENUM_X_ORDER_MODES rMode = X_ORDER_MODE_NOTHING; - bool isParentLong = IsLong(parent.type); - if (isParentLong) + if (entryPrice < parent.entry) { - // - if (entryPrice < parent.entry) - { - rMode = X_ORDER_MODE_STOP; - } - else if (entryPrice > parent.entry) - { - rMode = X_ORDER_MODE_LIMIT; - } - } - else - { - // - if (entryPrice < parent.entry) - { - rMode = X_ORDER_MODE_LIMIT; - } - else if (entryPrice > parent.entry) - { - rMode = X_ORDER_MODE_STOP; - } - } - - // - if (rMode != X_ORDER_MODE_NOTHING) - { - // - mResult = result.Prepare( - parent.symbol, - XEQMSupportToken + XDirectionalToken, - parent.period, - parent.type, - rMode, - parent.entry, - parent.volume, - 0, - 0 // - ); - } - - // - if (mResult) - { - // - string comment = GenerateSupportTag(parent.ticket); - result.comment = comment; + rMode = X_ORDER_MODE_STOP; } } else { // - // TODO: Implement ... + if (entryPrice > parent.entry) + { + rMode = X_ORDER_MODE_STOP; + } + } + + // + mResult = rMode != X_ORDER_MODE_NOTHING; + if (!mResult) + { + return mResult; + } + + // + mResult = result.Prepare( + parent.symbol, + XEQMSupportToken + XDirectionalToken, + parent.period, + parent.type, + rMode, + parent.entry, + parent.volume, + 0, + 0 // + ); + + // + if (mResult) + { + // + string comment = GenerateSupportTag(parent.ticket); + result.comment = comment; + } + else + { + result.Clean(); } // @@ -3232,60 +3285,6 @@ struct X121SignalGenerator rTP = 0; rEntry = 0; isSignalPrepared = false; - - // - // Here we Create Related Directional Order ... - - // - // Directional ... - if (isLong) - { - // - rType = POSITION_TYPE_BUY; - rMode = X_ORDER_MODE_LIMIT; - } - else - { - // - rType = POSITION_TYPE_SELL; - rMode = X_ORDER_MODE_LIMIT; - } - - // - rEntry = source.entry; - rVolume = source.volume; - - // - isSignalPrepared = rSignal.Prepare( - source.symbol, - XEQMSupportToken + XDirectionalToken, - source.period, - rType, - rMode, - rEntry, - rVolume, - rSL, - rTP // - ); - - // - if (isSignalPrepared) - { - // - AddRef( - rSignal, - supports // - ); - - // - rSignal.Clean(); - - // - rSL = 0; - rTP = 0; - rEntry = 0; - isSignalPrepared = false; - } } } diff --git a/Classes/x-saherelm.x121.setup.xea.mq5 b/Classes/x-saherelm.x121.setup.xea.mq5 index b52ac745..848b6b42 100644 --- a/Classes/x-saherelm.x121.setup.xea.mq5 +++ b/Classes/x-saherelm.x121.setup.xea.mq5 @@ -116,6 +116,7 @@ struct XTradeInfo // ResetGrid(); + ResetSupport(); ResetRecovery(); } @@ -181,6 +182,15 @@ struct XTradeInfo bullishScore = _bullishScore; bearishScore = _bearishScore; + // + X121SignalGenerator generator; + XSignal tSignal = _signal; + generator.GenerateSupportPivots( + tSignal, + pivots // + ); + tSignal.Clean(); + // return result; } @@ -427,6 +437,183 @@ struct XTradeInfo // These are Some Helpers for Trading Controls and do not relate to Main // purpose of Trade Handler ... + // + // Supporting ... + + // + double pivots[]; + + // + void ResetSupport() + { + Clean(pivots); + } + + // + // Retrieve Support Signal ... + bool HasSupport( + XSignal &support // + ) + { + // + bool result = false; + + // + result = IsValid(); + if (!result) + { + return result; + } + + // + int pivotsCount = ArraySize(pivots); + result = IsValidSize(pivotsCount); + if (!result) + { + return result; + } + + // + support.Clean(); + + // + ENUM_POSITION_TYPE sType = + type == X_POSITION_TYPE_LONG + ? POSITION_TYPE_BUY + : POSITION_TYPE_SELL; + + // + bool isLong = IsLong(type); + + // + ENUM_POSITION_TYPE cType = GetOpposit(sType); + double cEntry = GetEntry( + symbol, + cType // + ); + + // + double pivot = 0; + int pivotIndex = -1; + for (int i = 0; i < pivotsCount; i++) + { + // + double iPivot = pivots[i]; + if (iPivot <= 0) + { + continue; + } + + // + result = + isLong + ? cEntry < entry && cEntry <= iPivot + : cEntry > entry && cEntry >= iPivot; + if (result) + { + // + pivot = iPivot; + pivotIndex = i; + break; + } + } + + // + if (result) + { + // + result = support.Prepare( + symbol, + XEQMSupportToken + XINDirectionalToken, + period, + cType, + X_ORDER_MODE_MARKET, + cEntry, + volume * 2, + 0, + 0 // + ); + if (result) + { + // + string comment = GenerateSupportTag(ticket); + support.comment = comment; + + // + // Prepare Directional Support ... + XSignal mSup; + result = mSup.Prepare( + symbol, + XEQMSupportToken + XDirectionalToken, + period, + sType, + X_ORDER_MODE_STOP, + entry, + volume, + 0, + 0 // + ); + if (result) + { + // + string comment = GenerateSupportTag(ticket); + mSup.comment = comment; + + // + AddRef( + mSup, + support.supports // + ); + + // + result = ArrayRemove( + pivots, + pivotIndex, + 1 // + ); + } + } + } + + // + if (!result) + { + support.Clean(); + } + + // + return result; + } + + // + bool RemovePivot( + double pivot, + int index // + ) + { + // + bool result = false; + + // + result = IsValid() && + IsValidIndex(index) && + ArraySize(pivots) > index; + if (!result) + { + return result; + } + + // + result = ArrayRemove( + pivots, + index, + 1 // + ); + + // + return result; + } + // // Grid System Calculations ... @@ -1860,8 +2047,7 @@ public: } // - // Supporting ... - if (EnableSupport()) + if (AllowSupport()) { // XSignal supports[]; @@ -2148,10 +2334,6 @@ protected: // int result = 0; - // - // TODO: Complete this Later ... - return result; - // Clean(signals); @@ -2165,104 +2347,137 @@ protected: Sync(); // - // Validate Setups Count ... - int setupsCount = ArraySize(setups); - if (!IsValidSize(setupsCount)) + int count = Count(); + if (!IsValidSize(count)) { return result; } // - // Implement Supporting Mechanism ... - - // - // Extracting In Drawdown Positions ... - XPosition positions[]; - int positionsCount = mTrader.GetInDrawdownPositions( - positions // - ); - if (!IsValidSize(positionsCount)) - { - return result; - } - - // - double supportDistance = SupportDistance(); - int maxNUmberOfSupports = MaxNumberOfSupports(); - double maxAllowedVolume = MaxAllowedSupportVolume(); - double volumeMultiplier = SupportVolumeMultiplier(); - - // - X121SignalGenerator signalGenerator; - - // - for (int i = 0; i < positionsCount; i++) + for (int i = 0; i < count; i++) { // - XPosition iPosition = positions[i]; - - // - // Check Position is not Support Position ... - bool isSupport = IsSupport(iPosition.comment); - if (isSupport) - { - continue; - } - - // - // Find Related Setp ... - int idx = FindSetupIndex( - iPosition.symbol, - setups // + XSignal iSupport; + double iPivot = -1; + int iPivotIDX = -1; + bool hasSupport = data[i].HasSupport( + iSupport // ); - if (!IsValidIndex(idx)) + if (hasSupport) { - continue; + // + AddRef( + iSupport, + signals // + ); } - - // - int index = FindIndex(iPosition.ticket); - if (!IsValidIndex(index)) - { - continue; - } - - // - // Check Position must atleast 20 Point in drawdown ... - - // - double onePointPrice = PointToPrice( - 1, - iPosition.symbol // - ); - - // - double iExitPrice = GetExit( - iPosition.symbol, - iPosition.type // - ); - - // - double priceDiff = MathAbs(iPosition.entry - iExitPrice); - double priceDiffPoints = priceDiff / onePointPrice; - - // - bool isFarEnough = priceDiffPoints >= (supportDistance / 2); - if (!isFarEnough) - { - continue; - } - - // - // Check Support COunts and etc ... - // implementing supporting mechanis ... - - // } // result = ArraySize(signals); + // + // TODO: Complete this Later ... + return result; + // + // Validate Setups Count ... + // int setupsCount = ArraySize(setups); + // if (!IsValidSize(setupsCount)) + // { + // return result; + // } + + // // + // // Implement Supporting Mechanism ... + + // // + // // Extracting In Drawdown Positions ... + // XPosition positions[]; + // int positionsCount = mTrader.GetInDrawdownPositions( + // positions // + // ); + // if (!IsValidSize(positionsCount)) + // { + // return result; + // } + + // // + // double supportDistance = SupportDistance(); + // int maxNUmberOfSupports = MaxNumberOfSupports(); + // double maxAllowedVolume = MaxAllowedSupportVolume(); + // double volumeMultiplier = SupportVolumeMultiplier(); + + // // + // X121SignalGenerator signalGenerator; + + // // + // for (int i = 0; i < positionsCount; i++) + // { + // // + // XPosition iPosition = positions[i]; + + // // + // // Check Position is not Support Position ... + // bool isSupport = IsSupport(iPosition.comment); + // if (isSupport) + // { + // continue; + // } + + // // + // // Find Related Setp ... + // int idx = FindSetupIndex( + // iPosition.symbol, + // setups // + // ); + // if (!IsValidIndex(idx)) + // { + // continue; + // } + + // // + // int index = FindIndex(iPosition.ticket); + // if (!IsValidIndex(index)) + // { + // continue; + // } + + // // + // // Check Position must atleast 20 Point in drawdown ... + + // // + // double onePointPrice = PointToPrice( + // 1, + // iPosition.symbol // + // ); + + // // + // double iExitPrice = GetExit( + // iPosition.symbol, + // iPosition.type // + // ); + + // // + // double priceDiff = MathAbs(iPosition.entry - iExitPrice); + // double priceDiffPoints = priceDiff / onePointPrice; + + // // + // bool isFarEnough = priceDiffPoints >= (supportDistance / 2); + // if (!isFarEnough) + // { + // continue; + // } + + // // + // // Check Support COunts and etc ... + // // implementing supporting mechanis ... + + // // + // } + + // // + // result = ArraySize(signals); + // return result; } @@ -4029,10 +4244,10 @@ public: } // - // Handle Support of Supports here ... - void OnPositionsChangedHandler(int count) override + // Orders Changed ... + void OnOrdersChangedHandler(int count) override { - // EQMHandleSupportProtection(count); + EQMHandleSupportProtection(count); } // @@ -4756,98 +4971,103 @@ private: } // - void EQMHandleSupportProtection(int positionsCount) + void EQMHandleSupportProtection(int count) { - // - if (!IsValidSize(positionsCount)) - { - return; - } + // // + // if (IsValidSize(count)) + // { + // return; + // } - // - ulong ticket = mTrader.GetLastOpenPositionTicket(); - XPosition position; - bool hasPosition = mTrader.GetPosition( - ticket, - position // - ); - if (!hasPosition) - { - return; - } + // // + // // This means on order Executed ... + // ulong ticket = mTrader.GetLastOpenPositionTicket(); - // - bool isSupport = IsSupport(position.comment); - if (!isSupport) - { - return; - } + // // + // XPosition position; + // bool hasPosition = mTrader.GetPosition( + // ticket, + // position // + // ); + // if (!hasPosition) + // { + // return; + // } - // - bool isINDIRSupport = position.provider == XEQMSupportToken + XINDirectionalToken; - if (!isINDIRSupport) - { - return; - } + // // + // bool isSupport = IsSupport(position.comment); + // if (!isSupport) + // { + // return; + // } - // - ulong parentTicket = ExtractParentTicket(position.comment); - XPosition parent; - bool hasParent = mTrader.GetPosition( - parentTicket, - parent // - ); - if (!hasParent) - { - return; - } + // // + // bool isInDirectional = position.provider == XEQMSupportToken + XINDirectionalToken; + // if (!isInDirectional) + // { + // return; + // } - // - // Remove Parent TP SL ... - if (parent.tp > 0 || parent.sl > 0) - { - // - string comment = XEQMSupportToken + "Remoove TPSL due Protect ..."; - bool isModeified = mTrader.Modify( - parentTicket, - 0, - 0, - comment // - ); - if (isModeified) - { - // - string message = XEQMSupportToken + " Removes Position (" + ToString(parentTicket) + ")' TP/SL Due Start Supporting ..."; + // // + // XPosition parent; + // ulong parentTicket = ExtractParentTicket(position.comment); + // bool hasParent = mTrader.GetPosition( + // parentTicket, + // parent // + // ); - // - Alert(message); - } - } + // // + // if (!hasParent) + // { + // return; + // } - // - XSignal signal; - X121SignalGenerator signalGenerator; - bool hasSupport = signalGenerator.PrepareDirectionalSupportSignals( - position, - parent, - signal // - ); - if (!hasSupport) - { - return; - } + // // + // // Remove Parent TP SL ... + // if (parent.tp > 0 || parent.sl > 0) + // { + // // + // string comment = XEQMSupportToken + " Remoove TP/SL due Protect ..."; + // bool isModeified = mTrader.Modify( + // parentTicket, + // 0, + // 0, + // comment // + // ); + // if (isModeified) + // { + // // + // string message = XEQMSupportToken + " Removes Position (" + ToString(parentTicket) + ")' TP/SL Due Start Supporting ..."; - // - XSignal signals[]; + // // + // Alert(message); + // } + // } - // - AddRef( - signal, - signals // - ); + // // + // XSignal signal; + // X121SignalGenerator signalGenerator; + // bool hasSupport = signalGenerator.PrepareDirectionalSupportSignals( + // position, + // parent, + // signal // + // ); + // if (!hasSupport) + // { + // return; + // } - // - EQMExecuteSignals(signals); + // // + // XSignal signals[]; + + // // + // AddRef( + // signal, + // signals // + // ); + + // // + // EQMExecuteSignals(signals); } // diff --git a/Classes/x-saherelm.xea.class.mq5 b/Classes/x-saherelm.xea.class.mq5 index 8c6fb145..ce0a253f 100644 --- a/Classes/x-saherelm.xea.class.mq5 +++ b/Classes/x-saherelm.xea.class.mq5 @@ -497,11 +497,11 @@ public: // this Method Calls For Retrieve Support Signal ... // if it's Provided, Execute it ... virtual bool RequestForSupport( - XSignal &support, // Holds Support Signal, if Provided - string provider, // Positions Provided by Specific Provider - string symbol, // Trading Symbol + XSignal &support, // Holds Support Signal, if Provided + string provider, // Positions Provided by Specific Provider + string symbol, // Trading Symbol ENUM_X_POSITION_TYPES type, // Trading Type - ENUM_TIMEFRAMES period // Specified Time Frame + ENUM_TIMEFRAMES period // Specified Time Frame ) { return false; @@ -1091,7 +1091,7 @@ protected: // XPosition positions[]; int positionsCount = mTrader.GetPositions(positions); - result = IsValidSize(positions); + result = positionsCount > 1; if (!result) { return result; @@ -1264,7 +1264,7 @@ protected: void HandleGuardHedgeAction( string provider, // Positions Provided by Specific Provider string symbol, // Trading Symbol - ENUM_X_POSITION_TYPES type, // Trading Type + ENUM_X_POSITION_TYPES type, // Trading Type ENUM_TIMEFRAMES period, // Specified Time Frame double minProfitForHedging = 1, // Specified Profit for Hedge double minProfitForHedgingVolumeFactor = 0.01 // Specified Volume Factor for Hedge @@ -1323,7 +1323,7 @@ protected: void HandleGuardCloseInProfitAction( string provider, // Positions Provided by Specific Provider string symbol, // Trading Symbol - ENUM_X_POSITION_TYPES type, // Trading Type + ENUM_X_POSITION_TYPES type, // Trading Type ENUM_TIMEFRAMES period, // Specified Time Frame ENUM_X_POSITION_SELECT_METHODS method = NULL // Select Method ) @@ -1365,7 +1365,7 @@ protected: void HandleGuardCloseInDrawdownAction( string provider, // Positions Provided by Specific Provider string symbol, // Trading Symbol - ENUM_X_POSITION_TYPES type, // Trading Type + ENUM_X_POSITION_TYPES type, // Trading Type ENUM_TIMEFRAMES period, // Specified Time Frame ENUM_X_POSITION_SELECT_METHODS method = NULL // Select Method ) @@ -1405,11 +1405,11 @@ protected: // // Force Close Specified Set Of Position ... void HandleGuardForceCloseAction( - string provider, // Positions Provided by Specific Provider - string symbol, // Trading Symbol + string provider, // Positions Provided by Specific Provider + string symbol, // Trading Symbol ENUM_X_POSITION_TYPES type, // Trading Type - ENUM_TIMEFRAMES period, // Specified Time Frame - ulong ticket = 0 // Specific Ticket + ENUM_TIMEFRAMES period, // Specified Time Frame + ulong ticket = 0 // Specific Ticket ) { // @@ -1482,11 +1482,11 @@ protected: // // Trailing Specified Set Of Positions Stop Losses ... void HandleGuardTrailingStopAction( - string provider, // Positions Provided by Specific Provider - string symbol, // Trading Symbol + string provider, // Positions Provided by Specific Provider + string symbol, // Trading Symbol ENUM_X_POSITION_TYPES type, // Trading Type - ENUM_TIMEFRAMES period, // Specified Time Frame - double sl // Desired SL Value ... + ENUM_TIMEFRAMES period, // Specified Time Frame + double sl // Desired SL Value ... ) { // @@ -1560,10 +1560,10 @@ protected: // // Add Support Signal for Specified Positions ... void HandleGuardAddSupportPositionAction( - string provider, // Positions Provided by Specific Provider - string symbol, // Trading Symbol + string provider, // Positions Provided by Specific Provider + string symbol, // Trading Symbol ENUM_X_POSITION_TYPES type, // Trading Type - ENUM_TIMEFRAMES period // Specified Time Frame + ENUM_TIMEFRAMES period // Specified Time Frame ) { // @@ -1614,10 +1614,10 @@ protected: // // Cancel all Placed Orders ... void HandleGuardCancelPendingOrdersAction( - string provider, // Positions Provided by Specific Provider - string symbol, // Trading Symbol + string provider, // Positions Provided by Specific Provider + string symbol, // Trading Symbol ENUM_X_POSITION_TYPES type, // Trading Type - ENUM_TIMEFRAMES period // Specified Time Frame + ENUM_TIMEFRAMES period // Specified Time Frame ) { // diff --git a/Libraries/x-saherelm.xtrade.lib.mq5 b/Libraries/x-saherelm.xtrade.lib.mq5 index f97d8b58..6c020205 100644 --- a/Libraries/x-saherelm.xtrade.lib.mq5 +++ b/Libraries/x-saherelm.xtrade.lib.mq5 @@ -202,7 +202,7 @@ bool IsLong(ENUM_X_POSITION_TYPES value) } // -bool ToPositinType( +bool ToPositionType( ENUM_POSITION_TYPE &dest, ENUM_X_POSITION_TYPES source // )