From d63c5596fc6e18eabd4e2f6e596c1381db1dd8ee Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Thu, 23 Oct 2025 21:09:12 +0330 Subject: [PATCH] last works on XFIMA ... --- Classes/x-saherelm.x-signaller.class.mq5 | 6 +- Experts/x-saherelm.xfima.backtester.ea.mq5 | 40 +- .../x-saherelm.xfima.signaller.class.mq5 | 443 ++++++++++-------- 3 files changed, 278 insertions(+), 211 deletions(-) diff --git a/Classes/x-saherelm.x-signaller.class.mq5 b/Classes/x-saherelm.x-signaller.class.mq5 index d5810258..4e51238f 100644 --- a/Classes/x-saherelm.x-signaller.class.mq5 +++ b/Classes/x-saherelm.x-signaller.class.mq5 @@ -57,7 +57,9 @@ class XCBaseSignaller : public XCBase ENUM_TIMEFRAMES period; // Period ... // - double slAdditionDistance; // SL Addition Distance ... + bool ignoreTargets; // Ignore Signal Targets ... + double slAdditionDistance; // SL Addition Distance ... + double maxAllowedRiskDistance; // Max Allowed Risk In Points ... // // Restrictions ... @@ -92,7 +94,9 @@ class XCBaseSignaller : public XCBase period = _period; // + ignoreTargets = false; slAdditionDistance = 0; + maxAllowedRiskDistance = 0; // processAllTicks = _processAllTicks; diff --git a/Experts/x-saherelm.xfima.backtester.ea.mq5 b/Experts/x-saherelm.xfima.backtester.ea.mq5 index 2ff8e182..58492c64 100644 --- a/Experts/x-saherelm.xfima.backtester.ea.mq5 +++ b/Experts/x-saherelm.xfima.backtester.ea.mq5 @@ -131,17 +131,17 @@ input ENUM_BASE_CORNER barTimeCorner = CORNER_RIGHT_LOWER; // Text Position // // Presentation ... input group "Presentation"; -input bool showPV = true; // Show PV -input bool showHK = true; // Show HK -input bool showSAR = true; // Show Sar -input bool showBars = true; // Show Bars -input bool showMAFast = true; // Show MA Fast -input bool showMASlow = true; // Show MA Slow -input bool showMABond = true; // Show MA Bond -input bool showHKBond = true; // Show HK Bond -input bool showBarTime = true; // Show Bar Time -input bool showZigZag = true; // Show ZigZag -input bool showFiboZone = true; // Show Fibo Zone +input bool showPV = false; // Show PV +input bool showHK = false; // Show HK +input bool showSAR = false; // Show Sar +input bool showBars = true; // Show Bars +input bool showMAFast = false; // Show MA Fast +input bool showMASlow = false; // Show MA Slow +input bool showMABond = false; // Show MA Bond +input bool showHKBond = false; // Show HK Bond +input bool showBarTime = true; // Show Bar Time +input bool showZigZag = false; // Show ZigZag +input bool showFiboZone = false; // Show Fibo Zone // input int startCalculationForLastBars = 1500; // Calculate Last n Bars @@ -163,16 +163,6 @@ XCBackTesterEA *eaExpert; // EA Expert ... // Registered Signallers ... XCXFIMAStrategy1Signaller *fima1Signaller; // XFIMA1 Signaller ... -// -// XFIMAMarketStructure marketStructure; // POI Analysis Result ... -// XFIMAPatternDetectorConfig detectorConfig; // POI Detector Config ... -// XFIMAMarketStructureDrawerConfig marketStructureDrawerConfig; // POI Drawer Config ... - -// -// Drawer and eaDraws ... -CArrayObj eaDraws; // Drawn Objects ... -CArrayObj *marketStructureObjects; // Market Structure Objects ... - // XBarTracker eaBarTracker; // EA Bar Traker ... @@ -195,9 +185,6 @@ color eaBarBearishColor = clrRed; color eaBarBullishColor = clrLime; color eaBarVolumesColor = clrGreen; -// -// Define Indicators Here ... - // // Event Handlers ... @@ -362,7 +349,12 @@ int OnInit() eaSignallerRiskInPoint, // Default Risk In Points ... eaSignallerVolume // Defaul Volume ... ); + + // + // Configure Signaller ... + fima1Signaller.ignoreTargets = false; fima1Signaller.slAdditionDistance = 30; + fima1Signaller.maxAllowedRiskDistance = 200; fima1Signaller.AddOnSignalEventHandler(OnSignalTriggered); // diff --git a/XFIMAEA/Classes/x-saherelm.xfima.signaller.class.mq5 b/XFIMAEA/Classes/x-saherelm.xfima.signaller.class.mq5 index 94e9dac7..7f354d83 100644 --- a/XFIMAEA/Classes/x-saherelm.xfima.signaller.class.mq5 +++ b/XFIMAEA/Classes/x-saherelm.xfima.signaller.class.mq5 @@ -41,7 +41,6 @@ class XCXFIMABaseSignaller : public XCBaseSignaller // Props ... // - bool ignoreTargets; // Ignore Signal Targets ... CArrayObj drawnObjects; // Drawing Objects Holder ... XFIMAInputs fimaInputs; // XFIMA Inputs ... XCXFIMAHelper *fimaHelper; // XFIMA Indicator Helper ... @@ -256,8 +255,6 @@ class XCXFIMAStrategy1Signaller : public XCXFIMABaseSignaller public: // // Props ... - XFIMAConditions conditions; // XFIMA Conditions ... - XFIMAMarketStructure structure; // XFIMA Market Structure ... // // Constructor ... @@ -283,13 +280,6 @@ class XCXFIMAStrategy1Signaller : public XCXFIMABaseSignaller // Configure ... structureConfig.Default(); structureDrawConfig.Default(); - - // - structure.Clean(); - - // - // Ignore Signal Targets ... - ignoreTargets = false; } // @@ -298,9 +288,6 @@ class XCXFIMAStrategy1Signaller : public XCXFIMABaseSignaller { // Destroy(); - - // - structure.Clean(); } // @@ -328,12 +315,10 @@ class XCXFIMAStrategy1Signaller : public XCXFIMABaseSignaller // XOHCL iBar; - int count = 0; XBoxZone iZone; XSignal iSignal; bool has = false; XTarget iTargets[]; - string iName = NULL; bool isBullish = false; bool isBearish = false; ENUM_X_DIRECTION iDir = X_DIRECTION_NONE; @@ -354,7 +339,7 @@ class XCXFIMAStrategy1Signaller : public XCXFIMABaseSignaller // // Strategy X ... - has = StrategyXHasSignal( + has = HasSignal( iBar, iZone, iTargets, @@ -365,13 +350,6 @@ class XCXFIMAStrategy1Signaller : public XCXFIMABaseSignaller // Handle Resting Signals ... has = HasDirection(iDir) && iZone.IsValid(); - if (has) - { - // - // has = false; - // iZone.Clean(); - // iDir = X_DIRECTION_NONE; - } // // Prepare Signal ... @@ -383,81 +361,41 @@ class XCXFIMAStrategy1Signaller : public XCXFIMABaseSignaller // // Provided SL ... - double _point = GetPoints(iBar.symbol); + double points = GetPoints(symbol); + double spread = GetSpread(symbol); + double entry = GetEntry(symbol, iDir); + + // + // Calculate SL Addition ... double slAddition = slAdditionDistance <= 0 ? 0 - : slAdditionDistance * _point; - double slPrice = + : slAdditionDistance * points; + + // + // Calculate SL Price ... + double sl = isBullish ? iZone.lower - slAddition : iZone.upper + slAddition; // - // Preparing Signal Targets ... - XTarget iTarget; - double spread = GetSpread(symbol); - double entry = GetEntry(symbol, iZone.dir); - double risk = MathAbs(entry - slPrice); + // Prepare Signal ... + has = PrepareSignal( + iSignal, + iDir, + iTargets, + 0, // Use Default R2R ... + sl, // Provided Price for SL ... + 0 // Specified Risk in Point .... + ); + } - // - // Ignore Signal if Risk is too Much ... - bool hasToomuchRisk = risk > (iZone.upper - iZone.lower) + slAddition + (100 * _point); - if (hasToomuchRisk) - { - // - iZone.Clean(); - Print("Too Much Risk ..."); - } - - // - // Handle Partial Close Position if Reached TP 1.5 ... - // iTarget.Clean(); - // iTarget.doRF = false; - // iTarget.actingDistance = 5; - // iTarget.doRFOnEntry = false; - // iTarget.volumeMultiplier = 0.5; - // iTarget.target = isBullish ? entry + (4 * risk) : entry - (4 * risk); - // AddRef(iTarget, iTargets); - - // - // Handle Ignore Signal Targets if Forced ... - if (ignoreTargets) - { - XClean(iTargets); - } - - // - // Handle Restrictions Based Signal Targets ... - if (risk > 0 && HasRestrictions()) - { - // - // Here we Can Do Target Preparing if Signaller Restricted and - // then ignore restrictions if required ... - ResetRestrictions(); - } - - // - // Prepare Founded Signal ... - has = iZone.IsValid() && - PrepareSignal( - iSignal, - iDir, - iTargets, - 0, // Use Default R2R ... - slPrice, // Provided Price for SL ... - 0 // Specified Risk in Point .... - ); - if (has) - { - // - // Notify On Signal Event ... - NotifySignalEventListeners(iSignal); - } - - // - // Cleanup ... - iTarget.Clean(); + // + // Notify On Signal Event ... + if (has) + { + NotifySignalEventListeners(iSignal); } // @@ -465,7 +403,7 @@ class XCXFIMAStrategy1Signaller : public XCXFIMABaseSignaller iBar.Clean(); iZone.Clean(); iSignal.Clean(); - SpecifiedClean(iTargets); + XClean(iTargets); } /** @@ -487,7 +425,7 @@ class XCXFIMAStrategy1Signaller : public XCXFIMABaseSignaller int result = 0; // - SpecifiedClean(guards); + XClean(guards); // int positionsCount = ArraySize(positions); @@ -585,7 +523,7 @@ class XCXFIMAStrategy1Signaller : public XCXFIMABaseSignaller * * @return ( bool ) */ - bool StrategyXHasSignal( + bool HasSignal( XOHCL &bar, XBoxZone &zone, XTarget &targets[], @@ -595,6 +533,9 @@ class XCXFIMAStrategy1Signaller : public XCXFIMABaseSignaller // bool result = false; + // + // Configuration Flags ... + // // Signaller Flags ... bool useSignaller1 = true; @@ -604,7 +545,36 @@ class XCXFIMAStrategy1Signaller : public XCXFIMABaseSignaller // // Validator Flags ... - bool validateByOrderFlow = true; + string provider = NULL; + bool validateByOrderFlow = false; + + // + // Target Flags ... + bool hasPartialCloseOnTp1 = true; + + // + // Draw Flags ... + bool draw = true; + bool drawZone = true; + bool drawOrderFlow = true; + XPOIDrawConfig iDrawConfig; + + // + // Signal Checking ... + + // + // Detecting OrderFlow ... + XBoxZone orderFlow[]; + ENUM_X_DIRECTION orderFlowDir; + bool hasOrderFlow = poiDetector.HasOrderFlow( + bar, + orderFlowDir, + orderFlow, + 2, + false, // Contains OB ... + true, // Contains FVG ... + false // Contains Support and Resistances ... + ); // // Signaller 1 ... @@ -617,6 +587,12 @@ class XCXFIMAStrategy1Signaller : public XCXFIMABaseSignaller targets, dir // ); + + // + if (result) + { + provider = "1"; + } } // @@ -630,6 +606,12 @@ class XCXFIMAStrategy1Signaller : public XCXFIMABaseSignaller targets, dir // ); + + // + if (result) + { + provider = "2"; + } } // @@ -643,6 +625,12 @@ class XCXFIMAStrategy1Signaller : public XCXFIMABaseSignaller targets, dir // ); + + // + if (result) + { + provider = "3"; + } } // @@ -656,26 +644,21 @@ class XCXFIMAStrategy1Signaller : public XCXFIMABaseSignaller targets, dir // ); + + // + if (result) + { + provider = "4"; + } } // - // Validate Signals by Order Flow ... - if (validateByOrderFlow) - { - // - // Detecting OrderFlow ... - XBoxZone orderFlow[]; - ENUM_X_DIRECTION orderFlowDir; - bool hasOrderFlow = poiDetector.HasOrderFlow( - bar, - orderFlowDir, - orderFlow, - 2, - false, // Contains OB ... - true, // Contains FVG ... - false // Contains Support and Resistances ... - ); + // Signal Validating ... + // + // Validate Signals by Order Flow ... + if (result && validateByOrderFlow) + { // bool isBullish = hasOrderFlow && IsXBullish(dir) && @@ -692,11 +675,171 @@ class XCXFIMAStrategy1Signaller : public XCXFIMABaseSignaller isBearish; } - // // - // if (result) - // { - // Print("Salam"); - // } + // + // Targets, Restrictions and Risk Management ... + if (result) + { + // + // Requirements ... + XTarget iTarget; + + // + // Checking Direction of Signal ... + bool isBullish = IsXBullish(dir); + bool isBearish = IsXBearish(dir); + + // + // Provided SL ... + double points = GetPoints(symbol); + double spread = GetSpread(symbol); + double entry = GetEntry(symbol, dir); + + // + // Calculate SL Addition ... + double slAddition = + slAdditionDistance <= 0 + ? 0 + : slAdditionDistance * points; + + // + // Calculate SL Price ... + double sl = + isBullish + ? zone.lower - slAddition + : zone.upper + slAddition; + + // + // Calculate Risk ... + double risk = MathAbs(entry - sl); + + // + // Calculate Reward ... + double reward = defaultSignalR2R * risk; + + // + // Calculate TP1 ... + double tp1 = isBullish + ? entry + risk + : entry - risk; + double tp2 = isBullish + ? entry + (2 * risk) + : entry - (2 * risk); + + // + // Calculate TP ... + double tp = isBullish + ? entry + reward + : entry - reward; + + // + // Check Max Allowed Risk ... + bool hasTooMuchRisk = maxAllowedRiskDistance <= 0 + ? false + : risk >= (maxAllowedRiskDistance * points); + if (hasTooMuchRisk) + { + // + // Clean Signal if Too much Risk ... + zone.Clean(); + dir = X_DIRECTION_NONE; + } + + // + // Common Targets ... + + // + // Partial Close on TP 1 ... + if (hasPartialCloseOnTp1 && tp > tp1) + { + // + iTarget.Clean(); + iTarget.target = tp1; + iTarget.doRFOnEntry = true; + iTarget.actingDistance = 5; + iTarget.volumeMultiplier = 0.8; + + // + AddRef(iTarget, targets); + } + + // + // Custom Signaller Targets ... + + // + // Restrictions Handling ... + if (risk > 0 && HasRestrictions()) + { + // + // Reset Restricitons if necessary ... + ResetRestrictions(); + + // + // Handle Custom Restrictions Senario ... + } + + // + // Ignore Targets ... + if (ignoreTargets) + { + XClean(targets); + } + + // + // Summarize Result ... + result = zone.IsValid() && + HasDirection(dir); + + // + // Cleanup ... + iTarget.Clean(); + } + + // + // Draws ... + if (result && draw) + { + // + // Prepare Draw Config ... + iDrawConfig = structureDrawConfig.drawConfig; + iDrawConfig.zonesWidth = 3; + iDrawConfig.zonesStyle = STYLE_DASH; + iDrawConfig.bullishColor = clrYellow; + iDrawConfig.bearishColor = clrDarkOrange; + + // + // Draw Zone ... + if (drawZone) + { + poiDrawer.DrawZone(zone, iDrawConfig); + } + + // + // Draw Order Flow ... + if (drawOrderFlow && HasChild(orderFlow)) + { + poiDrawer.DrawZones(orderFlow, iDrawConfig); + } + + // + // Other Drawing Materials ... + } + + // + // Cleanup ... + if (!result) + { + // + zone.Clean(); + XClean(targets); + dir = X_DIRECTION_NONE; + } + + // + // Debug Breake ... + if (result) + { + DebugBreak(); + } // return result; @@ -1231,36 +1374,6 @@ class XCXFIMAStrategy1Signaller : public XCXFIMABaseSignaller // Breaked Zones ... ExtractBreakeds(bar, breakedZones, affectedZones); - // - // Draw Affected Zones ... - if (HasChild(affectedZones)) - { - // - UpdateToTime(affectedZones, bar.time); - poiDrawer.DrawZones(affectedZones, structureDrawConfig.drawConfig); - } - - // - // Draw Acted Zones ... - // if (HasChild(actedZones)) - // { - // poiDrawer.DrawZones(actedZones, structureDrawConfig.drawConfig); - // } - - // - // Draw Inside Zones ... - // if (HasChild(insideZones)) - // { - // poiDrawer.DrawZones(insideZones, structureDrawConfig.drawConfig); - // } - - // // - // // Draw Breaked Zones ... - // if (HasChild(breakedZones)) - // { - // poiDrawer.DrawZones(breakedZones, structureDrawConfig.drawConfig); - // } - // // Cleanup ... XClean(tmps); @@ -1271,19 +1384,6 @@ class XCXFIMAStrategy1Signaller : public XCXFIMABaseSignaller XClean(affectedZones); } - // - if (result) - { - // - zone.to = bar.time; - - // - poiDrawer.DrawZone( - zone, - structureDrawConfig.drawConfig // - ); - } - // result = zone.IsValid(); } @@ -1493,40 +1593,11 @@ class XCXFIMAStrategy1Signaller : public XCXFIMABaseSignaller // Validate ... result = isBullish || isBearish; - if (result) - { - // - dir = zone.dir; - zone.to = bar.time; - XPOIDrawConfig iConfig = structureDrawConfig.drawConfig; - iConfig.zonesWidth = 3; - iConfig.bullishColor = clrYellow; - iConfig.bearishColor = clrDarkOrange; - poiDrawer.DrawZone(zone, iConfig); - Print("Consolidation"); - } // // Prepare Zone ... if (result) { - // - // zone.Clean(); - // result = ToBox( - // p2Bar, - // zone, - // dir, - // X_BAR_PATTERN_PINNED // - // ); - - // - // Manage Stops ... - if (result) - { - } - - // - result = zone.IsValid(); } //