/////////////////////////////////////////////////////// // // 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" // bool DetectX121SMCGuards( // string signalProvider, // Provider ... X121SMCGuard &guards[], const XPosition &positions[], X121SMCStrategySignalProviderData &provider, // // Guard Types ... ENUM_X_121_SMC_GUARD_ACTIONS &ignoreGuards[], // // Required Configurations ... double breakEvenTPLevel = 2.0 // ) { // bool result = false; // // // bool isCycleHelpersValid = // // // visionCycleHelper != NULL && // analyseCycleHelper != NULL && // decisionCycleHelper != NULL && // consolidationCycleHelper != NULL // // // ; // // // // Ignore Guards ... // bool hasIgnoreGuards = HasChild(ignoreGuards); // bool isColseIgnored = // hasIgnoreGuards && // Contains( // X_121_SMC_GUARD_ACTION_CLOSE, // ignoreGuards // // ); // bool isColseAllIgnored = // hasIgnoreGuards && // Contains( // X_121_SMC_GUARD_ACTION_CLOSE_ALL, // ignoreGuards // // ); // bool isColseLongsIgnored = // hasIgnoreGuards && // Contains( // X_121_SMC_GUARD_ACTION_CLOSE_LONGS, // ignoreGuards // // ); // bool isColseShortsIgnored = // hasIgnoreGuards && // Contains( // X_121_SMC_GUARD_ACTION_CLOSE_SHORTS, // ignoreGuards // // ); // bool isPartialColseIgnored = // hasIgnoreGuards && // Contains( // X_121_SMC_GUARD_ACTION_PARTIAL_CLOSE, // ignoreGuards // // ); // bool isTrailStopIgnored = // hasIgnoreGuards && // Contains( // X_121_SMC_GUARD_ACTION_TRAIL_STOP, // ignoreGuards // // ); // bool isTrailTargetIgnored = // hasIgnoreGuards && // Contains( // X_121_SMC_GUARD_ACTION_TRAIL_TARGET, // ignoreGuards // // ); // bool isHedgeIgnored = // hasIgnoreGuards && // Contains( // X_121_SMC_GUARD_ACTION_HEDGE, // ignoreGuards // // ); // // // bool isAllIgnores = // // // isColseIgnored && // isHedgeIgnored && // isColseAllIgnored && // isTrailStopIgnored && // isColseLongsIgnored && // isColseShortsIgnored && // isTrailTargetIgnored && // isPartialColseIgnored // // // ; // result = !isAllIgnores && // isCycleHelpersValid; // if (!result) // { // return result; // } // // // int count = ArraySize(positions); // result = IsValidSize(count); // if (!result) // { // return result; // } // // // string symbol = decisionCycleHelper.GetSymbol(); // ENUM_TIMEFRAMES period = decisionCycleHelper.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) // { // // // zBar.Clean(); // result = HasChild(guards); // return result; // } // // // XOHCL cBar; // result = cBar.Init( // symbol, // period, // cIndex // // ); // if (!result) // { // // // zBar.Clean(); // cBar.Clean(); // result = HasChild(guards); // return result; // } // // // XOHCL pBar; // result = pBar.Init( // symbol, // period, // pIndex // // ); // if (!result) // { // // // zBar.Clean(); // cBar.Clean(); // pBar.Clean(); // result = HasChild(guards); // return result; // } // // // XOHCL ppBar; // result = ppBar.Init( // symbol, // period, // ppIndex // // ); // if (!result) // { // // // zBar.Clean(); // cBar.Clean(); // pBar.Clean(); // ppBar.Clean(); // result = HasChild(guards); // return result; // } // // // double ask = GetAsk(symbol); // double bid = GetBid(symbol); // datetime cTime = TimeCurrent(); // double points = GetPoints(symbol); // // // // // // Cycles Conditions ... // // // // Decision ... // ENUM_XPOI_EVENTS decisionPoiEvents[]; // ENUM_X_CYCLE_EVENTS decisionCycleEvents[]; // X121SMCCycleConditions decisionConditions; // result = decisionCycleHelper.GetConditions( // decisionCycleEvents, // decisionPoiEvents, // decisionConditions, // zIndex, // loopback // // ); // if (!result) // { // // // zBar.Clean(); // cBar.Clean(); // pBar.Clean(); // ppBar.Clean(); // result = HasChild(guards); // // // Clean(decisionPoiEvents); // Clean(decisionCycleEvents); // // // decisionConditions.Clean(); // return result; // } // // // // Analyse ... // ENUM_XPOI_EVENTS analysePoiEvents[]; // ENUM_X_CYCLE_EVENTS analyseCycleEvents[]; // X121SMCCycleConditions analyseConditions; // result = analyseCycleHelper.GetConditions( // analyseCycleEvents, // analysePoiEvents, // analyseConditions, // zIndex, // loopback // // ); // if (!result) // { // // // zBar.Clean(); // cBar.Clean(); // pBar.Clean(); // ppBar.Clean(); // // // Clean(decisionPoiEvents); // Clean(decisionCycleEvents); // // // decisionConditions.Clean(); // // // Clean(analysePoiEvents); // Clean(analyseCycleEvents); // // // analyseConditions.Clean(); // // // result = HasChild(guards); // return result; // } // // // // Consolidation ... // ENUM_XPOI_EVENTS consolidationPoiEvents[]; // ENUM_X_CYCLE_EVENTS consolidationCycleEvents[]; // X121SMCCycleConditions consolidationConditions; // result = consolidationCycleHelper.GetConditions( // consolidationCycleEvents, // consolidationPoiEvents, // consolidationConditions, // zIndex, // loopback // // ); // if (!result) // { // // // zBar.Clean(); // cBar.Clean(); // pBar.Clean(); // ppBar.Clean(); // // // Clean(decisionPoiEvents); // Clean(decisionCycleEvents); // // // decisionConditions.Clean(); // // // Clean(analysePoiEvents); // Clean(analyseCycleEvents); // // // analyseConditions.Clean(); // // // Clean(consolidationPoiEvents); // Clean(consolidationCycleEvents); // // // consolidationConditions.Clean(); // // // result = HasChild(guards); // return result; // } // // // // Vision ... // ENUM_XPOI_EVENTS visionPoiEvents[]; // ENUM_X_CYCLE_EVENTS visionCycleEvents[]; // X121SMCCycleConditions visionConditions; // result = visionCycleHelper.GetConditions( // visionCycleEvents, // visionPoiEvents, // visionConditions, // zIndex, // loopback // // ); // if (!result) // { // // // zBar.Clean(); // cBar.Clean(); // pBar.Clean(); // ppBar.Clean(); // // // Clean(decisionPoiEvents); // Clean(decisionCycleEvents); // // // decisionConditions.Clean(); // // // Clean(analysePoiEvents); // Clean(analyseCycleEvents); // // // analyseConditions.Clean(); // // // Clean(consolidationPoiEvents); // Clean(consolidationCycleEvents); // // // consolidationConditions.Clean(); // // // Clean(visionPoiEvents); // Clean(visionCycleEvents); // // // visionConditions.Clean(); // // // result = HasChild(guards); // return result; // } // // // // Cycles POI States ... // XPOIState decisionState = decisionConditions.state; // XPOIState analyseState = analyseConditions.state; // XPOIState consolidationState = consolidationConditions.state; // XPOIState visionState = visionConditions.state; // // // // Cycle X121Conditions ... // X121Conditions decisionXConditions = decisionConditions.x121Conditions; // X121Conditions analyseXConditions = analyseConditions.x121Conditions; // X121Conditions consolidationXConditions = consolidationConditions.x121Conditions; // X121Conditions visionXConditions = visionConditions.x121Conditions; // // // // // // // // // // // // Detect Consolidation Zone ... // // // // // // Detect Consolidation Zones ... // // // // // XBoxZone lastBullishBox; // // XBoxZone prevLastBullishBox; // // bool hasLastBullishBox = false; // // bool hasPrevLastBullishBox = false; // // XConsolidationZone lastBullishZone; // // bool isLastBullishAbovePrev = false; // // bool isLastBullishBelowPrev = false; // // XConsolidationZone prevLastBullishZone; // // // // // XBoxZone lastBearishBox; // // XBoxZone prevLastBearishBox; // // bool hasLastBearishBox = false; // // bool hasPrevLastBearishBox = false; // // XConsolidationZone lastBearishZone; // // bool isLastBearishAbovePrev = false; // // bool isLastBearishBelowPrev = false; // // XConsolidationZone prevLastBearishZone; // // // // // int bullishConsolidationZonesCount = 0; // // int bearishConsolidationZonesCount = 0; // // bool hasBullishConsolidationZones = false; // // bool hasBearishConsolidationZones = false; // // XConsolidationZone bullishConsolidationZones[]; // // XConsolidationZone bearishConsolidationZones[]; // // bool hasConsolidationZones = decisionCycleHelper.HasConsolidationZones(); // // int consolidationZonesCount = decisionCycleHelper.CountConsolidationZones(); // // if (hasConsolidationZones) // // { // // // // // bullishConsolidationZonesCount = decisionCycleHelper // // .FillConsolidationZones( // // X_DIRECTION_BULLISH, // // bullishConsolidationZones // // // ); // // hasBullishConsolidationZones = IsValidSize(bullishConsolidationZonesCount); // // // // // bearishConsolidationZonesCount = decisionCycleHelper // // .FillConsolidationZones( // // X_DIRECTION_BEARISH, // // bearishConsolidationZones // // // ); // // hasBearishConsolidationZones = IsValidSize(bearishConsolidationZonesCount); // // // // // // // // // // // // // // if (hasBullishConsolidationZones && // // bullishConsolidationZonesCount >= 2) // // { // // // // // lastBullishZone = bullishConsolidationZones[bullishConsolidationZonesCount - 1]; // // prevLastBullishZone = bullishConsolidationZones[bullishConsolidationZonesCount - 2]; // // // // // hasLastBullishBox = ToBox( // // lastBullishBox, // // lastBullishZone // // // ); // // hasPrevLastBullishBox = ToBox( // // prevLastBullishBox, // // prevLastBullishZone // // // ); // // // // // isLastBullishAbovePrev = hasLastBullishBox && // // hasPrevLastBullishBox && // // IsBoxAbove( // // lastBullishBox, // // prevLastBullishBox // // // ); // // // // // isLastBullishBelowPrev = hasLastBullishBox && // // hasPrevLastBullishBox && // // IsBoxBelow( // // lastBullishBox, // // prevLastBullishBox // // // ); // // } // // // // // if (hasBearishConsolidationZones && // // bearishConsolidationZonesCount >= 2) // // { // // // // // lastBearishZone = bearishConsolidationZones[bearishConsolidationZonesCount - 1]; // // prevLastBearishZone = bearishConsolidationZones[bearishConsolidationZonesCount - 2]; // // // // // hasLastBearishBox = ToBox( // // lastBearishBox, // // lastBearishZone // // // ); // // hasPrevLastBearishBox = ToBox( // // prevLastBearishBox, // // prevLastBearishZone // // // ); // // // // // isLastBearishAbovePrev = hasLastBearishBox && // // hasPrevLastBearishBox && // // IsBoxAbove( // // lastBearishBox, // // prevLastBearishBox // // // ); // // // // // isLastBearishBelowPrev = hasLastBearishBox && // // hasPrevLastBearishBox && // // IsBoxBelow( // // lastBearishBox, // // prevLastBearishBox // // // ); // // } // // // // // } // // // // Detect High Probability Swings ... // // bool isSwingLow = // // // // // cBar.low > pBar.low && // // ppBar.low > pBar.low && // // // // // zBar.low > pBar.low // // // // // ; // // bool isSwingHigh = // // // // // cBar.high < pBar.high && // // ppBar.high < pBar.high && // // // // // zBar.high < pBar.high // // // // // ; // // bool isStrongSwingLow = // // pBar.IsBullish() && // // pBar.GetLowShadow() >= pBar.GetBody() * 2; // // bool isStrongSwingHigh = // // pBar.IsBearish() && // // pBar.GetHighShadow() >= pBar.GetBody() * 2; // // // // // bool isStrongSwingLowAboveLastBullishBox = // // isStrongSwingLow && // // hasLastBullishBox && // // pBar.low > lastBullishBox.upper; // // // // // bool isStrongSwingHighBelowLastBearishBox = // // isStrongSwingHigh && // // hasLastBearishBox && // // pBar.high < lastBearishBox.lower; // // // // Here We Have all Requirements for Guarding Positions ... // // // // Available Guards: // // X_121_SMC_GUARD_ACTION_NONE // // X_121_SMC_GUARD_ACTION_CLOSE // // X_121_SMC_GUARD_ACTION_CLOSE_ALL // // X_121_SMC_GUARD_ACTION_CLOSE_LONGS // // X_121_SMC_GUARD_ACTION_CLOSE_SHORTS // // X_121_SMC_GUARD_ACTION_PARTIAL_CLOSE // // X_121_SMC_GUARD_ACTION_TRAIL_STOP // // X_121_SMC_GUARD_ACTION_TRAIL_TARGET // // X_121_SMC_GUARD_ACTION_HEDGE // // // // X_121_SMC_GUARD_ACTION_CLOSE: // // Close Specified Position on Specified Conditions ... // if (!isColseIgnored) // { // // // // TODO: // // - [] Implement Close on Nearest on TP ... // } // // // // X_121_SMC_GUARD_ACTION_CLOSE_ALL: // // Close All Positions on Specified Conditions ... // if (!isColseAllIgnored) // { // // // // TODO: // // - [] Implement Close All Positions on Weekend ... // } // // // // X_121_SMC_GUARD_ACTION_CLOSE_LONGS: // // Close All Long Positions on Specified Conditions ... // if (!isColseLongsIgnored) // { // // // } // // // // X_121_SMC_GUARD_ACTION_CLOSE_SHORTS: // // Close All Short Positions on Specified Conditions ... // if (!isColseShortsIgnored) // { // // // } // // // // X_121_SMC_GUARD_ACTION_PARTIAL_CLOSE: // // Partial Close Specified Position on Specified Conditions ... // if (!isPartialColseIgnored) // { // // // // Breake Even ... // // Usually Break Even Works on TP2 Reached ... // // when tp reached to required Level, we Close 50% of Entered Volume ... // bool canBreakEven = breakEvenTPLevel >= 0; // // // for (int i = 0; i < count; i++) // { // // // XPosition iPosition = ownPositions[i]; // // // bool isInProfit = iPosition.profit > 0; // // // double reachedTPLevel = 0; // double reachedTPPrice = 0; // if (isInProfit) // { // // // iPosition.CalculateReahedTP( // reachedTPLevel, // reachedTPPrice // // ); // } // // // // Break Event ... // if (canBreakEven) // { // // // // Position Must be In Profit ... // if (!isInProfit) // { // continue; // } // // // bool canDoBreakEven = reachedTPLevel == breakEvenTPLevel && // reachedTPPrice > 0; // if (canDoBreakEven) // { // // // 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 // // ); // } // } // } // } // // // // X_121_SMC_GUARD_ACTION_TRAIL_STOP: // // Trail Stop Specified Position on Specified Conditions ... // if (!isTrailStopIgnored) // { // // // for (int i = 0; i < count; i++) // { // // // XPosition iPosition = ownPositions[i]; // // // bool isLong = IsLong(iPosition.type); // bool isInProfit = iPosition.profit > 0; // // // double reachedTPLevel = 0; // double reachedTPPrice = 0; // if (isInProfit) // { // // // iPosition.CalculateReahedTP( // reachedTPLevel, // reachedTPPrice // // ); // } // // // // Trai Stop ... // if (!isInProfit) // { // continue; // } // // // double trailedStop = 0; // // // // Trail Stop Senarios ... // // - occures two same type consolidation zones in order of positions type; // // - occures a swing in order of consolidation zones ... // // // // // bool canTrailBasedOnTwoZone = // // isLong // // ? isLastBullishAbovePrev // // : isLastBearishBelowPrev; // // if (canTrailBasedOnTwoZone) // // { // // // // // trailedStop = // // isLong // // ? prevLastBullishBox.lower // // : prevLastBearishBox.upper; // // } // // // // // bool canTrailBasedOnZoneSwing = // // trailedStop <= 0 && // // (isLong // // ? isStrongSwingLowAboveLastBullishBox // // : isStrongSwingHighBelowLastBearishBox); // // if (canTrailBasedOnZoneSwing) // // { // // // // // trailedStop = // // isLong // // ? pBar.low // // : pBar.high; // // } // // // // // bool canDoTrailStop = // // trailedStop > 0 && // // ( // // // // // iPosition.sl == 0 // // ? (isLong // // ? trailedStop > iPosition.entry && // // trailedStop < iPosition.price // // : trailedStop < iPosition.entry && // // trailedStop > iPosition.price) // // : (isLong // // ? trailedStop > iPosition.sl && // // trailedStop > iPosition.entry && // // trailedStop < iPosition.price // // : trailedStop < iPosition.sl && // // trailedStop < iPosition.entry && // // trailedStop > iPosition.price) // // // // // ) // // // // // ; // // // // // if (canDoTrailStop) // // { // // // // // X121SMCGuard iGuard; // // // // // iGuard.time = cTime; // // iGuard.symbol = symbol; // // iGuard.provider = provider; // // iGuard.action = X_121_SMC_GUARD_ACTION_TRAIL_STOP; // // // // // iGuard.ticket = iPosition.ticket; // // // // // iGuard.sl = trailedStop; // // // // // AddGuard( // // iGuard, // // guards // // // ); // // } // } // } // // // // X_121_SMC_GUARD_ACTION_TRAIL_TARGET: // // Trail Target Specified Position on Specified Conditions ... // if (!isTrailTargetIgnored) // { // // // } // // // // X_121_SMC_GUARD_ACTION_HEDGE: // // Hedge all Positions on Specified Conditions ... // if (!isHedgeIgnored) // { // // // } // // // // Cleanup Collections ... // // // Clean(ownPositions); // Clean(ownLongPositions); // Clean(ownShortPositions); // // // zBar.Clean(); // cBar.Clean(); // pBar.Clean(); // ppBar.Clean(); // // // Clean(visionPoiEvents); // Clean(analysePoiEvents); // Clean(visionCycleEvents); // Clean(decisionPoiEvents); // Clean(analyseCycleEvents); // Clean(decisionCycleEvents); // Clean(consolidationPoiEvents); // Clean(consolidationCycleEvents); // // // visionState.Clean(); // analyseState.Clean(); // decisionState.Clean(); // visionConditions.Clean(); // analyseConditions.Clean(); // visionXConditions.Clean(); // decisionConditions.Clean(); // consolidationState.Clean(); // analyseXConditions.Clean(); // decisionXConditions.Clean(); // consolidationConditions.Clean(); // consolidationXConditions.Clean(); // // // // lastBullishBox.Clean(); // // lastBearishBox.Clean(); // // lastBullishZone.Clean(); // // lastBearishZone.Clean(); // // prevLastBullishBox.Clean(); // // prevLastBearishBox.Clean(); // // prevLastBullishZone.Clean(); // // prevLastBearishZone.Clean(); // // // count = ArraySize(guards); // result = IsValidSize(count); // return result; }