From 3294ea9f21ada3d6e8359547a25d59e38e06daf6 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Tue, 16 Jul 2024 04:17:13 +0330 Subject: [PATCH] last works on tools library for drawing all different states ... --- Libraries/x-saherelm.tools.lib.mq5 | 2696 +++++++++------------- Strategies/x-saherelm.xsrbr.strategy.mq5 | 18 +- 2 files changed, 1143 insertions(+), 1571 deletions(-) diff --git a/Libraries/x-saherelm.tools.lib.mq5 b/Libraries/x-saherelm.tools.lib.mq5 index 8eb6c156..c1448b70 100644 --- a/Libraries/x-saherelm.tools.lib.mq5 +++ b/Libraries/x-saherelm.tools.lib.mq5 @@ -28,1691 +28,1253 @@ // Definitions ... // -// Detect Trends ... -struct XTrendDetector -{ - // - string symbol; - ENUM_TIMEFRAMES period; - - // - double higherHighs[]; - datetime higherHighTimes[]; - - // - double higherLows[]; - datetime higherLowTimes[]; - - // - double lowerHighs[]; - datetime lowerHighTimes[]; - - // - double lowerLows[]; - datetime lowerLowTimes[]; - - // - bool isFirstLow; - - // - bool isUpTrend; - bool isDownTrend; - - // - int _numberOfPivots; - int _pivotShoulders; - - // - // Constructor ... - XTrendDetector() - { - Clean(); - } - - // - bool FindTrend( - string _symbol, - ENUM_TIMEFRAMES _period, - int barIndex = 0, - int _requiredPivots = 3, - int _requiredShoulders = 3 // - ) - { - // - bool result = false; - - // - Clean(); - - // - if (barIndex < 0) - { - barIndex = 0; - } - - // - if (_requiredPivots < 3) - { - _requiredPivots = 3; - } - - // - if (_requiredShoulders < 3) - { - _requiredShoulders = 3; - } - - // - result = - // - IsValid(_symbol) && - IsValid(_period) - // - ; - if (!result) - { - Clean(); - return result; - } - - // - // Find Swing Lows and Swing Highs ... - - // - int hhIndexes[]; - datetime hhTimes[]; - double hhs[]; - - // - int llIndexes[]; - datetime llTimes[]; - double lls[]; - - // - bool isEnough = false; - - // - int lastHHIndex = barIndex; - int lastLLIndex = barIndex; - while (!isEnough) - { - // - int hhSize = ArraySize(hhs); - if (hhSize < _requiredPivots) - { - // - lastHHIndex = FindSwing( - _symbol, - _period, - X_SWING_HIGH, - _requiredShoulders, - lastHHIndex + 1 // - ); - if (lastHHIndex >= 0) - { - // - XOHCL hBar; - bool isInited = hBar.Init( - _symbol, - _period, - lastHHIndex // - ); - if (isInited) - { - // - Add( - hBar.high, - hhs // - ); - - // - Add( - hBar.time, - hhTimes // - ); - - // - Add( - lastHHIndex, - hhIndexes // - ); - } - } - } - - // - int llSize = ArraySize(lls); - if (llSize < _requiredPivots) - { - // - lastLLIndex = FindSwing( - _symbol, - _period, - X_SWING_LOW, - _requiredShoulders, - lastLLIndex + 1 // - ); - if (lastLLIndex >= 0) - { - // - XOHCL lBar; - bool isInited = lBar.Init( - _symbol, - _period, - lastLLIndex // - ); - if (isInited) - { - // - Add( - lBar.low, - lls // - ); - - // - Add( - lBar.time, - llTimes // - ); - - // - Add( - lastHHIndex, - llIndexes // - ); - } - } - } - - // - hhSize = ArraySize(hhs); - llSize = ArraySize(lls); - isEnough = - // - hhSize == _requiredPivots && - llSize == _requiredPivots - // - ; - } - - // - // Draws ... - // for (int i = 0; i < ArraySize(hhs); i++) - // { - // // - // DrawArrow( - // ChartID(), - // "XTRND_HH" + ToString(i), - // 0, - // hhTimes[i], - // hhs[i], - // 234, - // ANCHOR_BOTTOM, - // clrMagenta, - // STYLE_SOLID, - // 3 // - // ); - - // // - // DrawArrow( - // ChartID(), - // "XTRND_LL" + ToString(i), - // 0, - // llTimes[i], - // lls[i], - // 233, - // ANCHOR_TOP, - // clrAqua, - // STYLE_SOLID, - // 3 // - // ); - // } - - // - isUpTrend = - // - isFirstLow - ? - // - lls[0] > lls[1] && - lls[1] > lls[2] && - // - hhs[0] > hhs[1] && - hhs[1] > hhs[2] - // - : - // - hhs[0] > hhs[1] && - hhs[1] > hhs[2] && - // - lls[0] > lls[1] && - lls[1] > lls[2] - // - ; - isDownTrend = - // - isFirstLow - ? - // - lls[0] < lls[1] && - lls[1] < lls[2] && - // - hhs[0] < hhs[1] && - hhs[1] < hhs[2] - // - : - // - hhs[0] < hhs[1] && - hhs[1] < hhs[2] && - // - lls[0] < lls[1] && - lls[1] < lls[2] - // - ; - for (int i = 1; i < _requiredPivots; i++) - { - // - // Lows ... - - // - // Lower Lows ... - if (lls[i - 1] < lls[i]) - { - // - Add( - lls[i - 1], - lowerLows // - ); - - // - Add( - llTimes[i - 1], - lowerLowTimes // - ); - } - else - // - // Higher Lows ... - if (lls[i - 1] > lls[i]) - { - // - Add( - lls[i - 1], - higherLows // - ); - - // - Add( - llTimes[i - 1], - higherLowTimes // - ); - } - - // - // Highs ... - - // - // Higher Highs ... - if (hhs[i - 1] > hhs[i]) - { - // - Add( - hhs[i - 1], - higherHighs // - ); - - // - Add( - hhTimes[i - 1], - higherHighTimes // - ); - } - else - // - // Lower Highs ... - if (hhs[i - 1] < hhs[i]) - { - // - Add( - hhs[i - 1], - lowerHighs // - ); - - // - Add( - hhTimes[i - 1], - lowerHighTimes // - ); - } - } - - // - // Remove Draws ... - // RemoveDraws("XTRND"); - - // - result = isUpTrend || isDownTrend; - if (!result) - { - Clean(); - } - - // - symbol = _symbol; - period = _period; - _numberOfPivots = _requiredPivots; - _pivotShoulders = _requiredShoulders; - - // - result = IsValid(); - if (!result) - { - Clean(); - } - - // - return result; - } - - // - bool HasTrend() - { - // - bool result = isUpTrend || isDownTrend; - - // - return result; - } - - // - // Tools ... - - // - // Cleanup ... - void Clean() - { - // - symbol = NULL; - period = NULL; - - // - isUpTrend = false; - isFirstLow = false; - isDownTrend = false; - - // - _numberOfPivots = 0; - _pivotShoulders = 0; - - // - Clean(higherHighs); - Clean(higherHighTimes); - - // - Clean(higherLows); - Clean(higherLowTimes); - - // - Clean(lowerHighs); - Clean(lowerHighTimes); - - // - Clean(lowerLows); - Clean(lowerLowTimes); - } - - // - // Validate ... - bool IsValid() - { - // - bool result = false; - - // - result = - // - IsValid(symbol) && - IsValid(period) - // - ; - - // - return result; - } -}; +// Drawing Tools ... // -// a Pivot Point ... -struct XPivot +// Hammer ... + +// +string GetHammerTag(XOHCL &bar) +{ + return "Hammer-" + GetBarTimeTag(bar); +} + +// +bool DrawHammer( + XOHCL &bar, + long chartID, + color clr = clrAqua, + uint width = 1, + uchar arrow = 233 // +) { // - string symbol; // Trading Symbol ... - ENUM_TIMEFRAMES period; // Trading TimeFrame ... + bool result = false; // - double price; // Price ... - datetime time; // Time ... + string name = GetHammerTag(bar); // - int breakedAsSupport; // Number of Breaked As Support ... - int rejectedAsSupport; // Number of Rejected As Support ... + result = DrawArrow( + chartID, + name, + 0, + bar.time, + bar.low, + arrow, + ANCHOR_TOP, + clr, + STYLE_SOLID, + width // + ); // - int breakedAsResistance; // Number of Breaked As Resistance ... - int rejectedAsResistance; // Number of Rejected As Resistance ... + return result; +} + +// +void RemoveHammer(XOHCL &bar) +{ + RemoveDraws(GetHammerTag(bar)); +} + +// +// Shooting Star ... + +// +string GetShootingStarTag(XOHCL &bar) +{ + return "ShootingStar-" + GetBarTimeTag(bar); +} + +// +bool DrawShootingStar( + XOHCL &bar, + long chartID, + color clr = clrMagenta, + uint width = 1, + uchar arrow = 234 // +) +{ + // + bool result = false; // - bool isSupport; - int supportLength; - datetime supportTime; + string name = GetShootingStarTag(bar); // - bool isResistance; - int resistanceLength; - datetime resistanceTime; + result = DrawArrow( + chartID, + name, + 0, + bar.time, + bar.high, + arrow, + ANCHOR_BOTTOM, + clr, + STYLE_SOLID, + width // + ); // - double _threshold; // threshold in Point ... - int _lastCalculatedBar; // last calculated bars ... - int _loopbackForSupportAndResistance; // Number of LoopBacks for Support or Resistance ... + return result; +} + +// +void RemoveShootingStar(XOHCL &bar) +{ + RemoveDraws(GetShootingStarTag(bar)); +} + +// +// Bullish Engulfing ... + +// +string GetBullishEngulfingTag(XOHCL &bar) +{ + return "BullishEngulfing-" + GetBarTimeTag(bar); +} + +// +bool DrawBullishEngulfing( + XOHCL &bar, + long chartID, + color clr = clrAqua, + bool fill = false, + uint width = 1, + ENUM_LINE_STYLE style = STYLE_SOLID // +) +{ + // + bool result = false; // - // Constructor ... - XPivot() + XOHCL pBar; + result = bar.GetPreviousBar(pBar); + if (!result) { - Clean(); - } - - // - // Find Last Support for Market and - // Initialize Pivot Based On it ... - bool FindLastSupport( - string _symbol, - ENUM_TIMEFRAMES _period, - int barIndex = 0, - int _supResVerificationsStep = 21, - double __threshold = 5 // - ) - { - // - bool result = false; - - // - Clean(); - - // - if (barIndex < 0) - { - barIndex = 0; - } - - // - if (__threshold < 0) - { - __threshold = 0; - } - - // - if (_supResVerificationsStep < 0) - { - _supResVerificationsStep = 21; - } - - // - result = - // - IsValid(_symbol) && - IsValid(_period) && - IsValidSize(_supResVerificationsStep) - // - ; - - if (!result) - { - // - Clean(); - return result; - } - - // - // Find Resistance ... - - // - // Retrieve Current Bar ... - XOHCL cBar; - result = cBar.Init( - _symbol, - _period, - barIndex // - ); - if (!result) - { - // - Clean(); - return result; - } - - // - // Calculate Available Bars ... - - // - int bars = iBars( - _symbol, - _period // - ); - int maxAllowedBars = bars / 10; - int currentLength = _supResVerificationsStep; - - // - // Find Highest High ... - for (int i = barIndex; i < maxAllowedBars; i++) - { - // - // Validate and Regenrate Current Length ... - if (i > 1) - { - // - currentLength = _supResVerificationsStep * i; - - // - if (currentLength > maxAllowedBars) - { - currentLength = maxAllowedBars; - } - } - - // - // Clean ... - Clean(); - - // - int llIndex = cBar.FindLowestIndex( - currentLength, - MODE_LOW // - ); - XOHCL llBar; - result = llBar.Init( - _Symbol, - _Period, - llIndex // - ); - if (!result) - { - continue; - } - - // - symbol = _symbol; - period = _period; - price = llBar.low; - - // - _threshold = __threshold; - _loopbackForSupportAndResistance = _supResVerificationsStep; - - // - Calculate( - barIndex // - ); - - // - result = isSupport; - if (result) - { - // - supportTime = llBar.time; - supportLength = currentLength; - break; - } - } - - // - result = IsValid() && isSupport; - if (!result) - { - Clean(); - } - - // return result; } // - // Find Last Resistance for Market and - // Initialized Pivot Based On it ... - bool FindLastResistance( - string _symbol, - ENUM_TIMEFRAMES _period, - int barIndex = 0, - int _supResVerificationsStep = 21, - double __threshold = 5 // - ) + string name = GetBullishEngulfingTag(bar); + + // + DrawRectangle( + chartID, + name, + 0, + pBar.time, + pBar.low, + bar.time, + bar.high, + clr, + style, + width, + fill // + ); + + // + return result; +} + +// +void RemoveBullishEngulfing(XOHCL &bar) +{ + RemoveDraws(GetBullishEngulfingTag(bar)); +} + +// +// Bearish Engulfing ... + +// +string GetBearishEngulfingTag(XOHCL &bar) +{ + return "BearishEngulfing-" + GetBarTimeTag(bar); +} + +// +bool DrawBearishEngulfing( + XOHCL &bar, + long chartID, + color clr = clrMagenta, + bool fill = false, + uint width = 1, + ENUM_LINE_STYLE style = STYLE_SOLID // +) +{ + // + bool result = false; + + // + XOHCL pBar; + result = bar.GetPreviousBar(pBar); + if (!result) { - // - bool result = false; - - // - Clean(); - - // - if (barIndex < 0) - { - barIndex = 0; - } - - // - if (__threshold < 0) - { - __threshold = 0; - } - - // - if (_supResVerificationsStep < 0) - { - _supResVerificationsStep = 21; - } - - // - result = - // - IsValid(_symbol) && - IsValid(_period) && - IsValidSize(_supResVerificationsStep) - // - ; - - if (!result) - { - // - Clean(); - return result; - } - - // - // Find Resistance ... - - // - // Retrieve Current Bar ... - XOHCL cBar; - result = cBar.Init( - _symbol, - _period, - barIndex // - ); - if (!result) - { - // - Clean(); - return result; - } - - // - // Calculate Available Bars ... - - // - int bars = iBars( - _symbol, - _period // - ); - int maxAllowedBars = bars / 10; - int currentLength = _supResVerificationsStep; - - // - // Find Highest High ... - for (int i = barIndex; i < maxAllowedBars; i++) - { - // - // Validate and Regenrate Current Length ... - if (i > 1) - { - // - currentLength = _supResVerificationsStep * i; - - // - if (currentLength > maxAllowedBars) - { - currentLength = maxAllowedBars; - } - } - - // - // Clean ... - Clean(); - - // - int hhIndex = cBar.FindHighestIndex( - currentLength, - MODE_HIGH // - ); - XOHCL hhBar; - result = hhBar.Init( - _Symbol, - _Period, - hhIndex // - ); - if (!result) - { - continue; - } - - // - symbol = _symbol; - period = _period; - price = hhBar.high; - - // - _threshold = __threshold; - _loopbackForSupportAndResistance = _supResVerificationsStep; - - // - Calculate( - barIndex // - ); - - // - result = isResistance; - if (result) - { - // - resistanceTime = hhBar.time; - resistanceLength = currentLength; - break; - } - } - - // - result = IsValid() && isResistance; - if (!result) - { - Clean(); - } - - // return result; } // - // Check Rejection Based On Pivot Type ... - bool IsRejected() + string name = GetBearishEngulfingTag(bar); + + // + DrawRectangle( + chartID, + name, + 0, + pBar.time, + pBar.low, + bar.time, + bar.high, + clr, + style, + width, + fill // + ); + + // + return result; +} + +// +void RemoveBearishEngulfing(XOHCL &bar) +{ + RemoveDraws(GetBearishEngulfingTag(bar)); +} + +// +// Morning Star ... + +// +string GetMorningStarTag(XOHCL &bar) +{ + return "MorningStar-" + GetBarTimeTag(bar); +} + +// +bool DrawMorningStar( + XOHCL &bar, + long chartID, + color clr = clrAqua, + uint width = 1, + uchar arrow = 233 // +) +{ + // + bool result = false; + + // + string name = GetMorningStarTag(bar); + + // + result = DrawArrow( + chartID, + name, + 0, + bar.time, + bar.low, + arrow, + ANCHOR_TOP, + clr, + STYLE_SOLID, + width // + ); + + // + return result; +} + +// +void RemoveMorningStar(XOHCL &bar) +{ + RemoveDraws(GetMorningStarTag(bar)); +} + +// +// Evening Star ... + +// +string GetEveningStarTag(XOHCL &bar) +{ + return "EveningStar-" + GetBarTimeTag(bar); +} + +// +bool DrawEveningStar( + XOHCL &bar, + long chartID, + color clr = clrMagenta, + uint width = 1, + uchar arrow = 234 // +) +{ + // + bool result = false; + + // + string name = GetEveningStarTag(bar); + + // + result = DrawArrow( + chartID, + name, + 0, + bar.time, + bar.high, + arrow, + ANCHOR_BOTTOM, + clr, + STYLE_SOLID, + width // + ); + + // + return result; +} + +// +void RemoveEveningStar(XOHCL &bar) +{ + RemoveDraws(GetEveningStarTag(bar)); +} + +// +// Swing High ... + +// +string GetSwingHighTag(XOHCL &bar) +{ + return "SwingHigh-" + GetBarTimeTag(bar); +} + +// +bool DrawSwingHigh( + XOHCL &bar, + long chartID, + color clr = clrMagenta, + uint width = 1, + uchar arrow = 108 // +) +{ + // + bool result = false; + + // + string name = GetSwingHighTag(bar); + + // + result = DrawArrow( + chartID, + name, + 0, + bar.time, + bar.high, + arrow, + ANCHOR_BOTTOM, + clr, + STYLE_SOLID, + width // + ); + + // + return result; +} + +// +void RemoveSwingHigh(XOHCL &bar) +{ + RemoveDraws(GetSwingHighTag(bar)); +} + +// +// Swing Low ... + +// +string GetSwingLowTag(XOHCL &bar) +{ + return "SwingLow-" + GetBarTimeTag(bar); +} + +// +bool DrawSwingLow( + XOHCL &bar, + long chartID, + color clr = clrAqua, + uint width = 1, + uchar arrow = 108 // +) +{ + // + bool result = false; + + // + string name = GetSwingLowTag(bar); + + // + result = DrawArrow( + chartID, + name, + 0, + bar.time, + bar.low, + arrow, + ANCHOR_TOP, + clr, + STYLE_SOLID, + width // + ); + + // + return result; +} + +// +void RemoveSwingLow(XOHCL &bar) +{ + RemoveDraws(GetSwingLowTag(bar)); +} + +// +// Bullish FVG ... + +// +string GetBullishFVGTag(XOHCL &bar) +{ + return "BullishFVG-" + GetBarTimeTag(bar); +} + +// +bool DrawBullishFVG( + XOHCL &bar, + double upper, + double lower, + long chartID, + color clr = clrAqua, + bool fill = false, + uint width = 1, + ENUM_LINE_STYLE style = STYLE_SOLID // +) +{ + // + bool result = false; + + // + XOHCL pBar; + result = bar.GetPreviousBar(pBar); + if (!result) { - // - bool result = false; - - // - result = - // - IsValid() && - ( - // - isSupport || - isResistance - // - ) - // - ; - if (!result) - { - return result; - } - - // - result = - // - isSupport - ? IsSupportRejected() - : IsResistanceRejected() - // - ; - - // return result; } // - // Check Breake Based On Pivot Type ... - bool IsBreaked() + XOHCL ppBar; + result = pBar.GetPreviousBar(ppBar); + if (!result) { - // - bool result = false; - - // - result = - // - IsValid() && - ( - // - isSupport || - isResistance - // - ) - // - ; - if (!result) - { - return result; - } - - // - result = - // - isSupport - ? IsSupportBreaked() - : IsResistanceBreaked() - // - ; - - // return result; } // - // Tools ... + string name = GetBullishFVGTag(bar); // - // Check Support Rejected ... - bool IsSupportRejected( - int barIndex = 0 // - ) + DrawRectangle( + chartID, + name, + 0, + ppBar.time, + lower, + bar.time, + upper, + clr, + style, + width, + fill // + ); + + // + return result; +} + +// +void RemoveBullishFVG(XOHCL &bar) +{ + RemoveDraws(GetBullishFVGTag(bar)); +} + +// +// Bearish FVG ... + +// +string GetBearishFVGTag(XOHCL &bar) +{ + return "BearishFVG-" + GetBarTimeTag(bar); +} + +// +bool DrawBearishFVG( + XOHCL &bar, + double upper, + double lower, + long chartID, + color clr = clrMagenta, + bool fill = false, + uint width = 1, + ENUM_LINE_STYLE style = STYLE_SOLID // +) +{ + // + bool result = false; + + // + XOHCL pBar; + result = bar.GetPreviousBar(pBar); + if (!result) { - // - bool result = false; - - // - if (barIndex < 0) - { - barIndex = 0; - } - - // - result = isSupport; - if (!result) - { - return result; - } - - // - // Initialize Required Bars ... - - // - XOHCL cBar; - result = cBar.Init( - symbol, - period, - 1 // - ); - if (!result) - { - return result; - } - - // - XOHCL pBar; - result = pBar.Init( - symbol, - period, - 2 // - ); - if (!result) - { - return result; - } - - // - double threshold = GetPoints(symbol) * _threshold; - - // - result = - // - // Check Support Touch or Fake Break ... - pBar.low < price && - ( - // - pBar.low < price || - pBar.low < price - threshold - // - ) - // - && - // - // Check Rejection ... - cBar.low > price && - ( - // - cBar.low > price || - cBar.low > price + threshold - // - ) - // - && - // - // Check Rejection Must be Bullish ... - cBar.IsBullish() - // - ; - - // return result; } // - // Check Support Breaked ... - bool IsSupportBreaked( - int barIndex = 0 // - ) + XOHCL ppBar; + result = pBar.GetPreviousBar(ppBar); + if (!result) { - // - bool result = false; - - // - if (barIndex < 0) - { - barIndex = 0; - } - - // - result = isSupport; - if (!result) - { - return result; - } - - // - // Initialize Required Bars ... - - // - XOHCL cBar; - result = cBar.Init( - symbol, - period, - barIndex + 1 // - ); - if (!result) - { - return result; - } - - // - XOHCL pBar; - result = pBar.Init( - symbol, - period, - barIndex + 2 // - ); - if (!result) - { - return result; - } - - // - double threshold = GetPoints(symbol) * _threshold; - - // - result = - // - // Check Support Breaks ... - pBar.close < price && - ( - // - pBar.close < price - threshold || - pBar.low < price - threshold - // - ) - // - && - // - // Check 50% ... - ( - // - (price - pBar.close) > (pBar.GetBody() / 100) * 50 || - (price - cBar.close) > (cBar.GetBody() / 100) * 50 - // - ) - // - && - // - // Also Next Bar must Closed Below Support ... - cBar.high < price && - cBar.high < price - threshold - // - && - // - cBar.close < price && - cBar.close < price - threshold - // - && - // - cBar.IsBearish() - // - ; - if (result) - { - // DrawArrow( - // 0, - // "PBar", - // 0, - // pBar.time, - // pBar.low, - // 233, - // ANCHOR_TOP, - // clrYellow // - // ); - } - - // return result; } // - // Check Resistance Rejected ... - bool IsResistanceRejected( - int barIndex = 0 // - ) - { - // - bool result = false; + string name = GetBearishFVGTag(bar); - // - if (barIndex < 0) - { - barIndex = 0; - } + // + DrawRectangle( + chartID, + name, + 0, + ppBar.time, + upper, + bar.time, + lower, + clr, + style, + width, + fill // + ); - // - result = isResistance; - if (!result) - { - return result; - } + // + return result; +} - // - // Initialize Required Bars ... +// +void RemoveBearishFVG(XOHCL &bar) +{ + RemoveDraws(GetBearishFVGTag(bar)); +} - // - XOHCL cBar; - result = cBar.Init( - symbol, - period, - 1 // - ); - if (!result) - { - return result; - } +// +// Bullish Order Block ... - // - XOHCL pBar; - result = pBar.Init( - symbol, - period, - 2 // - ); - if (!result) - { - return result; - } +// +string GetBullishOrderBlockTag(XOHCL &bar) +{ + return "BullishOrderBlock-" + GetBarTimeTag(bar); +} - // - double threshold = GetPoints(symbol) * _threshold; +// +bool DrawBullishOrderBlock( + XOHCL &bar, + long chartID, + bool ray = true, + color clr = clrAqua, + ENUM_LINE_STYLE style = STYLE_DOT, + uint width = 1 // +) +{ + // + bool result = false; - // - result = - // - // Check Resistance Touch or Fake Break ... - pBar.high > price && - ( - // - pBar.high > price || - pBar.high > price + threshold - // - ) - // - && - // - // Check Rejection ... - cBar.high < price && - ( - // - cBar.high < price || - cBar.high < price - threshold - // - ) - // - && - // - // Check Rejection Must be Bearish ... - cBar.IsBearish() - // - ; + // + string name = GetBullishOrderBlockTag(bar); + datetime cTime = TimeCurrent(); - // + // + // Draw Upper Line ... + result = DrawTrendLine( + chartID, + name + "-Upper", + 0, + bar.time, + bar.high, + cTime, + bar.high, + clr, + style, + width, + false, + false, + ray // + ); + if (!result) { return result; } // - // Check Resistance Breaked ... - bool IsResistanceBreaked( - int barIndex = 0 // - ) - { - // - bool result = false; - - // - if (barIndex < 0) - { - barIndex = 0; - } - - // - result = isResistance; - if (!result) - { - return result; - } - - // - // Initialize Required Bars ... - - // - XOHCL cBar; - result = cBar.Init( - symbol, - period, - 1 // - ); - if (!result) - { - return result; - } - - // - XOHCL pBar; - result = pBar.Init( - symbol, - period, - 2 // - ); - if (!result) - { - return result; - } - - // - double threshold = GetPoints(symbol) * _threshold; - - // - result = - // - // Check Resistance Breaks ... - pBar.close > price && - ( - // - pBar.close > price + threshold || - pBar.high > price + threshold - // - ) - // - && - // - // Check 50% ... - ( - // - (pBar.close - price) > (pBar.GetBody() / 100) * 50 || - (cBar.close - price) > (cBar.GetBody() / 100) * 50 - // - ) - // - && - // - // Also Next Bar Must Closed Above Resistance ... - cBar.low > price && - cBar.low > price + threshold - // - && - // - cBar.close > price && - cBar.close > price + threshold - // - && - // - cBar.IsBullish() - // - ; - - // + // Draw Lower Line ... + result = DrawTrendLine( + chartID, + name + "-Lower", + 0, + bar.time, + bar.low, + cTime, + bar.low, + clr, + style, + width, + false, + false, + ray // + ); + if (!result) { return result; } // - // Cleanup ... - void Clean() - { - // - time = NULL; - symbol = NULL; - period = NULL; - supportTime = NULL; - resistanceTime = NULL; + return result; +} - // - price = 0; +// +void RemoveBullishOrderBlock(XOHCL &bar) +{ + RemoveDraws(GetBullishOrderBlockTag(bar)); +} - // - supportLength = 0; - resistanceLength = 0; - breakedAsSupport = 0; - rejectedAsSupport = 0; - breakedAsResistance = 0; - rejectedAsResistance = 0; +// +// Bearish Order Block ... - // - _lastCalculatedBar = -1; +// +string GetBearishOrderBlockTag(XOHCL &bar) +{ + return "BearishOrderBlock-" + GetBarTimeTag(bar); +} - // - isSupport = false; - isResistance = false; - - // - _objName = NULL; - } +// +bool DrawBearishOrderBlock( + XOHCL &bar, + long chartID, + bool ray = true, + color clr = clrMagenta, + ENUM_LINE_STYLE style = STYLE_DOT, + uint width = 1 // +) +{ + // + bool result = false; // - // Validate Model ... - bool IsValid() - { - // - bool result = false; + string name = GetBearishOrderBlockTag(bar); + datetime cTime = TimeCurrent(); - // - result = - // - IsValid(symbol) && - IsValid(period) && - price > 0 - // - ; - - // + // + // Draw Upper Line ... + result = DrawTrendLine( + chartID, + name + "-Upper", + 0, + bar.time, + bar.high, + cTime, + bar.high, + clr, + style, + width, + false, + false, + ray // + ); + if (!result) { return result; } // - // Calculate Required Data ... - void Calculate( - int barIndex, - int loopback = 288 // - ) - { - // - if (isSupport || isResistance) - { - return; - } - - // - int bars = iBars( - symbol, - period // - ); - - // - loopback += barIndex; - - // - // Normalizing Loopback ... - if (loopback > bars) - { - loopback = bars - 2 - _loopbackForSupportAndResistance; - } - - // - // Check Previous Calculation ... - if (bars == _lastCalculatedBar) - { - return; - } - - // - _lastCalculatedBar = bars; - - // - // First Get Current State of Pivot ... - // Means Use Zero Index ... - XOHCL pBar; - bool isInited = pBar.Init( - symbol, - period, - barIndex + 1 // - ); - if (!isInited) - { - return; - } - - // - // Retrieve Highest High and Lowest Low of SupRes Loopback ... - double hhValue = pBar - .FindHighest( - _loopbackForSupportAndResistance, - MODE_HIGH // - ); - - // - double llValue = pBar - .FindLowest( - _loopbackForSupportAndResistance, - MODE_LOW // - ); - - // - isSupport = llValue > price; - isResistance = hhValue < price; - - // - // Prevent Moving Forward if it isn't Support or Resistance ... - if (!isSupport && !isResistance) - { - return; - } - - // - time = TimeCurrent(); - - // - for (int i = 2; i < loopback; i++) - { - // - XOHCL iBar; - isInited = iBar.Init( - symbol, - period, - i // - ); - if (!isInited) - { - continue; - } - - // - XOHCL iPBar; - isInited = iPBar.Init( - symbol, - period, - i + 1 // - ); - if (!isInited) - { - continue; - } - - // - XOHCL iPPBar; - isInited = iPPBar.Init( - symbol, - period, - i + 2 // - ); - if (!isInited) - { - continue; - } - - // - bool isBreaked = false; - bool isRejected = false; - - // - if (isSupport) - { - // - // Count Support Rejects - // and Support Breaks ... - - // - // Support Rejected ... - isRejected = - // - // Check Support Touch or Fake Break ... - iPBar.low < price - // - && - // - // Check Rejection ... - iBar.low > price - // - ; - if (isRejected) - { - rejectedAsSupport++; - } - - // - // Support Breaked ... - isBreaked = - // - // Check Support Breaks ... - iPBar.close < price - // - && - // - // Check 50% ... - (price - iPBar.close) > (iPBar.GetBody() / 100) * 50 - // - && - // - // Also Next Bar must Closed Below Support ... - iBar.high < price - // - && - // - iBar.close < price - // - && - // - iBar.IsBullish() - // - ; - if (isBreaked) - { - breakedAsSupport++; - } - } - else if (isResistance) - { - // - // Count Resistance Reject - // or Resistance Breaks ... - - // - // Resistance Rejected ... - isRejected = - // - // Check Resistance Touch or Fake Break ... - iPBar.high > price - // - && - // - // Check Rejection ... - iBar.high < price - // - ; - if (isRejected) - { - rejectedAsResistance++; - } - - // - // Resistance Breaked ... - isBreaked = - // - // Check Resistance Breaks ... - iPBar.close > price - // - && - // - // Check 50% ... - (iPBar.close - price) > (iPBar.GetBody() / 100) * 50 - // - && - // - // Also Next Bar Must Closed Above Resistance ... - iBar.low > price - // - && - // - iBar.close > price - // - && - // - iBar.IsBullish() - // - ; - if (isBreaked) - { - breakedAsResistance++; - } - } - } + // Draw Lower Line ... + result = DrawTrendLine( + chartID, + name + "-Lower", + 0, + bar.time, + bar.low, + cTime, + bar.low, + clr, + style, + width, + false, + false, + ray // + ); + if (!result) { + return result; } // - string GetTag() - { - return "XPivot"; - } + return result; +} + +// +void RemoveBearishOrderBlock(XOHCL &bar) +{ + RemoveDraws(GetBearishOrderBlockTag(bar)); +} + +// +// Support ... + +// +string GetSupportTag(XOHCL &bar) +{ + return "Support-" + GetBarTimeTag(bar); +} + +// +bool DrawSupport( + XOHCL &bar, + long chartID, + double _threshold = 0, + bool ray = true, + color clr = clrGreen, + ENUM_LINE_STYLE style = STYLE_DASH, + uint width = 1, + color thresholdColor = clrAqua // +) +{ + // + bool result = false; // - // Draw Functions ... + datetime time2 = TimeCurrent(); // - // Last Draw Object Name ... - string _objName; + string name = GetSupportTag(bar); // - // Draw ... - bool Draw( - long _chartId, - string _prefix = "", - color _color = clrYellow, - color _thresholdColor = clrAqua, - ENUM_LINE_STYLE _style = STYLE_DOT, - bool _ray = true, - int _width = 1, - int _subWindow = 0, - bool forcePrefix = false // - ) + result = DrawTrendLine( + chartID, + name, + 0, + bar.time, + bar.high, + time2, + bar.high, + clr, + style, + width, + false, + false, + ray // + ); + + // + if (result && _threshold > 0) { // - bool result = false; + double threshold = GetPoints(bar.symbol) * _threshold; // - if (!isSupport && !isResistance) - { - return result; - } - - // - _objName = (IsValid(_prefix) && forcePrefix) - ? _prefix - : GetTag() + "_" + (IsValid(_prefix) ? _prefix + "_" : "") + ToString(NormalizeDouble(price, 2)); - - // - int timeBarIndex = iBarShift( - symbol, - period, - time, - false // - ); - datetime time1 = - // - isSupport - ? supportTime - : resistanceTime - // - ; + string upThresholdName = name + "UP"; + string downThresholdName = name + "DOWN"; // result = DrawTrendLine( - _chartId, - _objName, - _subWindow, - time1, - price, - time, - price, - _color, - _style, - _width, + chartID, + upThresholdName, + 0, + bar.time, + bar.high + threshold, + time2, + bar.high + threshold, + thresholdColor, + style, + width, false, false, - _ray // + ray // ); // - if (result && _threshold > 0) + if (result) { - // - // Calculate Threshodl Value ... - double diff = GetPoints(symbol) * _threshold; // - // Up Threshold ... result = DrawTrendLine( - _chartId, - _objName + "_UPTRSH", - _subWindow, - time1, - price + diff, - time, - price + diff, - _thresholdColor, - _style, - _width, + chartID, + downThresholdName, + 0, + bar.time, + bar.high - threshold, + time2, + bar.high - threshold, + thresholdColor, + style, + width, false, false, - _ray // + ray // ); - - // - if (result) - { - // - // Down Threshold ... - result = DrawTrendLine( - _chartId, - _objName + "_DOWNTRSH", - _subWindow, - time1, - price - diff, - time, - price - diff, - _thresholdColor, - _style, - _width, - false, - false, - _ray // - ); - } } + } + + // + return result; +} + +// +void RemoveSupport(XOHCL &bar) +{ + RemoveDraws(GetSupportTag(bar)); +} + +// +// Support ... + +// +string GetResistanceTag(XOHCL &bar) +{ + return "Resistance-" + GetBarTimeTag(bar); +} + +// +bool DrawResistance( + XOHCL &bar, + long chartID, + double _threshold = 0, + bool ray = true, + color clr = clrRed, + ENUM_LINE_STYLE style = STYLE_DASH, + uint width = 1, + color thresholdColor = clrMagenta // +) +{ + // + bool result = false; + + // + datetime time2 = TimeCurrent(); + + // + string name = GetResistanceTag(bar); + + // + result = DrawTrendLine( + chartID, + name, + 0, + bar.time, + bar.low, + time2, + bar.low, + clr, + style, + width, + false, + false, + ray // + ); + + // + if (result && _threshold > 0) + { + // + double threshold = GetPoints(bar.symbol) * _threshold; // + string upThresholdName = name + "UP"; + string downThresholdName = name + "DOWN"; + + // + result = DrawTrendLine( + chartID, + upThresholdName, + 0, + bar.time, + bar.low + threshold, + time2, + bar.low + threshold, + thresholdColor, + style, + width, + false, + false, + ray // + ); + + // + if (result) + { + + // + result = DrawTrendLine( + chartID, + downThresholdName, + 0, + bar.time, + bar.low - threshold, + time2, + bar.low - threshold, + thresholdColor, + style, + width, + false, + false, + ray // + ); + } + } + + // + return result; +} + +// +void RemoveResistance(XOHCL &bar) +{ + RemoveDraws(GetResistanceTag(bar)); +} + +// +// Bullish Trend ... + +// +string GetBullishTrendTag(XOHCL &bar) +{ + return "BullishTrend-" + GetBarTimeTag(bar); +} + +// +bool DrawBullishTrend( + XOHCL &bar, + XOHCL &highSwingBars[], + XOHCL &lowSwingBars[], + long chartID, + bool ray = true, + bool drawSwings = true, + color clr = clrAqua, + ENUM_LINE_STYLE style = STYLE_DOT, + uint width = 1, + uchar swingHighArrow = 202, + color swingHighColor = clrAqua, + uchar swingLowArrow = 202, + color swingLowColor = clrMagenta // +) +{ + // + bool result = false; + + // + string name = GetBullishTrendTag(bar); + + // + XOHCL first; + XOHCL last; + + // + // Draw Upper Trend Line ... + first = highSwingBars[ArraySize(highSwingBars) - 1]; + last = highSwingBars[0]; + + // + result = DrawTrendLine( + chartID, + name + "-Upper", + 0, + first.time, + first.high, + last.time, + last.high, + clr, + style, + width, + false, + false, + ray // + ); + if (!result) { return result; } // - // Remove Draw ... - void Remove() + // Draw Lower Trend Line ... + first = lowSwingBars[ArraySize(lowSwingBars) - 1]; + last = lowSwingBars[0]; + + // + result = DrawTrendLine( + chartID, + name + "-Lower", + 0, + first.time, + first.high, + last.time, + last.high, + clr, + style, + width, + false, + false, + ray // + ); + if (!result) { + return result; + } + + // + if (result && drawSwings) { // - if (!IsValid(_objName)) + // High Swings ... + for (int i = 0; i < ArraySize(highSwingBars); i++) { - return; + // + XOHCL iBar = highSwingBars[i]; + + // + result = DrawArrow( + chartID, + name + "-SwingHigh-" + ToString(i), + 0, + iBar.time, + iBar.high, + swingHighArrow, + ANCHOR_BOTTOM, + swingHighColor, + STYLE_SOLID, + width // + ); + if (!result) + { + break; + } } // - RemoveDraws(_objName); + // Low Swings ... + for (int i = 0; i < ArraySize(lowSwingBars); i++) + { + // + XOHCL iBar = lowSwingBars[i]; + + // + result = DrawArrow( + chartID, + name + "-SwingLow-" + ToString(i), + 0, + iBar.time, + iBar.low, + swingLowArrow, + ANCHOR_TOP, + swingLowColor, + STYLE_SOLID, + width // + ); + if (!result) + { + break; + } + } } -}; + + // + return result; +} + +// +void RemoveBullishTrend(XOHCL &bar) +{ + RemoveDraws(GetBullishTrendTag(bar)); +} + +// +// Bearish Trend ... + +// +string GetBearishTrendTag(XOHCL &bar) +{ + return "BearishTrend-" + GetBarTimeTag(bar); +} + +// +bool DrawBearishTrend( + XOHCL &bar, + XOHCL &highSwingBars[], + XOHCL &lowSwingBars[], + long chartID, + bool ray = true, + bool drawSwings = true, + color clr = clrMagenta, + ENUM_LINE_STYLE style = STYLE_DOT, + uint width = 1, + uchar swingHighArrow = 202, + color swingHighColor = clrMagenta, + uchar swingLowArrow = 202, + color swingLowColor = clrAqua // +) +{ + // + bool result = false; + + // + string name = GetBearishTrendTag(bar); + + // + XOHCL first; + XOHCL last; + + // + // Draw Upper Trend Line ... + first = highSwingBars[ArraySize(highSwingBars) - 1]; + last = highSwingBars[0]; + + // + result = DrawTrendLine( + chartID, + name + "-Upper", + 0, + first.time, + first.high, + last.time, + last.high, + clr, + style, + width, + false, + false, + ray // + ); + if (!result) { + return result; + } + + // + // Draw Lower Trend Line ... + first = lowSwingBars[ArraySize(lowSwingBars) - 1]; + last = lowSwingBars[0]; + + // + result = DrawTrendLine( + chartID, + name + "-Lower", + 0, + first.time, + first.high, + last.time, + last.high, + clr, + style, + width, + false, + false, + ray // + ); + if (!result) { + return result; + } + + // + if (result && drawSwings) + { + // + // High Swings ... + for (int i = 0; i < ArraySize(highSwingBars); i++) + { + // + XOHCL iBar = highSwingBars[i]; + + // + result = DrawArrow( + chartID, + name + "-SwingHigh-" + ToString(i), + 0, + iBar.time, + iBar.high, + swingHighArrow, + ANCHOR_BOTTOM, + swingHighColor, + STYLE_SOLID, + width // + ); + if (!result) + { + break; + } + } + + // + // Low Swings ... + for (int i = 0; i < ArraySize(lowSwingBars); i++) + { + // + XOHCL iBar = lowSwingBars[i]; + + // + result = DrawArrow( + chartID, + name + "-SwingLow-" + ToString(i), + 0, + iBar.time, + iBar.low, + swingLowArrow, + ANCHOR_TOP, + swingLowColor, + STYLE_SOLID, + width // + ); + if (!result) + { + break; + } + } + } + + // + return result; +} + +// +void RemoveBearishTrend(XOHCL &bar) +{ + RemoveDraws(GetBearishTrendTag(bar)); +} + +// +// +// + +// +string GetBarTimeTag(XOHCL &bar) +{ + // + string result = NULL; + + // + result = + // + ToString((int)bar.time) + // + ; + + // + return result; +} + +// +string GeShootingStarTag(XOHCL &bar) +{ + return "ShootingStar-" + GetBarTimeTag(bar); +} + +// +// string GeShootingStarTag(XOHCL &bar) +// { +// return "ShootingStar-" + GetBarTimeTag(bar); +// } // \ No newline at end of file diff --git a/Strategies/x-saherelm.xsrbr.strategy.mq5 b/Strategies/x-saherelm.xsrbr.strategy.mq5 index 12331d78..5971c18f 100644 --- a/Strategies/x-saherelm.xsrbr.strategy.mq5 +++ b/Strategies/x-saherelm.xsrbr.strategy.mq5 @@ -158,14 +158,14 @@ public: bool isInBearishTrend = false; // - isInBullishTrend = bar.HasBullishTrend( + isInBullishTrend = cBar.HasBullishTrend( highSwingBars, lowSwingBars // ); if (!isInBullishTrend) { // - isInBearishTrend = bar.HasBearishTrend( + isInBearishTrend = cBar.HasBearishTrend( highSwingBars, lowSwingBars // ); @@ -264,7 +264,12 @@ public: // if (result) { + // type = POSITION_TYPE_BUY; + sl = support.low - threshold; + + // + support.Clean(); } } else if (isInBearishTrend) @@ -327,7 +332,12 @@ public: // if (result) { + // type = POSITION_TYPE_SELL; + sl = resistance.high + threshold; + + // + resistance.Clean(); } } } @@ -461,8 +471,8 @@ private: // Finding Analyzing Bar ... result = bar.Init( mSymbol, - mAnalyzingPeriod, - 1 // + mPeriod, // mAnalyzingPeriod, + 1 // ); if (!result) {