Skip to content

Pattern Algorithms

Benny Thadikaran edited this page Dec 27, 2023 · 6 revisions

Contents

Identifying Pivots

Implementation: utils.py:getMaxMin

Chart showing pivot formation

By default,

  • a pivot high is a high, unbroken by 6 candles before or after.
  • a pivot low is a low, unbroken by 6 candles before or after.

This means after a new local High or Low is formed, it takes 6 additional candles to confirm the pivot.

The getMaxMin(df, barsLeft=6, barsRight=6) function returns a Pandas Series of the pivot high and lows. The default argument ensures only major pivots are returned. The pivots are used to identify patterns.

You can tweak the result by changing the barsLeft and barsRight argument. Reducing the values will result in more pivots (major and minor) being returned.


Are Two points forming a straight line

Two points are considered on a straight line if their absolute difference falls within the average candle range (High - Low).

For ex: A and B are in a straight line in abs(a - b) <= averageCandleRange.

This is followed throughout the script, whenever straight lines are desired. Lines drawn across these points will often appear with some degree of slope.


Head & Shoulders (Bearish Pattern)

Rules

Implementation: utils.py:isHNS

c > max(a, e) and max(b, d) < min(a, e) and f < e and abs(b - d) < avgBarLength

  • C is the highest point
  • A and E are below C
  • B and D are lower than all other points
  • F is the Close and has not breached E or D
  • avgBarLength is the average candle range (High - Low) between B and D

HEAD & SHOULDERS (BEARISH)
                C
                /\
        A      /  \      E
        /\    /    \    /\
       /  \  /      \  /  \F
      /    \/________\/_______Neckline
     /      B         D
    /

Some patterns will have a degree of slope in the neckline. see Straight Line

Algorithm

Implementation: utils.py:findHNS

  1. Create a list of High and Low pivot points from the price data and assign it to P.
  2. F is the last Close value in the price data
  3. Set the highest point in P to C.
  4. From the pivots P:
    1. Set the highest point before C to A.
    2. Set the lowest point between A and C to B
    3. Set the highest point after C to E
    4. Set the lowest point between C and E to D
  5. Check if points A, B, C, D, E, and F validate against the Head and Shoulder rules.
  6. If not validated, set C equal to E (E being the next highest pivot) and repeat step 4.
  7. If validated:
    1. Draw a trendline (Neckline) below B and D.
    2. If the last closing price has not breached below the neckline, we have a confirmed pattern.

Pennants - Symetrical / Ascending / Descending Triangles, Wedge

Rules

Implementation: Utils.py:isPennant

         Symetrical                 Ascending                   Descending
          A                        A         C       E             A
         /\        C              /\        /\      /\            /\        C
        /  \      /\    E        /  \      /  \    /  \          /  \      /\      E
       /    \    /  \  /\       /    \    /    \  /    \        /    \    /  \    /\
      /      \  /    \/  F     /      \  /      \/      F      /      \  /    \  /  \
     /        \/      D       /        \/        D            /        \/      \/    F
    /         B              /          B                    /          B       D

Returns the first pattern that matches:

  • Symetrical Triangle: A, C and E are lower highs and B and D are higher lows. F has not breached E or D.
a > c > e and b < d < f and e > f
  • Ascending Triangle: A, C and E form a relatively straight line. B, D and F are forming higher lows. F has not breached E or D
`abs(a - c) <= avgBarLength and abs(c - e) <= avgBarLength and b < d < f < e`
  • Descending Triangles: B, D are in a relatively straight line. A, C and E are forming lower lows. F has not breached E or D
`abs(b - d) <= avgBarLength and a > c > e > f and f > d`
  • AvgBarLength is the average candle range (High - Low) between A to D.

Ascending and Descending Triangles can appear pointing upward or downward. In some rare cases, triangles may appear in a broadening formation. These are valid patterns and rules are purposefully set to allow these variations.

Algorithm

Implementation: utils.py:findHNS

  1. Create a list of High and Low pivot points from the price data and assign it to P.
  2. F is the last Close value in the price data
  3. Set the highest point in P to A.
  4. From the pivots P:
    1. Set the lowest point after A to B
    2. Set the lowest point after B to D
    3. Set the highest point between B and D to C.
    4. Set the highest point between D and F to E.
    5. Get the average candle range between A and D
  5. Check if the points validate against the pennant rules.
  6. If validated, check for additional conditions:
    1. Any candle Close after C, has breached the point C or any candle Close after D, has breached the point D. If yes, assign point C to A and continue from step 4.
    2. Draw a trendline across the highs of A and C and lows of B and D. If the lines have crossed eachother at the last close, the pattern has played out. Skip to next symbol.
  7. Finally if not validated, assign point C to A and continue from `step 4.

Double Top (Bearish Pattern)

Rules

Implementation: utils.py:isDoubleTop

Double Top
          A     C
         /\    /\
        /  \  /  \
       /    \/    D
      /      B
     /

abs(a - c) <= avgBarLength and cVol < aVol and b < min(a, c) and b < d < c

  • A and C form a relatively straight line.
  • B is less than A and C and D (Last Close price) has not breached C or B.
  • Volume on C is lower than on A.
  • AvgBarLength is calculated as the average candle range between A and C.

Algorithm

Implementation: utils.py:findDoubleTop

  1. Create a list of High and Low pivot points from the price data and assign it to P.
  2. D is the last Close value in the price data
  3. Set the highest point in P to A. Set the volume on A to aVol
  4. From the pivots P:
    1. Set the highest point after A to C. Set the volume on C to cVol.
    2. Check if C was breached on a closing basis after its formation. If yes, assign price and volume of C to A and continue from step 4.
    3. Set the minimum point between A and C to B.
    4. Calculate the average candle range between A and C
  5. Check if the above points, validate against Double Top rules. If no, follow step 6 else perform additional checks.
    • Check if C and D was breached by any candle close after its formation. If yes, follow step 6.
  6. Assign price and volume of C to A and continue from step 4.

Volatility Contraction Pattern (Bullish Pattern)

Rules

Implementation: utils.py:bullishVCP

Volatilty Contraction pattern

       A        C
         \      /\    E
          \    /  \  /
           \  /    \/
            \/      D
             B

abs(a - c) <= avgBarLength and b < min(a, c, d, e) and d < min(a, c, e) and e < c

  • B is the lowest.
  • D is the second lowest.
  • A and C for a relatively straight line.
  • E is the last Close price, and has not breached C or D

Algorithm

Implementation: utils.py:findBullishVCP

  1. Create a list of High and Low pivot points from the price data and assign it to P.
  2. E is the last Close in the price data
  3. Set the highest point in P to A.
  4. From the pivots P:
    1. Set the lowest point from A to B.
    2. Set the lowest point after B to D.
    3. Set the highest point between B and D to C.
    4. Get the average candle range between A and C
  5. Check if above points, validate against bullish VCP rules. If No, follow step 6, else perform additional checks:
    • Check if C and D was breached by any candle close after its formation. If yes, follow step 6.
  6. If not validated, assign the value of C to A and continue from step 4.
Clone this wiki locally