From 9a1e0ba7ef6dabfa592f00850fa9249e32cb62e5 Mon Sep 17 00:00:00 2001 From: kenorb Date: Sat, 7 Aug 2021 15:46:39 +0100 Subject: [PATCH] Pattern: Improves patterns --- Pattern.enum.h | 95 ++++++++++++++++++++++++++++-------------------- Pattern.struct.h | 60 ++++++++++++++++++++++-------- 2 files changed, 99 insertions(+), 56 deletions(-) diff --git a/Pattern.enum.h b/Pattern.enum.h index 94fb89d90..8448472f3 100644 --- a/Pattern.enum.h +++ b/Pattern.enum.h @@ -42,8 +42,8 @@ enum ENUM_PATTERN_1CANDLE { PATTERN_1CANDLE_BODY_GT_PP_OPEN = 1 << 5, // Body is above the pivot price (OHLC/4) PATTERN_1CANDLE_BODY_GT_WEIGHTED = 1 << 6, // Body is above the weighted price (OH2C/4) PATTERN_1CANDLE_BODY_GT_WICKS = 1 << 7, // Body is greater than sum of wicks - PATTERN_1CANDLE_CHANGE_GT_02PC = 1 << 8, // Price change is greater than 0.2% of the price change - PATTERN_1CANDLE_CHANGE_GT_05PC = 1 << 9, // Price change is greater than 0.5% of the price change + PATTERN_1CANDLE_CHANGE_GT_01PC = 1 << 8, // Price change is greater than 0.1% of the price change + PATTERN_1CANDLE_CHANGE_GT_02PC = 1 << 9, // Price change is greater than 0.2% of the price change PATTERN_1CANDLE_CLOSE_GT_MED = 1 << 10, // Close price is above the median price PATTERN_1CANDLE_CLOSE_GT_PP = 1 << 11, // Close price is above the pivot price (HLC/3) PATTERN_1CANDLE_CLOSE_GT_PP_DM = 1 << 12, // Close price is above the Tom DeMark pivot price @@ -95,38 +95,42 @@ enum ENUM_PATTERN_1CANDLE { /* Enumeration for 2-candle patterns. */ enum ENUM_PATTERN_2CANDLE { - PATTERN_2CANDLE_NONE = 0 << 0, // None. - PATTERN_2CANDLE_BEARS = 1 << 0, // Two bear candles. - PATTERN_2CANDLE_BODY_GT_BODY = 1 << 1, // Body size is greater than the previous one. - PATTERN_2CANDLE_BULLS = 1 << 2, // Two bulls candles. - PATTERN_2CANDLE_CLOSE_GT_CLOSE = 1 << 3, // Close is greater than the previous one. - PATTERN_2CANDLE_CLOSE_GT_HIGH = 1 << 4, // Close is greater than previous high. - PATTERN_2CANDLE_CLOSE_LT_LOW = 1 << 5, // Close is lower than previous low. - PATTERN_2CANDLE_HIGH_GT_HIGH = 1 << 6, // High is greater than the previous one. - PATTERN_2CANDLE_HIGH_GT_HOC = 1 << 7, // High is greater than the previous higher price (open or close). - PATTERN_2CANDLE_HOC_GT_HIGH = 1 << 8, // Higher price (open or close) is greater than the previous high. - PATTERN_2CANDLE_HOC_GT_HOC = 1 << 9, // Higher price (open or close) is greater than the previous one. - PATTERN_2CANDLE_LOC_LT_LOC = 1 << 10, // Lower price (open or close) is lower than the previous one. - PATTERN_2CANDLE_LOC_LT_LOW = 1 << 11, // Lower price (open or close) is lower than the previous low. - PATTERN_2CANDLE_LOW_LT_LOC = 1 << 12, // Low is lower than the previous lower price (open or close). - PATTERN_2CANDLE_LOW_LT_LOW = 1 << 13, // Low is lower than the previous one. - PATTERN_2CANDLE_OPEN_GT_OPEN = 1 << 14, // Open is greater than the previous one. - PATTERN_2CANDLE_PP_GT_PP = 1 << 15, // Pivot price is greater than the previous one (HLC/3). - PATTERN_2CANDLE_PP_GT_PP_OPEN = 1 << 16, // Pivot price open is greater than the previous one (OHLC/4). - PATTERN_2CANDLE_RANGE_DBL_RANGE = 1 << 17, // Range size doubled from the previous one. - PATTERN_2CANDLE_RANGE_GT_RANGE = 1 << 18, // Range is greater than the previous one. - PATTERN_2CANDLE_TIME_GAP_DAY = 1 << 19, // Bars have over 24h gap. - // PATTERN_2CANDLE_RANGE_IN_BODY = 1 << 19, // Range is inside body - // of the previous candle. PATTERN_2CANDLE_RANGE_OUT_RANGE = 1 << 20, // - // Range is outside of range of the previous candle. - PATTERN_2CANDLE_WEIGHTED_GT_WEIGHTED = 1 << 21, // Weighted price is greater than the previous one (OH2C/4). - PATTERN_2CANDLE_WICKS_DBL_WICKS = 1 << 22, // Size of wicks doubled from the previous onces. - PATTERN_2CANDLE_WICKS_GT_WICKS = 1 << 23, // Size of wicks is greater than the previous onces. + PATTERN_2CANDLE_NONE = 0 << 0, // None. + PATTERN_2CANDLE_BEARS = 1 << 0, // Two bear candles. + PATTERN_2CANDLE_BODY_GT_BODY = 1 << 1, // Body size is greater than the previous one. + PATTERN_2CANDLE_BULLS = 1 << 2, // Two bulls candles. + PATTERN_2CANDLE_CLOSE_GT_CLOSE = 1 << 3, // Close is greater than the previous one. + PATTERN_2CANDLE_CLOSE_GT_HIGH = 1 << 4, // Close is greater than previous high. + PATTERN_2CANDLE_CLOSE_LT_LOW = 1 << 5, // Close is lower than previous low. + PATTERN_2CANDLE_HIGH_GT_HIGH = 1 << 6, // High is greater than the previous one. + PATTERN_2CANDLE_HIGH_GT_HOC = 1 << 7, // High is greater than the previous higher price (open or close). + PATTERN_2CANDLE_HOC_GT_HIGH = 1 << 8, // Higher price (open or close) is greater than the previous high. + PATTERN_2CANDLE_HOC_GT_HOC = 1 << 9, // Higher price (open or close) is greater than the previous one. + PATTERN_2CANDLE_LOC_LT_LOC = 1 << 10, // Lower price (open or close) is lower than the previous one. + PATTERN_2CANDLE_LOC_LT_LOW = 1 << 11, // Lower price (open or close) is lower than the previous low. + PATTERN_2CANDLE_LOW_LT_LOC = 1 << 12, // Low is lower than the previous lower price (open or close). + PATTERN_2CANDLE_LOW_LT_LOW = 1 << 13, // Low is lower than the previous one. + PATTERN_2CANDLE_OPEN_GT_OPEN = 1 << 14, // Open is greater than the previous one. + PATTERN_2CANDLE_PP_GT_PP = 1 << 15, // Pivot price is greater than the previous one (HLC/3). + PATTERN_2CANDLE_PP_GT_PP_OPEN = 1 << 16, // Pivot price open is greater than the previous one (OHLC/4). + PATTERN_2CANDLE_RANGE_DBL_RANGE = 1 << 17, // Range size doubled from the previous one. + PATTERN_2CANDLE_RANGE_GT_RANGE = 1 << 18, // Range is greater than the previous one. + PATTERN_2CANDLE_TIME_GAP_DAY = 1 << 19, // Bars have over 24h gap. + PATTERN_2CANDLE_WEIGHTED_GT_WEIGHTED = 1 << 20, // Weighted price is greater than the previous one (OH2C/4). + PATTERN_2CANDLE_WICKS_DBL_WICKS = 1 << 21, // Size of wicks doubled from the previous onces. + PATTERN_2CANDLE_WICKS_GT_WICKS = 1 << 22, // Size of wicks is greater than the previous onces. + // --- + PATTERN_2CANDLE_BODY_IN_BODY = 1 << 23, + PATTERN_2CANDLE_BODY_OUT_BODY = 1 << 24, + PATTERN_2CANDLE_RANGE_IN_RANGE = 1 << 25, + PATTERN_2CANDLE_RANGE_OUT_RANGE = 1 << 26, // Patterns based on the existing patterns. - PATTERN_2CANDLE_BODY_IN_BODY = ~PATTERN_2CANDLE_HOC_GT_HOC | ~PATTERN_2CANDLE_LOC_LT_LOC, - PATTERN_2CANDLE_BODY_OUT_BODY = PATTERN_2CANDLE_HOC_GT_HOC | PATTERN_2CANDLE_LOC_LT_LOC, - PATTERN_2CANDLE_RANGE_IN_RANGE = ~PATTERN_2CANDLE_HIGH_GT_HIGH | ~PATTERN_2CANDLE_LOW_LT_LOW, - PATTERN_2CANDLE_RANGE_OUT_RANGE = PATTERN_2CANDLE_HIGH_GT_HIGH | PATTERN_2CANDLE_LOW_LT_LOW, + // PATTERN_2CANDLE_BODY_IN_BODY = ~PATTERN_2CANDLE_HOC_GT_HOC | ~PATTERN_2CANDLE_LOC_LT_LOC, + // PATTERN_2CANDLE_BODY_OUT_BODY = PATTERN_2CANDLE_HOC_GT_HOC | PATTERN_2CANDLE_LOC_LT_LOC, + // PATTERN_2CANDLE_RANGE_IN_RANGE = ~PATTERN_2CANDLE_HIGH_GT_HIGH | ~PATTERN_2CANDLE_LOW_LT_LOW, + // PATTERN_2CANDLE_RANGE_OUT_RANGE = PATTERN_2CANDLE_HIGH_GT_HIGH | PATTERN_2CANDLE_LOW_LT_LOW, + // PATTERN_2CANDLE_RANGE_IN_BODY = 1 << ??, // Range is inside body of the previous candle. + // PATTERN_2CANDLE_RANGE_OUT_RANGE = 1 << ??, // Range is outside of range of the previous candle. // Bearish engulfing pattern. // A lot of momentum in favor of price falling. PATTERN_2CANDLE_BEAR_ENGULFING = ~PATTERN_2CANDLE_BEARS | ~PATTERN_2CANDLE_BULLS | ~PATTERN_2CANDLE_CLOSE_GT_CLOSE | @@ -137,18 +141,29 @@ enum ENUM_PATTERN_2CANDLE { PATTERN_2CANDLE_BULL_ENGULFING = ~PATTERN_2CANDLE_BEARS | ~PATTERN_2CANDLE_BULLS | PATTERN_2CANDLE_CLOSE_GT_CLOSE | PATTERN_2CANDLE_BODY_OUT_BODY | PATTERN_2CANDLE_RANGE_OUT_RANGE | PATTERN_2CANDLE_HOC_GT_HOC | PATTERN_2CANDLE_LOC_LT_LOC, + // Body is inside of the previous candle's range. - PATTERN_2CANDLE_BODY_IN_RANGE = ~PATTERN_2CANDLE_HOC_GT_HIGH | ~PATTERN_2CANDLE_LOC_LT_LOW, + PATTERN_2CANDLE_BODY_IN_RANGE = 1 << 27, + // PATTERN_2CANDLE_BODY_IN_RANGE = ~PATTERN_2CANDLE_HOC_GT_HIGH | ~PATTERN_2CANDLE_LOC_LT_LOW, + // Body is outside of the previous candle's range (partial Bullish engulfing). - PATTERN_2CANDLE_BODY_OUT_RANGE = PATTERN_2CANDLE_HOC_GT_HIGH | PATTERN_2CANDLE_LOC_LT_LOW, + PATTERN_2CANDLE_BODY_OUT_RANGE = 1 << 28, + // PATTERN_2CANDLE_BODY_OUT_RANGE = PATTERN_2CANDLE_HOC_GT_HIGH | PATTERN_2CANDLE_LOC_LT_LOW, + // Range is inside of the previous candle's body (partial Harami pattern). - PATTERN_2CANDLE_RANGE_IN_BODY = ~PATTERN_2CANDLE_HIGH_GT_HOC | ~PATTERN_2CANDLE_LOW_LT_LOC, + PATTERN_2CANDLE_RANGE_IN_BODY = 1 << 29, + // PATTERN_2CANDLE_RANGE_IN_BODY = ~PATTERN_2CANDLE_HIGH_GT_HOC | ~PATTERN_2CANDLE_LOW_LT_LOC, + // Range is outside of the previous candle's body (partial Harami pattern). - PATTERN_2CANDLE_RANGE_OUT_BODY = ~PATTERN_2CANDLE_HIGH_GT_HOC | ~PATTERN_2CANDLE_LOW_LT_LOC, + PATTERN_2CANDLE_RANGE_OUT_BODY = 1 << 30, + // PATTERN_2CANDLE_RANGE_OUT_BODY = PATTERN_2CANDLE_HIGH_GT_HOC | PATTERN_2CANDLE_LOW_LT_LOC, + // Harami pattern. // Neutral pattern where price is being pushed into a tighter range. - PATTERN_2CANDLE_HARAMI = PATTERN_2CANDLE_BODY_IN_BODY | PATTERN_2CANDLE_RANGE_IN_BODY | ~PATTERN_2CANDLE_HOC_GT_HOC | - ~PATTERN_2CANDLE_LOC_LT_LOC, + PATTERN_2CANDLE_HARAMI = 1 << 31, + // PATTERN_2CANDLE_HARAMI = PATTERN_2CANDLE_BODY_IN_BODY | PATTERN_2CANDLE_RANGE_IN_BODY | ~PATTERN_2CANDLE_HOC_GT_HOC + // | ~PATTERN_2CANDLE_LOC_LT_LOC, + // Dark cloud cover. // Price acted like it was continuing upwards, but then reversed. PATTERN_2CANDLE_DARK_CLOUD_COVER = ~PATTERN_2CANDLE_BEARS | ~PATTERN_2CANDLE_BULLS | ~PATTERN_2CANDLE_CLOSE_GT_CLOSE | @@ -230,7 +245,7 @@ enum ENUM_PATTERN_4CANDLE { PATTERN_4CANDLE_RANGE_DEC = 1 << 23, // Range size decreases. PATTERN_4CANDLE_RANGE_INC = 1 << 24, // Range size increases. PATTERN_4CANDLE_SHOOT_STAR = 1 << 25, // Shooting star (UU^DD). - PATTERN_4CANDLE_TIME_EVEN = 1 << 26, // Bar time is consistent (no time gaps). + PATTERN_4CANDLE_TIME_GAPS = 1 << 26, // Bar time is not consistent (has time gaps). PATTERN_4CANDLE_WICKS0_GT_SUM = 1 << 27, // Size of wicks are greater than sum of others. PATTERN_4CANDLE_WICKS_DEC = 1 << 28, // Size of wicks increases. PATTERN_4CANDLE_WICKS_GT_BODY = 1 << 29, // Sum of wicks are greater than sum of bodies. diff --git a/Pattern.struct.h b/Pattern.struct.h index 4d649a236..66ab293c2 100644 --- a/Pattern.struct.h +++ b/Pattern.struct.h @@ -205,12 +205,12 @@ struct PatternCandle1 : PatternCandle { case PATTERN_1CANDLE_BODY_GT_WICKS: // Body is greater than sum of wicks. return _c.GetBody() > _c.GetWickSum(); + case PATTERN_1CANDLE_CHANGE_GT_01PC: + // Price change is greater than 0.1% of the price change. + return _c.GetChangeInPct() > 0.1; case PATTERN_1CANDLE_CHANGE_GT_02PC: // Price change is greater than 0.2% of the price change. return _c.GetChangeInPct() > 0.2; - case PATTERN_1CANDLE_CHANGE_GT_05PC: - // Price change is greater than 0.5% of the price change. - return _c.GetChangeInPct() > 0.5; case PATTERN_1CANDLE_CLOSE_GT_MED: // Close price is above the median price. return _c.GetClose() > _c.GetMedian(); @@ -239,43 +239,51 @@ struct PatternCandle1 : PatternCandle { return _c.GetClose() < _c.GetWeighted(); case PATTERN_1CANDLE_HAS_WICK_LW: // Has lower shadow. - return _c.GetWickLowerInPct() > 0.1; + return _c.GetWickLowerInPct() > 2; case PATTERN_1CANDLE_HAS_WICK_UP: // Has upper shadow. - return _c.GetWickUpperInPct() > 0.1; + return _c.GetWickUpperInPct() > 2; case PATTERN_1CANDLE_IS_DOJI_DRAGON: // Has doji dragonfly pattern (upper). - return _c.GetWickLowerInPct() >= 98; + return _c.GetWickLowerInPct() > 95; case PATTERN_1CANDLE_IS_DOJI_GRAVE: // Has doji gravestone pattern (lower). - return _c.GetWickUpperInPct() >= 98; + return _c.GetWickUpperInPct() > 95; case PATTERN_1CANDLE_IS_HAMMER_INV: // Has an inverted hammer (also a shooting star) pattern. + // A bottom reversal signal. return _c.GetWickUpperInPct() > _c.GetBodyInPct() * 2 && _c.GetWickLowerInPct() < 2; case PATTERN_1CANDLE_IS_HAMMER_UP: // Has an upper hammer pattern. + // A bullish pattern during a downtrend. return _c.GetWickLowerInPct() > _c.GetBodyInPct() * 2 && _c.GetWickUpperInPct() < 2; case PATTERN_1CANDLE_IS_HANGMAN: // Has a hanging man pattern. - return _c.GetWickLowerInPct() > 80 && _c.GetWickLowerInPct() < 98; + // A bearish pattern during an uptrend. + return _c.GetWickLowerInPct() > _c.GetBodyInPct() * 3 && _c.GetWickUpperInPct() < 2; case PATTERN_1CANDLE_IS_LONG_SHADOW_LW: // Has long lower shadow pattern. - return _c.GetWickLowerInPct() >= 60; + return _c.GetWickLowerInPct() > 60; case PATTERN_1CANDLE_IS_LONG_SHADOW_UP: // Has long upper shadow pattern. - return _c.GetWickUpperInPct() >= 60; + return _c.GetWickUpperInPct() > 60; case PATTERN_1CANDLE_IS_MARUBOZU: - // Has body with no or small wicks. - return _c.GetBodyInPct() >= 98; + // Has a big body with no or small wicks. + return _c.GetBodyInPct() > 96; case PATTERN_1CANDLE_IS_SHAVEN_LW: // Has a shaven bottom (lower) pattern. + // A bottom reversal signal. return _c.GetWickUpperInPct() > 50 && _c.GetWickLowerInPct() < 2; case PATTERN_1CANDLE_IS_SHAVEN_UP: // Has a shaven head (upper) pattern. + // A bullish pattern during a downtrend + // and a bearish pattern during an uptrend. return _c.GetWickLowerInPct() > 50 && _c.GetWickUpperInPct() < 2; case PATTERN_1CANDLE_IS_SPINNINGTOP: // Has a spinning top pattern. - return _c.GetWickLowerInPct() > 30 && _c.GetWickLowerInPct() > 30; + // A candlestick with a small body. + // A neutral pattern. + return _c.GetBodyInPct() < 30 && _c.GetWickLowerInPct() > 20 && _c.GetWickUpperInPct() > 20; case PATTERN_1CANDLE_NONE: default: break; @@ -365,6 +373,26 @@ struct PatternCandle2 : PatternCandle { case PATTERN_2CANDLE_WICKS_GT_WICKS: // Size of wicks is greater than the previous onces. return _c[0].GetWickSum() > _c[1].GetWickSum(); + // --- + case PATTERN_2CANDLE_BODY_IN_BODY: + return !CheckPattern(PATTERN_2CANDLE_HOC_GT_HOC, _c) && !CheckPattern(PATTERN_2CANDLE_LOC_LT_LOC, _c); + case PATTERN_2CANDLE_BODY_OUT_BODY: + return CheckPattern(PATTERN_2CANDLE_HOC_GT_HOC, _c) && CheckPattern(PATTERN_2CANDLE_LOC_LT_LOC, _c); + case PATTERN_2CANDLE_RANGE_IN_RANGE: + return !CheckPattern(PATTERN_2CANDLE_HIGH_GT_HIGH, _c) && !CheckPattern(PATTERN_2CANDLE_LOW_LT_LOW, _c); + case PATTERN_2CANDLE_RANGE_OUT_RANGE: + return CheckPattern(PATTERN_2CANDLE_HIGH_GT_HIGH, _c) && CheckPattern(PATTERN_2CANDLE_LOW_LT_LOW, _c); + case PATTERN_2CANDLE_BODY_IN_RANGE: + return !CheckPattern(PATTERN_2CANDLE_HOC_GT_HIGH, _c) && !CheckPattern(PATTERN_2CANDLE_LOC_LT_LOW, _c); + case PATTERN_2CANDLE_BODY_OUT_RANGE: + return CheckPattern(PATTERN_2CANDLE_HOC_GT_HIGH, _c) && CheckPattern(PATTERN_2CANDLE_LOC_LT_LOW, _c); + case PATTERN_2CANDLE_RANGE_IN_BODY: + return !CheckPattern(PATTERN_2CANDLE_HIGH_GT_HOC, _c) && !CheckPattern(PATTERN_2CANDLE_LOW_LT_LOC, _c); + case PATTERN_2CANDLE_RANGE_OUT_BODY: + return CheckPattern(PATTERN_2CANDLE_HIGH_GT_HOC, _c) && CheckPattern(PATTERN_2CANDLE_LOW_LT_LOC, _c); + case PATTERN_2CANDLE_HARAMI: + return CheckPattern(PATTERN_2CANDLE_BODY_IN_BODY, _c) && CheckPattern(PATTERN_2CANDLE_RANGE_IN_BODY, _c) && + !CheckPattern(PATTERN_2CANDLE_HOC_GT_HOC, _c) && !CheckPattern(PATTERN_2CANDLE_LOC_LT_LOC, _c); case PATTERN_2CANDLE_NONE: default: break; @@ -624,10 +652,10 @@ struct PatternCandle4 : PatternCandle { /* Bull 3 */ _c[3].open < _c[3].close && /* Lower spike */ fmax(_c[1].GetWickLower(), _c[2].GetWickLower()) > _c[0].GetWickSum() + _c[3].GetWickSum(); - case PATTERN_4CANDLE_TIME_EVEN: + case PATTERN_4CANDLE_TIME_GAPS: // Bar time is consistent (no time gaps). - return (_c[0].time - _c[1].time) / 120 == (_c[1].time - _c[2].time) / 120 && - (_c[1].time - _c[2].time) / 120 == (_c[2].time - _c[3].time) / 120; + return (_c[0].time - _c[1].time) / 120 != (_c[1].time - _c[2].time) / 120 || + (_c[1].time - _c[2].time) / 120 != (_c[2].time - _c[3].time) / 120; case PATTERN_4CANDLE_WICKS0_GT_SUM: // Size of wicks are greater than sum of others. return _c[0].GetWickSum() > _c[1].GetWickSum() + _c[2].GetWickSum() + _c[3].GetWickSum();