Library · Pine Script v5 · Library

HarmonicPattern

by ricardosantos79· code credited to RicardoSantos

This Pine Script library provides helper functions to detect and validate harmonic patterns using price and time ratio calculations.

View on GitHub

GitHub stars261on the repository
Pine versionv5runs as is
Lines of code279
Settings0inputs
LicenceNone statedfile: MPL-2.0
Last update4 years ago13 Nov 2021

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new library, select all of the template text and paste. Press Save and give it a name.
  4. 4Publish or import itA library draws nothing. Publish it from the editor, then import its functions in your own scripts.

Source Code

279 lines of Pine Script v5 from beta/libs/HarmonicPattern.pinescript, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

HarmonicPattern.pinescript
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RicardoSantos

//@version=5

// @description Functions to detect/check harmonic patterns from provided values.
library("HarmonicPattern")

// @function Compute the price rate of the line AB divided by the the line BC
// @param point_c float, the price at point C.
// @param point_b float, the price at point B.
// @param point_a float, the price at point A.
// @returns float
export line_price_rate (float point_c, float point_b, float point_a) => //{
//  C   A
//   \ /
//    B
    (point_a - point_b) / (point_b - point_c)
//}

// @function Compute the time rate of the line AB divided by the the line BC
// @param _c float, the time or bar_index at point C.
// @param _b float, the time or bar_index at point B.
// @param _a float, the time or bar_index at point A.
// @returns float
export line_time_rate (float point_c, float point_b, float point_a) => //{
//  C   A
//   \ /
//    B
    (0 - (point_a - point_b)) / (point_b - point_c)
//}

// @function Check if value is within min/max range of tolerance.
// @param value float, value to check tolerance.
// @param min float, minimum value in range of tolerance.
// @param max float, maximum value in range of tolerance.
// @returns bool
export is_inrange (float value, float min, float max) => //{
    value <= max and value >= min
//}


//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//  rates are expected in a backwards manner and of negative value.           ||
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//  G   E   C   A                                                             ||
//   \ / \ / \ /                                                              ||
//    F   D   B                                                               ||
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

// @function Check if the rate(s) correspond to pattern ("Harmonic Triangle").
// @param rate_cba float, percent rate of the triangle CBA. expects a negative rate.
// @param margin_of_error float, percent rate of expected error margin, default 0.05(5%).
// @returns bool
export isHarmonicTriangle (float rate_cba, float margin_of_error=0.05) => //{
    bool _return = false
    // return true if its rate is near a harmonic rate:
    // this is analogous with the fibonnaci sequence, but is capped at (-5 --> 12) to limit resource usage
    // 0.146, 0.236, 0.382, 0.618, 1, 1.618, 2.618, 4.236, 6.854, 11.089, 17.942, 29.03
    for _i = 1 to 12
        // calculate the derivative of phi sequence:
        float _phid = 0 - math.pow(math.phi, -5.0 + _i)
        if is_inrange(rate_cba, (_phid - margin_of_error), (_phid + margin_of_error))
            _return := true
            break
    _return
//}

// @function Check if the rate(s) correspond to pattern ("2Tap", 'Double Top / Bottom').
// @param rate_cba float, percent rate of the triangle CBA. expects a negative rate.
// @param margin_of_error float, percent rate of expected error margin, default 0.05(5%).
// @returns bool
export is2Tap (float rate_cba, float margin_of_error=0.05) => //{
    is_inrange(rate_cba, -1.000 - margin_of_error, -1.000 + margin_of_error)
//}

// @function Check if the rate(s) correspond to pattern ("3Tap", "Triple Top / Bottom").
// @param rate_edc float, percent rate of the triangle EDC. expects a negative rate.
// @param rate_cba float, percent rate of the triangle CBA. expects a negative rate.
// @param margin_of_error float, percent rate of expected error margin, default 0.05(5%).
// @returns bool
export is3Tap (float rate_edc, float rate_cba, float margin_of_error=0.05) => //{
    _is_edc = is_inrange(rate_edc, -1.000 - margin_of_error, -1.000 + margin_of_error)
    _is_cba = is_inrange(rate_cba, -1.000 - margin_of_error, -1.000 + margin_of_error)
    _is_edc and _is_cba
//}

// @function Check if the rate(s) correspond to pattern ("4Tap", "Quadruple Top / Bottom").
// @param rate_gfe float, percent rate of the triangle GFE. expects a negative rate.
// @param rate_edc float, percent rate of the triangle EDC. expects a negative rate.
// @param rate_cba float, percent rate of the triangle CBA. expects a negative rate.
// @param margin_of_error float, percent rate of expected error margin, default 0.05(5%).
// @returns bool
export is4Tap (float rate_gfe, float rate_edc, float rate_cba, float margin_of_error=0.05) => //{
    _is_gfe = is_inrange(rate_gfe, -1.000 - margin_of_error, -1.000 + margin_of_error)
    _is_edc = is_inrange(rate_edc, -1.000 - margin_of_error, -1.000 + margin_of_error)
    _is_cba = is_inrange(rate_cba, -1.000 - margin_of_error, -1.000 + margin_of_error)
    _is_gfe and _is_edc and _is_cba
//}

// @function Check if the rate(s) correspond to pattern ("AB=CD").
// @param rate_cba float, percent rate of the triangle CBA. expects a negative rate.
// @param rate_dcb float, percent rate of the triangle DCB. expects a negative rate.
// @param margin_of_error float, percent rate of expected error margin, default 0.05(5%).
// @returns bool
export isABCD (float rate_cba, float rate_dcb, float margin_of_error=0.05) => //{
    _is_cba = is_inrange(rate_cba, -1.618 - margin_of_error, -1.270 + margin_of_error)
    _is_dcb = is_inrange(rate_dcb, -0.786 - margin_of_error, -0.618 + margin_of_error)
    _is_cba and _is_dcb
//}

// @function Check if the rate(s) correspond to pattern ("Bat").
// @param rate_edc float, percent rate of the triangle EDC. expects a negative rate.
// @param rate_dcb float, percent rate of the triangle DCB. expects a negative rate.
// @param rate_cba float, percent rate of the triangle CBA. expects a negative rate.
// @param rate_eda float, percent rate of the triangle EDA. expects a negative rate.
// @param margin_of_error float, percent rate of expected error margin, default 0.05(5%).
// @returns bool
export isBat (float rate_edc, float rate_dcb, float rate_cba, float rate_eda, float margin_of_error=0.05) => //{
    _is_edc = is_inrange(rate_edc, -0.500 - margin_of_error, -0.382 + margin_of_error)
    _is_dcb = is_inrange(rate_dcb, -0.886 - margin_of_error, -0.382 + margin_of_error)
    _is_cba = is_inrange(rate_cba, -2.618 - margin_of_error, -1.618 + margin_of_error)
    _is_eda = is_inrange(rate_eda, -0.886 - margin_of_error, -0.886 + margin_of_error)
    _is_edc and _is_dcb and _is_cba and _is_eda
//}

// @function Check if the rate(s) correspond to pattern ("Butterfly").
// @param rate_edc float, percent rate of the triangle EDC. expects a negative rate.
// @param rate_dcb float, percent rate of the triangle DCB. expects a negative rate.
// @param rate_cba float, percent rate of the triangle CBA. expects a negative rate.
// @param rate_eda float, percent rate of the triangle EDA. expects a negative rate.
// @param margin_of_error float, percent rate of expected error margin, default 0.05(5%).
// @returns bool
export isButterfly (float rate_edc, float rate_dcb, float rate_cba, float rate_eda, float margin_of_error=0.05) => //{
    _is_edc = is_inrange(rate_edc, -0.786 - margin_of_error, -0.786 + margin_of_error)
    _is_dcb = is_inrange(rate_dcb, -0.886 - margin_of_error, -0.382 + margin_of_error)
    _is_cba = is_inrange(rate_cba, -2.618 - margin_of_error, -1.618 + margin_of_error)
    _is_eda = is_inrange(rate_eda, -1.618 - margin_of_error, -1.270 + margin_of_error)
    _is_edc and _is_dcb and _is_cba and _is_eda
//}

// @function Check if the rate(s) correspond to pattern ("Gartley").
// @param rate_edc float, percent rate of the triangle EDC. expects a negative rate.
// @param rate_dcb float, percent rate of the triangle DCB. expects a negative rate.
// @param rate_cba float, percent rate of the triangle CBA. expects a negative rate.
// @param rate_eda float, percent rate of the triangle EDA. expects a negative rate.
// @param margin_of_error float, percent rate of expected error margin, default 0.05(5%).
// @returns bool
export isGartley (float rate_edc, float rate_dcb, float rate_cba, float rate_eda, float margin_of_error=0.05) => //{
    _is_edc = is_inrange(rate_edc, -0.618 - margin_of_error, -0.618 + margin_of_error)
    _is_dcb = is_inrange(rate_dcb, -0.886 - margin_of_error, -0.382 + margin_of_error)
    _is_cba = is_inrange(rate_cba, -2.618 - margin_of_error, -1.130 + margin_of_error)
    _is_eda = is_inrange(rate_eda, -0.786 - margin_of_error, -0.786 + margin_of_error)
    _is_edc and _is_dcb and _is_cba and _is_eda
//}

// @function Check if the rate(s) correspond to pattern ("Crab").
// @param rate_edc float, percent rate of the triangle EDC. expects a negative rate.
// @param rate_dcb float, percent rate of the triangle DCB. expects a negative rate.
// @param rate_cba float, percent rate of the triangle CBA. expects a negative rate.
// @param rate_eda float, percent rate of the triangle EDA. expects a negative rate.
// @param margin_of_error float, percent rate of expected error margin, default 0.05(5%).
// @returns bool
export isCrab (float rate_edc, float rate_dcb, float rate_cba, float rate_eda, float margin_of_error=0.05) => //{
    _is_edc = is_inrange(rate_edc, -0.886 - margin_of_error, -0.886 + margin_of_error)
    _is_dcb = is_inrange(rate_dcb, -0.886 - margin_of_error, -0.382 + margin_of_error)
    _is_cba = is_inrange(rate_cba, -3.618 - margin_of_error, -2.000 + margin_of_error)
    _is_eda = is_inrange(rate_eda, -1.618 - margin_of_error, -1.618 + margin_of_error)
    _is_edc and _is_dcb and _is_cba and _is_eda
//}

// @function Check if the rate(s) correspond to pattern ("Shark").
// @param rate_edc float, percent rate of the triangle EDC. expects a negative rate.
// @param rate_dcb float, percent rate of the triangle DCB. expects a negative rate.
// @param rate_cba float, percent rate of the triangle CBA. expects a negative rate.
// @param rate_eda float, percent rate of the triangle EDA. expects a negative rate.
// @param margin_of_error float, percent rate of expected error margin, default 0.05(5%).
// @returns bool
export isShark(float rate_edc, float rate_dcb, float rate_cba, float rate_eda, float margin_of_error=0.05) => //{
    _is_edc = is_inrange(rate_edc, -0.886 - margin_of_error, -0.886 + margin_of_error)
    _is_dcb = is_inrange(rate_dcb, -1.618 - margin_of_error, -1.130 + margin_of_error)
    _is_cba = is_inrange(rate_cba, -2.240 - margin_of_error, -1.270 + margin_of_error)
    _is_eda = is_inrange(rate_eda, -1.130 - margin_of_error, -0.886 + margin_of_error)
    _is_edc and _is_dcb and _is_cba and _is_eda
//}

// @function Check if the rate(s) correspond to pattern ("5o").
// @param rate_edc float, percent rate of the triangle EDC. expects a negative rate.
// @param rate_dcb float, percent rate of the triangle DCB. expects a negative rate.
// @param rate_cba float, percent rate of the triangle CBA. expects a negative rate.
// @param rate_eda float, percent rate of the triangle EDA. expects a negative rate.
// @param margin_of_error float, percent rate of expected error margin, default 0.05(5%).
// @returns bool
export is5o(float rate_edc, float rate_dcb, float rate_cba, float rate_eda, float margin_of_error=0.05) => //{
    _is_edc = is_inrange(rate_edc, -1.618 - margin_of_error, -1.130 + margin_of_error)
    _is_dcb = is_inrange(rate_dcb, -2.240 - margin_of_error, -1.618 + margin_of_error)
    _is_cba = is_inrange(rate_cba, -0.500 - margin_of_error, -0.500 + margin_of_error)
    _is_eda = is_inrange(rate_eda, -0.236 - margin_of_error, +0.236 + margin_of_error)
    _is_edc and _is_dcb and _is_cba and _is_eda
//}

// @function Check if the rate(s) correspond to pattern ("Wolfe").
// @param rate_edc float, percent rate of the triangle EDC. expects a negative rate.
// @param rate_dcb float, percent rate of the triangle DCB. expects a negative rate.
// @param rate_cba float, percent rate of the triangle CBA. expects a negative rate.
// @param rate_eda float, percent rate of the triangle EDA. expects a negative rate.
// @param margin_of_error float, percent rate of expected error margin, default 0.05(5%).
// @returns bool
export isWolfe(float rate_edc, float rate_dcb, float rate_cba, float rate_eda, float margin_of_error=0.05) => //{
    _is_edc = is_inrange(rate_edc, -1.618 - margin_of_error, -1.270 + margin_of_error)
    _is_dcb = is_inrange(rate_dcb, -5.000 - margin_of_error, -0.000 + margin_of_error)
    _is_cba = is_inrange(rate_cba, -1.618 - margin_of_error, -1.270 + margin_of_error)
    _is_eda = is_inrange(rate_eda, -5.000 - margin_of_error, -0.000 + margin_of_error)
    _is_edc and _is_dcb and _is_cba and _is_eda
//}

// @function Check if the rate(s) correspond to pattern ("3 Driver").
// @param rate_edc float, percent rate of the triangle EDC. expects a negative rate.
// @param rate_dcb float, percent rate of the triangle DCB. expects a negative rate.
// @param rate_cba float, percent rate of the triangle CBA. expects a negative rate.
// @param rate_eda float, percent rate of the triangle EDA. expects a negative rate.
// @param margin_of_error float, percent rate of expected error margin, default 0.05(5%).
// @returns bool
export is3Driver(float rate_edc, float rate_dcb, float rate_cba, float rate_eda, float margin_of_error=0.05) => //{
    _is_edc = is_inrange(rate_edc, -1.618 - margin_of_error, -1.270 + margin_of_error)
    _is_dcb = is_inrange(rate_dcb, -5.000 - margin_of_error, -0.000 + margin_of_error)
    _is_cba = is_inrange(rate_cba, -1.618 - margin_of_error, -1.270 + margin_of_error)
    _is_eda = is_inrange(rate_eda, -5.000 - margin_of_error, -0.000 + margin_of_error)
    _is_edc and _is_dcb and _is_cba and _is_eda
//}

// @function Check if the rate(s) correspond to pattern ("Contracting Triangle").
// @param rate_edc float, percent rate of the triangle EDC. expects a negative rate.
// @param rate_dcb float, percent rate of the triangle DCB. expects a negative rate.
// @param rate_cba float, percent rate of the triangle CBA. expects a negative rate.
// @param rate_eda float, percent rate of the triangle EDA. expects a negative rate.
// @param margin_of_error float, percent rate of expected error margin, default 0.05(5%).
// @returns bool
export isConTria(float rate_edc, float rate_dcb, float rate_cba, float rate_eda, float margin_of_error=0.05) => //{
    _is_edc = is_inrange(rate_edc, -0.886 - margin_of_error, -0.236 + margin_of_error)
    _is_dcb = is_inrange(rate_dcb, -0.886 - margin_of_error, -0.236 + margin_of_error)
    _is_cba = is_inrange(rate_cba, -0.886 - margin_of_error, -0.236 + margin_of_error)
    _is_eda = is_inrange(rate_eda, -0.886 - margin_of_error, -0.236 + margin_of_error)
    _is_edc and _is_dcb and _is_cba and _is_eda
//}

// @function Check if the rate(s) correspond to pattern ("Expanding Triangle").
// @param rate_edc float, percent rate of the triangle EDC. expects a negative rate.
// @param rate_dcb float, percent rate of the triangle DCB. expects a negative rate.
// @param rate_cba float, percent rate of the triangle CBA. expects a negative rate.
// @param rate_eda float, percent rate of the triangle EDA. expects a negative rate.
// @param margin_of_error float, percent rate of expected error margin, default 0.05(5%).
// @returns bool
export isExpTria(float rate_edc, float rate_dcb, float rate_cba, float rate_eda, float margin_of_error=0.05) => //{
    _is_edc = is_inrange(rate_edc, -2.618 - margin_of_error, -1.125 + margin_of_error)
    _is_dcb = is_inrange(rate_dcb, -2.618 - margin_of_error, -1.125 + margin_of_error)
    _is_cba = is_inrange(rate_cba, -2.618 - margin_of_error, -1.125 + margin_of_error)
    _is_eda = is_inrange(rate_eda, -2.618 - margin_of_error, -1.125 + margin_of_error)
    _is_edc and _is_dcb and _is_cba and _is_eda
//}

// @function Check if the rate(s) correspond to pattern ("Head and Shoulders").
// @param rate_fed float, percent rate of the triangle FED. expects a negative rate.
// @param rate_feb float, percent rate of the triangle FEB. expects a negative rate.
// @param rate_edc float, percent rate of the triangle EDC. expects a negative rate.
// @param rate_dcb float, percent rate of the triangle DCB. expects a negative rate.
// @param rate_cba float, percent rate of the triangle CBA. expects a negative rate.
// @param rate_eda float, percent rate of the triangle EDA. expects a negative rate.
// @param margin_of_error float, percent rate of expected error margin, default 0.05(5%).
// @returns bool
export isHnS(float rate_fed, float rate_feb, float rate_edc, float rate_dcb, float rate_cba, float rate_eda, float margin_of_error=0.05) => //{
    _is_fed = is_inrange(rate_fed, -0.618 - margin_of_error, -0.090 + margin_of_error)
    _is_feb = is_inrange(rate_feb, -0.886 - margin_of_error, -0.090 + margin_of_error)
    _is_edc = is_inrange(rate_edc, -9.999 - margin_of_error, -1.000 + margin_of_error)
    _is_dcb = is_inrange(rate_dcb, -1.250 - margin_of_error, -0.750 + margin_of_error)
    _is_cba = is_inrange(rate_cba, -0.886 - margin_of_error, -0.090 + margin_of_error)
    _is_eda = is_inrange(rate_eda, -1.618 - margin_of_error, -0.090 + margin_of_error)
    _is_fed and _is_feb and _is_edc and _is_eda and _is_dcb and _is_cba
//}

Overview

HarmonicPattern includes utilities to calculate price and time rates between consecutive pattern points, check if values fall within specified ranges with customizable margins of error, and evaluate specific pattern configurations such as double tops or bottoms and harmonic triangle ratios derived from Fibonacci powers.

Details

Published by

ricardosantos79 on GitHub

Code credit

RicardoSantos, as written in the script's header

Type

Library, Pine Script v5

Draws

Library (no plots)

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

KernelFunctions

by artificial-intelligence-edge
75 stars

KernelFunctions is a Pine Script v5 library that exports kernel weight functions for Nadaraya-Watson style estimation. It calculates a Rational Quadratic kernel, a Gaussian kernel based on a radial basis function, and a periodic kernel that models repeating be

Library v5
Updated 2 days ago · MIT GitHub repository of KernelFunctions

Ta

by nersent
29 stars

This Pine Script v5 library exports thin wrappers around built-in technical analysis functions. It covers moving averages, momentum measures such as RSI and CCI, volatility tools including ATR and standard deviation, range extremes, cross detection, and runnin

Library v5
Updated 11 months ago · Custom licence GitHub repository of Ta

Bands

by heyphat
26 stars

This library exports a Band type and functions that build a simple-moving-average basis with upper and lower envelopes from standard deviation. It classifies a source as above, below, or inside those envelopes, measures band width, and scales an imported z-sco

Library v6 Volatility & bandsMoving averages
Updated 2 months ago · AGPL-3.0 GitHub repository of Bands

Mylib

by ferranbt
21 stars

This script serves as a minimal utility library written in Pine Script v6. It provides an exported helper function that calculates the sum of two numeric arguments. As a reusable code library, it produces no visual plots, charts, or trading signals, and it con

Library v6
Updated 7 weeks ago · MPL-2.0 GitHub repository of Mylib

HLevel

by stupenkov
14 stars

HLevel is a Pine Script v5 utility library designed to manage horizontal price levels for cryptocurrency charts. It defines custom types for pivots and levels, along with methods to initialize, add, track, and expire price levels based on pivot high and pivot

Library v5
Updated 3 years ago · None stated GitHub repository of HLevel

Zignaly2Beta

by ruckard
13 stars

This library provides helper functions to format and build JSON webhook alert payloads for the Zignaly automated trading platform. It constructs formatted messages containing parameters such as trading pair, exchange, account type, leverage, margin mode, hedge

Library v5
Updated 1 year ago · None stated GitHub repository of Zignaly2Beta

More libraries

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers