diff --git a/Classes/x-saherelm.x121.setup.xea.mq5 b/Classes/x-saherelm.x121.setup.xea.mq5 index 636aaace..10784acf 100644 --- a/Classes/x-saherelm.x121.setup.xea.mq5 +++ b/Classes/x-saherelm.x121.setup.xea.mq5 @@ -681,9 +681,10 @@ struct XTradeInfo : selectedEntry; // - double nextVolume = rLevel % 2 == 1 - ? volume - : volume * volumeMultiplier; + double nextVolume = selectedVolume * volumeMultiplier; + // rLevel % 2 == 1 + // ? volume + // : volume * volumeMultiplier; // double zoneUpper = @@ -1855,11 +1856,7 @@ public: // Trailling ... if (EnableTrailling()) { - // - if (!forceState) - { - int traillings = HandleTrailling(); - } + int traillings = HandleTrailling(); } // @@ -3699,12 +3696,12 @@ public: : signalGenerator.HasShortVerifications(otherConditions)) // ; - + // bool forceState = ForceState(); // - if (!isValid || forceState) + if (!isValid || (forceState && isValid)) { // // Clear Other Conditions ... @@ -3744,7 +3741,8 @@ public: mBullishSigns, mBearishScore, mBearishSigns, - otherConditions // + otherConditions, + isValid // ); continue; } @@ -4361,40 +4359,64 @@ private: // EQM Functions ... // - void DoEQMForceClose() + bool DoEQMForceClose( + double requiredProfit = 0 // Required Profit For EQM Force Hedge ... + ) { // - bool canDo = mForceCloseProfit > 0; + bool result = false; + + // + double hedgingProfit = + requiredProfit == 0 + ? mForceCloseProfit + : requiredProfit; + bool canDo = hedgingProfit > 0; if (!canDo) { - return; + return result; } // XPosition positions[]; int positionsCount = mTrader.GetPositions(positions); - if (!IsValidSize(positionsCount)) + result = IsValidSize(positionsCount); + if (!result) { - return; + return result; } // double profit = SpecifiedCalculatePositionsProfit(positions); - if (profit > 0 && profit > mForceCloseProfit) + result = + hedgingProfit > 0 + ? profit > 0 && profit > hedgingProfit + : profit <= hedgingProfit; + if (!result) { - // - string comment = XEQMSupportToken + " Hedge ..."; - int closed = Close( - positions, - comment // - ); - - // - if (IsValidSize(closed)) - { - // - } + return result; } + + // + string comment = XEQMSupportToken + " Hedge ..."; + int closed = Close( + positions, + comment // + ); + + // + result = IsValidSize(closed); + + // + // Handle Sleeping ... + int restingTime = RestingAfterHedge(); + if (result && IsValidSize(restingTime)) + { + Sleep(restingTime * 1000); + } + + // + return result; } // @@ -4487,6 +4509,43 @@ private: // Checking Force Sate Conditions ... if (ForceState()) { + // + // Check Force State Hedging ... + bool isHedged = DoEQMForceClose(2); + if (isHedged) + { + // + ForceState(false); + return; + } + + // + isHedged = DoEQMForceClose(1); + if (isHedged) + { + // + ForceState(false); + return; + } + + // + isHedged = DoEQMForceClose(0.5); + if (isHedged) + { + // + ForceState(false); + return; + } + + isHedged = DoEQMForceClose(0.3); + if (isHedged) + { + // + ForceState(false); + return; + } + + // return; } @@ -4532,7 +4591,47 @@ private: ; if (isForceState) { + // ForceState(true); + + // + return; + } + + // + XPosition supports[]; + int supportsCount = ExtractSupports( + positions, + supports // + ); + if (!IsValidSize(supportsCount)) + { + return; + } + + // + int maxSupports = mTradeHandler.EnableSupport() + ? MaxNumberOfSupports() + : 0; + int maxGrids = mTradeHandler.EnableGrid() + ? MaxNumberOfGrids() + : 0; + int maxRecoveries = mTradeHandler.EnableRecovery() + ? MaxNumberOfRecoveries() + : 0; + + // + // Supporting Force States ... + bool isProtectionForce = + supportsCount >= 1; + // supportsCount >= (maxSupports + maxGrids + maxRecoveries); + if (isProtectionForce) + { + // + ForceState(true); + + // + return; } // @@ -4541,14 +4640,15 @@ private: // // 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 + 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 + bool isValid = false // IsValid Signal ... ) { // @@ -4559,199 +4659,7 @@ private: } // - // 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; - } - } + // TODO: Implement it ... } // diff --git a/Documents/BKP/usefull.mq5 b/Documents/BKP/usefull.mq5 index 01ce9cd4..dd190bbf 100644 --- a/Documents/BKP/usefull.mq5 +++ b/Documents/BKP/usefull.mq5 @@ -1409,3 +1409,336 @@ // } // } // } + + +/////////////////////////////////////////////////////////////// + + // + // 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; + } + } + + +//////////////////////////////////////////////////////////////////////// + + // + 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); + + // + if (isValid) + { + // + // Ignore Hind Time Frame Signals ... + + // + signal.volume *= 1; + + // + // Here we Can do Signals SL TP Manipulations ... + signal.tp = 0; + signal.sl = 0; + + // + signal.provider = XEQMSupportToken; + signal.comment = GenerateEQMSupportTag(0); + + // + bool isLong = IsLong(signal.type); + + // + bool canDirect = + // + (isLong && + longProfits > shortProfits) + // + || + // + (!isLong && + shortProfits > longProfits) + // + ; + bool canInDirect = + // + (isLong && + shortProfits > longProfits) + // + || + // + (!isLong && + longProfits > shortProfits) + // + ; + + // + XSignal tmpSignal; + if (canDirect) + { + tmpSignal = signal; + } + else + { + // + bool isGenerated = signal.GenerateOpposit( + tmpSignal // + ); + + // + if (!isGenerated) + { + tmpSignal.Clean(); + } + } + + // + if (tmpSignal.IsValid()) + { + // + 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; + } diff --git a/Experts/x-saherelm.x121.ea.mq5 b/Experts/x-saherelm.x121.ea.mq5 index dc54cd24..2b956dec 100644 --- a/Experts/x-saherelm.x121.ea.mq5 +++ b/Experts/x-saherelm.x121.ea.mq5 @@ -81,7 +81,7 @@ input double x121EAMaxAllowedSupportVolume = 0.8; // Max Allowed Support Signals // input group "Grid"; -input bool x121EAAllowGrid = false; // Allow Grid Signals +input bool x121EAAllowGrid = true; // Allow Grid Signals input int x121EAMaxNumberOfGrids = 5; // Max Number of Grid Signals input double x121EAGridDistance = 100; // Grid Distance input double x121EAGridVolumeMultiplier = 2.0; // Grid Volume Multiplier @@ -89,8 +89,8 @@ input double x121EAMaxAllowedGridVolume = 0.8; // Max Allowed Grid Signals Volum // input group "Recovery"; -input bool x121EAAllowRecovery = true; // Allow Recovery Signals -input int x121EAMaxNumberOfRecoveries = 2; // Max Number of Recovery Signals +input bool x121EAAllowRecovery = true; // Allow Recovery Signals +input int x121EAMaxNumberOfRecoveries = 3; // Max Number of Recovery Signals input double x121EARecoveryDistance = 100; // Recovery Distance input double x121EARecoveryVolumeMultiplier = 2; // Recovery Volume Multiplier input double x121EAMaxAllowedRecoveryVolume = 0.8; // Max Allowed Recovery Signals Volume