/////////////////////////////////////////////////////// // // SaherElm IT Center MQL5 Class Library // -------------------------------------- // Name: XCX121SMCTestStrategy // Description: provides all based classes for use ... // // // 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 "../../Helpers/x-saherelm.x3ma.helper.mq5" #include "../../Helpers/x-saherelm.x3vwap.helper.mq5" #include "../../Helpers/x-saherelm.xpv.helper.mq5" #include "../Classes/x-121.smc.base.strategy.class.mq5" // // Helper(s) ... // // XStrategy Class Implementation(s) ... class XCX121SMCTestStrategy : public XCX121SMCBaseStrategy { // // Public ... public: // // // Constructor(s) ... void XCX121SMCTestStrategy( // // Base ... string _symbol, // Trading Symbol ENUM_TIMEFRAMES _period // Trading TimeFrame ) : XCX121SMCBaseStrategy(_symbol, _period) { InitialHelpers(); } // // Deconstructur ... void ~XCX121SMCTestStrategy() { } // // Overrides Actions ... /** * Destroy All Class Implementations ... */ void Destroy() override { // delete xpvHelper; delete x3maHelper; delete x3vwapHelper; } /** * Check Conditions For Signal ... */ bool HasSignal( XSignal &signal, X121SMCStrategyConditions &conditions // ) override { // bool result = false; // signal.Clean(); conditions.Clean(); // int pushers = 0; // double sl = 0; double tp = 0; double risk = 0; double entry = 0; double reward = 0; double r2r = R2R(); double volume = Volume(); ENUM_POSITION_TYPE type = POSITION_TYPE_BUY; ENUM_X_ORDER_MODES mode = X_ORDER_MODE_MARKET; // bool hasLong = false; bool hasShort = false; // // Start Calculations ... // string _symbol = GetSymbol(); ENUM_TIMEFRAMES _period = GetPeriod(); // double points = GetPoints(_symbol); double pip = GetPipPrice(_symbol); double pip2 = 2 * pip; // int zIndex = 0; int cIndex = zIndex + 1; int pIndex = cIndex + 1; int ppIndex = pIndex + 1; // // Retrieve Bars ... // // Zero Bar ... XOHCL zBar; result = zBar.Init( _symbol, _period, zIndex // ); if (!result) { return result; } // // Last Bar ... XOHCL cBar; result = cBar.Init( _symbol, _period, cIndex // ); if (!result) { return result; } // // Previous Bar ... XOHCL pBar; result = pBar.Init( _symbol, _period, pIndex // ); if (!result) { return result; } // bool isSpreadPassed = IsSpreadPass(); result = isSpreadPassed; if (!result) { return result; } // // Try to Implement SMC Based Conditions ... result = DetectConditions(conditions); if (!result) { return result; } // XPVPivot lastPivot; result = GetLastItem( lastPivot, mOrderFlow // ); if (!result) { return result; } XPOIState pivotState; DetectPivotPOIs( lastPivot, pivotState // ); // // Prepare Areas ... double bid = GetBid(_symbol); bool isBullish = IsBullish(mOrderFlowDir); bool isBearish = IsBearish(mOrderFlowDir); double pivotMid = lastPivot.CalculateMid(); // // Looks For Long ... // - [] Bullish Direction; // - [] Discount Area; bool lookupLong = isBullish && bid < pivotMid && bid > lastPivot.lower; // // Looks For Short ... // - [] Bearish Direction ... // - [] Premium Area ... bool lookupShort = isBearish && bid > pivotMid && bid < lastPivot.upper; // // Check Order Flow Validation ... // // For Bullish Order Flow ... // if Price Breakes Lower to Down, means Change of Charcter Happens ... bool isCHOCHHappens = isBullish ? bid < lastPivot.lower - pip : bid > lastPivot.upper + pip; // // For Bullish Order Flow ... // if Price Breakes Upper to Up, means Breake of Structure Happens ... bool isBOSHappens = isBullish ? bid > lastPivot.upper + pip : bid < lastPivot.lower - pip; // if (isCHOCHHappens) { // DrawStateSwings(_5MState); Print("CHOCH ..."); } // if (isBOSHappens) { // DrawStateSwings(_5MState); Print("BOS ..."); } // result = lookupLong || lookupShort; if (!result) { return result; } // result = hasLong || hasShort; // // Rmove Signal Condition ... if (result) { // // TYPE ... type = hasLong ? POSITION_TYPE_BUY : POSITION_TYPE_SELL; ENUM_X_POSITION_TYPES xType = hasLong ? X_POSITION_TYPE_LONG : X_POSITION_TYPE_SHORT; // entry = GetEntry( conditions.symbol, type // ); // double candidates[]; DetectSLCandidates( candidates, entry, conditions, xType // ); double selectedSL = hasLong ? GetMax(candidates) : GetMin(candidates); sl = hasLong ? selectedSL - pip2 : selectedSL + pip2; // // RISK Reward ... risk = hasLong ? entry - sl : sl - entry; reward = risk * r2r; tp = hasLong ? entry + reward : entry - reward; // ClearDraws(); // signal.sl = sl; signal.tp = tp; signal.type = type; signal.mode = mode; signal.entry = entry; signal.volume = volume; signal.symbol = _symbol; signal.pushers = pushers; signal.provider = GetTag(); signal.time = TimeCurrent(); signal.period = _5MState.period; // result = PrepareSignal(signal); // hasLong = false; hasShort = false; } // return result; } /** * Detect SL Candidates based on Strategy and Conditions ... * * @param result: Double array reference ... * @param entry: Double, Position Entry Price ... * @param conditions: X121SMCStrategyConditions instance, Provides Configurations ... * @param type: ENUM_X_POSITION_TYPES member, Specified Position Type ... */ void DetectSLCandidates( double &result[], double entry, X121SMCStrategyConditions &conditions, ENUM_X_POSITION_TYPES type // ) override { } /** * Customize Strategy Identifier ... */ string GetTag() override { return X121SMCStrategyToken; } // // Protected ... protected: // // // Props ... // // XPV ... XPVInputs xpvInputs; XCXPVHelper *xpvHelper; // // X3MA ... X3MAInputs x3maInputs; XCX3MAHelper *x3maHelper; // // X3VWAP ... X3VWAPInputs x3vwapInputs; XCX3VWAPHelper *x3vwapHelper; // XPVPivot mOrderFlow[]; ENUM_X_DIRECTION mOrderFlowDir; // // Actions ... /** * Detect and Parse Market Conditions ... * * @return ( bool ) */ bool DetectConditions( X121SMCStrategyConditions &conditions // ) { // bool result = false; // string symbol = GetSymbol(); ENUM_TIMEFRAMES period = GetPeriod(); // int zIndex = 0; int cIndex = zIndex + 1; int pIndex = cIndex + 1; int ppIndex = pIndex + 1; // conditions.symbol = symbol; conditions.period = period; conditions.time = TimeCurrent(); // int conditionsLoopback = 7; // // XPV ... result = xpvHelper.GetConditions( conditions.xpvConditions, cIndex, conditionsLoopback // ); if (!result) { return result; } // // X3MA ... result = x3maHelper.GetConditions( conditions.x3maConditions, cIndex, conditionsLoopback // ); if (!result) { return result; } // // X3VWAP ... result = x3vwapHelper.GetConditions( conditions.x3vwapConditions, cIndex, conditionsLoopback // ); if (!result) { return result; } // // Only Detect Order Flow when there isn't ... if (ArraySize(mOrderFlow) == 0 || mOrderFlowDir == X_DIRECTION_NONE) { // // int samePivots = 40; int requiredPivots = 10; // DetectPVPivots( cIndex, samePivots, requiredPivots, mOrderFlow, mOrderFlowDir // ); // int orderFlowCount = ArraySize(mOrderFlow); if (IsValidSize(orderFlowCount)) { // for (int i = 0; i < orderFlowCount; i++) { // XPVPivot iPivot = mOrderFlow[i]; // XCXPVPivotObject *iObj; iObj = new XCXPVPivotObject(); bool isCreated = iObj.Create( mPOIDrawer.ChartIdentification(), mPOIDrawer.SubWindowIdentification(), iPivot // ); if (isCreated) { // if (iPivot.IsPeak()) { // iObj.PivotWidth(1); iObj.PivotFill(false); iObj.PivotColor(clrAqua); iObj.PivotStyle(STYLE_SOLID); } // if (iPivot.IsVale()) { // iObj.PivotWidth(1); iObj.PivotFill(false); iObj.PivotStyle(STYLE_SOLID); iObj.PivotColor(clrMagenta); } // AddObjectIfNotExists(iObj); } } } // result = IsValidSize(ArraySize(mOrderFlow)); if (!result) { return result; } } // // Check Order Flow Direction ... result = mOrderFlowDir != X_DIRECTION_NONE; if (!result) { return result; } // return result; } /** * Detect XPV Based POI(s) ... * * @param barIndex: Start Bar Index ... * @param minRepetition: minimum Repeate of POI for Validation ... * @param requiredPivots: Number of Min Requirement POI(s) ... * @param orderFlow: XPVPivot instance Array Reference ... * @param orderFlowDirection: ENUM_X_DIRECTION member Reference which represents OrderFlow Direction ... * @param maxAllowedLoopbackBars: Integer, Max Allowed Loopback Bars ... * * @return ( bool ) */ bool DetectPVPivots( int barIndex, int minRepetition, int requiredPivots, XPVPivot &orderFlow[], // OrderFlows ... ENUM_X_DIRECTION &orderFlowDirection, // Order Flow Direction ... int maxAllowedLoopbackBars = 576 // ) { // bool result = false; // orderFlowDirection = X_DIRECTION_NONE; // if (maxAllowedLoopbackBars <= 0) { maxAllowedLoopbackBars = 1000; } // int start = barIndex; int end = start + maxAllowedLoopbackBars; // Clean(orderFlow); // result = minRepetition > 0 && requiredPivots > 0; if (!result) { return result; } // string symbol = GetSymbol(); ENUM_TIMEFRAMES period = GetPeriod(); // double peak = 0; XPVPivot peaks[]; int peakRepeate = 0; int peakToBarIndex = 0; int peakFromBarIndex = 0; // double vale = 0; XPVPivot vales[]; int valeRepeate = 0; int valeToBarIndex = 0; int valeFromBarIndex = 0; // bool canLookupPeak = true; bool canLookupVale = true; bool canLookup = true; while (canLookup) { // // Check Peak ... double iPeak = xpvHelper.GetPeak(barIndex); if (iPeak != peak) { // if (peak != 0 && peakRepeate >= minRepetition) { // bool isExists = false; int count = ArraySize(peaks); if (IsValidSize(count)) { // for (int i = 0; i < count; i++) { // XPVPivot iPivot = peaks[i]; // if (iPivot.upper == iPeak) { // isExists = true; break; } } } if (!isExists) { // XPVPivot iPivot; // iPivot.lower = 0; iPivot.upper = peak; iPivot.symbol = symbol; iPivot.period = period; iPivot.type = XPV_PEAK; iPivot.repetition = peakRepeate; iPivot.to = iTime( symbol, period, peakToBarIndex // ); iPivot.from = iTime( symbol, period, peakFromBarIndex // ); // iPivot.lower = iPivot.CalculateLower(); // bool isValid = iPivot.IsValid(); if (isValid) { // AddRef( iPivot, peaks // ); } } } // peak = iPeak; peakRepeate = 0; peakFromBarIndex = 0; peakToBarIndex = barIndex; } else if (iPeak == peak) { // peakRepeate++; peakFromBarIndex = barIndex; } // // Check Vale ... double iVale = xpvHelper.GetVale(barIndex); if (iVale != vale) { // if (vale != 0 && valeRepeate >= minRepetition) { // bool isExists = false; int count = ArraySize(vales); if (IsValidSize(count)) { // for (int i = 0; i < count; i++) { // XPVPivot iPivot = vales[i]; // if (iPivot.lower == iVale) { // isExists = true; break; } } } if (!isExists) { // XPVPivot iPivot; // iPivot.upper = 0; iPivot.lower = vale; iPivot.symbol = symbol; iPivot.period = period; iPivot.type = XPV_VALE; iPivot.repetition = valeRepeate; iPivot.to = iTime( symbol, period, valeToBarIndex // ); iPivot.from = iTime( symbol, period, valeFromBarIndex // ); // iPivot.upper = iPivot.CalculateUpper(); // bool isValid = iPivot.IsValid(); if (isValid) { // AddRef( iPivot, vales // ); } } } // vale = iVale; valeRepeate = 0; valeFromBarIndex = 0; valeToBarIndex = barIndex; } else if (iVale == vale) { // valeRepeate++; valeFromBarIndex = barIndex; } // canLookupPeak = ArraySize(peaks) < requiredPivots; canLookupVale = ArraySize(vales) < requiredPivots; canLookup = barIndex < end && (canLookupPeak || canLookupVale); if (canLookup) { barIndex++; } } // result = IsValidSize(ArraySize(peaks)) || IsValidSize(ArraySize(vales)); if (!result) { return result; } // // Detect Order Flow ... Copy( peaks, orderFlow, false // ); Copy( vales, orderFlow, false // ); int count = ArraySize(orderFlow); result = IsValidSize(orderFlow); if (!result) { return result; } // // Sort Pivots ... SortPivots(orderFlow); // // Filter Broken Order Flows ... FilterBrokenPivots(orderFlow); // // Detect Order Flow Direction ... result = DetectPivotDirection( orderFlow, orderFlowDirection // ); if (!result) { return result; } // return result; } void DetectPivotPOIs( XPVPivot &pivot, XPOIState &state, bool lookupSwingHighs = true, bool lookupSwingLows = true, bool lookupSupportZones = true, bool lookupResistanceZones = true, bool lookupSupplyZones = true, bool lookupDemandZones = true, bool lookupBullishOrderBlocks = true, bool lookupBearishOrderBlocks = true, bool lookupBullishFairValueGaps = true, bool lookupBearishFairValueGaps = true, bool lookupBullishRejectionBars = true, bool lookupBearishRejectionBars = true, bool lookupBullishMomentumBars = true, bool lookupBearishMomentumBars = true // ) { // state.Clean(); // if (!pivot.IsValid()) { return; } // int count = 0; // // Swings ... // // Swing High ... if (lookupSwingHighs) { // count = ArraySize(state.swingHighs); if (IsValidSize(count)) { // for (int i = 0; i < count; i++) { // XCSwing iSwing = state.swingHighs[i]; // } } } // // Swing Low ... if (lookupSwingLows) { // count = ArraySize(state.swingLows); if (IsValidSize(count)) { // for (int i = 0; i < count; i++) { } } } // // Support and Resistance Zones ... // // Support Zones ... if (lookupSupportZones) { // count = ArraySize(state.supportZones); if (IsValidSize(count)) { // for (int i = 0; i < count; i++) { } } } // // Resistance Zones ... if (lookupResistanceZones) { // count = ArraySize(state.resistanceZones); if (IsValidSize(count)) { // for (int i = 0; i < count; i++) { } } } // // Supply and Demand Zones ... // // Supply Zones ... if (lookupSupplyZones) { // count = ArraySize(state.supplyZones); if (IsValidSize(count)) { // for (int i = 0; i < count; i++) { } } } // // Demand Zones ... if (lookupDemandZones) { // count = ArraySize(state.demandZones); if (IsValidSize(count)) { // for (int i = 0; i < count; i++) { } } } // // Order Blocks ... // // Bullish Order Blocks ... if (lookupBullishOrderBlocks) { // count = ArraySize(state.bullishOrderBlocks); if (IsValidSize(count)) { // for (int i = 0; i < count; i++) { } } } // // Bearish Order Blocks ... if (lookupBearishOrderBlocks) { // count = ArraySize(state.bearishOrderBlocks); if (IsValidSize(count)) { // for (int i = 0; i < count; i++) { } } } // // Fair Value Gaps ... // // Bullish Fair Value Gaps ... if (lookupBullishFairValueGaps) { // count = ArraySize(state.bullishFairValueGaps); if (IsValidSize(count)) { // for (int i = 0; i < count; i++) { } } } // // Bearish Fair Value Gaps ... if (lookupBearishFairValueGaps) { // count = ArraySize(state.bearishFairValueGaps); if (IsValidSize(count)) { // for (int i = 0; i < count; i++) { } } } // // Rejection Bars ... // // Bullish Rejection Bars ... if (lookupBullishRejectionBars) { // count = ArraySize(state.bullishRejectionBars); if (IsValidSize(count)) { // for (int i = 0; i < count; i++) { } } } // // Bearish Rejection Bars ... if (lookupBearishRejectionBars) { // count = ArraySize(state.bearishRejectionBars); if (IsValidSize(count)) { // for (int i = 0; i < count; i++) { } } } // // Momentum Bars ... // // Bullish Momentum Bars ... if (lookupBullishMomentumBars) { // count = ArraySize(state.bullishMomentumBars); if (IsValidSize(count)) { // for (int i = 0; i < count; i++) { } } } // // Bearish Momenum Bars ... if (lookupBearishMomentumBars) { // count = ArraySize(state.bearishMomentumBars); if (IsValidSize(count)) { // for (int i = 0; i < count; i++) { } } } } /** * This function fulfills the will of the developer * @param state: Argument 1 */ void DrawStateSwings(XPOIState &state) { // DrawState( state, // State ... true, // Draw SwingHighs .. true, // Draw SwingLows .. false, // Draw SupportZones .. false, // Draw ResistanceZones .. false, // Draw SupplyZones .. false, // Draw DemandZones .. true, // Draw BullishOrderBlocks .. true, // Draw BearishOrderBlocks .. true, // Draw BullishFairValueGaps .. true, // Draw BearishFairValueGaps .. true, // Draw BullishRejectionBars .. true, // Draw BearishRejectionBars .. true, // Draw BullishMomentumBars .. true // Draw BearishMomentumBars .. ); } // // Private ... private: // // Props ... /** * Initial Required Indicators Helper ... */ void InitialHelpers() { // string symbol = GetSymbol(); ENUM_TIMEFRAMES period = GetPeriod(); // // Short ... ENUM_X_PERIOD_METHOD scMethod = X_PERIOD_MANUALLY; // How to Find Period ENUM_TIMEFRAMES scPeriod = PERIOD_M15; // Time Period // // Medium ... ENUM_X_PERIOD_METHOD mcMethod = X_PERIOD_MANUALLY; // How to Find Period ENUM_TIMEFRAMES mcPeriod = PERIOD_M30; // Time Period // // Long ... ENUM_X_PERIOD_METHOD lcMethod = X_PERIOD_MANUALLY; // How to Find Period ENUM_TIMEFRAMES lcPeriod = PERIOD_H1; // Time Period // // Hind ... ENUM_X_PERIOD_METHOD hcMethod = X_PERIOD_MANUALLY; // How to Find Period ENUM_TIMEFRAMES hcPeriod = PERIOD_H2; // Time Period // // XPV ... // xpvInputs.Default(); // xpvInputs.scMethod = scMethod; xpvInputs.scPeriod = scPeriod; xpvInputs.mcMethod = mcMethod; xpvInputs.mcPeriod = mcPeriod; xpvInputs.lcMethod = lcMethod; xpvInputs.lcPeriod = lcPeriod; xpvInputs.hcMethod = hcMethod; xpvInputs.hcPeriod = hcPeriod; // xpvInputs.showPeaksAndVales = true; xpvInputs.showLevels = false; xpvInputs.showConsolidations = false; xpvInputs.showFibo1Levels = false; xpvInputs.showFibo2Levels = false; xpvInputs.showFibo3Levels = false; xpvInputs.showFibo4Levels = false; xpvInputs.showFibo5Levels = false; // xpvHelper = new XCXPVHelper(); xpvHelper.Init( symbol, period, xpvInputs // ); // // X3MA ... // x3maInputs.Default(); // x3maInputs.showFast = false; x3maInputs.showMid = false; x3maInputs.showSlow = false; // x3maHelper = new XCX3MAHelper(); x3maHelper.Init( symbol, period, x3maInputs // ); // // X3VWAP ... // x3vwapInputs.Default(); // x3vwapInputs.showFast = false; x3vwapInputs.showMid = false; x3vwapInputs.showSlow = false; // x3vwapHelper = new XCX3VWAPHelper(); x3vwapHelper.Init( symbol, period, x3vwapInputs // ); } // }; //