From dc4ee66b83a9c6f9fdef1c0d342ad0ecdd52ef47 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Thu, 23 May 2024 09:15:17 +0330 Subject: [PATCH] prepare X5121 EA for Works ... --- Classes/x-saherelm.x5.provider.class.mq5 | 7 - Experts/x-test.x5121ea.mq5 | 293 ++++++++++++++++++++--- 2 files changed, 264 insertions(+), 36 deletions(-) diff --git a/Classes/x-saherelm.x5.provider.class.mq5 b/Classes/x-saherelm.x5.provider.class.mq5 index e0116eff..6babc825 100644 --- a/Classes/x-saherelm.x5.provider.class.mq5 +++ b/Classes/x-saherelm.x5.provider.class.mq5 @@ -2934,14 +2934,7 @@ public: // if (result) { - // mWaitsUntilNewBar = true; - - // - string providerTypeStr = GetSymbol() + ", " + provider + "(" + (hasLong ? "Long" : "Short") + ")"; - string mConditionsStr = mConditions.GenerateSummary(providerTypeStr, true); - mConditionsCollector.Add(mConditionsStr); - Print(mConditionsStr); } } diff --git a/Experts/x-test.x5121ea.mq5 b/Experts/x-test.x5121ea.mq5 index ce91150c..e7ca75bb 100644 --- a/Experts/x-test.x5121ea.mq5 +++ b/Experts/x-test.x5121ea.mq5 @@ -23,6 +23,8 @@ #define ShortName "X5121EA" // +#include "../Classes/x-saherelm.xalert.class.mq5" +#include "../Classes/x-saherelm.xtrade.class.mq5" #include "../Classes/x-saherelm.x5.provider.class.mq5" #include "../Classes/x-saherelm.x121.provider.class.mq5" @@ -302,28 +304,39 @@ struct X5121ProviderDescriptor // // Inputs ... -long x5EAMagicNumber = 78692110; // Magic Number -int x5EASlippage = 10; // Slippgae -string x5EASymbols = "EURUSDb"; // Symbols +long x5121EAMagicNumber = 78692110; // Magic Number +int x5121EASlippage = 10; // Slippgae +string x5121EASymbols = "EURUSDb"; // Symbols // -bool x5EAAllowLong = true; // Allow Long Trades -bool x5EAAllowShort = true; // Allow Short Trades -int x5EAManageInterval = 5; // Manager Check Intervals Seconds -int x5EAMaxAllowedPositions = 15; // Max Allowed Positions -double x5EAMaxAllowedSpread = 0; // Max Allowed Spred for Opening Trades -double x5EAMinProfitPerTrade = 0; // Min Profit Per Trade based On Volume Factor (Hedge) -double x5EAMinProfitPerVolumeFactor = 0; // Min Volume Factor for Calculating Profit (Hedge) -double x5EAMaxAllowedDrawdownFactor = 0; // Max Allowed Drawdown Factor +bool x5121EAAllowLong = true; // Allow Long Trades +bool x5121EAAllowShort = true; // Allow Short Trades +int x5121EAManageInterval = 5; // Manager Check Intervals Seconds +int x5121EAMaxAllowedPositions = 15; // Max Allowed Positions +double x5121EAMaxAllowedSpread = 0; // Max Allowed Spred for Opening Trades +double x5121EAMinProfitPerTrade = 0; // Min Profit Per Trade based On Volume Factor (Hedge) +double x5121EAMinProfitPerVolumeFactor = 0; // Min Volume Factor for Calculating Profit (Hedge) +double x5121EAMaxAllowedDrawdownFactor = 0; // Max Allowed Drawdown Factor // -double x5EAVolume = 0.01; +double x5121EAVolume = 0.01; +// +bool x5121EAEnableAlerts = true; // Enable Alerts +bool x5121EALogAlerts = true; // Log Alerts +bool x5121EATerminalAlerts = false; // Terminal Alerts +bool x5121EAMailAlerts = false; // Mail Alerts +bool x5121EAPushAlerts = false; // Push Alerts // // Vars ... -string x5EASymbol; -ENUM_TIMEFRAMES x5EAPeriod; +string x5121EASymbol; +ENUM_TIMEFRAMES x5121EAPeriod; // -MqlTick x5EATick; +MqlTick x5121EATick; + +// +// Trade Class Instance ... +XSCTrade *x5121EATrader; +XSCAlert *x5121EAAlert; // // Providers ... @@ -337,15 +350,15 @@ X5121ProviderDescriptor x5121EAProviders[]; int OnInit() { // - if (x5EASymbol == NULL || StringLen(x5EASymbol) <= 0) + if (x5121EASymbol == NULL || StringLen(x5121EASymbol) <= 0) { - x5EASymbol = _Symbol; + x5121EASymbol = _Symbol; } // - if (x5EAPeriod == NULL) + if (x5121EAPeriod == NULL) { - x5EAPeriod = _Period; + x5121EAPeriod = _Period; } // @@ -396,13 +409,54 @@ void OnTick() { // // Reading Tick ... - if (!SymbolInfoTick(x5EASymbol, x5EATick)) + if (!SymbolInfoTick(x5121EASymbol, x5121EATick)) { return; } // - // mX5121EA.OnTick(); + // Draw ... + HandleOnDraw(); + + // + // Check Trade Events ... + XOnTradeHandlerState tState; + x5121EATrader.HandleOnTradeEvent(tState); + + // + // First Check For Any Guard Actions ... + // then Handle Guards if Provided ... + XGuard guards[]; + bool hasGuard = CheckForGuard(guards); + if (hasGuard) + { + // + // TODO: Complete this ... + // DoGuards(guards); + } + + // + // Second Check for Position Management or Equity Management + // Actions like Supports or etc ... + bool isIgnored = HandleStateManagement(); + if (isIgnored) + { + return; + } + + // + // Second Check For Signals ... + // then Execute Signals if Provided ... + XSignal signals[]; + int signalsCount = RequestForSignal(signals); + if (signalsCount <= 0) + { + return; + } + + // + // Execute Signals ... + HandleSignalsExecution(signals); } // @@ -418,8 +472,8 @@ bool X5121EAValidateInputs() // result = // - x5EASlippage > 0 && - x5EAMagicNumber > 0 + x5121EASlippage > 0 && + x5121EAMagicNumber > 0 // ; @@ -442,7 +496,7 @@ bool InitialEA() // - XAUUSD/M5; // - result = IsValid(x5EASymbols); + result = IsValid(x5121EASymbols); if (!result) { return result; @@ -452,13 +506,37 @@ bool InitialEA() string symbols[]; int symbolsCount = SplitContent( symbols, - x5EASymbols); + x5121EASymbols); result = symbolsCount > 0; if (!result) { return result; } + // + // Instance XSCTrade Class ... + x5121EATrader = new XSCTrade( + x5121EASlippage, + x5121EAMagicNumber, + x5121EAManageInterval, + x5121EAMaxAllowedSpread, + x5121EAMaxAllowedPositions, + x5121EAMaxAllowedDrawdownFactor + // + ); + + // + // Instantiate Alert Class ... + x5121EAAlert = new XSCAlert( + "X5121EA", + x5121EAEnableAlerts, + x5121EALogAlerts, + x5121EATerminalAlerts, + x5121EAMailAlerts, + x5121EAPushAlerts + // + ); + // // Define Signallers ... ENUM_X5_SIGNAL_PROVIDERS signallers[]; @@ -478,8 +556,8 @@ bool InitialEA() // iDescriptor.symbol = symbols[i]; iDescriptor.period = PERIOD_M5; - iDescriptor.allowLong = x5EAAllowLong; - iDescriptor.allowShort = x5EAAllowShort; + iDescriptor.allowLong = x5121EAAllowLong; + iDescriptor.allowShort = x5121EAAllowShort; Copy( signallers, iDescriptor.signallers); @@ -675,7 +753,15 @@ bool InitialEA() bool isX5InputsValid = iDescriptor.x5Inputs.IsValid(); if (!isX5InputsValid) { - continue; + break; + } + + // + // Init X121 Inputs ... + bool isX121InputsInit = iDescriptor.x121Inputs.Init(); + if (!isX121InputsInit) + { + break; } // @@ -685,7 +771,7 @@ bool InitialEA() bool isDescriptorInitialized = iDescriptor.Init(); if (!isDescriptorInitialized) { - continue; + break; } // @@ -704,4 +790,153 @@ bool InitialEA() return result; } +// +void HandleOnDraw() {} + +// +bool HandleStateManagement() +{ + return false; +} + +// +bool CheckForGuard(XGuard &guards[]) +{ + return false; +} + +// +int RequestForSignal( + XSignal &signals[] // Holds Signals ... +) +{ + // + int result = 0; + + // + Clean(signals); + + // + int descriptorsCount = ArraySize(x5121EAProviders); + if (descriptorsCount <= 0) + { + return result; + } + + // + for (int i = 0; i < descriptorsCount; i++) + { + // + X5121ProviderDescriptor iDescriptor = x5121EAProviders[i]; + + // + bool iHasSignal = iDescriptor.HasSignal(0); + if (iHasSignal) + { + // + AddRef( + iDescriptor.signal, + signals // + ); + } + } + + // + result = ArraySize(signals); + + // + return result; +} + +// +void HandleSignalsExecution(XSignal &signals[]) +{ + // + int signalsCount = ArraySize(signals); + if (signalsCount <= 0) + { + return; + } + + // + // Filter Signals Based on their Types ... + XSignal filteredSignals[]; + + // + // Try to Filter Signals ... + for (int i = 0; i < signalsCount; i++) + { + // + XSignal iSignal = signals[i]; + + // + bool isLong = IsLong(iSignal.type); + + // + if ((isLong && !x5121EAAllowLong) || (!isLong && !x5121EAAllowShort)) + { + continue; + } + + // + AddRef( + iSignal, + filteredSignals // + ); + } + + // + int filteredSignalsCount = ArraySize(filteredSignals); + if (filteredSignalsCount <= 0) + { + return; + } + + // + // Try to Execute Signals ... + ENUM_X_SIGNAL_EXECUTION_RESULT reasons[]; + int executedSignalsCount = x5121EATrader.ExecuteSignals( + filteredSignals, + reasons + // + ); + + // + string message = NULL; + if (executedSignalsCount == 0) + { + // + message = "(" + ToString(filteredSignalsCount) + ") Signals Execution Failed ..."; + + // + for (int j = 0; j < ArraySize(reasons); j++) + { + // + string iReason = ToString(reasons[j]); + + // + // ToString(GetSpread(filteredSignals[j].symbol)) + "," + message += "\n" + ToString(j) + ": " + iReason; + } + } + else if (executedSignalsCount < filteredSignalsCount) + { + // + int diff = filteredSignalsCount - executedSignalsCount; + + // + message = "Failed to Execute (" + ToString(diff) + ") Signals ..."; + } + else if (executedSignalsCount == filteredSignalsCount) + { + message = "(" + ToString(executedSignalsCount) + ") Signals Executed Successfully ..."; + } + + // + if (IsValid(message)) + { + x5121EAAlert.Alert(message); + } +} + // \ No newline at end of file