From 326d3f49297db67d8a9c3305230adb1b765f83dd Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Tue, 18 Jun 2024 05:14:13 +0330 Subject: [PATCH] last works on Force State Management ... --- Classes/x-saherelm.x121.setup.class.mq5 | 154 +++++-- Classes/x-saherelm.x121.setup.xea.mq5 | 542 +++++++++++++++++++++--- Experts/x-saherelm.x121.ea.mq5 | 2 +- Libraries/x-saherelm.xtrade.lib.mq5 | 30 +- 4 files changed, 628 insertions(+), 100 deletions(-) diff --git a/Classes/x-saherelm.x121.setup.class.mq5 b/Classes/x-saherelm.x121.setup.class.mq5 index 4dfa25e6..e09f9b0e 100644 --- a/Classes/x-saherelm.x121.setup.class.mq5 +++ b/Classes/x-saherelm.x121.setup.class.mq5 @@ -2061,16 +2061,13 @@ struct X121SignalGenerator } // - bool IsConditionsValidForLong( - X121SetupConditions &conditions, // Conditions - int verification = 4 // Number of Verifications + // Count Number of Long Validations ... + int CountLongValidations( + X121SetupConditions &conditions // Conditions ) { // - bool result = false; - - // - int signs = 0; + int result = 0; // // XTD ... @@ -2082,7 +2079,7 @@ struct X121SignalGenerator ; if (isTDPassed) { - signs++; + result++; } // @@ -2100,7 +2097,7 @@ struct X121SignalGenerator ; if (isHKPassed) { - signs++; + result++; } // @@ -2115,7 +2112,7 @@ struct X121SignalGenerator ; if (isCHEPassed) { - signs++; + result++; } // @@ -2142,7 +2139,7 @@ struct X121SignalGenerator ; if (isICHPassed) { - signs++; + result++; } // @@ -2155,7 +2152,7 @@ struct X121SignalGenerator ; if (isSTRPassed) { - signs++; + result++; } // @@ -2168,7 +2165,7 @@ struct X121SignalGenerator ; if (isMRBPassed) { - signs++; + result++; } // @@ -2182,7 +2179,7 @@ struct X121SignalGenerator ; if (isVLMPassed) { - signs++; + result++; } // @@ -2195,7 +2192,7 @@ struct X121SignalGenerator ; if (isSSLCPassed) { - signs++; + result++; } // @@ -2210,7 +2207,7 @@ struct X121SignalGenerator ; if (isHULLPassed) { - signs++; + result++; } // @@ -2224,7 +2221,7 @@ struct X121SignalGenerator ; if (isADXTDPassed) { - signs++; + result++; } // @@ -2252,13 +2249,31 @@ struct X121SignalGenerator ; if (isMATDPassed) { - signs++; + result++; } + // + return result; + } + + // + bool IsConditionsValidForLong( + X121SetupConditions &conditions, // Conditions + int verifications = 4 // Number of Verifications + ) + { + // + bool result = false; + + // + int signs = CountLongValidations(conditions); + // result = // - signs > verification + verifications <= 0 + ? signs > 0 + : signs >= verifications // ; @@ -2303,16 +2318,12 @@ struct X121SignalGenerator } // - bool IsConditionsValidForShort( - X121SetupConditions &conditions, // Conditions - int verification = 4 // Number of Verifications + int CountShortValidations( + X121SetupConditions &conditions // Conditions ) { // - bool result = false; - - // - int signs = 0; + int result = 0; // // XTD ... @@ -2324,7 +2335,7 @@ struct X121SignalGenerator ; if (isTDPassed) { - signs++; + result++; } // @@ -2342,7 +2353,7 @@ struct X121SignalGenerator ; if (isHKPassed) { - signs++; + result++; } // @@ -2357,7 +2368,7 @@ struct X121SignalGenerator ; if (isCHEPassed) { - signs++; + result++; } // @@ -2384,7 +2395,7 @@ struct X121SignalGenerator ; if (isICHPassed) { - signs++; + result++; } // @@ -2397,7 +2408,7 @@ struct X121SignalGenerator ; if (isSTRPassed) { - signs++; + result++; } // @@ -2410,7 +2421,7 @@ struct X121SignalGenerator ; if (isMRBPassed) { - signs++; + result++; } // @@ -2424,7 +2435,7 @@ struct X121SignalGenerator ; if (isVLMPassed) { - signs++; + result++; } // @@ -2437,7 +2448,7 @@ struct X121SignalGenerator ; if (isSSLCPassed) { - signs++; + result++; } // @@ -2452,7 +2463,7 @@ struct X121SignalGenerator ; if (isHULLPassed) { - signs++; + result++; } // @@ -2466,7 +2477,7 @@ struct X121SignalGenerator ; if (isADXTDPassed) { - signs++; + result++; } // @@ -2494,13 +2505,31 @@ struct X121SignalGenerator ; if (isMATDPassed) { - signs++; + result++; } + // + return result; + } + + // + bool IsConditionsValidForShort( + X121SetupConditions &conditions, // Conditions + int verifications = 4 // Number of Verifications + ) + { + // + bool result = false; + + // + int signs = CountShortValidations(conditions); + // result = // - signs > verification + verifications <= 0 + ? signs > 0 + : signs >= verifications // ; @@ -3410,3 +3439,52 @@ private: // Props ... X121SetupInputs inputs; // Setup Configurations ... }; + +// +// Tools ... + +// +int FindSignalConditionsIndex( + XSignal &signal, + X121SetupConditions &conditions[] // +) +{ + // + int result = -1; + + // + // Check Signal is Valid ... + if (!signal.IsValid()) + { + return result; + } + + // + // Check Conditions Count ... + int conditionsCount = ArraySize(conditions); + if (!IsValidSize(conditionsCount)) + { + return result; + } + + // + for (int i = 0; i < conditionsCount; i++) + { + // + X121SetupConditions iConditions = conditions[i]; + + // + if (signal.symbol == iConditions.symbol && + signal.period == iConditions.period) + { + // + result = i; + break; + } + } + + // + return result; +} + +// \ No newline at end of file diff --git a/Classes/x-saherelm.x121.setup.xea.mq5 b/Classes/x-saherelm.x121.setup.xea.mq5 index 78c0fc05..636aaace 100644 --- a/Classes/x-saherelm.x121.setup.xea.mq5 +++ b/Classes/x-saherelm.x121.setup.xea.mq5 @@ -681,7 +681,9 @@ struct XTradeInfo : selectedEntry; // - double nextVolume = selectedVolume * volumeMultiplier; + double nextVolume = rLevel % 2 == 1 + ? volume + : volume * volumeMultiplier; // double zoneUpper = @@ -821,15 +823,15 @@ struct XTradeInfo string comment = GenerateSupportTag(ticket); signal.comment = comment; + // + rLevel++; + // PrepareNextRecovery( distance, volumeMultiplier, rType // ); - - // - rLevel++; } // @@ -1838,8 +1840,9 @@ public: // // Handle Do All Protecting ... int DoProtect( - XSignal &signals[], // if PRotecting Generate Some Support Signals, Holds them here - XSCX121SetupCycles *&setups[] // Number of Setups + XSignal &signals[], // if PRotecting Generate Some Support Signals, Holds them here + XSCX121SetupCycles *&setups[], // Number of Setups + bool forceState = false // ) { // @@ -1853,7 +1856,10 @@ public: if (EnableTrailling()) { // - int traillings = HandleTrailling(); + if (!forceState) + { + int traillings = HandleTrailling(); + } } // @@ -3070,6 +3076,18 @@ public: mForceCloseProfit = value; } + // + bool ForceState() + { + return mForceState; + } + + // + void ForceState(bool value) + { + mForceState = value; + } + // // Overrides ... @@ -3681,36 +3699,83 @@ public: : signalGenerator.HasShortVerifications(otherConditions)) // ; + + // + bool forceState = ForceState(); // - if (!isValid) + if (!isValid || forceState) { + // + // Clear Other Conditions ... + Clean(otherConditions); + + // + // Fill All Conditions to Other Conditions ... + AddRef( + cConditions, + otherConditions // + ); + AddRef( + sConditions, + otherConditions // + ); + AddRef( + mConditions, + otherConditions // + ); + AddRef( + lConditions, + otherConditions // + ); + AddRef( + hConditions, + otherConditions // + ); + + // + // If Recieved Any Unverified Signal + // we Passed it to EQM Manager ... + EQMHandleInvalidSignal( + signal, + pushers, + mVolumeOSC, + mBullishScore, + mBullishSigns, + mBearishScore, + mBearishSigns, + otherConditions // + ); continue; } // - // Add Signal to Execution Result ... - AddRef( - signal, - signals // - ); + if (!forceState) + { + // + // Add Signal to Execution Result ... + AddRef( + signal, + signals // + ); - // - // Add new Signal Info ... - mTradeHandler.Add( - signal, - pushers, - mVolumeOSC, - mBullishSigns, - mBearishSigns, - mBullishScore, - mBearishScore, - cConditions, - sConditions, - mConditions, - lConditions, - hConditions // - ); + // + // Add new Signal Info ... + mTradeHandler.Add( + signal, + pushers, + mVolumeOSC, + mBullishSigns, + mBearishSigns, + mBullishScore, + mBearishScore, + cConditions, + sConditions, + mConditions, + lConditions, + hConditions // + ); + } } } @@ -4122,6 +4187,15 @@ private: int mCloseOnSpecificTime; // Close All Trades in Specific Time int mStartSignallingHoure; // Start Signalling at Specific Time + // + // Force State: + // we can do many things in force state: + // - Prepare Force Hedging; + // - Receive Signals with minimal Verifications; + // - Increase Volume Size of in Profit Positions; + // - etc ... + bool mForceState; // Determine in Force State or not + // XTradeHandler *mTradeHandler; // Handle all Event which related to Signals and Positions @@ -4327,11 +4401,20 @@ private: // Do Gridding Mechanism ... void DoEQMProtect() { + // + // Check if there are open Positions + // and their doesn't have TP and SL and + // 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(); + // XSignal supports[]; int supportsCount = mTradeHandler.DoProtect( supports, - mSetups // + mSetups, + ForceState() // ); // @@ -4341,35 +4424,6 @@ private: } } - // - void EQMExecuteSignals( - XSignal &signals[] // - ) - { - // - int signalsCount = ArraySize(signals); - if (!IsValidSize(signals)) - { - return; - } - - // - // - // Execute Supports ... - ENUM_X_SIGNAL_EXECUTION_RESULT states[]; - int executed = ExecuteSignals( - signals, - states, - false // Ignore Policies ... - ); - if (executed != signalsCount) - { - // - // TODO: Dow What We Want ... - // Print("Error On Executing Support Signal ..."); - } - } - // // Handle Close Trades on Specific Time ... void DoEQMCloseOnSpecificTime() @@ -4406,6 +4460,376 @@ private: Alert(message); } } + + // + // Handle Force State Management ... + void EQMHandleForceState() + { + // + XPosition positions[]; + int positionsCount = mTrader.GetPositions(positions); + if (!IsValidSize(positionsCount)) + { + // + // Disable Force State if there isn't any open Positions + // and it's Enabled ... + // this means Hedged positions ... + if (ForceState()) + { + ForceState(false); + } + + // + return; + } + + // + // Checking Force Sate Conditions ... + if (ForceState()) + { + return; + } + + // + int longs = 0; + double longVolumes = 0; + double longProfits = 0; + + // + int shorts = 0; + double shortVolumes = 0; + double shortProfits = 0; + + // + // Retrieve Required Informations of Positions ... + CountPositions( + positions, + longs, + longProfits, + longVolumes, + shorts, + shortProfits, + shortVolumes // + ); + + // + // Retrieve Youngest Age ... + XPosition youngest; + int youngestAge = GetYoungest( + youngest, + positions // + ); + + // + // Now Comparing items for enabling force State ... + bool isForceState = + // + longs > 0 && + shorts > 0 && + youngestAge >= 20 && // TODO: Make this Configurable ... + longVolumes == shortVolumes + // + ; + if (isForceState) + { + ForceState(true); + } + + // + } + + // + // Handle any Invalid Signals ... + void EQMHandleInvalidSignal( + XSignal &signal, // Invalid Signal + int pushers, // Pushers + double volumeOSC, // Volume Summary of Signal + double bullishScore, // Bullish Score Summary + int bullishSigns, // Number of Bullish Signs + double bearishScore, // Bearish Score Summary + int bearishSigns, // Number of Bearish Signs + X121SetupConditions &conditions[] // All Time Frames Conditions + ) + { + // + // Check Force State ... + if (!ForceState()) + { + return; + } + + // + // Create an Instance of Signal Generator Structure ... + X121SignalGenerator signalGenerator; + + // + int signalConditionsIDX = FindSignalConditionsIndex( + signal, + conditions // + ); + if (!IsValidIndex(signalConditionsIDX)) + { + return; + } + + // + int longValidations = 0; + int shortValidations = 0; + + // + bool isOSCValidForLong = signalGenerator + .IsOSCLongVerified(conditions[signalConditionsIDX]); + bool isOSCValidForShort = signalGenerator + .IsOSCShortVerified(conditions[signalConditionsIDX]); + + // + // Calculate Long/Short Validations ... + for (int i = 0; i < ArraySize(conditions); i++) + { + // + int longValids = signalGenerator + .CountLongValidations(conditions[i]); + longValidations += longValids; + + // + int shortValids = signalGenerator + .CountShortValidations(conditions[i]); + shortValidations += shortValids; + } + + // + bool isLong = IsLong(signal.type); + + // + // Reading Required Informations ... + XPosition positions[]; + int positionsCount = mTrader.GetPositions(positions); + if (!IsValidSize(positionsCount)) + { + return; + } + + // + int longs = 0; + double longVolumes = 0; + double longProfits = 0; + + // + int shorts = 0; + double shortVolumes = 0; + double shortProfits = 0; + + // + // Retrieve Required Informations of Positions ... + CountPositions( + positions, + longs, + longProfits, + longVolumes, + shorts, + shortProfits, + shortVolumes // + ); + + // + longVolumes = NormalizeDouble(longVolumes, 2); + shortVolumes = NormalizeDouble(shortVolumes, 2); + + // + // Select Signal for Execution ... + bool canSelect = + // + ((isLong && + longProfits > 0 && + bullishScore > 20) + // + || + // + (!isLong && + shortProfits > 0 && + bearishScore > 0)) + // + && + // + longVolumes == shortVolumes + // + ; + if (canSelect) + { + // + // Here we Can do Signals SL TP Manipulations ... + signal.tp = 0; + signal.sl = 0; + + // + XSignal signals[]; + AddRef( + signal, + signals // + ); + + // + // Add new Signal Info ... + mTradeHandler.Add( + signal, + pushers, + volumeOSC, + bullishSigns, + bearishSigns, + bullishScore, + bearishScore, + conditions[0], + conditions[1], + conditions[2], + conditions[3], + conditions[4] // + ); + + // + EQMExecuteSignals(signals); + + // + return; + } + + // + canSelect = + // + ((isLong && shortProfits > 0) || + (!isLong && longProfits > 0)) + // + && + // + longVolumes == shortVolumes + // + ; + if (canSelect) + { + // + // Generate Opposit Signal ... + + // + signal.tp = 0; + signal.sl = 0; + + // + XSignal oppositSignal; + bool isGenerated = signal.GenerateOpposit( + oppositSignal // + ); + + // + if (isGenerated) + { + // + XSignal signals[]; + AddRef( + oppositSignal, + signals // + ); + + // + // Add new Signal Info ... + mTradeHandler.Add( + oppositSignal, + pushers, + volumeOSC, + bullishSigns, + bearishSigns, + bullishScore, + bearishScore, + conditions[0], + conditions[1], + conditions[2], + conditions[3], + conditions[4] // + ); + + // + EQMExecuteSignals(signals); + + // + return; + } + } + } + + // + void EQMExecuteSignals( + XSignal &signals[] // + ) + { + // + int signalsCount = ArraySize(signals); + if (!IsValidSize(signals)) + { + return; + } + + // + // + // Execute Supports ... + ENUM_X_SIGNAL_EXECUTION_RESULT states[]; + int executed = ExecuteSignals( + signals, + states, + false // Ignore Policies ... + ); + if (executed != signalsCount) + { + // + // TODO: Dow What We Want ... + // Print("Error On Executing Support Signal ..."); + } + + // + // Remove Supported Signals TP and SL and leave them for Handling using HEDGE Mechanism ... + if (IsValidSize(executed)) + { + // + for (int i = 0; i < signalsCount; i++) + { + // + ENUM_X_SIGNAL_EXECUTION_RESULT iState = states[i]; + if (iState != X_SIGNAL_EXECUTION_SUCCEED) + { + continue; + } + + // + XSignal iSignal = signals[i]; + if (!iSignal.IsValid() || !iSignal.IsExecuted()) + { + continue; + } + + // + ulong iParentTicket = ExtractParentTicket(iSignal.comment); + if (iParentTicket <= 0) + { + continue; + } + + // + // Remove TP and SL of Supported Positions ... + string comment = "EQM Remove TPSL ..."; + bool isModified = Modify( + iParentTicket, + 0, + 0, + comment // + ); + if (isModified) + { + // + // TODO: + // do What we want ... + } + } + } + } + + // }; // diff --git a/Experts/x-saherelm.x121.ea.mq5 b/Experts/x-saherelm.x121.ea.mq5 index 5adb7ec1..dc54cd24 100644 --- a/Experts/x-saherelm.x121.ea.mq5 +++ b/Experts/x-saherelm.x121.ea.mq5 @@ -58,7 +58,7 @@ input bool x121EAIgnoreTP = false; // Ignore Calculated TP // // Protection ... input group "Signalling"; -input int x121EAStartSignallingHoure = 0; // Start Signalling at Specific Time +input int x121EAStartSignallingHoure = 3; // Start Signalling at Specific Time input int x121EAEndSignallingHoure = 22; // End Signalling at Specific Time input int x121EACloseOnSpecificTime = -1; // Close All Trades in Specific Time input bool x121EACloseOnOpposit = false; // Close all Positions on Opposit diff --git a/Libraries/x-saherelm.xtrade.lib.mq5 b/Libraries/x-saherelm.xtrade.lib.mq5 index 9f5fd2ba..6380e9d6 100644 --- a/Libraries/x-saherelm.xtrade.lib.mq5 +++ b/Libraries/x-saherelm.xtrade.lib.mq5 @@ -4181,6 +4181,32 @@ bool IsSupport(string value) return result; } +// +ulong ExtractParentTicket(string value) +{ + // + ulong result = 0; + + // + if (!IsSupport(value)) + { + return result; + } + + // + result = ExtractSupportedTicket(value); + if (result > 0) + { + return result; + } + + // + result = ExtractEQMSupportedTicket(value); + + // + return result; +} + // // SL Trails ... @@ -4482,14 +4508,14 @@ void CountPositions( // longs++; longProfits += iPosition.profit; - longVolumes = iPosition.volume; + longVolumes += iPosition.volume; } else { // shorts++; shortProfits += iPosition.profit; - shortVolumes = iPosition.volume; + shortVolumes += iPosition.volume; } } }