From 34ff9b147753a644e690bf245a054630d9c44397 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Mon, 26 May 2025 09:00:28 +0330 Subject: [PATCH] last ... --- Experts/xcatb.test.ea.mq5 | 148 ++-- Helpers/x-saherelm.x121.xcatb.helper.mq5 | 921 +++++++++++++++++++++-- Indicators/x-saherelm.x121.xdelta.mq5 | 550 ++++++++++++++ Libraries/x-saherelm.x-poi.lib.mq5 | 94 +++ Libraries/x-saherelm.x121.xcatb.lib.mq5 | 496 ++++++++++++ 5 files changed, 2101 insertions(+), 108 deletions(-) create mode 100644 Indicators/x-saherelm.x121.xdelta.mq5 diff --git a/Experts/xcatb.test.ea.mq5 b/Experts/xcatb.test.ea.mq5 index 64be395a..670b803a 100644 --- a/Experts/xcatb.test.ea.mq5 +++ b/Experts/xcatb.test.ea.mq5 @@ -256,6 +256,9 @@ void OnTick() return; } + // + eaHelper.Check(barIndex); + // // Reading Required Bars ... XOHCL zBar; @@ -288,72 +291,87 @@ void OnTick() // // Check Trigger Block Detected or Not ... XTriggerBlock iTB; + X121XCatbConditions iConditions; + + // + // has = DetectTriggerBlock( + // // + // _Symbol, + // _Period, + // // + // iTB, + // // + // eaBarAnalyser, + // // + // // Required Buffers ... + // eaHelper.atrBuffer, + // eaHelper.rsiBuffer, + // eaHelper.adxBuffer, + // eaHelper.adxpBuffer, + // eaHelper.adxnBuffer, + // eaHelper.peakBuffer, + // eaHelper.valeBuffer, + // eaHelper.deltaBuffer, + // eaHelper.sarBuffer, + // eaHelper.sarStateBuffer, + // eaHelper.trendBuffer, + // eaHelper.trendStateBuffer, + // eaHelper.atrUpperBuffer, + // eaHelper.atrLowerBuffer, + // eaHelper.sBarOpenBuffer, + // eaHelper.sBarCloseBuffer, + // eaHelper.hkSBarOpenBuffer, + // eaHelper.hkSBarCloseBuffer, + // eaHelper.peakGoldenBuffer, + // eaHelper.valeGoldenBuffer, + // eaHelper.deltaSignalBuffer, + // eaHelper.bullishVolumeSignalBuffer, + // eaHelper.bearishVolumeSignalBuffer, + // // + // cBarIndex, + // // + // // Signalling ... + // eaInputs.signalR2R, + // eaInputs.slAtrMultiplier, + // // + // // Configs ... + // eaInputs.rsiOBLevel, + // eaInputs.rsiOSLevel, + // eaInputs.adxThreshold, + // // + // // Validators ... + // eaInputs.forceObBarType, + // eaInputs.forceOBFVGBarType, + // eaInputs.forceHasSwing, + // eaInputs.forceHasFLiquidity, + // eaInputs.forceHasRLiquidity, + // eaInputs.validateGapSequence, + // eaInputs.validateBlockEdgeBreakout, + // // + // // Filters ... + // eaInputs.filterBasedOnPV, + // eaInputs.filterBasedOnSar, + // eaInputs.filterBasedOnRSI, + // eaInputs.filterBasedOnADX, + // eaInputs.filterBasedOnATR, + // eaInputs.filterBasedOnTrend, + // eaInputs.filterBasedOnDelta, + // eaInputs.filterBasedOnVolume, + // eaInputs.filterBasedOnSignalBar, + // eaInputs.filterBasedOnHKSignalBar // + // ); + + // has = DetectTriggerBlock( - // - _Symbol, - _Period, - // + eaDrawer, + eaHelper, + iConditions, iTB, - // - eaBarAnalyser, - // - // Required Buffers ... - eaHelper.atrBuffer, - eaHelper.rsiBuffer, - eaHelper.adxBuffer, - eaHelper.adxpBuffer, - eaHelper.adxnBuffer, - eaHelper.peakBuffer, - eaHelper.valeBuffer, - eaHelper.deltaBuffer, - eaHelper.sarBuffer, - eaHelper.sarStateBuffer, - eaHelper.trendBuffer, - eaHelper.trendStateBuffer, - eaHelper.atrUpperBuffer, - eaHelper.atrLowerBuffer, - eaHelper.sBarOpenBuffer, - eaHelper.sBarCloseBuffer, - eaHelper.hkSBarOpenBuffer, - eaHelper.hkSBarCloseBuffer, - eaHelper.peakGoldenBuffer, - eaHelper.valeGoldenBuffer, - eaHelper.deltaSignalBuffer, - eaHelper.bullishVolumeSignalBuffer, - eaHelper.bearishVolumeSignalBuffer, - // - cBarIndex, - // - // Signalling ... - eaInputs.signalR2R, - eaInputs.slAtrMultiplier, - // - // Configs ... - eaInputs.rsiOBLevel, - eaInputs.rsiOSLevel, - eaInputs.adxThreshold, - // - // Validators ... - eaInputs.forceObBarType, - eaInputs.forceOBFVGBarType, - eaInputs.forceHasSwing, - eaInputs.forceHasFLiquidity, - eaInputs.forceHasRLiquidity, - eaInputs.validateGapSequence, - eaInputs.validateBlockEdgeBreakout, - // - // Filters ... - eaInputs.filterBasedOnPV, - eaInputs.filterBasedOnSar, - eaInputs.filterBasedOnRSI, - eaInputs.filterBasedOnADX, - eaInputs.filterBasedOnATR, - eaInputs.filterBasedOnTrend, - eaInputs.filterBasedOnDelta, - eaInputs.filterBasedOnVolume, - eaInputs.filterBasedOnSignalBar, - eaInputs.filterBasedOnHKSignalBar // + barIndex, + 10 // ); + + // if (has) { // @@ -671,8 +689,12 @@ void InitRequirements() // we Can make Show all requirements here ... // + eaInputs.showPV = true; + eaInputs.showSar = true; + eaInputs.showTrend = true; eaInputs.drawSignals = false; eaInputs.showATRBand = false; + eaInputs.showPVGolden = true; // // Signal ... diff --git a/Helpers/x-saherelm.x121.xcatb.helper.mq5 b/Helpers/x-saherelm.x121.xcatb.helper.mq5 index 3caac822..d1c6796b 100644 --- a/Helpers/x-saherelm.x121.xcatb.helper.mq5 +++ b/Helpers/x-saherelm.x121.xcatb.helper.mq5 @@ -500,6 +500,32 @@ struct X121XCatbConditions bool isRSICrossedUnderOB; bool isRSICrossedUnderOS; + // + // DELTA ... + + // + bool isDeltaUp; + bool isDeltaDown; + + // + bool isDeltaBullish; + bool isDeltaBearish; + + // + bool isDeltaSwitchedToBullish; + bool isDeltaSwitchedToBearish; + + // + // VOLUME ... + + // + bool isVolumeBullish; + bool isVolumeBearish; + + // + bool isVolumeSwitchedToBullish; + bool isVolumeSwitchedToBearish; + // // SIGNAL Bar ... @@ -738,6 +764,31 @@ struct X121XCatbConditions isRSICrossedUnderOB = false; isRSICrossedUnderOS = false; + // + // DELTA ... + + // + isDeltaUp = false; + isDeltaDown = false; + + // + isDeltaBullish = false; + isDeltaBearish = false; + + // + isDeltaSwitchedToBullish = false; + isDeltaSwitchedToBearish = false; + + // + // VOLUME ... + + // + isVolumeBullish = false; + isVolumeBearish = false; + + // + isVolumeSwitchedToBullish = false; + isVolumeSwitchedToBearish = false; // // SIGNAL Bar ... @@ -955,6 +1006,60 @@ struct X121XCatbConditions bearishScore += score; } + // + // DELTA ... + + // + if (isDeltaUp) + { + bullishScore += score; + } + if (isDeltaBullish) + { + bullishScore += score; + } + if (isDeltaSwitchedToBullish) + { + bullishScore += score; + } + + // + if (isDeltaDown) + { + bearishScore += score; + } + if (isDeltaBearish) + { + bearishScore += score; + } + if (isDeltaSwitchedToBearish) + { + bearishScore += score; + } + + // + // VOLUME ... + + // + if (isVolumeBullish) + { + bullishScore += score; + } + if (isVolumeSwitchedToBullish) + { + bullishScore += score; + } + + // + if (isVolumeBearish) + { + bearishScore += score; + } + if (isVolumeSwitchedToBearish) + { + bearishScore += score; + } + // // SIGNAL Bar ... @@ -1165,6 +1270,24 @@ struct X121XCatbConditions ToString("isRSICrossedUnderOS", isRSICrossedUnderOS, ignoreFalseConditions, separator) + // "-----------------------" + separator + + "DELTA: " + separator + + "-----------------------" + separator + + ToString("isDeltaUp", isDeltaUp, ignoreFalseConditions, separator) + + ToString("isDeltaDown", isDeltaDown, ignoreFalseConditions, separator) + + ToString("isDeltaBullish", isDeltaBullish, ignoreFalseConditions, separator) + + ToString("isDeltaBearish", isDeltaBearish, ignoreFalseConditions, separator) + + ToString("isDeltaSwitchedToBullish", isDeltaSwitchedToBullish, ignoreFalseConditions, separator) + + ToString("isDeltaSwitchedToBearish", isDeltaSwitchedToBearish, ignoreFalseConditions, separator) + + // + "-----------------------" + separator + + "VOLUME: " + separator + + "-----------------------" + separator + + ToString("isVolumeBullish", isVolumeBullish, ignoreFalseConditions, separator) + + ToString("isVolumeBearish", isVolumeBearish, ignoreFalseConditions, separator) + + ToString("isVolumeSwitchedToBullish", isVolumeSwitchedToBullish, ignoreFalseConditions, separator) + + ToString("isVolumeSwitchedToBearish", isVolumeSwitchedToBearish, ignoreFalseConditions, separator) + + // + "-----------------------" + separator + "SIGNAL Bar: " + separator + "-----------------------" + separator + ToString("isSBarBullish", isSBarBullish, ignoreFalseConditions, separator) + @@ -1292,6 +1415,39 @@ class XCX121XCatbHelper : public XCBaseHelper double bullishVolumeSignalBuffer[]; double bearishVolumeSignalBuffer[]; + // + // Market Shift ... + + // + // SAR ... + XDirectionShift lastSarBullish; + XDirectionShift lastSarBearish; + + // + // TREND ... + XDirectionShift lastTrendBullish; + XDirectionShift lastTrendBearish; + + // + // DELTA ... + XDirectionShift lastDeltaBullish; + XDirectionShift lastDeltaBearish; + + // + // VOLUME ... + XDirectionShift lastVolumeBullish; + XDirectionShift lastVolumeBearish; + + // + // SIGNAL Bar ... + XDirectionShift lastSignalBarBullish; + XDirectionShift lastSignalBarBearish; + + // + // XHK SIGNAL Bar ... + XDirectionShift lastHKSignalBarBullish; + XDirectionShift lastHKSignalBarBearish; + // // Constructors ... XCX121XCatbHelper() @@ -1477,6 +1633,453 @@ class XCX121XCatbHelper : public XCBaseHelper return result; } + // + void Check(int barIndex = 0) + { + // + bool has = false; + + // + bool isBullish = false; + bool isBearish = false; + + // + barIndex = NormalizeInt(barIndex, 0); + + // + X121XCatbConditions conditions; + has = GetConditions( + conditions, + barIndex, + 4 // + ); + if (!has) + { + // + conditions.Clean(); + return; + } + + // + // Checking Shifts ... + + // + // SAR ... + + // + isBullish = conditions.isSarSwitchedToBullish; + isBearish = conditions.isSarSwitchedToBearish; + + // + has = isBullish || + isBearish; + if (has) + { + // + // Bullish ... + has = + isBullish && + (!lastSarBullish.IsValid() + ? true + : lastSarBullish.at < conditions.bars[1].time); + if (has) + { + // + lastSarBullish.Clean(); + + // + lastSarBullish.type = "SAR"; + lastSarBullish.symbol = conditions.symbol; + lastSarBullish.period = conditions.period; + lastSarBullish.at = conditions.bars[1].time; + lastSarBullish.shiftTo = X_DIRECTION_BULLISH; + lastSarBullish.after = conditions.sarBuffer[1]; + lastSarBullish.before = conditions.sarBuffer[2]; + } + + // + // Bearish ... + has = + isBearish && + (!lastSarBearish.IsValid() + ? true + : lastSarBearish.at < conditions.bars[1].time); + if (has) + { + // + lastSarBearish.Clean(); + + // + lastSarBearish.type = "SAR"; + lastSarBearish.symbol = conditions.symbol; + lastSarBearish.period = conditions.period; + lastSarBearish.at = conditions.bars[1].time; + lastSarBearish.shiftTo = X_DIRECTION_BEARISH; + lastSarBearish.after = conditions.sarBuffer[1]; + lastSarBearish.before = conditions.sarBuffer[2]; + } + } + + // + // TREND ... + + // + isBullish = conditions.isTrendSwitchedToBullish; + isBearish = conditions.isTrendSwitchedToBearish; + + // + has = isBullish || + isBearish; + if (has) + { + // + // Bullish ... + has = + isBullish && + (!lastTrendBullish.IsValid() + ? true + : lastTrendBullish.at < conditions.bars[1].time); + if (has) + { + // + lastTrendBullish.Clean(); + + // + lastTrendBullish.type = "TREND"; + lastTrendBullish.symbol = conditions.symbol; + lastTrendBullish.period = conditions.period; + lastTrendBullish.at = conditions.bars[1].time; + lastTrendBullish.shiftTo = X_DIRECTION_BULLISH; + lastTrendBullish.after = conditions.trendBuffer[1]; + lastTrendBullish.before = conditions.trendBuffer[2]; + } + + // + // Bearish ... + has = + isBearish && + (!lastTrendBearish.IsValid() + ? true + : lastTrendBearish.at < conditions.bars[1].time); + if (has) + { + // + lastTrendBearish.Clean(); + + // + lastTrendBearish.type = "TREND"; + lastTrendBearish.symbol = conditions.symbol; + lastTrendBearish.period = conditions.period; + lastTrendBearish.at = conditions.bars[1].time; + lastTrendBearish.shiftTo = X_DIRECTION_BEARISH; + lastTrendBearish.after = conditions.trendBuffer[1]; + lastTrendBearish.before = conditions.trendBuffer[2]; + } + } + + // + // DELTA ... + + // + isBullish = + // + ( + // + // Delta is Up ... + conditions.deltaBuffer[1] > conditions.deltaBuffer[2] && + // + // Delta Signal is Up ... + conditions.deltaSignalBuffer[1] > conditions.deltaSignalBuffer[2] && + // + // Delta Crossed Over Delta ... + conditions.deltaBuffer[1] > conditions.deltaSignalBuffer[1] && + conditions.deltaBuffer[2] <= conditions.deltaSignalBuffer[2] + // + ) + // + ; + + // + isBearish = + // + ( + // + // Delta is Down ... + conditions.deltaBuffer[1] < conditions.deltaBuffer[2] && + // + // Delta Signal is Down ... + conditions.deltaSignalBuffer[1] < conditions.deltaSignalBuffer[2] && + // + // Delta Crossed Under Delta Signal ... + conditions.deltaBuffer[1] < conditions.deltaSignalBuffer[1] && + conditions.deltaBuffer[2] >= conditions.deltaSignalBuffer[2] + // + ) + // + ; + + // + has = isBullish || + isBearish; + if (has) + { + // + // Bullish ... + has = + isBullish && + (!lastDeltaBullish.IsValid() + ? true + : lastDeltaBullish.at < conditions.bars[1].time); + if (has) + { + // + lastDeltaBullish.Clean(); + + // + lastDeltaBullish.type = "DELTA"; + lastDeltaBullish.symbol = conditions.symbol; + lastDeltaBullish.period = conditions.period; + lastDeltaBullish.at = conditions.bars[1].time; + lastDeltaBullish.shiftTo = X_DIRECTION_BULLISH; + lastDeltaBullish.after = conditions.bars[1].close; + lastDeltaBullish.before = conditions.bars[2].close; + } + + // + // Bearish ... + has = + isBearish && + (!lastDeltaBearish.IsValid() + ? true + : lastDeltaBearish.at < conditions.bars[1].time); + if (has) + { + // + lastDeltaBearish.Clean(); + + // + lastDeltaBearish.type = "DELTA"; + lastDeltaBearish.symbol = conditions.symbol; + lastDeltaBearish.period = conditions.period; + lastDeltaBearish.at = conditions.bars[1].time; + lastDeltaBearish.shiftTo = X_DIRECTION_BEARISH; + lastDeltaBearish.after = conditions.bars[1].close; + lastDeltaBearish.before = conditions.bars[2].close; + } + } + + // + // VOLUME ... + + // + isBullish = + // + ( + // + // Volume is Up ... + conditions.bullishVolumeBuffer[1] > conditions.bullishVolumeBuffer[2] && + // + // Volume Signal is Up ... + conditions.bullishVolumeSignalBuffer[1] > conditions.bullishVolumeSignalBuffer[2] && + // + // Volume Signals Crossed Over ... + conditions.bullishVolumeSignalBuffer[1] > conditions.bearishVolumeSignalBuffer[1] && + conditions.bullishVolumeSignalBuffer[2] <= conditions.bearishVolumeSignalBuffer[2] + // + ) + // + ; + + // + isBullish = + // + ( + // + // Volume is Up ... + conditions.bearishVolumeBuffer[1] > conditions.bearishVolumeBuffer[2] && + // + // Volume Signal is Up ... + conditions.bearishVolumeSignalBuffer[1] > conditions.bearishVolumeSignalBuffer[2] && + // + // Volume Signals Crossed Over ... + conditions.bearishVolumeSignalBuffer[1] > conditions.bullishVolumeSignalBuffer[1] && + conditions.bearishVolumeSignalBuffer[2] <= conditions.bullishVolumeSignalBuffer[2] + // + ) + // + ; + + // + has = isBullish || + isBearish; + if (has) + { + // + // Bullish ... + has = + isBullish && + (!lastVolumeBullish.IsValid() + ? true + : lastVolumeBullish.at < conditions.bars[1].time); + if (has) + { + // + lastVolumeBullish.Clean(); + + // + lastVolumeBullish.type = "VOLUME"; + lastVolumeBullish.symbol = conditions.symbol; + lastVolumeBullish.period = conditions.period; + lastVolumeBullish.at = conditions.bars[1].time; + lastVolumeBullish.shiftTo = X_DIRECTION_BULLISH; + lastVolumeBullish.after = conditions.bars[1].close; + lastVolumeBullish.before = conditions.bars[2].close; + } + + // + // Bearish ... + has = + isBearish && + (!lastVolumeBearish.IsValid() + ? true + : lastVolumeBearish.at < conditions.bars[1].time); + if (has) + { + // + lastVolumeBearish.Clean(); + + // + lastVolumeBearish.type = "VOLUME"; + lastVolumeBearish.symbol = conditions.symbol; + lastVolumeBearish.period = conditions.period; + lastVolumeBearish.at = conditions.bars[1].time; + lastVolumeBearish.shiftTo = X_DIRECTION_BEARISH; + lastVolumeBearish.after = conditions.bars[1].close; + lastVolumeBearish.before = conditions.bars[2].close; + } + } + + // + // SIGNAL Bar ... + + // + isBullish = conditions.isSBarSwitchedToBullish; + isBearish = conditions.isSBarSwitchedToBearish; + + // + has = isBullish || + isBearish; + if (has) + { + // + // Bullish ... + has = + isBullish && + (!lastSignalBarBullish.IsValid() + ? true + : lastSignalBarBullish.at < conditions.bars[1].time); + if (has) + { + // + lastSignalBarBullish.Clean(); + + // + lastSignalBarBullish.type = "SIGNALBAR"; + lastSignalBarBullish.symbol = conditions.symbol; + lastSignalBarBullish.period = conditions.period; + lastSignalBarBullish.at = conditions.bars[1].time; + lastSignalBarBullish.shiftTo = X_DIRECTION_BULLISH; + lastSignalBarBullish.after = conditions.sBarCloseBuffer[1]; + lastSignalBarBullish.before = conditions.sBarCloseBuffer[2]; + } + + // + // Bearish ... + has = + isBearish && + (!lastSignalBarBearish.IsValid() + ? true + : lastSignalBarBearish.at < conditions.bars[1].time); + if (has) + { + // + lastSignalBarBearish.Clean(); + + // + lastSignalBarBearish.type = "SIGNALBAR"; + lastSignalBarBearish.symbol = conditions.symbol; + lastSignalBarBearish.period = conditions.period; + lastSignalBarBearish.at = conditions.bars[1].time; + lastSignalBarBearish.shiftTo = X_DIRECTION_BEARISH; + lastSignalBarBearish.after = conditions.sBarCloseBuffer[1]; + lastSignalBarBearish.before = conditions.sBarCloseBuffer[2]; + } + } + + // + // HKSIGNAL Bar ... + + // + isBullish = conditions.isHKSBarSwitchedToBullish; + isBearish = conditions.isHKSBarSwitchedToBearish; + + // + has = isBullish || + isBearish; + if (has) + { + // + // Bullish ... + has = + isBullish && + (!lastHKSignalBarBullish.IsValid() + ? true + : lastHKSignalBarBullish.at < conditions.bars[1].time); + if (has) + { + // + lastHKSignalBarBullish.Clean(); + + // + lastHKSignalBarBullish.type = "HKSIGNALBAR"; + lastHKSignalBarBullish.symbol = conditions.symbol; + lastHKSignalBarBullish.period = conditions.period; + lastHKSignalBarBullish.at = conditions.bars[1].time; + lastHKSignalBarBullish.shiftTo = X_DIRECTION_BULLISH; + lastHKSignalBarBullish.after = conditions.hkSBarCloseBuffer[1]; + lastHKSignalBarBullish.before = conditions.hkSBarCloseBuffer[2]; + } + + // + // Bearish ... + has = + isBearish && + (!lastHKSignalBarBearish.IsValid() + ? true + : lastHKSignalBarBearish.at < conditions.bars[1].time); + if (has) + { + // + lastHKSignalBarBearish.Clean(); + + // + lastHKSignalBarBearish.type = "HKSIGNALBAR"; + lastHKSignalBarBearish.symbol = conditions.symbol; + lastHKSignalBarBearish.period = conditions.period; + lastHKSignalBarBearish.at = conditions.bars[1].time; + lastHKSignalBarBearish.shiftTo = X_DIRECTION_BEARISH; + lastHKSignalBarBearish.after = conditions.hkSBarCloseBuffer[1]; + lastHKSignalBarBearish.before = conditions.hkSBarCloseBuffer[2]; + } + } + + // + // Cleanup Resources ... + + // + conditions.Clean(); + } + // // Inputs ... @@ -3895,6 +4498,23 @@ class XCX121XCatbHelper : public XCBaseHelper double p2SBarC = conditions.sBarCloseBuffer[p2IDX]; double p3SBarC = conditions.sBarCloseBuffer[p3IDX]; + // + // XHK SIGNAL Bar ... + + // + double zHKSBarO = conditions.hkSBarOpenBuffer[zIDX]; + double cHKSBarO = conditions.hkSBarOpenBuffer[cIDX]; + double pHKSBarO = conditions.hkSBarOpenBuffer[pIDX]; + double p2HKSBarO = conditions.hkSBarOpenBuffer[p2IDX]; + double p3HKSBarO = conditions.hkSBarOpenBuffer[p3IDX]; + + // + double zHKSBarC = conditions.hkSBarCloseBuffer[zIDX]; + double cHKSBarC = conditions.hkSBarCloseBuffer[cIDX]; + double pHKSBarC = conditions.hkSBarCloseBuffer[pIDX]; + double p2HKSBarC = conditions.hkSBarCloseBuffer[p2IDX]; + double p3HKSBarC = conditions.hkSBarCloseBuffer[p3IDX]; + // // TREND ... @@ -3929,6 +4549,54 @@ class XCX121XCatbHelper : public XCBaseHelper double p2VALE = conditions.valeBuffer[p2IDX]; double p3VALE = conditions.valeBuffer[p3IDX]; + // + // DELTA ... + + // + double zDelta = conditions.deltaBuffer[zIDX]; + double cDelta = conditions.deltaBuffer[cIDX]; + double pDelta = conditions.deltaBuffer[pIDX]; + double p2Delta = conditions.deltaBuffer[p2IDX]; + double p3Delta = conditions.deltaBuffer[p3IDX]; + + // + double zDeltaSignal = conditions.deltaSignalBuffer[zIDX]; + double cDeltaSignal = conditions.deltaSignalBuffer[cIDX]; + double pDeltaSignal = conditions.deltaSignalBuffer[pIDX]; + double p2DeltaSignal = conditions.deltaSignalBuffer[p2IDX]; + double p3DeltaSignal = conditions.deltaSignalBuffer[p3IDX]; + + // + // VOLUME ... + + // + double zBullishVolume = conditions.bullishVolumeBuffer[zIDX]; + double cBullishVolume = conditions.bullishVolumeBuffer[cIDX]; + double pBullishVolume = conditions.bullishVolumeBuffer[pIDX]; + double p2BullishVolume = conditions.bullishVolumeBuffer[p2IDX]; + double p3BullishVolume = conditions.bullishVolumeBuffer[p3IDX]; + + // + double zBearishVolume = conditions.bearishVolumeBuffer[zIDX]; + double cBearishVolume = conditions.bearishVolumeBuffer[cIDX]; + double pBearishVolume = conditions.bearishVolumeBuffer[pIDX]; + double p2BearishVolume = conditions.bearishVolumeBuffer[p2IDX]; + double p3BearishVolume = conditions.bearishVolumeBuffer[p3IDX]; + + // + double zBullishVolumeSignal = conditions.bullishVolumeSignalBuffer[zIDX]; + double cBullishVolumeSignal = conditions.bullishVolumeSignalBuffer[cIDX]; + double pBullishVolumeSignal = conditions.bullishVolumeSignalBuffer[pIDX]; + double p2BullishVolumeSignal = conditions.bullishVolumeSignalBuffer[p2IDX]; + double p3BullishVolumeSignal = conditions.bullishVolumeSignalBuffer[p3IDX]; + + // + double zBearishVolumeSignal = conditions.bearishVolumeSignalBuffer[zIDX]; + double cBearishVolumeSignal = conditions.bearishVolumeSignalBuffer[cIDX]; + double pBearishVolumeSignal = conditions.bearishVolumeSignalBuffer[pIDX]; + double p2BearishVolumeSignal = conditions.bearishVolumeSignalBuffer[p2IDX]; + double p3BearishVolumeSignal = conditions.bearishVolumeSignalBuffer[p3IDX]; + // // ATR Band ... @@ -3943,117 +4611,270 @@ class XCX121XCatbHelper : public XCBaseHelper // bool isClosedOverATRUpper = conditions.bars[cIDX].close > cATRUpper; - bool isClosedUnderATRLower = false; + bool isClosedUnderATRLower = + conditions.bars[cIDX].close < cATRLower; // // SAR ... // - bool isSarBullish = false; - bool isSarBearish = false; + bool isSarBullish = cSarState > 0; + bool isSarBearish = cSarState < 0; // - bool isSarSwitchedToBullish = false; - bool isSarSwitchedToBearish = false; + bool isSarSwitchedToBullish = + cSarState > 0 && + pSarState <= 0; + bool isSarSwitchedToBearish = + cSarState < 0 && + pSarState >= 0; // // ADX ... // - bool isADXBiggerLast = false; - bool isADXLesserLast = false; + bool isADXBiggerLast = + cADX > pADX; + bool isADXLesserLast = + cADX < pADX; // - bool isADXOverThreshold = false; - bool isADXUnderThreshold = false; + bool isPADXBiggerLast = + pADX > p2ADX; + bool isPADXLesserLast = + pADX < p2ADX; // - bool isDXPBiggerThanDXN = false; - bool isDXNBiggerThanDXP = false; + bool isADXOverThreshold = + cADX > adxThreshold; + bool isADXUnderThreshold = + cADX < adxThreshold; // - bool isADXBullish = false; - bool isADXBearish = false; + bool isPADXOverThreshold = + pADX > adxThreshold; + bool isPADXUnderThreshold = + pADX < adxThreshold; // - bool isADXSwitchedToBullish = false; - bool isADXSwitchedToBearish = false; + bool isDXPBiggerThanDXN = + cADXP > cADXN; + bool isDXNBiggerThanDXP = + cADXN > cADXP; + + // + bool isPDXPBiggerThanDXN = + pADXP > pADXN; + bool isPDXNBiggerThanDXP = + pADXN > pADXP; + + // + bool isADXBullish = + isADXBiggerLast && + isADXOverThreshold; + bool isADXBearish = + isADXLesserLast && + isADXUnderThreshold; + + // + bool isPADXBullish = + isPADXBiggerLast && + isPADXOverThreshold; + bool isPADXBearish = + isPADXLesserLast && + isPADXUnderThreshold; + + // + bool isADXSwitchedToBullish = + isADXBullish && + !isPADXBullish; + bool isADXSwitchedToBearish = + isADXBearish && + !isPADXBearish; // // RSI ... // - bool isRSIBullish = false; - bool isRSIBearish = false; + bool isRSIBullish = + cRSI > pRSI; + bool isRSIBearish = + cRSI < pRSI; // - bool isRSISwitchedToBullish = false; - bool isRSISwitchedToBearish = false; + bool isRSISwitchedToBullish = + cRSI > pRSI && + pRSI <= cRSI; + bool isRSISwitchedToBearish = + cRSI < pRSI && + pRSI >= cRSI; // - bool isRSIOB = false; - bool isRSIOS = false; + bool isRSIOB = + cRSI > rsiOBLevel; + bool isRSIOS = + cRSI < rsiOSLevel; // - bool isRSICrossedOverOB = false; - bool isRSICrossedOverOS = false; + bool isRSICrossedOverOB = + cRSI > rsiOBLevel && + pRSI <= rsiOBLevel; + bool isRSICrossedOverOS = + cRSI > rsiOSLevel && + pRSI <= rsiOSLevel; // - bool isRSICrossedUnderOB = false; - bool isRSICrossedUnderOS = false; + bool isRSICrossedUnderOB = + cRSI < rsiOBLevel && + pRSI >= rsiOBLevel; + bool isRSICrossedUnderOS = + cRSI < rsiOSLevel && + pRSI >= rsiOSLevel; + + // + // DELTA ... + + // + bool isDeltaUp = + cDelta > pDelta; + bool isDeltaDown = + cDelta < pDelta; + + // + bool isDeltaBullish = + cDelta > pDelta && + cDeltaSignal > cDelta && + cDeltaSignal > pDeltaSignal; + bool isPDeltaBullish = + pDelta > p2Delta && + pDeltaSignal > pDelta && + pDeltaSignal > p2DeltaSignal; + + // + bool isDeltaBearish = + cDelta < pDelta && + cDeltaSignal < cDelta && + cDeltaSignal < pDeltaSignal; + bool isPDeltaBearish = + pDelta < p2Delta && + pDeltaSignal < pDelta && + pDeltaSignal < p2DeltaSignal; + + // + bool isDeltaSwitchedToBullish = + isDeltaBullish && + !isPDeltaBullish; + bool isDeltaSwitchedToBearish = + isDeltaBearish && + !isPDeltaBearish; + + // + // VOLUME ... + + // + bool isVolumeBullish = + cBullishVolume > pBullishVolume && + cBullishVolume > cBearishVolume; + bool isVolumeBearish = + cBearishVolume > pBearishVolume && + cBearishVolume > cBullishVolume; + + // + bool isVolumeSwitchedToBullish = + isVolumeBullish && + cBullishVolumeSignal > cBearishVolumeSignal && + pBullishVolumeSignal <= pBearishVolumeSignal; + bool isVolumeSwitchedToBearish = + isVolumeBearish && + cBearishVolumeSignal > cBullishVolumeSignal && + pBearishVolumeSignal <= pBullishVolumeSignal; // // SIGNAL Bar ... // - bool isSBarBullish = false; - bool isSBarBearish = false; + bool isSBarBullish = + cSBarO < cSBarC; + bool isSBarBearish = + cSBarO > cSBarC; // - bool isSBarSwitchedToBullish = false; - bool isSBarSwitchedToBearish = false; + bool isSBarSwitchedToBullish = + cSBarO < cSBarC && + pSBarO >= pSBarC; + bool isSBarSwitchedToBearish = + cSBarO > cSBarC && + pSBarO <= pSBarC; // // XHK Signal Bar ... // - bool isHKSBarBullish = false; - bool isHKSBarBearish = false; + bool isHKSBarBullish = + cHKSBarO < cHKSBarC; + bool isHKSBarBearish = + cHKSBarO > cHKSBarC; // - bool isHKSBarSwitchedToBullish = false; - bool isHKSBarSwitchedToBearish = false; + bool isHKSBarSwitchedToBullish = + cHKSBarO < cHKSBarC && + pHKSBarO >= pHKSBarC; + bool isHKSBarSwitchedToBearish = + cHKSBarO > cHKSBarC && + pHKSBarO <= pHKSBarC; // // TREND ... // - bool isTrendUp = false; - bool isTrendDown = false; + bool isTrendUp = + cTrend > pTrend; + bool isTrendDown = + cTrend < pTrend; // - bool isTrendBullish = false; - bool isTrendBearish = false; + bool isTrendBullish = + cTrendState > 0; + bool isTrendBearish = + cTrendState < 0; // - bool isTrendSwitchedToBullish = false; - bool isTrendSwitchedToBearish = false; + bool isTrendSwitchedToBullish = + isTrendUp && + cTrendState > 0 && + pTrendState <= 0; + bool isTrendSwitchedToBearish = + isTrendDown && + cTrendState < 0 && + pTrendState >= 0; // // PEAK ... // - bool isPeakSameLast = false; - bool isPeakOverLast = false; - bool isPeakUnderLast = false; + bool isPeakSameLast = + cPEAK > 0 && + cPEAK == pPEAK; + bool isPeakOverLast = + pPEAK > 0 && + cPEAK > pPEAK; + bool isPeakUnderLast = + cPEAK > 0 && + cPEAK < pPEAK; // // VALE ... // - bool isValeSameLast = false; - bool isValeOverLast = false; - bool isValeUnderLast = false; + bool isValeSameLast = + cVALE > 0 && + cVALE == pVALE; + bool isValeOverLast = + pVALE > 0 && + cVALE > pVALE; + bool isValeUnderLast = + cVALE > 0 && + cVALE < pVALE; // // Attached ... @@ -4062,10 +4883,12 @@ class XCX121XCatbHelper : public XCBaseHelper conditions.isATRUp = isATRUp; conditions.isRSIOB = isRSIOB; conditions.isRSIOS = isRSIOS; + conditions.isDeltaUp = isDeltaUp; conditions.isATRDown = isATRDown; conditions.isTrendUp = isTrendUp; conditions.rsiOBLevel = rsiOBLevel; conditions.rsiOSLevel = rsiOSLevel; + conditions.isDeltaDown = isDeltaDown; conditions.isTrendDown = isTrendDown; conditions.adxThreshold = adxThreshold; conditions.isSarBullish = isSarBullish; @@ -4076,12 +4899,16 @@ class XCX121XCatbHelper : public XCBaseHelper conditions.isRSIBearish = isRSIBearish; conditions.isSBarBullish = isSBarBullish; conditions.isSBarBearish = isSBarBearish; + conditions.isDeltaBullish = isDeltaBullish; + conditions.isDeltaBearish = isDeltaBearish; conditions.isTrendBullish = isTrendBullish; conditions.isTrendBearish = isTrendBearish; conditions.isPeakSameLast = isPeakSameLast; conditions.isPeakOverLast = isPeakOverLast; conditions.isValeSameLast = isValeSameLast; conditions.isValeOverLast = isValeOverLast; + conditions.isVolumeBullish = isVolumeBullish; + conditions.isVolumeBearish = isVolumeBearish; conditions.isADXBiggerLast = isADXBiggerLast; conditions.isADXLesserLast = isADXLesserLast; conditions.isHKSBarBullish = isHKSBarBullish; @@ -4108,6 +4935,10 @@ class XCX121XCatbHelper : public XCBaseHelper conditions.isSBarSwitchedToBearish = isSBarSwitchedToBearish; conditions.isTrendSwitchedToBullish = isTrendSwitchedToBullish; conditions.isTrendSwitchedToBearish = isTrendSwitchedToBearish; + conditions.isDeltaSwitchedToBullish = isDeltaSwitchedToBullish; + conditions.isDeltaSwitchedToBearish = isDeltaSwitchedToBearish; + conditions.isVolumeSwitchedToBullish = isVolumeSwitchedToBullish; + conditions.isVolumeSwitchedToBearish = isVolumeSwitchedToBearish; conditions.isHKSBarSwitchedToBullish = isHKSBarSwitchedToBullish; conditions.isHKSBarSwitchedToBearish = isHKSBarSwitchedToBearish; diff --git a/Indicators/x-saherelm.x121.xdelta.mq5 b/Indicators/x-saherelm.x121.xdelta.mq5 new file mode 100644 index 00000000..9cadee69 --- /dev/null +++ b/Indicators/x-saherelm.x121.xdelta.mq5 @@ -0,0 +1,550 @@ +/////////////////////////////////////////////////////// +// +// SaherElm IT Center MQL5 Ocillator +// ------------------------------------------------- +// Name: X121 XDelta +// Description: Detect Comulative Volumes Delta ... +// +// +// Maintainer: +// ------------ +// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com) +// +////////////////////////////////////////////////////// +// +// Global Properties ... +#property copyright "Copyright 2023, SaherElm IT Center" +#property link "https://saherelm.ir" +#property version "1.00" +#property description "SaherElm X121 XDelta Ocillator" +#property strict + +// +// Definitions ... + +// +#define ShortName "X121_XDELTA" + +// +// Imports ... +#include "../Libraries/x-saherelm.common.lib.mq5" + +// +// Inputs ... + +// +// Makret ... +input group "Market"; +input int maLength = 14; // Moving Averge Length +input ENUM_X_MA_METHOD maMethod = X_MA_MODE_EMA; // Moving Averge Method + +// +// Presentation ... +input group "Presentation"; +input int startCalculationForLastBars = 1000; // Calculate Last n Bars +input bool showDeltaLine = true; // Show Delta Line +input bool showDeltaHistogram = true; // Show Delta Histogram +input bool showDeltaMa = true; // Show Delta Moving Average + +// +// Buffers Props ... + +// +#property indicator_separate_window + +// +#property indicator_buffers 6 +#property indicator_plots 3 + +// +#define hideColorIDX 0 +#define bullishColorIDX 1 +#define bearishColorIDX 2 + +// +// Buffers ... + +// +#define deltaLineBufferIndex 0 +double deltaLineBuffer[]; + +#property indicator_label1 "Delta Line" +#property indicator_type1 DRAW_LINE +#property indicator_color1 clrYellow +#property indicator_style1 STYLE_SOLID +#property indicator_width1 1 + +// +#define deltaHistogramBufferIndex 1 +double deltaHistogramBuffer[]; + +// +#define deltaHistogramColorBufferIndex 2 +double deltaHistogramColorBuffer[]; + +#property indicator_label2 "Delta Histogram" +#property indicator_type2 DRAW_COLOR_HISTOGRAM +#property indicator_color2 CLR_NONE, clrGreen, clrDarkRed +#property indicator_style2 STYLE_DOT +#property indicator_width2 1 + +// +#define deltaMaBufferIndex 3 +double deltaMaBuffer[]; + +#property indicator_label3 "Delta MA" +#property indicator_type3 DRAW_LINE +#property indicator_color3 clrOrchid +#property indicator_style3 STYLE_SOLID +#property indicator_width3 1 + +// +// Data Buffers ... + +#define mLastBufferIndex 3 + +// +#define rawDeltaBufferIndex mLastBufferIndex + 1 +double rawDeltaBuffer[]; + +// +#define deltaStateBufferIndex mLastBufferIndex + 2 +double deltaStateBuffer[]; + +// +// Variables, Properties and etc ... + +// +// this counts Available Bars ... +int limit; + +// +int maxLength; + +// +int firstBarIndex; + +// +// Event Handlers ... + +/** + * Initialize Indicator ... + * + * @return ( int ) + */ +int OnInit() +{ + // + // Validate Inputs ... + if (!ValidateInputs()) + { + return INIT_PARAMETERS_INCORRECT; + } + + // + // Initialize Indicator Handlers ... + + // + // because in some cases we may have more than one input for + // calculation and we must prevent any calculation + // untill we pass the biggest input length, here we get max Input length + // and then wait until pass it ... + maxLength = ExtractMaxLengthOfInputs(); + + // + // Define Index Buffers ... + DefineBuffers(); + + // + // Set Indicator ShortName ... + SetIndicatorName(); + IndicatorSetInteger(INDICATOR_DIGITS, 2); + + // + // Init Succeed ... + return INIT_SUCCEEDED; +} + +/** + * De Initialize Indicator ... + * + * @param reason: Integer, De Initialization Reason ... + */ +void OnDeinit(const int reason) +{ + // + // REASON_PROGRAM 0 The EA has stopped working calling the ExpertRemove() function + // REASON_REMOVE 1 Program removed from a chart + // REASON_RECOMPILE 2 Program recompiled + // REASON_CHARTCHANGE 3 A symbol or a chart period is changed + // REASON_CHARTCLOSE 4 Chart closed + // REASON_PARAMETERS 5 Inputs changed by a user + // REASON_ACCOUNT 6 Another account has been activated or reconnection to the trade server has occurred due to changes in the account settings + // REASON_TEMPLATE 7 Another chart template applied + // REASON_INITFAILED 8 The OnInit() handler returned a non-zero value + // REASON_CLOSE 9 Terminal closed +} + +/** + * Calculate Bars ... + * + * @param rates_total: Integer, Total Bars on Chart ... + * @param prev_calculated: Integer, Total Calculated Bars on Charts ... + * @param time: DateTime Array, History of Open Time ... + * @param open: Double Array, History of Open Prices ... + * @param high: Double Array, History of High Prices ... + * @param low: Double Array, History of Low Prices ... + * @param close: Double Array, History of Close Prices ... + * @param tick_volume: Long, History of Tick Volumes on Bar ... + * @param volume: Long, History of Trade Volumes ... + * @param spread: Double, History of Spread Price ... + * + * @return ( int ) + */ +int OnCalculate( + const int rates_total, + const int prev_calculated, + const datetime &time[], + const double &open[], + const double &high[], + const double &low[], + const double &close[], + const long &tick_volume[], + const long &volume[], + const int &spread[] // +) +{ + // + // Prepare Buffers ... + ArraySetAsSeries(time, true); + ArraySetAsSeries(open, true); + ArraySetAsSeries(high, true); + ArraySetAsSeries(low, true); + ArraySetAsSeries(close, true); + ArraySetAsSeries(tick_volume, true); + ArraySetAsSeries(volume, true); + ArraySetAsSeries(spread, true); + + // + limit = (prev_calculated > rates_total || prev_calculated <= 0) + ? rates_total + : (rates_total - prev_calculated) + 1; + + // + // Main Loop ... + for (int i = limit - 1; i >= 0 && !IsStopped(); i--) + { + // + CalculateBuffers( + // + i, + prev_calculated, + rates_total, + // + open, + high, + close, + low, + tick_volume // + ); + } + + // + return rates_total; +} + +// +// Functions ... + +/** + * Validate Input Args for Initialization ... + * + * @return ( bool ) + */ +bool ValidateInputs() +{ + // + bool result = false; + + // + result = maLength > 0 && + IsValid(maMethod); + + // + return result; +} + +/** + * Extract Max Length of Inputs ... + * + * @return ( int ) + */ +int ExtractMaxLengthOfInputs() +{ + // + int result = 0; + + // + result = MathMax(0, maLength); + + // + return result; +} + +/** + * Set Indicator Short Name and also we can define Buffers Labels ... + */ +void SetIndicatorName() +{ + IndicatorSetString(INDICATOR_SHORTNAME, ShortName); +} + +/** + * Define Required Buffers ... + */ +void DefineBuffers() +{ + // + // Plot Buffers ... + + // + // Delta Line ... + ENUM_DRAW_TYPE deltaLineDrawType = showDeltaLine ? DRAW_LINE : DRAW_NONE; + + // + ArraySetAsSeries(deltaLineBuffer, true); + SetIndexBuffer(deltaLineBufferIndex, deltaLineBuffer, INDICATOR_DATA); + PlotIndexSetDouble(deltaLineBufferIndex, PLOT_EMPTY_VALUE, 0.0); + PlotIndexSetInteger(deltaLineBufferIndex, PLOT_SHOW_DATA, showDeltaLine); + PlotIndexSetInteger(deltaLineBufferIndex, PLOT_DRAW_TYPE, deltaLineDrawType); + + // + // Delta Histogram ... + ENUM_DRAW_TYPE deltaHistogramDrawType = showDeltaHistogram ? DRAW_COLOR_HISTOGRAM : DRAW_NONE; + + // + ArraySetAsSeries(deltaHistogramBuffer, true); + SetIndexBuffer(deltaHistogramBufferIndex, deltaHistogramBuffer, INDICATOR_DATA); + PlotIndexSetDouble(deltaHistogramBufferIndex, PLOT_EMPTY_VALUE, 0.0); + PlotIndexSetInteger(deltaHistogramBufferIndex, PLOT_SHOW_DATA, showDeltaHistogram); + PlotIndexSetInteger(deltaHistogramBufferIndex, PLOT_DRAW_TYPE, deltaHistogramDrawType); + + // + ArraySetAsSeries(deltaHistogramColorBuffer, true); + SetIndexBuffer(deltaHistogramColorBufferIndex, deltaHistogramColorBuffer, INDICATOR_COLOR_INDEX); + + // + // Delta Ma ... + ENUM_DRAW_TYPE deltaMaDrawType = showDeltaMa ? DRAW_LINE : DRAW_NONE; + + // + ArraySetAsSeries(deltaMaBuffer, true); + SetIndexBuffer(deltaMaBufferIndex, deltaMaBuffer, INDICATOR_DATA); + PlotIndexSetDouble(deltaMaBufferIndex, PLOT_EMPTY_VALUE, EMPTY_VALUE); + PlotIndexSetInteger(deltaMaBufferIndex, PLOT_SHOW_DATA, showDeltaMa); + PlotIndexSetInteger(deltaMaBufferIndex, PLOT_DRAW_TYPE, deltaMaDrawType); + + // + // Data Buffers ... + + // + ArraySetAsSeries(rawDeltaBuffer, true); + SetIndexBuffer(rawDeltaBufferIndex, rawDeltaBuffer, INDICATOR_CALCULATIONS); + + // + ArraySetAsSeries(deltaStateBuffer, true); + SetIndexBuffer(deltaStateBufferIndex, deltaStateBuffer, INDICATOR_CALCULATIONS); +} + +/** + * Calculate Custom Buffers ... + * + * @param bar_index: Integer, Represent Current Bar ... + * @param prevCalculated: Integer, Represent Previous Calculated Bars ... + * @param ratesTotal: Integer, Represents All Available Bars ... + * @param open: Double Array, History of Open Prices ... + * @param high: Double Array, History of High Prices ... + * @param close: Double Array, History of Close Prices ... + * @param low: Double Array, History of Low Prices ... + * @param tickVolume: Long, History of Tick Volumes on Bar ... + */ +void CalculateBuffers( + int bar_index, // Selected Bar Index + const int prevCalculated, + const int ratesTotal, + const double &open[], + const double &high[], + const double &close[], + const double &low[], + const long &tickVolume[] // +) +{ + // + int barsLimit = startCalculationForLastBars > 0 + ? startCalculationForLastBars + : 0; + if (barsLimit == 0) + { + // + barsLimit = ratesTotal; + firstBarIndex = barsLimit - 1; + } + else + { + // + firstBarIndex = startCalculationForLastBars; + } + + // + // bool canCalculate = true; + bool canCalculate = + bar_index <= barsLimit; + if (canCalculate) + { + // + CalculateValues( + bar_index, + prevCalculated, + ratesTotal, + open, + high, + close, + low, + tickVolume // + ); + } + else + { + FillBuffersZero(bar_index); + } +} + +/** + * Fill All Bufers to Zero Vlue for Specified Bar Index ... + * + * @param barIndex: Integer ... + */ +void FillBuffersZero(int barIndex) +{ + // + rawDeltaBuffer[barIndex] = 0.0; + deltaLineBuffer[barIndex] = 0.0; + deltaStateBuffer[barIndex] = 0.0; + deltaHistogramBuffer[barIndex] = 0.0; + deltaMaBuffer[barIndex] = 0.0; + deltaHistogramColorBuffer[barIndex] = hideColorIDX; +} + +/** + * Calculate Vales ... + * + * @param bar_index: Integer, Represent Current Bar ... + * @param prevCalculated: Integer, Represent Previous Calculated Bars ... + * @param ratesTotal: Integer, Represents All Available Bars ... + * @param open: Double Array, History of Open Prices ... + * @param high: Double Array, History of High Prices ... + * @param close: Double Array, History of Close Prices ... + * @param low: Double Array, History of Low Prices ... + * @param tickVolume: Long, History of Tick Volumes on Bar ... + */ +void CalculateValues( + int bar_index, // Selected Bar Index + const int prevCalculated, + const int ratesTotal, + const double &open[], + const double &high[], + const double &close[], + const double &low[], + const long &tickVolume[] // +) +{ + // + // Check Prev Bar ... + int lastBarIndex = bar_index + 1; + bool isFirstBar = + startCalculationForLastBars > 0 + ? bar_index == startCalculationForLastBars + : bar_index == firstBarIndex; + + // + XOHCL bar; + bool has = bar.Init( + _Symbol, + _Period, + bar_index // + ); + if (!has) + { + return; + } + + // + double iDelta = 0.0; + + // + // Detecting Delta Volume ... + if (bar.IsBullish()) + { + iDelta = (double)bar.volume; + } + else if (bar.IsBearish()) + { + iDelta = -(double)bar.volume; + } + + // + // Setting iDelta Value ... + if (isFirstBar) + { + // + rawDeltaBuffer[bar_index] = iDelta; + deltaLineBuffer[bar_index] = iDelta; + deltaHistogramBuffer[bar_index] = iDelta; + } + else + { + // + iDelta += deltaLineBuffer[bar_index + 1]; + + // + rawDeltaBuffer[bar_index] = iDelta; + deltaLineBuffer[bar_index] = iDelta; + deltaHistogramBuffer[bar_index] = iDelta; + } + + // + // Setting iDelta State ... + double iState = iDelta > 0 + ? bullishColorIDX + : iDelta < 0 + ? bearishColorIDX + : hideColorIDX; + // + // Setting iDelta Color ... + double iDeltaColor = iDelta > 0 + ? bullishColorIDX + : iDelta < 0 + ? bearishColorIDX + : hideColorIDX; + + // + deltaStateBuffer[bar_index] = iState; + deltaHistogramColorBuffer[bar_index] = iDeltaColor; + + // + // Calculate Moving Averages ... + int deltaMAsCount = iMAOnBuffer( + ratesTotal, + prevCalculated, + bar_index, + maLength, + rawDeltaBuffer, + deltaMaBuffer, + maMethod // + ); + + // + bar.Clean(); +} + +// diff --git a/Libraries/x-saherelm.x-poi.lib.mq5 b/Libraries/x-saherelm.x-poi.lib.mq5 index 02d1fd29..c22c7ad8 100644 --- a/Libraries/x-saherelm.x-poi.lib.mq5 +++ b/Libraries/x-saherelm.x-poi.lib.mq5 @@ -5923,3 +5923,97 @@ struct XBoxZone }; // +struct XDirectionShift +{ + // + // Props ... + string type; + datetime at; + double after; + double before; + string symbol; + ENUM_TIMEFRAMES period; + ENUM_X_DIRECTION shiftTo; + + // + // Constructor ... + XDirectionShift() + { + Clean(); + } + + // + // Tools ... + + // + // Cleanup ... + void Clean() + { + // + at = NULL; + after = 0; + before = 0; + type = NULL; + symbol = NULL; + period = NULL; + shiftTo = X_DIRECTION_NONE; + + // + ZeroMemory(this); + } + + // + // Validate ... + bool IsValid() + { + // + bool result = false; + + // + result = + after > 0 && + IsValid(at) && + IsValid(type) && + after != before && + IsValid(symbol) && + IsValid(period) && + HasDirection(shiftTo); + + // + return result; + } + + // + // Check Same ... + bool IsSameAs(XDirectionShift &item) + { + // + bool result = false; + + // + result = IsValid() && + item.IsValid(); + if (!result) + { + return result; + } + + // + result = + // + at == item.at && + type == item.type && + after == item.after && + before == item.before && + symbol == item.symbol && + period == item.period && + shiftTo == item.shiftTo + // + ; + + // + return result; + } +}; + +// diff --git a/Libraries/x-saherelm.x121.xcatb.lib.mq5 b/Libraries/x-saherelm.x121.xcatb.lib.mq5 index 2cd5a39d..85da990b 100644 --- a/Libraries/x-saherelm.x121.xcatb.lib.mq5 +++ b/Libraries/x-saherelm.x121.xcatb.lib.mq5 @@ -23,6 +23,7 @@ // Imports ... #include "../Classes/x-saherelm.x-bar.analyser.class.mq5" #include "../Classes/x-saherelm.x-poi.drawer.class.mq5" +#include "../Helpers/x-saherelm.x121.xcatb.helper.mq5" #include "../Libraries/x-saherelm.common.lib.mq5"; #include "../Libraries/x-saherelm.x-poi.lib.mq5" @@ -135,6 +136,501 @@ struct XTriggerBlock // // Extensions ... +bool DetectTriggerBlock( + XCPOIDrawer *drawer, + XCX121XCatbHelper *helper, + X121XCatbConditions &conditions, + XTriggerBlock &tb, + int barIndex = 0, + int loopback = 10 // +) +{ + // + bool result = false; + + // + // Normalize Args ... + tb.Clean(); + conditions.Clean(); + barIndex = NormalizeInt(barIndex, 0); + loopback = NormalizeInt(loopback, 0); + + // + // Validate Args ... + result = helper != NULL; + if (!result) + { + return result; + } + + // + // Reading Market Conditions ... + result = helper.GetConditions( + conditions, + barIndex, + loopback // + ); + if (!result) + { + // + tb.Clean(); + conditions.Clean(); + } + + // + int zIDX = 0; + int cIDX = zIDX + 1; + int pIDX = cIDX + 1; + int p2IDX = pIDX + 1; + + // + // Checking Conditions ... + + // + // Detect Indicator Base Start Directional Movement ... + + // + // SAR ... + + // + bool isSarBullishStart = + // + ( + // + // Current SAR is Directional ... + conditions.isSarBullish && + // + // Previous InDirectional Sar Exists ... + helper.lastSarBearish.IsValid() && + // + // Current Close is Over previous Sar ... + conditions.bars[cIDX].close > helper.lastSarBearish.after && + // + // Before Close is not Over previous Sar ... + conditions.bars[pIDX].close <= helper.lastSarBearish.after + // + ) + // + ; + + // + bool isSarBearishStart = + // + ( + // + // Current SAR is Directional ... + conditions.isSarBearish && + // + // Previous InDirectional Sar Exists ... + helper.lastSarBullish.IsValid() && + // + // Current Close is Under previous ... + conditions.bars[cIDX].close < helper.lastSarBullish.after && + // + // Before Close is not Under previous ... + conditions.bars[pIDX].close >= helper.lastSarBullish.after + // + ) + // + ; + + // + // TREND ... + + // + bool isTrendBullishStart = + // + ( + // + // Current Trend is Directional ... + conditions.isTrendBullish && + // + // Previous InDirectiona; Trend Exists ... + helper.lastTrendBearish.IsValid() && + // + // Current Close is Over previous ... + conditions.bars[cIDX].close > helper.lastTrendBearish.after && + // + // Before close is not Over previous ... + conditions.bars[pIDX].close <= helper.lastTrendBearish.after + // + ) + // + ; + + // + bool isTrendBearishStart = + // + ( + // + // Current Trend is Directional ... + conditions.isTrendBearish && + // + // Previous InDirectiona; Trend Exists ... + helper.lastTrendBullish.IsValid() && + // + // Current Close is Under previous ... + conditions.bars[cIDX].close < helper.lastTrendBullish.after && + // + // Before close is not Under previous ... + conditions.bars[pIDX].close >= helper.lastTrendBullish.after + // + ) + // + ; + + // + // ADX ... + + // + bool isADXBullishStart = + // + ( + // + // ADX Value is Switched to Strong ... + conditions.adxBuffer[cIDX] > conditions.adxThreshold && + conditions.adxBuffer[pIDX] <= conditions.adxThreshold && + // + // ADX must Up ... + conditions.adxBuffer[cIDX] > conditions.adxBuffer[pIDX] && + // + // ADX +DI is Bigger than -DI ... + conditions.adxpBuffer[cIDX] > conditions.adxnBuffer[cIDX] && + // + // ADX +DI is Up ... + conditions.adxpBuffer[cIDX] > conditions.adxpBuffer[pIDX] + // + ) + // + ; + + // + bool isADXBearishStart = + // + ( + // + // ADX Value is Switched to Strong ... + conditions.adxBuffer[cIDX] > conditions.adxThreshold && + conditions.adxBuffer[pIDX] <= conditions.adxThreshold && + // + // ADX must Up ... + conditions.adxBuffer[cIDX] > conditions.adxBuffer[pIDX] && + // + // ADX -DI is Bigger than +DI ... + conditions.adxnBuffer[cIDX] > conditions.adxpBuffer[cIDX] && + // + // ADX -DI is Up ... + conditions.adxnBuffer[cIDX] > conditions.adxnBuffer[pIDX] + // + ) + // + ; + + // + // DELTA ... + + // + bool isDeltaBullishStart = + // + // Check State ... + ( + // + // Delta is Up ... + conditions.deltaBuffer[cIDX] > conditions.deltaBuffer[pIDX] && + // + // Delta Signal is Up ... + conditions.deltaSignalBuffer[cIDX] > conditions.deltaSignalBuffer[pIDX] && + // + // Delta Crossed Over Delta Signal ... + conditions.deltaBuffer[cIDX] > conditions.deltaSignalBuffer[cIDX] + // + ) + // + && + // + // Check Last InDirectional Exists ... + helper.lastDeltaBearish.IsValid() && + // + // Current Close is Over last ... + conditions.bars[cIDX].close > helper.lastDeltaBearish.after && + // + // Before Close is not Over last ... + conditions.bars[pIDX].close <= helper.lastDeltaBearish.after + // + ; + + // + bool isDeltaBearishStart = + // + // Check State ... + ( + // + // Delta is Down ... + conditions.deltaBuffer[cIDX] < conditions.deltaBuffer[pIDX] && + // + // Delta Signal is Down ... + conditions.deltaSignalBuffer[cIDX] < conditions.deltaSignalBuffer[pIDX] && + // + // Delta Crossed Under Delta Signal ... + conditions.deltaBuffer[cIDX] < conditions.deltaSignalBuffer[cIDX] + // + ) + // + && + // + // Check Last InDirectional Exists ... + helper.lastDeltaBullish.IsValid() && + // + // Current Close is Under last ... + conditions.bars[cIDX].close < helper.lastDeltaBullish.after && + // + // Before Close is not Under last ... + conditions.bars[pIDX].close >= helper.lastDeltaBullish.after + // + ; + + // + // VOLUME ... + + // + bool isVolumeBullishStart = + // + ( + // + // + // Volume is Up ... + conditions.bullishVolumeBuffer[1] > conditions.bullishVolumeBuffer[2] && + // + // Volume Signal is Up ... + conditions.bullishVolumeSignalBuffer[1] > conditions.bullishVolumeSignalBuffer[2] && + // + // Volume Signals Crossed Over ... + conditions.bullishVolumeSignalBuffer[1] > conditions.bearishVolumeSignalBuffer[1] + // + ) + // + && + // + // Check Last InDirectional Exists ... + helper.lastVolumeBearish.IsValid() && + // + // Current Close is Over last ... + conditions.bars[cIDX].close > helper.lastVolumeBearish.after && + // + // Before Close is not Over last ... + conditions.bars[pIDX].close <= helper.lastVolumeBearish.after + // + ; + + // + bool isVolumeBearishStart = + // + ( + // + // + // Volume is Up ... + conditions.bearishVolumeBuffer[1] > conditions.bearishVolumeBuffer[2] && + // + // Volume Signal is Up ... + conditions.bearishVolumeSignalBuffer[1] > conditions.bearishVolumeSignalBuffer[2] && + // + // Volume Signals Crossed Over ... + conditions.bearishVolumeSignalBuffer[1] > conditions.bullishVolumeSignalBuffer[1] + // + ) + // + && + // + // Check Last InDirectional Exists ... + helper.lastVolumeBullish.IsValid() && + // + // Current Close is Over last ... + conditions.bars[cIDX].close < helper.lastVolumeBullish.after && + // + // Before Close is not Over last ... + conditions.bars[pIDX].close >= helper.lastVolumeBullish.after + // + ; + + // + // SIGNAL Bar ... + + // + bool isSBarBullishStart = + // + ( + // + // Check Direction ... + conditions.isSBarBullish && + // + // Previous InDirectional Exists ... + helper.lastSignalBarBearish.IsValid() && + // + // Current Signal Bar close over prev ... + conditions.sBarCloseBuffer[cIDX] > helper.lastSignalBarBearish.after && + // + // Prev Signal Bar not close over prev ... + conditions.sBarCloseBuffer[pIDX] <= helper.lastSignalBarBearish.after + // + ) + // + ; + + // + bool isSBarBearishStart = + // + ( + // + // Check Direction ... + conditions.isSBarBearish && + // + // Previous InDirectional Exists ... + helper.lastSignalBarBullish.IsValid() && + // + // Current Signal Bar close over prev ... + conditions.sBarCloseBuffer[cIDX] < helper.lastSignalBarBullish.after && + // + // Prev Signal Bar not close over prev ... + conditions.sBarCloseBuffer[pIDX] >= helper.lastSignalBarBullish.after + // + ) + // + ; + + // + // HKSIGNAL Bar ... + + // + bool isHKSBarBullishStart = + // + ( + // + // Check Direction ... + conditions.isHKSBarBullish && + // + // Previous InDirectional Exists ... + helper.lastHKSignalBarBearish.IsValid() && + // + // Current Signal Bar close over prev ... + conditions.hkSBarCloseBuffer[cIDX] > helper.lastHKSignalBarBearish.after && + // + // Prev Signal Bar not close over prev ... + conditions.hkSBarCloseBuffer[pIDX] <= helper.lastHKSignalBarBearish.after + // + ) + // + ; + + // + bool isHKSBarBearishStart = + // + ( + // + // Check Direction ... + conditions.isHKSBarBearish && + // + // Previous InDirectional Exists ... + helper.lastHKSignalBarBullish.IsValid() && + // + // Current Signal Bar close over prev ... + conditions.hkSBarCloseBuffer[cIDX] < helper.lastHKSignalBarBullish.after && + // + // Prev Signal Bar not close over prev ... + conditions.hkSBarCloseBuffer[pIDX] >= helper.lastHKSignalBarBullish.after + // + ) + // + ; + + // + // Try to Summarize Conditions ... + + // + bool isBullishPassed = + // + //isSarBullishStart && + // isTrendBullishStart && + // isADXBullishStart && + isDeltaBullishStart && + isVolumeBullishStart && + isSBarBullishStart + // && + // isHKSBarBullishStart + // + ; + + // + bool isBearishPassed = + // + // isSarBearishStart && + // isTrendBearishStart && + // isADXBearishStart && + isDeltaBearishStart && + isVolumeBearishStart && + isSBarBearishStart + // && + // isHKSBarBearishStart + // + ; + + // + // Draw Line for Detect Conditions States on Chart ... + if (isBullishPassed || + isBearishPassed) + { + // + color iColor = isBullishPassed + ? clrAqua + : clrMagenta; + + // + datetime iTime = conditions.bars[cIDX].time; + + // + string iName = "VL_" + ToFormatString(iTime); + + // + long chartID = drawer.ChartIdentification(); + int subWindow = drawer.SubWindowIdentification(); + + // + CChartObjectVLine *iObj; + iObj = new CChartObjectVLine(); + bool isDrawn = iObj.Create( + chartID, + iName, + subWindow, + iTime // + ); + if (isDrawn) + { + // + iObj.Color(iColor); + } + } + + // + // Summarizing result ... + + // + result = (isBullishPassed || + isBearishPassed); + + // + // Cleanup Resources ... + + // + if (!result) + { + // + tb.Clean(); + conditions.Clean(); + } + + // + return result; +} + // // Detect Trigger Block ... bool DetectTriggerBlock(