Skip to content

Pattern Algorithms

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

Identifying Pivots

Implementation: utils.py:getMaxMin

             Pivot
              /|\
6 Bars left  /   \  6 Bars right
            /     \
           /       \

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.

The getMaxMin(df, barsLeft=6, barsRight=6) function in src/utils.py 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.

Head & Shoulders (Bearish Pattern)

Rules

Implementation: utils.py:isHNS

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

  • 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
HEAD & SHOULDERS (BEARISH)
                C
                /\
        A      /  \      E
        /\    /    \    /\
       /  \  /      \  /  \F
      /    \/________\/_______Neckline
     /      B         D
    /

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.

Work in progress (More entries to follow soon)

Clone this wiki locally