From 8d26a582ddf53a548bd66f55c0bacb1ffa5cdc31 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Sun, 16 Jun 2024 05:35:07 +0330 Subject: [PATCH] last ... --- Classes/x-saherelm.x121.setup.cycle.class.mq5 | 79 ++++ Classes/x-saherelm.x121.setup.xea.mq5 | 382 +++++++++++++++++- Classes/x-saherelm.xczone.class.mq5 | 20 +- Classes/x-saherelm.xea.class.mq5 | 14 +- Documents/BKP/usefull.mq5 | 140 +++++++ Experts/x-saherelm.x121.ea.mq5 | 21 +- Libraries/x-saherelm.xtrade.lib.mq5 | 31 +- 7 files changed, 671 insertions(+), 16 deletions(-) create mode 100644 Documents/BKP/usefull.mq5 diff --git a/Classes/x-saherelm.x121.setup.cycle.class.mq5 b/Classes/x-saherelm.x121.setup.cycle.class.mq5 index d8dda788..d9bde2d1 100644 --- a/Classes/x-saherelm.x121.setup.cycle.class.mq5 +++ b/Classes/x-saherelm.x121.setup.cycle.class.mq5 @@ -514,6 +514,85 @@ public: return result; } + // + XSCZones *GetZones( + ENUM_X_MARKET_CYCLES cycle = X_MARKET_CYCLE_UNKNOWN, // + int barIndex = 0, // + int numberOfZones = 10 // + ) + { + // + XSCZonesInput zInputs; + zInputs.numberOfZones = numberOfZones; + zInputs.AddLevelColor( + 0.15, + clrYellow // + ); + zInputs.AddLevelColor( + 0.20, + clrOrange // + ); + zInputs.AddLevelColor( + 0.25, + clrRed // + ); + + // + int length = 0; + ENUM_TIMEFRAMES zPeriod = NULL; + switch (cycle) + { + // + case X_MARKET_CYCLE_SHORT: + // + zPeriod = sCycle.GetPeriod(); + break; + + // + case X_MARKET_CYCLE_MEDIUM: + // + zPeriod = mCycle.GetPeriod(); + break; + + // + case X_MARKET_CYCLE_LONG: + // + zPeriod = lCycle.GetPeriod(); + break; + + // + case X_MARKET_CYCLE_HIND: + // + zPeriod = hCycle.GetPeriod(); + break; + + // + default: + case X_MARKET_CYCLE_UNKNOWN: + // + zPeriod = cCycle.GetPeriod(); + break; + } + + // + ENUM_TIMEFRAMES cPeriod = GetPeriod(); + length = (PeriodSeconds(zPeriod) / PeriodSeconds(cPeriod)) + 1; + + // + zInputs.length = length; + XSCZones *result = new XSCZones( + GetSymbol(), + cPeriod // + ); + if (!result.Init(zInputs)) + { + return NULL; + } + + // + return result; + } + // // Protected ... protected: diff --git a/Classes/x-saherelm.x121.setup.xea.mq5 b/Classes/x-saherelm.x121.setup.xea.mq5 index aeb199c1..9a95918e 100644 --- a/Classes/x-saherelm.x121.setup.xea.mq5 +++ b/Classes/x-saherelm.x121.setup.xea.mq5 @@ -1042,6 +1042,118 @@ public: mSave = value; } + // + // Trailling Stop ... + + // + bool AllowTrailStop() + { + return mAllowTrailStop; + } + + // + void AllowTrailStop(bool value) + { + mAllowTrailStop = value; + } + + // + double NextTrailPoint() + { + return mNextTrailPoint; + } + + // + void NextTrailPoint(double value) + { + // + if (value < 0) + { + value = 0; + } + + // + mNextTrailPoint = value; + } + + // + double FirstTrailPoint() + { + return mFirstTrailPoint; + } + + // + void FirstTrailPoint(double value) + { + // + if (value < 0) + { + value = 0; + } + + // + mFirstTrailPoint = value; + } + + // + double NextTrailDistance() + { + return mNextTrailDistance; + } + + // + void NextTrailDistance(double value) + { + // + if (value < 0) + { + value = 0; + } + + // + mNextTrailDistance = value; + } + + // + double FirstTrailDistance() + { + return mFirstTrailDistance; + } + + // + void FirstTrailDistance(double value) + { + // + if (value < 0) + { + value = 0; + } + + // + mFirstTrailDistance = value; + } + + // + bool EnableTrailling() + { + // + bool result = false; + + // + result = + // + AllowTrailStop() && + NextTrailPoint() > 0 && + FirstTrailPoint() > 0 && + NextTrailDistance() > 0 && + FirstTrailDistance() > 0 + // + ; + + // + return result; + } + // // Grid System ... @@ -1469,6 +1581,140 @@ public: Finish(infoIDX); } + // + // Protection Handlers ... + + // + int HandleTrailling() + { + // + int result = 0; + + // + // Check Trailling is Enabled or not ... + if (!EnableTrailling()) + { + return result; + } + + // + // Do Synch Before Start Trailing ... + Sync(); + + // + // Since Trailing Done based On In Profit Positions ... + // First we Check if ... + XPosition positions[]; + int positionsCount = mTrader.GetInProfitPositions(positions); + if (!IsValidSize(positionsCount)) + { + return result; + } + + // + for (int i = 0; i < positionsCount; i++) + { + // + XPosition iPosition = positions[i]; + + // + // ignore Support Positions ... + bool isSupport = IsSupport(iPosition.comment); + if (isSupport) + { + continue; + } + + // + // Find Positin Info Index ... + int idx = FindIndex(iPosition.ticket); + if (!IsValidIndex(idx)) + { + continue; + } + + // + bool isLong = IsLong(iPosition.type); + + // + // Here we Select Price for SL Trailling ... + double usedPrice = + iPosition.sl > 0 && + isLong + ? iPosition.sl < iPosition.entry + ? iPosition.entry + : iPosition.sl + : iPosition.sl > iPosition.entry + ? iPosition.entry + : iPosition.sl; + + // + double nextTrailPoint = NextTrailPoint(); + double firstTrailPoint = FirstTrailPoint(); + double nextTrailDistance = NextTrailDistance(); + double firstTrailDistance = FirstTrailDistance(); + + // + bool isFirstTrail = usedPrice == iPosition.entry; + + // + double usedTrailDiff = + isFirstTrail + ? MathMax(firstTrailPoint, firstTrailDistance) + : MathMax(nextTrailPoint, nextTrailDistance); + + // + double iExit = GetExit( + iPosition.symbol, + iPosition.type // + ); + double iPriceDistance = MathAbs(usedPrice - iExit); + double iPriceDistanceInPoint = PriceToPoint( + iPriceDistance, + iPosition.symbol // + ); + + // + bool canTrail = iPriceDistanceInPoint >= usedTrailDiff; + if (!canTrail) + { + continue; + } + + // + usedTrailDiff = + isFirstTrail + ? MathMin(firstTrailPoint, firstTrailDistance) + : MathMin(nextTrailPoint, nextTrailDistance); + double usedTrailDiffPrice = PointToPrice( + usedTrailDiff, + iPosition.symbol // + ); + + // + double sl = + isLong + ? usedPrice + usedTrailDiffPrice + : usedPrice - usedTrailDiffPrice; + string comment = PrepareSLTrailTag(iPosition.comment); + + // + bool isModified = mTrader.Modify( + iPosition.ticket, + sl, + iPosition.tp, + comment // + ); + if (isModified) + { + result++; + } + } + + // + return result; + } + // int RequestForGrid(XSignal &signals[]) { @@ -1703,6 +1949,14 @@ protected: } } + // + // Stop Trailling ... + bool mAllowTrailStop; // Enable Trailling Stop Losses + double mFirstTrailDistance; // Start Trailling Distance + double mFirstTrailPoint; // First Stop Trail + double mNextTrailDistance; // Next Trailling Distance + double mNextTrailPoint; // Next Trailling Step + // // Grid ... bool mAllowGrid; // Use Grid System to Recovery Positions @@ -2006,6 +2260,69 @@ public: mUseSupport = value; } + // + // Trailling Stop ... + + // + bool AllowTrailStop() + { + return mTradeInfoHandler.AllowTrailStop(); + } + + // + void AllowTrailStop(bool value) + { + mTradeInfoHandler.AllowTrailStop(value); + } + + // + double NextTrailPoint() + { + return mTradeInfoHandler.NextTrailPoint(); + } + + // + void NextTrailPoint(double value) + { + mTradeInfoHandler.NextTrailPoint(value); + } + + // + double FirstTrailPoint() + { + return mTradeInfoHandler.FirstTrailPoint(); + } + + // + void FirstTrailPoint(double value) + { + mTradeInfoHandler.FirstTrailPoint(value); + } + + // + double NextTrailDistance() + { + return mTradeInfoHandler.NextTrailDistance(); + } + + // + void NextTrailDistance(double value) + { + mTradeInfoHandler.NextTrailDistance(value); + } + + // + double FirstTrailDistance() + { + return mTradeInfoHandler.FirstTrailDistance(); + } + + // + void FirstTrailDistance(double value) + { + mTradeInfoHandler.FirstTrailDistance(value); + } + // // Grid System ... @@ -3087,9 +3404,52 @@ private: return result; } + // + int FindSetupIndex( + string symbol // + ) + { + // + int result = -1; + + // + int count = CountSetups(); + if (!IsValidSize(count)) + { + return result; + } + + // + for (int i = 0; i < count; i++) + { + // + XSCX121SetupCycles *iSetup = mSetups[i]; + + // + X121SetupInputs iInputs = iSetup.GetInputs(); + + // + if (iInputs.symbol == symbol) + { + // + result = i; + break; + } + } + + // + return result; + } + // // EQM Functions ... + // + bool EnableTrailling() + { + return mTradeInfoHandler.EnableTrailling(); + } + // bool EnableGrid() { @@ -3105,7 +3465,7 @@ private: // bool EnableProtection() { - return EnableGrid() || EnableRecovery(); + return EnableGrid() || EnableRecovery() || EnableTrailling(); } // @@ -3347,6 +3707,22 @@ private: return; } + // + // Handle Trailling ... + if (EnableTrailling()) + { + // + int trailsCount = mTradeInfoHandler.HandleTrailling(); + if (IsValidSize(trailsCount)) + { + // + string message = XEQMSupportToken + " Successfully Trail Stop for: " + ToString(trailsCount) + " Position(s) ..."; + + // + Alert(message); + } + } + // XSignal signals[]; int signalsCount = mTradeInfoHandler @@ -3364,6 +3740,10 @@ private: states, false // Ignore Policies ... ); + if (executed != signalsCount) + { + Print("Error On Executing Support Signal ..."); + } } // diff --git a/Classes/x-saherelm.xczone.class.mq5 b/Classes/x-saherelm.xczone.class.mq5 index 3325003f..4b710f00 100644 --- a/Classes/x-saherelm.xczone.class.mq5 +++ b/Classes/x-saherelm.xczone.class.mq5 @@ -344,7 +344,8 @@ public: // // Find Highest Value in Range ... - mRangeHigh = mHighs[ArrayMaximum(mHighs)]; + int highIDX = ArrayMaximum(mHighs); + mRangeHigh = mHighs[highIDX]; // // Retrieve Lows ... @@ -359,7 +360,20 @@ public: // // Find Lowest Value in Range ... - mRangeLow = mLows[ArrayMinimum(mLows)]; + int lowIDX = ArrayMinimum(mLows); + mRangeLow = mLows[lowIDX]; + + // + datetime mTimes[]; + CopyTime( + mSymbol, + mPeriod, + mStartTime, + mEndTime, + mTimes // + ); + mRangeHighTime = mTimes[highIDX]; + mRangeLowTime = mTimes[lowIDX]; // double mRangeSize = GetRange(); @@ -632,7 +646,9 @@ protected: double mHighs[]; double mLows[]; double mRangeHigh; + datetime mRangeHighTime; double mRangeLow; + datetime mRangeLowTime; // CArrayObj mZones; diff --git a/Classes/x-saherelm.xea.class.mq5 b/Classes/x-saherelm.xea.class.mq5 index 92bc85ab..af9b9b8a 100644 --- a/Classes/x-saherelm.xea.class.mq5 +++ b/Classes/x-saherelm.xea.class.mq5 @@ -1280,7 +1280,7 @@ protected: string message = "Guard Hedge " + provider + "'s Positions on Profit: " + ToString(profit); // - mAlert.Alert(message); + Alert(message); } } @@ -1323,7 +1323,7 @@ protected: string message = "Guard Close (" + ToString(positionsCount) + ") Postion(s) In Profit ..."; // - mAlert.Alert(message); + Alert(message); } // @@ -1365,7 +1365,7 @@ protected: string message = "Guard Close (" + ToString(positionsCount) + ") Position(s) In Drawdown ..."; // - mAlert.Alert(message); + Alert(message); } // @@ -1441,7 +1441,7 @@ protected: // if (IsValid(comment)) { - mAlert.Alert(comment); + Alert(comment); } } @@ -1519,7 +1519,7 @@ protected: string message = "(" + ToString(modified) + ") Position's Stop Trailed Successfully ..."; // - mAlert.Alert(message); + Alert(message); } } @@ -1573,7 +1573,7 @@ protected: // if (IsValid(comment)) { - mAlert.Alert(comment); + Alert(comment); } } @@ -1613,7 +1613,7 @@ protected: string message = "Guard Cancel (" + ToString(canceledOrdersCount) + ") Pending Orders ..."; // - mAlert.Alert(message); + Alert(message); } } diff --git a/Documents/BKP/usefull.mq5 b/Documents/BKP/usefull.mq5 new file mode 100644 index 00000000..e5288549 --- /dev/null +++ b/Documents/BKP/usefull.mq5 @@ -0,0 +1,140 @@ + + // + XSCZones *mLastHindZones; + XSCZones *mLastLongZones; + XSCZones *mLastMediumZones; + + // + bool DoEQMProtectOfSupports() + { + // + bool result = false; + + // + // The Senario is When all Signals are Support and Account in Drawdown + // here we have to Do some Support Actions ... + + // + XPosition positions[]; + int positionsCount = mTrader.GetPositions(positions); + result = IsValidSize(positionsCount); + if (!result) + { + return result; + } + + // + // Noe We Have to Separate Support Positions ... + XPosition supports[]; + int supportsCount = ExtractSupportPositions( + positions, + supports // + ); + result = IsValidSize(supportsCount); + if (!result) + { + return result; + } + + // + // Now We Are sure we have Support Positions ... + // then we must Check the Supports are Long enough to make the Support ... + XPosition youngest; + int youngestAge = GetYoungest( + youngest, + supports // + ); + result = youngestAge > 2; + if (!result) + { + return result; + } + + // + // Try to Find Youngest Support's Setup ... + int setupIDX = FindSetupIndex( + youngest.symbol // + ); + result = IsValidIndex(setupIDX); + if (!result) + { + return result; + } + + // + // Now we can Implememnt Support of Supports based on + // Several Senarios ... + // 1. Recieve Market Conditions; + // 2. Recieve Summary of Long Verifications and Short Verifications; + // 3. Collect VolumeOSC and also Bullish and Bearish Scores; + // 4. Collect XSCZones of Last HIND Market; + + // + // Hind Zone ... + if (mLastHindZones == NULL) + { + // + mLastHindZones = mSetups[setupIDX].GetZones( + X_MARKET_CYCLE_HIND // + ); + result = mLastHindZones != NULL; + if (!result) + { + return result; + } + + // + result = mLastHindZones.DrawZones(0, "HIN"); + if (result) + { + Print("Zone Drawn ..."); + } + } + + // + // Long Zone ... + if (mLastLongZones == NULL) + { + // + mLastLongZones = mSetups[setupIDX].GetZones( + X_MARKET_CYCLE_LONG // + ); + result = mLastLongZones != NULL; + if (!result) + { + return result; + } + + // + result = mLastLongZones.DrawZones(0, "LON"); + if (result) + { + Print("Zone Drawn ..."); + } + } + + // + // Medium Zone ... + if (mLastMediumZones == NULL) + { + // + mLastMediumZones = mSetups[setupIDX].GetZones( + X_MARKET_CYCLE_MEDIUM // + ); + result = mLastMediumZones != NULL; + if (!result) + { + return result; + } + + // + result = mLastMediumZones.DrawZones(0, "MED"); + if (result) + { + Print("Zone Drawn ..."); + } + } + + // + return result; + } diff --git a/Experts/x-saherelm.x121.ea.mq5 b/Experts/x-saherelm.x121.ea.mq5 index 1fef4ccb..6b832ccf 100644 --- a/Experts/x-saherelm.x121.ea.mq5 +++ b/Experts/x-saherelm.x121.ea.mq5 @@ -49,16 +49,21 @@ input bool x121EAGetVerificationFromOtherTimeFrames = true; // Get Verifications // Risk Management ... input group "Risk Management"; input double x121EAVolume = 0.01; // Static Volume -input double x121EATPPoint = 100; // TP Point -input double x121EASLPoint = 100; // SL Point +input double x121EATPPoint = 200; // TP Point +input double x121EASLPoint = 200; // SL Point input int x121EAMaxAllowedTrades = 1; // Max Allowed Positions -input bool x121EAIgnoreSL = false; // Ignore Calculated SL +input bool x121EAIgnoreSL = false; // Ignore Calculated SL input bool x121EAIgnoreTP = false; // Ignore Calculated TP +input bool x121EAAllowTrailStop = true; // Enable Trailling Stop Losses +input double x121EAFirstTrailDistance = 60; // Start Trailling Distance +input double x121EAFirstTrailPoint = 50; // First Stop Trail +input double x121EANextTrailDistance = 10; // Next Trailling Distance +input double x121EANextTrailPoint = 5; // Next Trailling Step input bool x121EAUseSupport = false; // Use Support Signals input bool x121EAAllowGrid = false; // Allow Grid Signals input double x121EAGridDistance = 100; // Grid Distance input double x121EAGridVolumeMultiplier = 2; // Grid Volume Multiplier -input bool x121EAAllowRecovery = true; // Allow Recovery Signals +input bool x121EAAllowRecovery = false; // Allow Recovery Signals input double x121EARecoveryDistance = 100; // Recovery Distance input double x121EARecoveryVolumeMultiplier = 2; // Recovery Volume Multiplier input double x121EAMinRequiredProfitPerTrade = 0.5; // Minimum Required Profit for Hedging @@ -204,6 +209,14 @@ bool InitialEA() x121EA.RecoveryDistance(x121EARecoveryDistance); x121EA.RecoveryVolumeMultiplier(x121EARecoveryVolumeMultiplier); + // + // Stop Loss Trailing ... + x121EA.AllowTrailStop(x121EAAllowTrailStop); + x121EA.NextTrailPoint(x121EANextTrailPoint); + x121EA.FirstTrailPoint(x121EAFirstTrailPoint); + x121EA.NextTrailDistance(x121EANextTrailDistance); + x121EA.FirstTrailDistance(x121EAFirstTrailDistance); + // // Configure Alert ... x121EA.SetAlertPrefix(ShortName); diff --git a/Libraries/x-saherelm.xtrade.lib.mq5 b/Libraries/x-saherelm.xtrade.lib.mq5 index fc0a3e15..cee54666 100644 --- a/Libraries/x-saherelm.xtrade.lib.mq5 +++ b/Libraries/x-saherelm.xtrade.lib.mq5 @@ -4086,6 +4086,7 @@ ulong ExtractSupportedTicket(string value) } // +// Extract Support Positions ... int ExtractSupportPositions( XPosition &source[], XPosition &dest[] // Result ... @@ -4111,8 +4112,9 @@ int ExtractSupportPositions( XPosition iSource = source[i]; // - ulong iParentTicket = ExtractSupportedTicket(iSource.comment); - if (iParentTicket > 0) + ulong isSupport = ExtractSupportedTicket(iSource.comment) > 0; + ulong iEQMSupport = ExtractEQMSupportedTicket(iSource.comment) > 0; + if (isSupport || iEQMSupport) { // AddRef( @@ -4153,6 +4155,31 @@ ulong ExtractEQMSupportedTicket(string value) ); } +// +bool IsSupport(string value) +{ + // + bool result = false; + + // + bool isSupport = ExtractSupportedTicket(value) > 0; + bool isEQMSupport = ExtractEQMSupportedTicket(value) > 0; + + // + result = + // + isSupport + // + || + // + isEQMSupport + // + ; + + // + return result; +} + // // SL Trails ...