From a5e28b46796eb24bd92ead23c0be5c5ce396c77a Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Fri, 31 May 2024 00:58:22 +0330 Subject: [PATCH] Cleanup all EQM and Trail and State Manage ment ... --- Classes/Indicators/x-saherelm.mid.class.mq5 | 349 ------ Classes/x-saherelm.x121.xea.class.mq5 | 1189 ------------------- Classes/x-saherelm.xind.class.mq5 | 812 ------------- 3 files changed, 2350 deletions(-) delete mode 100644 Classes/Indicators/x-saherelm.mid.class.mq5 delete mode 100644 Classes/x-saherelm.xind.class.mq5 diff --git a/Classes/Indicators/x-saherelm.mid.class.mq5 b/Classes/Indicators/x-saherelm.mid.class.mq5 deleted file mode 100644 index 0d94a91f..00000000 --- a/Classes/Indicators/x-saherelm.mid.class.mq5 +++ /dev/null @@ -1,349 +0,0 @@ -/////////////////////////////////////////////////////// -// -// SaherElm IT Center MQL5 Class Library -// ---------------------------------------------- -// Name: XSCBaseInd -// Description: provides Indicator implementation -// requirements ... -// -// -// Maintainer: -// ------------ -// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com) -// -////////////////////////////////////////////////////// -// -// Global Properties ... -#property library -#property copyright "Copyright 2023, SaherElm IT Center" -#property link "https://www.saherelm.ir" -#property version "1.00" -#property strict - -// -// Imports ... -#include "../../Classes/x-saherelm.xind.class.mq5" - -// -// XSCMid Indicator Buffers ... -enum ENUM_XSCMID_BUFFERS -{ - XSCMID_MAIN_LINE = 0, // Main Buffer ... - XSCMID_STATE_LINE = 1, // State Buffer ... -}; - -// -// XSCMid Indicator Inputs ... -struct XSCMidInputs -{ - // - // Props ... - - // - int length; // Market Length ... - - // - // Constructor ... - XSCMidInputs() - { - Clean(); - } - - // - // Tools ... - - // - // Initial Inputs ... - bool Init( - int mLength // Market Length - ) - { - // - bool result = false; - - // - result = - // - mLength >= 2 - // - ; - if (!result) - { - return result; - } - - // - this.length = mLength; - - // - result = IsValid(); - - // - return result; - } - - // - // Cleanup ... - void Clean() - { - // - this.length = 0; - } - - // - // Default ... - void Default() - { - // - this.length = 2; - } - - // - // Validate ... - bool IsValid() - { - // - bool result = false; - - // - result = - // - length > 1 - // - ; - - // - return result; - } - - // - // Retrieve Max Length ... - int Max() - { - // - int result = 0; - - // - result = MathMax(this.length, this.length); - - // - return result; - } -}; - -// -// a Simple Indicator ... - -class XSCMid : public XSCBaseInd -{ - // - // Public ... -public: - // - // Props ... - - // - // Constructor(s) ... - void XSCMid( - string symbol, // Trading Symbol - ENUM_TIMEFRAMES period // Trading TimeFrame - ) : XSCBaseInd(symbol, period) - { - // - string token = GetTag(); - - // - SetAlertPrefix(token); - } - - // - // Deconstructor ... - void ~XSCMid() {} - - // - // Initialize Indicator ... - bool Init( - XSCMidInputs &inputs // Indicator Inputs ... - ) - { - // - bool result = false; - - // - result = inputs.IsValid(); - if (!result) - { - return result; - } - - // - // Set Inputs ... - this.mInputs = inputs; - - // - // Validate Indicator State ... - result = this.IsValid(); - if (!result) - { - return result; - } - - // - // Register Required Buffers ... - XBuffer main; - result = main.Init( - "XSCMid" // - ); - if (!result) - { - return result; - } - - // - result = SetBuffer(main); - if (!result) - { - return result; - } - - // - // Calculate First Time ... - Calculate(); - - // - return result; - } - - // - // Property Getter(s) / Setter(s) ... - - // - XSCMidInputs GetInputs() - { - return mInputs; - } - - // - bool SetInputs( - XSCMidInputs &inputs // Indicator Inputs - ) - { - return Init(inputs); - } - - // - // Override(s) ... - - // - // Get Tag ... - string GetTag() override - { - // - string result = NULL; - - // - result = GetToken(); - - // - return result; - } - - // - // Get Token ... - string GetToken() override - { - // - string result = NULL; - - // - result = GetSpecificToken(this); - - // - return result; - } - - // - // Validate Inputs ... - bool IsValidInputs() override - { - return mInputs.IsValid(); - } - - // - // Calculate Buffers ... - void Calculate() override - { - // - int max = mInputs.Max(); - int limit = CountLimit(); - int totalBars = CountBars(); - int prevCalculated = GetCalculatedBars(); - - // - // Prepare Buffers Index ... - int mainIDX = GetBufferIndex("XSCMid"); - - // - int start = prevCalculated; - if (start == totalBars) { - start-=1; - } - - // - // Loop through Bars ... - for (int i = start; i < totalBars - 1 && !IsStopped(); i++) - { - // - // XOHCL iBar; - // iBar.Init( - // mSymbol, - // mPeriod, - // i // - // ); - - // - double iValue = iClose(mSymbol, mPeriod, i); - // Print("BarIndex: " + ToString(i), ", iValue: ", iValue); - - // - int count = mBuffers[mainIDX].Count(); - if (count > 0 && i == count - 1) - { - mBuffers[mainIDX].Set(i, iValue); - } - else - { - mBuffers[mainIDX].Add(iValue); - } - } - - // - // Prevent Double Calculation Same Bars ... - mCalculatedBars = totalBars; - } - - // - // De Initialize Class ... - void DeInit(int reason) - { - } - - // - // Tools ... - - // - // Reading Values ... - - // - // Protected ... -protected: - // - // Private ... -private: - // - // Props ... - - // - // Inputs ... - XSCMidInputs mInputs; -}; \ No newline at end of file diff --git a/Classes/x-saherelm.x121.xea.class.mq5 b/Classes/x-saherelm.x121.xea.class.mq5 index 5edca6d1..60c1343f 100644 --- a/Classes/x-saherelm.x121.xea.class.mq5 +++ b/Classes/x-saherelm.x121.xea.class.mq5 @@ -282,1017 +282,6 @@ public: return result; } - // - // Handl Management Actions ... - // if returns true, it is going to prevent for processing forward ... - bool HandleStateManagement(XSignal &signals[]) override - { - // - bool result = false; - - // - XPosition positions[]; - int positionsCount = mTrader.GetPositions(positions); - if (positionsCount <= 0) - { - return result; - } - - // - // Synch Position Tracker ... - SyncPositionTracker(); - - // - HandleEQMState(signals); - - // - HandleEQMHedge(positions); - - // - HandleTrailManagement(positions); - - // - // Close all Positions on 22 ... - // TODO: Make it Configurable ... - int closeHour = -1; - MqlDateTime cTime = GetCurrentTime(); - if (closeHour >= 0 && cTime.hour == closeHour) - { - // - string comment = "EQM Close on Time ..."; - - // - XPosition positions[]; - int positionsCount = mTrader.GetPositions(positions); - if (positionsCount <= 0) - { - return result; - } - - // - int closed = mTrader.Close( - positions, - comment // - ); - if (closed > 0) - { - mAlert.Alert(comment); - } - } - - // - // Must false ... - return result; - } - - // - void HandleEQMHedge(XPosition &positions[]) - { - // - // TODO: - // Make them Configurable ... - int numberOfPositionsForHege = 3; - - // - if (!IsHedgeEnable()) - { - return; - } - - // - int positionsCount = ArraySize(positions); - if (positionsCount <= 0) - { - return; - } - - // - // Hedge ... - if (positionsCount >= numberOfPositionsForHege) - { - // - int maxAllowedPositions = GetMaxAllowedPositions(); - - // - double profit = CalculatePositionsProfit(positions); - double requiredProfit = CalculateRequiredProfitForHedge(positions); - - // - bool isReadyForHedge = - positionsCount < maxAllowedPositions - ? profit >= requiredProfit - : positionsCount >= maxAllowedPositions && profit >= (requiredProfit * 0.80) - ? true - : false; - if (!isReadyForHedge) - { - return; - } - - // - string comment = "EQM Hedge ..."; - int closed = mTrader.Close( - positions, - comment); - if (closed > 0) - { - // - string message = "EQM Hedge Positions on Profit: " + ToString(profit); - - // - mAlert.Alert(message); - - // - RemoveEQMOrders(); - - // - for (int i = 0; i < positionsCount; i++) - { - RemoveTrail(positions[i].ticket); - } - } - } - } - - // - void HandleEQMState(XSignal &signals[]) - { - // - XSignal accepted[]; - bool equityReached = !mTrader.CheckEquityForTrade(); - bool maxAllowedReached = !mTrader.CheckPositionsForTrade(); - bool enableEQM = equityReached || maxAllowedReached; - if (!enableEQM) - { - return; - } - - // - int signalsCount = ArraySize(signals); - if (IsValidSize(signalsCount)) - { - // - // Extract required Data ... - XPosition positions[]; - int positionsCount = mTrader.GetPositions( - positions); - if (!IsValidSize(positionsCount)) - { - return; - } - - // - int longs = 0; - double longProfits = 0; - double longVolumes = 0; - - // - int shorts = 0; - double shortProfits = 0; - double shortVolumes = 0; - - // - CountPositions( - positions, - longs, - longProfits, - longVolumes, - shorts, - shortProfits, - shortVolumes // - ); - - // - for (int i = 0; i < signalsCount; i++) - { - // - XSignal iSignal = signals[i]; - bool isLong = IsLong(iSignal.type); - - // - bool canAccept = - // - iSignal.provider == XEQMSupportToken - ? true - : isLong - ? longs < shorts // || longProfits > shortProfits - : shorts < longs // || shortProfits > longProfits - // - ; - if (!canAccept) - { - continue; - } - - iSignal.comment = ""; - iSignal.provider = XEQMSupportToken; - // iSignal.volume *= 2; - - // - AddRef( - iSignal, - accepted // - ); - } - - // - int acceptedCount = ArraySize(accepted); - if (!IsValidSize(acceptedCount)) - { - return; - } - - // - // Execute EQM Signals ... - ENUM_X_SIGNAL_EXECUTION_RESULT reasons[]; - int executed = mTrader.ExecuteSignals( - accepted, - reasons, - ORDER_TIME_GTC, - NULL, - false // - ); - if (executed > 0) - { - // - string message = "EQM Execute " + ToString(executed) + " Signals ..."; - - // - mAlert.Alert(message); - } - } - } - - // - // Add Or Update EQM Orders ... - void PlaceEQMOrders(XPosition &position) - { - // - // TODO: Make It Configurable ... - int maxEQMCount = 5; - return; - - // - // Position Protection Senarioos ... - if (!position.IsValid()) - { - return; - } - - // - int descriptorIDX = FindDescriptorIndex(position); - if (descriptorIDX <= -1) - { - return; - } - - // - X121ProviderDescriptor descriptor = mDescriptors[descriptorIDX]; - - // - XOHCLSupRes slSupRes; - bool hasSupRes = descriptor - .provider - .GenerateSupRes( - slSupRes, - position.type, - 5 // - ); - - // - XDrawSpecifications supDrawSpecs; - supDrawSpecs.draw = true; - supDrawSpecs.rayRight = true; - supDrawSpecs.clr = clrLightGreen; - - //, - XDrawSpecifications resDrawSpecs; - resDrawSpecs.draw = true; - resDrawSpecs.rayRight = true; - resDrawSpecs.clr = clrLightCoral; - - // - // DrawSupRes( - // slSupRes, - // supDrawSpecs, - // resDrawSpecs // - // ); - - // - XSignal supports[]; - int supportsCount = GenerateEQMOrders( - supports, - position, - slSupRes // - ); - if (!IsValidSize(supportsCount)) - { - return; - } - - // - XOrder untriggeredOrders[]; - int untriggeredOrdersCount = mTrader.GetOrders( - untriggeredOrders, - position.symbol, - XEQMSupportToken, - position.period, - NULL, // All Types ... - ORDER_STATE_PLACED, - true // - ); - - // - XPosition triggereSupports[]; - int tiggeredSupportsCount = mTrader.GetPositions( - triggereSupports, - position.symbol, - XEQMSupportToken, - position.period, - NULL, // All Types ... - true // - ); - - // - if (untriggeredOrdersCount + - tiggeredSupportsCount >= - maxEQMCount) - { - return; - } - - // - XSignal filteredSignals[]; - for (int i = 0; i < supportsCount; i++) - { - // - XSignal iSupport = supports[i]; - - // - double sl; - iSupport.RegularSL(sl); - - // - double tp; - iSupport.RegularTP(tp); - - // - if (!IsValidSize(untriggeredOrdersCount) && !IsValidSize(tiggeredSupportsCount)) - { - // - AddRef( - iSupport, - filteredSignals // - ); - - // - continue; - } - - // - bool isOrdersPassed = true; - if (!IsValidSize(untriggeredOrdersCount)) - { - isOrdersPassed = true; - } - else - { - // - for (int j = 0; j < untriggeredOrdersCount; j++) - { - // - XOrder jOrder = untriggeredOrders[j]; - - // - bool isSame = - // - jOrder.symbol == iSupport.symbol && - jOrder.period == iSupport.period && - jOrder.openPrice == iSupport.entry && - jOrder.sl == sl && - jOrder.tp == tp - // - ; - if (isSame) - { - // - isOrdersPassed = false; - break; - } - } - } - - // - bool isPositionsPassed = true; - if (!IsValidSize(tiggeredSupportsCount)) - { - isPositionsPassed = true; - } - else - { - // - for (int j = 0; j < tiggeredSupportsCount; j++) - { - // - XPosition jPosition = triggereSupports[j]; - - // - bool isSame = - // - jPosition.symbol == iSupport.symbol && - jPosition.period == iSupport.period && - jPosition.entry == iSupport.entry && - jPosition.sl == sl && - jPosition.tp == tp - // - ; - if (isSame) - { - // - isPositionsPassed = false; - break; - } - } - } - - // - if (isOrdersPassed && - isPositionsPassed) - { - // - AddRef( - iSupport, - filteredSignals // - ); - } - } - int filteredSignalsCount = ArraySize(filteredSignals); - if (!IsValidSize(filteredSignalsCount)) - { - return; - } - - // - ENUM_X_SIGNAL_EXECUTION_RESULT states[]; - int placedOrders = mTrader.ExecuteSignals( - filteredSignals, - states, - ORDER_TIME_GTC, - NULL, - false // Ignore Policies ... - ); - if (placedOrders > 0) - { - // - string message = "EQM Placed (" + ToString(placedOrders) + ") Support Orders ..."; - - // - mAlert.Alert(message); - } - } - - // - void RemoveEQMOrders() - { - // - XOrder untriggeredOrders[]; - int untriggeredOrdersCount = mTrader.GetOrders( - untriggeredOrders, - NULL, - XEQMSupportToken, - NULL, - NULL, // All Types ... - ORDER_STATE_PLACED, - true // - ); - if (!IsValidSize(untriggeredOrdersCount)) - { - return; - } - - // - int canceleds = mTrader.CancelOrders( - untriggeredOrders // - ); - if (canceleds > 0) - { - // - string message = "EQM Cancel Support ..."; - - // - mAlert.Alert(message); - } - } - - // - void HandleTrailManagement(XPosition &positions[]) - { - // - return; - - // - // TODO: - // Make them Configurable ... - int maxTrailTPs = 5; - // int levelsOnClose = 6; - int riskFreeLevel = 0; - int requiredAgeForTrail = 0; - int requiredDistanceForTrail = 50; - - // - int positionsCount = ArraySize(positions); - if (positionsCount <= 0) - { - return; - } - - // - double profit = CalculatePositionsProfit(positions); - - // - // Trail Positions Stop ... - for (int i = 0; i < positionsCount; i++) - { - // - XPosition iPosition = positions[i]; - - // - bool canManageState = - // - iPosition.provider == XEQMSupportToken - // - ; - - // - // Position Management for X786 ... - if (canManageState) - { - // - // When Position in Profit, Trail SL ... - if (profit > 0) - { - // - if (iPosition.profit > 0) - { - // - XTrail trail; - bool isTrailedBefore = GetSLTrail( - iPosition.ticket, - trail // - ); - - // - if (!isTrailedBefore) - { - // - // Add First Time ... - - // - // Check Position Age ... - int iPositionAge = iPosition.GetAge(iPosition.period); - if (iPositionAge < requiredAgeForTrail) - { - continue; - } - - // - // Check Position Profit Distance ... - // double reward = MathAbs(iPosition.tp - iPosition.entry); - double priceDistanceInPoint = PriceToPoint(iPosition.profit / iPosition.volume, iPosition.symbol); - double priceDistance = PointToPrice(priceDistanceInPoint, iPosition.symbol); - if (priceDistance < requiredDistanceForTrail) - { - continue; - } - - // - double slDiff = PointToPrice(priceDistance, iPosition.symbol); - double appliedSL = slDiff / 2; - - // - // double sl = - // IsLong(iPosition.type) - // ? iPosition.sl + appliedSL - // : iPosition.sl - appliedSL; - - // - double sl = - IsLong(iPosition.type) - ? iPosition.entry + appliedSL - : iPosition.entry - appliedSL; - - // - // double sl = - // IsLong(iPosition.type) - // ? iPosition.entry + appliedSL - // : iPosition.entry - appliedSL; - - // - bool isInit = trail.Init( - iPosition.ticket, - iPosition.type, - iPosition.profit, - iPosition.sl, - sl // - ); - if (!isInit) - { - continue; - } - } - else - { - // - // Update Exists ... - - // - // Check Profit ... - bool isProfitPassed = iPosition.profit > trail.profit; - if (!isProfitPassed) - { - continue; - } - - // - // Check Age Distance from Last ... - int age = iBarShift( - iPosition.symbol, - iPosition.period, - trail.time, - false // - ); - if (age < requiredAgeForTrail) - { - continue; - } - - // - double priceDistanceInPoint = PriceToPoint((iPosition.profit - trail.profit) / iPosition.volume, iPosition.symbol); - double priceDistance = PointToPrice(priceDistanceInPoint, iPosition.symbol); - if (priceDistance < requiredDistanceForTrail) - { - continue; - } - - // - bool isLong = IsLong(iPosition.type); - - // - double slDiff = PointToPrice(priceDistance, iPosition.symbol); - double appliedSL = (slDiff / 2); - - // - double selectedValue = - // - (riskFreeLevel > 0 && - trail.level == riskFreeLevel) || - (isLong - ? iPosition.sl < iPosition.entry - : iPosition.sl > iPosition.entry) - ? iPosition.entry - : iPosition.sl - // - ; - if (riskFreeLevel > 0 && trail.level > riskFreeLevel) - { - appliedSL = slDiff; - } - - // - double sl = - isLong - ? selectedValue + appliedSL - : selectedValue - appliedSL; - - // - // Update Trail Values ... - bool isUpdated = trail.Update( - sl, - iPosition.profit // - ); - if (!isUpdated) - { - continue; - } - } - - // - if (trail.IsValid()) - { - // - string comment = trail.GenerateTag(); - bool isModified = mTrader.Modify( - trail.ticket, - trail.after, - iPosition.tp, - comment // - ); - if (isModified) - { - // - RemoveTrail(trail.ticket); - - // - AddSLTrail(trail); - - // - string message = "Position (" + ToString(trail.ticket) + ") Trailed SL on Level: " + ToString(trail.level); - mAlert.Alert(message); - } - } - } - } - - // - // When Position in Drawdown Trail TP ... - if (profit < 0) - { - // - if (iPosition.profit < 0) - { - // - XTrail trail; - bool isTrailedBefore = GetTPTrail( - iPosition.ticket, - trail // - ); - - // - if (!isTrailedBefore) - { - // - // Add First Time ... - - // - // Check Position Age ... - int iPositionAge = iPosition.GetAge(iPosition.period); - if (iPositionAge < requiredAgeForTrail) - { - continue; - } - - // - // Check Position Profit Distance ... - double priceDistanceInPoint = PriceToPoint(MathAbs(iPosition.profit) / iPosition.volume, iPosition.symbol); - double priceDistance = PointToPrice(priceDistanceInPoint, iPosition.symbol); - if (priceDistance < requiredDistanceForTrail) - { - continue; - } - - // - double tpDiff = PointToPrice(priceDistance, iPosition.symbol); - double appliedTP = tpDiff / 2; - - // - double tp = - IsLong(iPosition.type) - ? iPosition.tp - appliedTP - : iPosition.tp + appliedTP; - - // - bool isInit = trail.Init( - iPosition.ticket, - iPosition.type, - iPosition.profit, - iPosition.tp, - tp // - ); - if (!isInit) - { - continue; - } - } - else - { - // - // Update Exists ... - - // - if (trail.level >= maxTrailTPs) - { - continue; - } - - // - // Check Profit ... - bool isProfitPassed = MathAbs(iPosition.profit) > MathAbs(trail.profit); - if (!isProfitPassed) - { - continue; - } - - // - // Check Age Distance from Last ... - int age = iBarShift( - iPosition.symbol, - iPosition.period, - trail.time, - false // - ); - if (age < requiredAgeForTrail) - { - continue; - } - - // - double priceDistanceInPoint = PriceToPoint((MathAbs(iPosition.profit) - MathAbs(trail.profit)) / iPosition.volume, iPosition.symbol); - double priceDistance = PointToPrice(priceDistanceInPoint, iPosition.symbol); - - // - bool isLong = IsLong(iPosition.type); - - // - double tpDiff = PointToPrice(priceDistance, iPosition.symbol); - double appliedTP = tpDiff / 2; - - // - double tp = - isLong - ? iPosition.tp - appliedTP - : iPosition.tp + appliedTP; - - // - // Update Trail Values ... - bool isUpdated = trail.Update( - tp, - iPosition.profit // - ); - if (!isUpdated) - { - continue; - } - } - - // - if (trail.IsValid()) - { - // - string comment = trail.GenerateTag(); - bool isModified = mTrader.Modify( - trail.ticket, - iPosition.sl, - trail.after, - comment // - ); - if (isModified) - { - // - RemoveTrail(trail.ticket); - - // - AddTPTrail(trail); - - // - string message = "Position (" + ToString(trail.ticket) + ") Trailed TP on Level: " + ToString(trail.level); - mAlert.Alert(message); - } - } - } - } - } - } - } - - // - // Here we Can Filter Signals before Sending them to Execution ... - // based on Providers and etc ... - // for Volumes and Benefits ... - int HandleSignalManagement(XSignal &signals[]) override - { - // - // TODO: Make this Configurable ... - int minDistanceBetweenTwoSameProviderSignal = 7; - - // - int result = 0; - - // - if (ArraySize(signals) <= 0) - { - return result; - } - - // - // Create temp Array for Filtering ... - XSignal tmps[]; - Copy( - signals, - tmps // - ); - int tmpsCount = ArraySize(tmps); - - // - // Cleaning Source ... - Clean(signals); - - // - XSignal mEQMManaged[]; - - // - // Loop through Signals for Management ... - for (int i = 0; i < tmpsCount; i++) - { - // - XSignal iSignal = tmps[i]; - - // - // dont Manage NON Signal Provider's signals ... - string provider = iSignal.provider; - bool canIgnore = !IsValid(provider) || provider == "EQM"; - if (canIgnore) - { - // - AddRef( - iSignal, - signals // - ); - - // - continue; - } - - // - XPosition positions[]; - int positionsCount = mTrader.GetPositions( - positions, - iSignal.symbol, - iSignal.provider, - NULL, // All Periods ... - iSignal.type // - ); - XPosition symbolPositions[]; - int symbolPositionsCount = mTrader.GetPositions( - positions, - iSignal.symbol, - NULL, // All Providers ... - NULL, // All Periods ... - iSignal.type // - ); - double profit = CalculatePositionsProfit(positions); - double symbolProfit = CalculatePositionsProfit(symbolPositions); - if (positionsCount <= 0) - { - // - // First Signal in a While of Specific Provider ... - - // - AddRef( - iSignal, - signals // - ); - - // - continue; - } - // - else if (positionsCount < GetMaxAllowedPositions()) - { - // - // Next Signal in a While of Specific Provider ... - - // - AddRef( - iSignal, - signals // - ); - - // - continue; - } - // - else - { - // - AddRef( - iSignal, - mEQMManaged // - ); - } - - // else if (profit > 0 && symbolProfit > 0) - // { - // // - // AddRef( - // iSignal, - // signals // - // ); - // } - } - - // - if (IsValidSize(ArraySize(mEQMManaged))) - { - HandleEQMState(mEQMManaged); - } - - // - result = ArraySize(signals); - - // - return result; - } - // // Request for Support Signals using Guard ... bool RequestForSupport( @@ -1317,184 +306,6 @@ public: // // Tools ... - // - // Management Tracker/Profit etc ... - - // - // Handle Position Changed Event ... - void OnPositionsChangedHandler(int count) override - { - // - // Synchronize Tracker ... - SyncPositionTracker(); - } - - // - // Called when a Position Going in Profit ... - void OnPositionGoingInProfit(XPositionTrack &track) override - { - // - string message = "Position (" + ToString(track.ticket) + ", " + track.symbol + ") Dropped in Profit ..."; - - // - mAlert.Alert(message); - } - - // - // Called when a Position Going to Drawdown ... - void OnPositionGoingToDrawdown(XPositionTrack &track) override - { - // - string message = "Position (" + ToString(track.ticket) + ", " + track.symbol + ") Dropped in Drawdown ..."; - - // - mAlert.Alert(message); - - // // - // XPosition position; - // bool isRecieved = mTrader.GetPosition( - // track.ticket, - // position // - // ); - // if (!isRecieved) - // { - // return; - // } - - // // - // XSignal signals[]; - - // // - // XSignal signal; - // bool isPrepared = signal.PrepareOpposit(position); - // if (!isPrepared) - // { - // return; - // } - - // // - // signal.comment = ""; - // signal.provider = XEQMSupportToken; - - // // - // AddRef( - // signal, - // signals // - // ); - - // // - // HandleEQMState(signals); - } - - // - // Called when Account Going in Profit ... - void OnGoingToProfit(XProfitTrack &track) override - { - // - string message = "Account In Profit ..."; - - // - mAlert.Alert(message); - - // - RemoveEQMOrders(); - } - - // - // Called when Account Going in Drawdown ... - void OnGoingToDrawdown(XProfitTrack &track) override - { - // - string message = "Account In Drawdown ..."; - - // - mAlert.Alert(message); - - // - // Here i Have to Detect In DrawDown Positions ... - - // - RemoveEQMOrders(); - - // - string mSymbols[]; - XSCPositionHolder *mHolders[]; - int symbolsCount = FilterSymbolBasedPositions( - mSymbols, - mHolders // - ); - - // - for (int i = 0; i < symbolsCount; i++) - { - // - string iSymbol = mSymbols[i]; - XSCPositionHolder *iHolder = mHolders[i]; - - // - XPosition iMaxInDP; - bool hasMaxInDP = iHolder.GetMaxInDrawdown(iMaxInDP); - if (!hasMaxInDP) - { - continue; - } - - // - // Try to Protect a Position ... - ProtectPosition(iMaxInDP); - } - } - - // - void ManageUnExecutedSignals( - ENUM_X_SIGNAL_EXECUTION_RESULT &states[], - XSignal &unExecutedSignals[] // - ) override - { - // - XSignal filteredSignals[]; - - // - int unExecutedSignalsCount = ArraySize(unExecutedSignals); - if (!IsValidSize(unExecutedSignalsCount)) - { - return; - } - - // - for (int i = 0; i < unExecutedSignalsCount; i++) - { - // - ENUM_X_SIGNAL_EXECUTION_RESULT iState = states[i]; - bool isStateValid = - // - iState == X_SIGNAL_EXECUTION_FAILED_NO_EQUITY || - iState == X_SIGNAL_EXECUTION_FAILED_MAX_REACHED - // - ; - if (!isStateValid) - { - continue; - } - - // - AddRef( - unExecutedSignals[i], - filteredSignals // - ); - } - - // - int filteredSignalsCount = ArraySize(filteredSignals); - if (!IsValidSize(filteredSignalsCount)) - { - return; - } - - // - HandleEQMState(filteredSignals); - } - // // Protected ... protected: diff --git a/Classes/x-saherelm.xind.class.mq5 b/Classes/x-saherelm.xind.class.mq5 deleted file mode 100644 index 6bfe4adb..00000000 --- a/Classes/x-saherelm.xind.class.mq5 +++ /dev/null @@ -1,812 +0,0 @@ -/////////////////////////////////////////////////////// -// -// SaherElm IT Center MQL5 Class Library -// ---------------------------------------------- -// Name: XSCBaseInd -// Description: provides all Indicator requirements ... -// -// -// Maintainer: -// ------------ -// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com) -// -////////////////////////////////////////////////////// -// -// Global Properties ... -#property library -#property copyright "Copyright 2023, SaherElm IT Center" -#property link "https://www.saherelm.ir" -#property version "1.00" -#property strict - -// -// Imports ... -#include "../Classes/x-saherelm.base.class.mq5" -#include "../Libraries/x-saherelm.draw.lib.mq5" -#include "../Classes/x-saherelm.xalert.class.mq5" - -// -// Definitions ... - -// -struct XBuffer -{ - // - string title; // Buffer Title - double values[]; // Buffer Values - - // - // Constructor ... - XBuffer() - { - Clean(); - } - - // - // Tools ... - bool Init( - string mTitle // Title - ) - { - // - bool result = false; - - // - result = IsValid(mTitle); - if (!result) - { - return result; - } - - // - Clean(); - - // - result = StringToLower(mTitle); - if (!result) - { - return result; - } - - // - title = mTitle; - // ArraySetAsSeries(values, false); - - // - result = IsValid(); - - // - return result; - } - - // - // Cleanup ... - void Clean() - { - // - Clean(values); - - // - title = NULL; - } - - // - // Validate ... - bool IsValid() - { - // - bool result = false; - - // - result = - // - IsValid(title) - // - ; - - // - return result; - } - - // - // Check Owwn ... - bool IsOwn( - string mTitle // Checking Title - ) - { - // - bool result = false; - - // - result = IsValid(mTitle); - if (!result) - { - return result; - } - - // - result = StringToLower(mTitle); - if (!result) - { - return result; - } - - // - result = this.title == mTitle; - - // - return result; - } - - // - // Count Size ... - int Count() - { - return ArraySize(values); - } - - // - // Add Value to Buffer ... - int Add(double value) - { - // - int result = -1; - - // - if (!IsValid()) - { - return result; - } - - // - int count = Count(); - ArrayResize( - values, - count + 1 // - ); - - // - values[count] = value; - - // - result = Count(); - - // - return result; - } - - // - void Set( - int index, - double value // - ) - { - // - NormalizeIndex(index, values); - - // - values[index] = value; - } - - // - // Get Specified Index Value ... - double Get( - int index // Specified Index - ) - { - // - double result = EMPTY_VALUE; - - // - NormalizeIndex( - index, - values // - ); - - // - result = values[index]; - - // - return result; - } - - // - // Remove Specified Index Value ... - bool Remove(int index) - { - // - bool result = false; - - // - int count = Count(); - result = IsValidSize(count); - if (!result) - { - return result; - } - - // - result = index > 0 && index < count - 1; - if (!result) - { - return result; - } - - // - result = ArrayRemove( - values, - index, - 1 // - ); - - // - return result; - } -}; - -// -struct XBufferPlotStyle -{ - // - int width; // Width - color clr; // Color - uchar arrow; // Arrow Code - ENUM_DRAW_TYPE type; // Type - ENUM_LINE_STYLE style; // Style - - // - // Constructor ... - XBufferPlotStyle() - { - Default(); - } - - // - // Tools ... - - // - // Default ... - void Default() - { - // - width = 1; - arrow = 0; - clr = CLR_NONE; - type = DRAW_NONE; - style = STYLE_SOLID; - } -}; - -// -// Base Indicator Class ... -class XSCBaseInd : public XSCBaseAlert -{ - // - // Public ... -public: - // - // Props ... - - // - // Indicators Buffers ... - XBuffer mBuffers[]; - - // - // Constructor(s) ... - void XSCBaseInd( - string symbol, // Trading Symbol - ENUM_TIMEFRAMES period // Trading TimeFrame - ) : XSCBaseAlert() - { - // - mSymbol = symbol; - mPeriod = period; - - // - mCalculatedBars = 0; - } - - // - // Deconstructor ... - void ~XSCBaseInd() {} - - // - // Property Getter(s) / Setter(s) ... - - // - string GetSymbol() - { - return mSymbol; - } - - // - ENUM_TIMEFRAMES GetPeriod() - { - return mPeriod; - } - - // - // Overrides ... - - // - virtual string GetTag(); - - // - virtual string GetToken(); - - // - virtual void DeInit(int reason); // Indicator DeInitialization - - // - // Validate Inputs ... - virtual bool IsValidInputs() - { - return false; - } - - // - // Calculate ... - virtual void Calculate() {} - - // - // Tools ... - - // - // Retrieve Calculate Bars ... - int GetCalculatedBars() - { - return mCalculatedBars; - } - - // - // Count available Bars ... - int CountBars() - { - // - return iBars( - mSymbol, - mPeriod // - ); - } - - // - // Validate Indicator States ... - bool IsValid() - { - // - bool result = false; - - // - result = IsValidInputs(); - if (!result) - { - return result; - } - - // - result = - // - IsSpecifiedValid(mSymbol) && - IsSpecifiedValid(mPeriod) - // - ; - - // - return result; - } - - // - // Calculate Limit ... - // this means Required Bars for Calculation ... - int CountLimit() - { - // - int result = 0; - - // - int totalBars = CountBars(); - int calculatedBars = GetCalculatedBars(); - result = MathAbs(totalBars - calculatedBars); - - // - return result; - } - - // - // Buffer Registrations ... - - // - // Count Registered Buffers ... - int CountBuffers() - { - return ArraySize(mBuffers); - } - - // - // Retrieve Specified Buffer Index ... - int GetBufferIndex( - string title // Specified Buffer Title - ) - { - // - int result = -1; - - // - if (!IsSpecifiedValid(title)) - { - return result; - } - - // - int buffersCount = CountBuffers(); - if (!IsValidSize(buffersCount)) - { - return result; - } - - // - for (int i = 0; i < buffersCount; i++) - { - // - XBuffer iBuffer = mBuffers[i]; - - // - bool isOwn = iBuffer.IsOwn(title); - if (isOwn) - { - // - result = i; - break; - } - } - - // - return result; - } - - // - // Check Contains Specific Buffer or not ... - bool HasBuffer( - string title // Specified Buffer Title - ) - { - // - bool result = false; - - // - int bufferIndex = GetBufferIndex(title); - result = bufferIndex >= 0; - - // - return result; - } - - // - // Retrieve Specified Buffer ... - bool GetBuffer( - string title, // Specified Buffer Title - XBuffer &buffer // Hold Result - ) - { - // - bool result = false; - - // - int idx = GetBufferIndex(title); - result = idx >= 0; - if (!result) - { - return result; - } - - // - buffer = mBuffers[idx]; - - // - result = buffer.IsValid(); - - // - return result; - } - - // - // Register Specified Buffer ... - bool SetBuffer( - XBuffer &buffer // Specified Buffer for Registration - ) - { - // - bool result = false; - - // - // Validate buffer ... - result = buffer.IsValid(); - if (!result) - { - return result; - } - - // - // Check Buffer Doesn't Exists before ... - int idx = GetBufferIndex(buffer.title); - result = idx == -1; - if (!result) - { - return result; - } - - // - AddRef( - buffer, - mBuffers // - ); - - // - return result; - } - - // - double GetBufferValue( - string mTitle, // Required Buffer Title - int index // Bar Index - ) - { - // - double result = EMPTY_VALUE; - - // - // Validate State ... - if (!IsValid()) - { - return result; - } - - // - int bufferIDX = GetBufferIndex(mTitle); - if (bufferIDX < 0) - { - return result; - } - - // - Calculate(); - - // - result = mBuffers[bufferIDX].Get(index); - - // - return result; - } - - // - // Protected ... -protected: - // - // Props ... - - // - string mSymbol; // Trading Symbol - ENUM_TIMEFRAMES mPeriod; // Trading TimeFrame - - // - int mCalculatedBars; // Calculated Bars - - // - // Tools ... - - // - // Private ... -private: - // - // Props ... -}; - -// -// Tools ... - -// -// Register a Buffer as Indexed Buffer ... -void SetIndexBuffer( - int &index, // the Index refrence for Buffer - double &buffer[], // Buffer to Set - XBufferPlotStyle &style, // Style to Apply to Buffer - double &colorBuffer[], // Color Buffer - string label = NULL, // Buffer Label - bool showData = true, // Show Data on Data Window - double empty = -1, // Set 0 or EMPTY_VALUE used for setting Empty Value - int drawBegin = -1 // if Set More Than -1 applied -) -{ - // - // Set Buffer Index ... - SetIndexBuffer(index, buffer, INDICATOR_DATA); - - // - // Show Date ... - PlotIndexSetInteger(index, PLOT_SHOW_DATA, showData); - - // - // Set Buffer Label ... - if (IsValid(label)) - { - PlotIndexSetString(index, PLOT_LABEL, label); - } - - // - if (empty == 0 || empty == EMPTY_VALUE) - { - PlotIndexSetDouble(index, PLOT_EMPTY_VALUE, empty); - } - - // - if (drawBegin > -1) - { - PlotIndexSetInteger(index, PLOT_DRAW_BEGIN, drawBegin); - } - - // - if (style.type == DRAW_ARROW) - { - PlotIndexSetInteger(index, PLOT_ARROW, style.arrow); - } - - // - PlotIndexSetInteger(index, PLOT_LINE_STYLE, style.style); - PlotIndexSetInteger(index, PLOT_LINE_WIDTH, style.width); - - // - string drawTypeStr = EnumToString(style.type); - bool hasColorBuffer = Contains("COLOR", drawTypeStr); - - // - if (!hasColorBuffer) - { - PlotIndexSetInteger(index, PLOT_LINE_COLOR, style.clr); - } - else - { - // - // Since Color Buffers must add as next Index of it's own Buffer - // here before applied Color Buffer increase index ... - index++; - - // - SetIndexBuffer(index, colorBuffer, INDICATOR_COLOR_INDEX); - } - - // - // Increase Index for Next Buffer ... - index++; -} - -// -void SetIndexCandleBuffers( - int &index, // the Index refrence for Buffer - string openLabel, // Open Label - double &open[], // Open Buffer - string highLabel, // High Label - double &high[], // High Buffer - string lowLabel, // Low Label - double &low[], // Low Buffer - string closeLabel, // Close Label - double &close[], // Close Buffer - bool showData = true, // Show Data on Data Window - double empty = -1, // Set 0 or EMPTY_VALUE used for setting Empty Value - int drawBegin = -1 // if Set More Than -1 applied -) -{ - // - bool hasLabel = - // - IsValid(openLabel) && - IsValid(highLabel) && - IsValid(lowLabel) && - IsValid(closeLabel) - // - ; - - // - string label = - !hasLabel - ? NULL - : openLabel + ";" + highLabel + ";" + lowLabel + ";" + closeLabel; - - // - // Set Buffer Index ... - SetIndexBuffer(index, open, INDICATOR_DATA); - - // - // Set Draw Candles Type ... - PlotIndexSetInteger(index, PLOT_LINE_STYLE, DRAW_CANDLES); - - // - // Show Date ... - PlotIndexSetInteger(index, PLOT_SHOW_DATA, showData); - - // - // Set Buffer Label ... - if (IsValid(label)) - { - PlotIndexSetString(index, PLOT_LABEL, label); - } - - // - if (empty == 0 || empty == EMPTY_VALUE) - { - PlotIndexSetDouble(index, PLOT_EMPTY_VALUE, empty); - } - - // - if (drawBegin > -1) - { - PlotIndexSetInteger(index, PLOT_DRAW_BEGIN, drawBegin); - } - - // - index++; - - // - // Set Buffer Index ... - SetIndexBuffer(index, high, INDICATOR_DATA); - index++; - - // - // Set Buffer Index ... - SetIndexBuffer(index, low, INDICATOR_DATA); - index++; - - // - // Set Buffer Index ... - SetIndexBuffer(index, close, INDICATOR_DATA); - index++; -} - -// -void SetIndexCandleBuffers( - int &index, // the Index refrence for Buffer - string openLabel, // Open Label - double &open[], // Open Buffer - string highLabel, // High Label - double &high[], // High Buffer - string lowLabel, // Low Label - double &low[], // Low Buffer - string closeLabel, // Close Label - double &close[], // Close Buffer - double &colorBuffer[], // Candle Color Buffer - bool showData = true, // Show Data on Data Window - double empty = -1, // Set 0 or EMPTY_VALUE used for setting Empty Value - int drawBegin = -1 // if Set More Than -1 applied -) -{ - // - SetIndexCandleBuffers( - index, - openLabel, - open, - highLabel, - high, - lowLabel, - low, - closeLabel, - close, - showData, - empty, - drawBegin // - ); - - // - int mFirstIndex = index - 4; - - // - // Set Draw Candles Type ... - PlotIndexSetInteger(mFirstIndex, PLOT_LINE_STYLE, DRAW_COLOR_CANDLES); - - // - // Set Color Buffer ... - SetIndexBuffer(index, colorBuffer, INDICATOR_COLOR_INDEX); - - // - index++; -} - -// \ No newline at end of file