/////////////////////////////////////////////////////// // // SaherElm IT Center MQL5 X121 SMC Signal Class // ------------------------------------------------- // Name: // Description: provide all Global Guarding functions ... // // // 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-121.smc.market.cycle.helper.class.mq5" #include "../Libraries/x-121.smc.lib.mq5" // XConsolidationZone x121SMCConsolidationZone; bool DetectX121SMCGuards( string provider, // Provider ... X121SMCGuard &guards[], const XPosition &positions[], XC121SMCCycleHelper *triggerCycleHelper, XC121SMCCycleHelper *decisionCycleHelper, XC121SMCCycleHelper *analyseCycleHelper, XC121SMCCycleHelper *verificationCycleHelper, XC121SMCCycleHelper *consolidationCycleHelper, XC121SMCCycleHelper *visionCycleHelper, // // Guard Types ... bool ignoreCloseOnTPNearGuard = false, bool ignoreCloseLongsOnBearishTrendGuard = false, bool ignoreCloseShortsOnBullishTrendGuard = false, bool ignoreCloseAllWeekendGuard = false, bool ignorePartialCloseOnTrendChangeGuard = false, bool ignoreTrailStopGuard = false, bool ignoreTrailTargetGuard = false, bool ignoreHedgeGuard = false // ) { // bool result = false; // bool isAllIgnore = // ignoreCloseOnTPNearGuard && ignoreCloseLongsOnBearishTrendGuard && ignoreCloseShortsOnBullishTrendGuard && ignoreCloseAllWeekendGuard && ignorePartialCloseOnTrendChangeGuard && ignoreTrailStopGuard && ignoreTrailTargetGuard && ignoreHedgeGuard // ; result = !isAllIgnore; if (!result) { return result; } // int count = ArraySize(positions); result = IsValidSize(count); if (!result) { return result; } // string symbol = triggerCycleHelper.GetSymbol(); ENUM_TIMEFRAMES period = triggerCycleHelper.GetPeriod(); // XPosition ownPositions[]; XPosition ownLongPositions[]; XPosition ownShortPositions[]; for (int i = 0; i < count; i++) { // XPosition iPosition = positions[i]; bool isOwn = iPosition .IsFiltersPassed( symbol, provider, NULL, X_POSITION_TYPE_ALL, iPosition.magic // ); if (isOwn) { // AddRef( iPosition, ownPositions // ); // bool isLong = IsLong(positions[i].type); if (isLong) { // AddRef( iPosition, ownLongPositions // ); } else { // AddRef( iPosition, ownShortPositions // ); } } } // count = ArraySize(ownPositions); // int longsCount = ArraySize(ownLongPositions); bool hasLongs = IsValidSize(longsCount); // int shortsCount = ArraySize(ownShortPositions); bool hasShorts = IsValidSize(shortsCount); // result = IsValidSize(count); if (!result) { // Clean(ownPositions); Clean(ownLongPositions); Clean(ownShortPositions); // return result; } // // Here we Are Sure there is Positions to Guard ... // based on Current Provider ... // // Detect Required Data for Analyse Market to Guard Positions ... // int zIndex = 0; int cIndex = zIndex + 1; int pIndex = cIndex + 1; int ppIndex = pIndex + 1; // int loopback = 10; // XOHCL zBar; result = zBar.Init( symbol, period, zIndex // ); if (!result) { return result; } // XOHCL cBar; result = cBar.Init( symbol, period, cIndex // ); if (!result) { return result; } // XOHCL pBar; result = pBar.Init( symbol, period, pIndex // ); if (!result) { return result; } // double ask = GetAsk(symbol); double bid = GetBid(symbol); datetime cTime = TimeCurrent(); double points = GetPoints(symbol); // int swingLoopback = 3; double ll = cBar.FindLowest(swingLoopback, MODE_LOW); double hh = cBar.FindHighest(swingLoopback, MODE_HIGH); double llDown = cBar.FindLowesttDown(swingLoopback); double hhUp = cBar.FindHighestUp(swingLoopback); // // Cycles Conditions ... // // Trigger ... ENUM_XPOI_EVENTS triggerPoiEvents[]; ENUM_X_CYCLE_EVENTS triggerCycleEvents[]; X121SMCCycleConditions triggerConditions; result = triggerCycleHelper.GetConditions( triggerCycleEvents, triggerPoiEvents, triggerConditions, zIndex, loopback // ); if (!result) { return result; } // // Decision ... ENUM_XPOI_EVENTS decisionPoiEvents[]; ENUM_X_CYCLE_EVENTS decisionCycleEvents[]; X121SMCCycleConditions decisionConditions; result = decisionCycleHelper.GetConditions( decisionCycleEvents, decisionPoiEvents, decisionConditions, zIndex, loopback // ); if (!result) { return result; } // // Analyse ... ENUM_XPOI_EVENTS analysePoiEvents[]; ENUM_X_CYCLE_EVENTS analyseCycleEvents[]; X121SMCCycleConditions analyseConditions; result = analyseCycleHelper.GetConditions( analyseCycleEvents, analysePoiEvents, analyseConditions, zIndex, loopback // ); if (!result) { return result; } // // Verification ... ENUM_XPOI_EVENTS verificationPoiEvents[]; ENUM_X_CYCLE_EVENTS verificationCycleEvents[]; X121SMCCycleConditions verificationConditions; result = verificationCycleHelper.GetConditions( verificationCycleEvents, verificationPoiEvents, verificationConditions, zIndex, loopback // ); if (!result) { return result; } // // Consolidation ... ENUM_XPOI_EVENTS consolidationPoiEvents[]; ENUM_X_CYCLE_EVENTS consolidationCycleEvents[]; X121SMCCycleConditions consolidationConditions; result = consolidationCycleHelper.GetConditions( consolidationCycleEvents, consolidationPoiEvents, consolidationConditions, zIndex, loopback // ); if (!result) { return result; } // // Vision ... ENUM_XPOI_EVENTS visionPoiEvents[]; ENUM_X_CYCLE_EVENTS visionCycleEvents[]; X121SMCCycleConditions visionConditions; result = visionCycleHelper.GetConditions( visionCycleEvents, visionPoiEvents, visionConditions, zIndex, loopback // ); if (!result) { return result; } // // Cycles POI States ... XPOIState triggerState = triggerConditions.state; XPOIState decisionState = decisionConditions.state; XPOIState analyseState = analyseConditions.state; XPOIState verificationState = verificationConditions.state; XPOIState consolidationState = consolidationConditions.state; XPOIState visionState = visionConditions.state; // // Cycle X121Conditions ... X121Conditions triggerXConditions = triggerConditions.x121Conditions; X121Conditions decisionXConditions = decisionConditions.x121Conditions; X121Conditions analyseXConditions = analyseConditions.x121Conditions; X121Conditions verificationXConditions = verificationConditions.x121Conditions; X121Conditions consolidationXConditions = consolidationConditions.x121Conditions; X121Conditions visionXConditions = visionConditions.x121Conditions; // // // // // Detect Consolidation Zone ... // int consolidationLoopback = 7; double consolidationZoneLower = 0; double consolidationZoneUpper = 0; bool isConsolidationZoneBreaked = false; bool isConsolidationZoneBreakedUp = false; bool isConsolidationZoneBreakedDown = false; bool hasConsolidationZone = x121SMCConsolidationZone.IsValid(); // if (!hasConsolidationZone) { // double upper = 0; double lower = 0; hasConsolidationZone = triggerCycleHelper .mBarAnalyser .IsConsolidate( cBar, upper, lower, consolidationLoopback // ); // if (hasConsolidationZone) { // hasConsolidationZone = x121SMCConsolidationZone.Init( cBar.symbol, cBar.period, upper, lower, consolidationLoopback // ); } } // if (hasConsolidationZone) { // x121SMCConsolidationZone.Update(); // consolidationZoneUpper = x121SMCConsolidationZone.upper; consolidationZoneLower = x121SMCConsolidationZone.lower; // isConsolidationZoneBreaked = x121SMCConsolidationZone.IsBreaked(); isConsolidationZoneBreakedUp = isConsolidationZoneBreaked && IsBullish(x121SMCConsolidationZone.breakDirection); isConsolidationZoneBreakedDown = isConsolidationZoneBreaked && IsBearish(x121SMCConsolidationZone.breakDirection); // consolidationZoneUpper = triggerXConditions.peaksBuffer[1]; consolidationZoneLower = triggerXConditions.valesBuffer[1]; // if (isConsolidationZoneBreaked) { x121SMCConsolidationZone.Clean(); } } // // Here We Have all Requirements for Guarding Positions ... // // // // // Close ... if (!ignoreCloseOnTPNearGuard) { // double tpCloseDiff = (15 * points); for (int i = 0; i < count; i++) { // XPosition iPosition = ownPositions[i]; // // Position Must be In Profit ... if (iPosition.profit <= 0) { continue; } // bool isPricePassed = MathAbs(iPosition.tp - iPosition.price) <= tpCloseDiff; if (!isPricePassed) { continue; } // bool isLong = IsLong(iPosition.type); // bool iHasGuard = // isLong ? // false // : // false // ; if (iHasGuard) { // X121SMCGuard iGuard; // iGuard.time = cTime; iGuard.symbol = symbol; iGuard.provider = provider; iGuard.action = X_121_SMC_GUARD_ACTION_CLOSE; // iGuard.ticket = iPosition.ticket; // AddGuard( iGuard, guards // ); } } } // if (hasLongs) { // if (!ignoreCloseLongsOnBearishTrendGuard) { // bool hasGuard = false; // hasDecisionBearishTrend; // if (hasGuard) { // for (int i = 0; i < longsCount; i++) { // XPosition iPosition = ownLongPositions[i]; // // Position must be in Draw Down ... if (iPosition.profit > 0) { continue; } // X121SMCGuard iGuard; // iGuard.time = cTime; iGuard.symbol = symbol; iGuard.provider = provider; iGuard.action = X_121_SMC_GUARD_ACTION_CLOSE; // iGuard.ticket = iPosition.ticket; // AddGuard( iGuard, guards // ); } } } } // if (hasShorts) { // if (!ignoreCloseShortsOnBullishTrendGuard) { // bool hasGuard = false; // hasDecisionBullishTrend; // if (hasGuard) { // for (int i = 0; i < shortsCount; i++) { // XPosition iPosition = ownShortPositions[i]; // // Position must be in Draw Down ... if (iPosition.profit > 0) { continue; } // X121SMCGuard iGuard; // iGuard.time = cTime; iGuard.symbol = symbol; iGuard.provider = provider; iGuard.action = X_121_SMC_GUARD_ACTION_CLOSE; // iGuard.ticket = iPosition.ticket; // AddGuard( iGuard, guards // ); } } } } // // Close All ... // // Close All Weekend ... if (!ignoreCloseAllWeekendGuard) { // MqlDateTime mDate; bool hasDate = TimeCurrent(mDate); if (hasDate) { // ENUM_X_WEEK_DAYS dayOfWeek = GetDayOfWeek(); // bool isWeekend = dayOfWeek == X_WEEK_DAY_FRIDAY; if (isWeekend && mDate.hour == 23 && mDate.min == 30) { // X121SMCGuard guard; // guard.time = cTime; guard.symbol = symbol; guard.provider = provider; guard.action = X_121_SMC_GUARD_ACTION_CLOSE_ALL; // AddGuard( guard, guards // ); } } } // // Close Longs ... if (hasLongs) { // bool hasGuard = // false // // hasAnalyseBearishTrend && // hasDecisionBearishTrend // ; // if (hasGuard) { // X121SMCGuard guard; // guard.time = cTime; guard.symbol = symbol; guard.provider = provider; guard.action = X_121_SMC_GUARD_ACTION_CLOSE_LONGS; // AddGuard( guard, guards // ); } } // // Close Shorts ... if (hasShorts) { // bool hasGuard = // false // // hasAnalyseBullishTrend && // hasDecisionBullishTrend // ; // if (hasGuard) { // X121SMCGuard guard; // guard.time = cTime; guard.symbol = symbol; guard.provider = provider; guard.action = X_121_SMC_GUARD_ACTION_CLOSE_SHORTS; // AddGuard( guard, guards // ); } } // // Partial Close ... // if (!ignorePartialCloseOnTrendChangeGuard) { // if (hasLongs) { // bool hasGuard = // false // // hasTriggerBearishTrend || // !hasDecisionBullishTrend // ; // if (hasGuard) { // for (int i = 0; i < longsCount; i++) { // XPosition iPosition = ownLongPositions[i]; // X121SMCGuard iGuard; // iGuard.time = cTime; iGuard.symbol = symbol; iGuard.provider = provider; iGuard.action = X_121_SMC_GUARD_ACTION_PARTIAL_CLOSE; // iGuard.ticket = iPosition.ticket; iGuard.volumeMultiplier = 0.5; // AddGuard( iGuard, guards // ); } } } // if (hasShorts) { // bool hasGuard = // false // // hasTriggerBullishTrend || // !hasDecisionBearishTrend // ; // if (hasGuard) { // for (int i = 0; i < shortsCount; i++) { // XPosition iPosition = ownShortPositions[i]; // X121SMCGuard iGuard; // iGuard.time = cTime; iGuard.symbol = symbol; iGuard.provider = provider; iGuard.action = X_121_SMC_GUARD_ACTION_PARTIAL_CLOSE; // iGuard.ticket = iPosition.ticket; iGuard.volumeMultiplier = 0.5; // AddGuard( iGuard, guards // ); } } } } // // Trail Stop ... if (!ignoreTrailStopGuard) { // if (hasLongs) { // bool hasTrendGuard = false; // hasDecisionBearishTrend; // bool hasConsolidationGuard = (hasConsolidationZone && isConsolidationZoneBreaked && isConsolidationZoneBreakedUp); // bool hasDecisionPriceZoneGuard = false; // (hasDecisionPriceZone && // isDecisionPriceZoneBullish && // isDecisionPriceZoneValidForVale && // isDecisionPriceZoneUpperBreakedUp); // bool hasAnalysePriceZoneGuard = false; // (hasAnalysePriceZone && // isAnalysePriceZoneBullish && // isAnalysePriceZoneValidForVale && // isAnalysePriceZoneUpperBreakedUp); // bool hasVerificationPriceZoneGuard = false; // (hasVerificationPriceZone && // isVerificationPriceZoneBullish && // isVerificationPriceZoneValidForVale && // isVerificationPriceZoneUpperBreakedUp); // bool hasGuard = // hasTrendGuard || hasConsolidationGuard || hasDecisionPriceZoneGuard || hasAnalysePriceZoneGuard || hasVerificationPriceZoneGuard // ; // double guardStop = hasConsolidationGuard ? consolidationZoneLower : hasDecisionPriceZoneGuard ? 0 // decisionPriceZoneLower : hasAnalysePriceZoneGuard ? 0 // analysePriceZoneLower : hasVerificationPriceZoneGuard ? 0 // verificationPriceZoneLower : 0; // if (hasGuard) { // for (int i = 0; i < longsCount; i++) { // XPosition iPosition = ownLongPositions[i]; // // Position Must Be In Profit ... if (iPosition.profit <= 0) { continue; } // if (hasTrendGuard) { guardStop = decisionXConditions.strBuffer[1] - decisionXConditions.atrBuffer[1]; } // // Price Must Greater than Zone Lower ... if (iPosition.price < guardStop) { continue; } // // Position Entry Must Lesser than Zone Lower ... if (iPosition.entry > guardStop) { continue; } // // Position SL must be Zero or Lesser than Zone Lower ... if (iPosition.sl != 0 && iPosition.sl > guardStop) { continue; } // X121SMCGuard iGuard; // iGuard.time = cTime; iGuard.symbol = symbol; iGuard.provider = provider; iGuard.action = X_121_SMC_GUARD_ACTION_TRAIL_STOP; // iGuard.sl = guardStop; iGuard.ticket = iPosition.ticket; // AddGuard( iGuard, guards // ); } } } // if (hasShorts) { // bool hasTrendGuard = false; // hasDecisionBullishTrend; // bool hasConsolidationGuard = (hasConsolidationZone && isConsolidationZoneBreaked && isConsolidationZoneBreakedDown); // bool hasDecisionPriceZoneGuard = false; // (hasDecisionPriceZone && // isDecisionPriceZoneBearish && // isDecisionPriceZoneValidForPeak && // isDecisionPriceZoneLowerBreakedDown); // bool hasAnalysePriceZoneGuard = false; // (hasAnalysePriceZone && // isAnalysePriceZoneBearish && // isAnalysePriceZoneValidForPeak && // isAnalysePriceZoneLowerBreakedDown); // bool hasVerificationPriceZoneGuard = false; // (hasVerificationPriceZone && // isVerificationPriceZoneBearish && // isVerificationPriceZoneValidForPeak && // isVerificationPriceZoneLowerBreakedDown); // bool hasGuard = // hasTrendGuard || hasConsolidationGuard || hasDecisionPriceZoneGuard || hasAnalysePriceZoneGuard || hasVerificationPriceZoneGuard // ; // double guardStop = hasConsolidationGuard ? consolidationZoneUpper : hasDecisionPriceZoneGuard ? 0 // decisionPriceZoneUpper : hasAnalysePriceZoneGuard ? 0 // analysePriceZoneUpper : hasVerificationPriceZoneGuard ? 0 // verificationPriceZoneUpper : 0; // if (hasGuard) { // for (int i = 0; i < shortsCount; i++) { // XPosition iPosition = ownShortPositions[i]; // // Position Must Be In Profit ... if (iPosition.profit <= 0) { continue; } // if (hasTrendGuard) { guardStop = decisionXConditions.strBuffer[1] + decisionXConditions.atrBuffer[1]; } // // Price Must Lesser than Zone Upper ... if (iPosition.price > guardStop) { continue; } // // Position Entry Must Greater than Zone Upper ... if (iPosition.entry < guardStop) { continue; } // // Position SL must be Zero or Greater than Zone Upper ... if (iPosition.sl != 0 && iPosition.sl < guardStop) { continue; } // X121SMCGuard iGuard; // iGuard.time = cTime; iGuard.symbol = symbol; iGuard.provider = provider; iGuard.action = X_121_SMC_GUARD_ACTION_TRAIL_STOP; // iGuard.sl = guardStop; iGuard.ticket = iPosition.ticket; // AddGuard( iGuard, guards // ); } } } } // // Trail Target ... if (!ignoreTrailTargetGuard) { // if (hasLongs) { // bool hasAnalyseGuard = // false // // (hasAnalysePriceZone && // isAnalysePriceZoneValidForPeak && // isAnalysePriceZoneUpperRejected) // ; // bool hasVerificationGuard = // false // // (hasVerificationPriceZone && // isVerificationPriceZoneValidForPeak && // isVerificationPriceZoneUpperRejected) // ; // bool hasGuard = // hasAnalyseGuard || hasVerificationGuard // ; // double guardTP = hasAnalyseGuard ? 0 // analysePriceZoneUpper : hasVerificationGuard ? 0 // verificationPriceZoneUpper : 0; // if (hasGuard) { // for (int i = 0; i < longsCount; i++) { // XPosition iPosition = ownLongPositions[i]; // // Position Must Be In Profit ... if (iPosition.profit <= 0) { continue; } // X121SMCGuard iGuard; // iGuard.time = cTime; iGuard.symbol = symbol; iGuard.provider = provider; iGuard.action = X_121_SMC_GUARD_ACTION_TRAIL_TARGET; // iGuard.tp = guardTP; iGuard.ticket = iPosition.ticket; // AddGuard( iGuard, guards // ); } } } // if (hasShorts) { // bool hasAnalyseGuard = // false // // (hasAnalysePriceZone && // isAnalysePriceZoneValidForVale && // isAnalysePriceZoneLowerRejected) // ; // bool hasVerificationGuard = // false // // (hasVerificationPriceZone && // isVerificationPriceZoneValidForVale && // isVerificationPriceZoneLowerRejected) // ; // bool hasGuard = // hasAnalyseGuard || hasVerificationGuard // ; // double guardTP = hasAnalyseGuard ? 0 // analysePriceZoneLower : hasVerificationGuard ? 0 // verificationPriceZoneLower : 0; // if (hasGuard) { // for (int i = 0; i < shortsCount; i++) { // XPosition iPosition = ownShortPositions[i]; // // Position Must Be In Profit ... if (iPosition.profit <= 0) { continue; } // X121SMCGuard iGuard; // iGuard.time = cTime; iGuard.symbol = symbol; iGuard.provider = provider; iGuard.action = X_121_SMC_GUARD_ACTION_TRAIL_TARGET; // iGuard.tp = guardTP; iGuard.ticket = iPosition.ticket; // AddGuard( iGuard, guards // ); } } } } // // Hedge ... if (!ignoreHedgeGuard) { } // // Cleanup Collections ... // Clean(ownPositions); Clean(ownLongPositions); Clean(ownShortPositions); // // x121SMCAnalysePriceZone.Clean(); // x121SMCDecisionPriceZone.Clean(); // x121SMCConsolidationZone.Clean(); // x121SMCVerificationPriceZone.Clean(); // // Release Arrays ... Clean(ownPositions); Clean(ownLongPositions); Clean(ownShortPositions); Clean(triggerPoiEvents); Clean(triggerCycleEvents); Clean(decisionPoiEvents); Clean(decisionCycleEvents); Clean(analysePoiEvents); Clean(analyseCycleEvents); Clean(verificationPoiEvents); Clean(verificationCycleEvents); Clean(consolidationPoiEvents); Clean(consolidationCycleEvents); Clean(visionPoiEvents); Clean(visionCycleEvents); // count = ArraySize(guards); result = IsValidSize(count); // return result; }