From a6905cbf985ea1bdc41f9945e390117cf618fe9d Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Thu, 1 Aug 2024 02:55:30 +0330 Subject: [PATCH] last ... --- Classes/x-saherelm.xstrategy.class.mq5 | 234 ++++++++++++++----------- Classes/x-saherelm.xtrade.class.mq5 | 3 +- Strategies/x-saherelm.xch.strategy.mq5 | 35 ++-- 3 files changed, 154 insertions(+), 118 deletions(-) diff --git a/Classes/x-saherelm.xstrategy.class.mq5 b/Classes/x-saherelm.xstrategy.class.mq5 index a5d8ef68..c8cbe531 100644 --- a/Classes/x-saherelm.xstrategy.class.mq5 +++ b/Classes/x-saherelm.xstrategy.class.mq5 @@ -716,6 +716,125 @@ public: bool result = false; // + // Prepare and Normalize Signal ... + result = PrepareSignal(signal); + if (!result) + { + return result; + } + + // + // Notify Signal Event Handler(s) ... + NotifyOnSignalEventHandlers(signal); + + // + // Check Signal Execution Enabled or not ... + result = !mIgnoreSignalExecution; + if (!result) + { + return result; + } + + // + bool isLong = IsLong(signal.type); + + // + ENUM_X_SIGNAL_EXECUTION_RESULT state; + result = trader.ExecuteSignal( + signal, + state // + ); + if (result) + { + // + string message = "Execute " + (isLong ? "Long" : "Short") + " " + signal.symbol + "/" + ToString(signal.period) + " ..."; + Alert(message); + + // + OnSignalExecuted(signal); + } + else + { + OnSignalExecutionFailed(signal, state); + } + + // + return result; + } + + // + // Calls When a Signal Executed Successfully ... + virtual void OnSignalExecuted(XSignal &signal) + { + } + + // + // Calls When a Signal Execution Failed ... + virtual void OnSignalExecutionFailed( + XSignal &signal, + ENUM_X_SIGNAL_EXECUTION_RESULT result // + ) + { + } + + // + // Protected ... +protected: + // + int mSlippage; // Trader Slippage + long mMagicNumber; // Trader Magic Number + bool mForceDisabled; // Force Disabled + bool mAllowLong; // Allow Long Signals + bool mAllowShort; // Allow Short Signals + int mMaxAllowedLongs; // Max Allowed Longs + int mMaxAllowedShorts; // Max Allowed Shorts + + // + bool mUseTPSLAsPoint; // Converts TP and SL by Provided Points + bool mForceMaxTPSLAsPoint; // Force Max TP and SL by Provided Points + double mTPPoint; // TP As Point + double mSLPoint; // SL As Point + bool mIgnoreTP; // TP set to 0 + bool mIgnoreSL; // SL set to 0 + bool mIgnoreSignalExecution; // Ignore Signal Execution + + // + string mSymbol; // Trading Symbol ... + ENUM_TIMEFRAMES mPeriod; // Trading TimeFrame ... + + // + double mR2R; // Risk/Rewar Ratio ... + double mVolume; // Static Volume Per Trades ... + + // + XBarTracker barTracker; // Strategy Time Tracker ... + + // + // Trend Detecting ... + int mRequiredPivots; // Required Pivots For Trend Detecting ... + int mRequiredShoulders; // Required Pivots Shoulders ... + + // + XSCTrade *trader; + + // + MqlTick lastTick; + + // + void Reset() + { + barTracker.Clean(); + } + + // + // Prepare and Normalize Signal for Execution in Strategy ... + bool PrepareSignal(XSignal &signal) + { + // + bool result = false; + + // + // Check Strategy Is Enables ... result = !IsDisabled(); if (!result) { @@ -723,6 +842,7 @@ public: } // + // Validate Signal ... result = signal.IsValid(); if (!result) { @@ -733,6 +853,7 @@ public: bool isLong = IsLong(signal.type); // + // Check Type Enabled ... result = // isLong @@ -756,20 +877,15 @@ public: // // Get and Count Positions ... - XPosition positions[]; - int positionsCount = trader.GetPositions( - positions // + XPosition longs[]; + XPosition shorts[]; + trader.GetPositions( + longs, + shorts, + signal.symbol // ); // - // Get and Count Lng and Short Positions ... - XPosition longs[]; - XPosition shorts[]; - ExtractPositions( - positions, - longs, - shorts // - ); int longsCount = ArraySize(longs); int shortsCount = ArraySize(shorts); @@ -865,106 +981,10 @@ public: signal.sl = 0; } - // - // Notify Signal Event Handler(s) ... - NotifyOnSignalEventHandlers(signal); - - // - // Check Signal Execution Enabled or not ... - result = !mIgnoreSignalExecution; - if (!result) - { - return result; - } - - // - ENUM_X_SIGNAL_EXECUTION_RESULT state; - result = trader.ExecuteSignal( - signal, - state // - ); - if (result) - { - // - string message = "Execute " + (isLong ? "Long" : "Short") + " " + signal.symbol + "/" + ToString(signal.period) + " ..."; - Alert(message); - - // - OnSignalExecuted(signal); - } - else - { - OnSignalExecutionFailed(signal, state); - } - // return result; } - // - // Calls When a Signal Executed Successfully ... - virtual void OnSignalExecuted(XSignal &signal) - { - } - - // - // Calls When a Signal Execution Failed ... - virtual void OnSignalExecutionFailed( - XSignal &signal, - ENUM_X_SIGNAL_EXECUTION_RESULT result // - ) - { - } - - // - // Protected ... -protected: - // - int mSlippage; // Trader Slippage - long mMagicNumber; // Trader Magic Number - bool mForceDisabled; // Force Disabled - bool mAllowLong; // Allow Long Signals - bool mAllowShort; // Allow Short Signals - int mMaxAllowedLongs; // Max Allowed Longs - int mMaxAllowedShorts; // Max Allowed Shorts - - // - bool mUseTPSLAsPoint; // Converts TP and SL by Provided Points - bool mForceMaxTPSLAsPoint; // Force Max TP and SL by Provided Points - double mTPPoint; // TP As Point - double mSLPoint; // SL As Point - bool mIgnoreTP; // TP set to 0 - bool mIgnoreSL; // SL set to 0 - bool mIgnoreSignalExecution; // Ignore Signal Execution - - // - string mSymbol; // Trading Symbol ... - ENUM_TIMEFRAMES mPeriod; // Trading TimeFrame ... - - // - double mR2R; // Risk/Rewar Ratio ... - double mVolume; // Static Volume Per Trades ... - - // - XBarTracker barTracker; // Strategy Time Tracker ... - - // - // Trend Detecting ... - int mRequiredPivots; // Required Pivots For Trend Detecting ... - int mRequiredShoulders; // Required Pivots Shoulders ... - - // - XSCTrade *trader; - - // - MqlTick lastTick; - - // - void Reset() - { - barTracker.Clean(); - } - // // Notified Signal Event Handler(s) ... void NotifyOnSignalEventHandlers(XSignal &signal) diff --git a/Classes/x-saherelm.xtrade.class.mq5 b/Classes/x-saherelm.xtrade.class.mq5 index 118c09f1..07627788 100644 --- a/Classes/x-saherelm.xtrade.class.mq5 +++ b/Classes/x-saherelm.xtrade.class.mq5 @@ -5477,7 +5477,7 @@ public: // bool ExecuteSignal( - XSignal &signal, // Signal for Execution + XSignal &signal, // Signal for Execution ENUM_X_SIGNAL_EXECUTION_RESULT &state, // Execution State bool ignorePolicies = false // Ignore Execution Policies ) @@ -5621,6 +5621,7 @@ public: // string msg = (signal.type == POSITION_TYPE_BUY ? "Long" : "Short") + " Signal Provided by: " + signal.provider + + (signal.pushers > 1 ? " Pushers: " + ToString(signal.pushers) : "") + " on: " + signal.symbol + " in: " + ToString(signal.period) + " Executed Successfully ..."; diff --git a/Strategies/x-saherelm.xch.strategy.mq5 b/Strategies/x-saherelm.xch.strategy.mq5 index 58e19e59..64d25500 100644 --- a/Strategies/x-saherelm.xch.strategy.mq5 +++ b/Strategies/x-saherelm.xch.strategy.mq5 @@ -1241,19 +1241,34 @@ public: sl, tp // ); - if (result) + if (!result) { - // - signal.pushers = pushers; - - // - // Notify XCH Specified Signal Event Handlers ... - NotifyOnXCHSignalEventHandlers( - signal, - conditions // - ); + return result; } + // + signal.pushers = pushers; + + // + // Prepare and Normalize Signal for Execution in Strategy ... + result = PrepareSignal(signal); + if (!result) + { + return result; + } + + // + // Notify XCH Specified Signal Event Handlers ... + NotifyOnXCHSignalEventHandlers( + signal, + conditions // + ); + + // + // Since we Ignore Signal Execution in this Strategy ... + // Make it the result false here for Preventing Double Execution ... + result = false; + // return result; }