Library · Pine Script v5 · Library

CandleAnalysis

by MarcosACH

CandleAnalysis is a Pine Script library providing exportable helper functions for detailed candlestick analysis in custom scripts.

View on GitHub

GitHub stars1on the repository
Pine versionv5runs as is
Lines of code113
Settings0inputs
LicenceNone statedfile: MPL-2.0
Last update1 year ago9 Jul 2025

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

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

candle_analysis.pinescript
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MarcosACH
// Created by Marcos Achaval
// Last updated: July 13, 2024

//@version=5
// @description A collection of frequently used candle analysis functions in my scripts.
library("CandleAnalysis")
import MarcosACH/MarketAnalysis/1 as MktA

// @function Checks if a specific bar is bullish.
// @param barsBack (int) The number of bars to look back. The default is 0 (current bar).
// @returns True if the bar is bullish, otherwise returns false.
export isBullish(int barsBack = 0) =>
    close[barsBack] > open[barsBack]

// @function Checks if a specific bar is bearish.
// @param barsBack (int) The number of bars to look back. The default is 0 (current bar).
// @returns True if the bar is bearish, otherwise returns false.
export isBearish(int barsBack = 0) =>
    close[barsBack] < open[barsBack]

// @function Checks if a specific bar is break even.
// @param barsBack (int) The number of bars to look back. The default is 0 (current bar).
// @returns True if the bar is break even, otherwise returns false.
export isBE(int barsBack = 0) =>
    close[barsBack] == open[barsBack]

// @function Calculates a specific candle's body size.
// @param barsBack (int) The number of bars to look back. The default is 0 (current bar).
// @param inPriceChg (bool) True to return the body size as a price change value. The default is false (in points).
// @returns The candle's body size in points.
export getBodySize(int barsBack = 0, bool inPriceChg = false) => 
    if inPriceChg
        math.abs(close[barsBack] - open[barsBack])
    else
        math.abs(close[barsBack] - open[barsBack]) / syminfo.mintick

// @function Calculates a specific candle's top wick size.
// @param barsBack (int) The number of bars to look back. The default is 0 (current bar).
// @param inPriceChg (bool) True to return the wick size as a price change value. The default is false (in points).
// @returns The candle's top wick size in points.
export getTopWickSize(int barsBack = 0, bool inPriceChg = false) => 
    if inPriceChg
        math.abs(high[barsBack] - (isBullish(barsBack) or isBE(barsBack) ? close[barsBack] : open[barsBack]))
    else
        math.abs(high[barsBack] - (isBullish(barsBack) or isBE(barsBack) ? close[barsBack] : open[barsBack])) / syminfo.mintick

// @function Calculates a specific candle's bottom wick size.
// @param barsBack (int) The number of bars to look back. The default is 0 (current bar).
// @param inPriceChg (bool) True to return the wick size as a price change value. The default is false (in points).
// @returns The candle's bottom wick size in points.
export getBottomWickSize(int barsBack = 0, bool inPriceChg = false) => 
    if inPriceChg
        math.abs((isBearish(barsBack) ? close[barsBack] : open[barsBack]) - low[barsBack])
    else
        math.abs((isBearish(barsBack) ? close[barsBack] : open[barsBack]) - low[barsBack]) / syminfo.mintick

// @function Calculates a specific candle's body size as a percentage of its entire size including its wicks.
// @param barsBack (int) The number of bars to look back. The default is 0 (current bar).
// @returns The candle's body size percentage.
export getBodyPercent(int barsBack = 0) => 
  math.abs(open[barsBack] - close[barsBack]) / math.abs(high[barsBack] - low[barsBack]) * 100

// @function Checks if a specific bar is a hammer candle based on a given fibonacci level.
// @param fib (float) The fibonacci level to base candle's body on. The default is 0.382.
// @param bullish (bool) True if the candle must to be green. The default is false.
// @param barsBack (int) The number of bars to look back. The default is 0 (current bar).
// @returns True if the bar matches the requirements of a hammer candle, otherwise returns false.
export isHammer(float fib = 0.382, bool bullish = false, int barsBack = 0) =>
    bullFib = MktA.bullFibRet(low[barsBack], high[barsBack], fib)
    bodyLow = isBullish(barsBack) or isBE(barsBack) ? open[barsBack] : close[barsBack]
    bodyLow >= bullFib and (not bullish or isBullish(barsBack) or isBE(barsBack))

// @function Checks if a specific bar is a shooting star candle based on a given fibonacci level.
// @param fib (float) The fibonacci level to base candle's body on. The default is 0.382.
// @param bearish (bool) True if the candle must to be red. The default is false.
// @param barsBack (int) The number of bars to look back. The default is 0 (current bar).
// @returns True if the bar matches the requirements of a shooting star candle, otherwise returns false.
export isShootingStar(float fib = 0.382, bool bearish = false, int barsBack = 0) =>
    bearFib = MktA.bearFibRet(low[barsBack], high[barsBack], fib)
    bodyHigh = isBullish(barsBack) or isBE(barsBack) ? close[barsBack] : open[barsBack]
    bodyHigh <= bearFib and (not bearish or isBearish(barsBack))
    
// @function Checks if a specific bar is a doji candle based on a given wick and body size.
// @param wickSize (float) The maximum top wick size compared to the bottom and vice versa. The default is 1.5.
// @param bodySize (bool) The maximum body size as a percentage compared to the entire candle size. The default is 5.
// @param barsBack (int) The number of bars to look back. The default is 0 (current bar).
// @returns True if the bar matches the requirements of a doji candle.
export isDoji(float wickSize = 1.5, float bodySize = 5, int barsBack = 0) =>
    getTopWickSize(barsBack) <= getBottomWickSize(barsBack) * wickSize and getBottomWickSize(barsBack) <= getTopWickSize(barsBack) * wickSize and getBodyPercent(barsBack) <= bodySize

// @function Checks if a specific bar is a bullish engulfing candle.
// @param gapTolernace (int) The maximum offset between the close of the engulfed candle and the open of the engulfing candle. This is a percentage of the engulfed candle. The default is 0.
// @param rejectionWickSize (int) The maximum top wick size compared to the body as a percentage. The default is 10.
// @param engulfWick (bool) True if the engulfed candle's wick requires to be engulfed as well. The default is false.
// @param barsBack (int) The number of bars to look back. The default is 0 (current bar).
// @returns True if the bar matches the requirements of a bullish engulfing candle.
export isBullishEC(int gapTolerance = 0, int rejectionWickSize = 10, bool engulfWick = false, int barsBack = 0) =>
  (isBearish(barsBack + 1) or isBE(barsBack + 1) and close[barsBack] > open[barsBack + 1] and open[barsBack] <= close[barsBack + 1] + gapTolerance) and 
  (not engulfWick or close[barsBack] >= high[barsBack + 1]) and 
  (getTopWickSize() / getBodySize() * 100 < rejectionWickSize)

// @function Checks if a specific bar is a bearish engulfing candle.
// @param gapTolernace (int) The maximum offset between the close of the engulfed candle and the open of the engulfing candle. This is a percentage of the engulfed candle. The default is 0.
// @param rejectionWickSize (int) The maximum bottom wick size compared to the body as a percentage. The default is 10.
// @param engulfWick (bool) True if the engulfed candle's wick requires to be engulfed as well. The default is false.
// @param barsBack (int) The number of bars to look back. The default is 0 (current bar).
// @returns True if the bar matches the requirements of a bearish engulfing candle.
export isBearishEC(int gapTolerance = 0, int rejectionWickSize = 10, bool engulfWick = false, int barsBack = 0) =>
  (isBullish(barsBack + 1) or isBE(barsBack + 1) and close[barsBack] < open[barsBack + 1] and open[barsBack] >= close[barsBack + 1] - gapTolerance) and 
  (not engulfWick or close[barsBack] <= low[barsBack + 1]) and
  (getBottomWickSize() / getBodySize() * 100 < rejectionWickSize)

Overview

CandleAnalysis evaluates whether a specified bar is bullish, bearish, or break-even, and computes metrics including body size, top wick size, and bottom wick size in either tick units or absolute price change. Additionally, the library calculates the candle body percentage relative to its total high-low range and provides pattern recognition checks, such as identifying hammer candlestick formations.

Details

Published by

MarcosACH on GitHub

Type

Library, Pine Script v5

Draws

Library (no plots)

Category

Candlestick patterns

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.

Topics

librariespinescripttradingvewtradingviewpinescript-libraries

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

261 stars

AIActivationFunctionsBipolarSigmoid is a Pine Script library that provides mathematical implementations of the bipolar sigmoid activation function and its derivatives for machine learning algorithms. It calculates bipolar sigmoid values bounded between -1 and

Library v5 AI / machine learning
Updated 4 years ago · None stated GitHub repository of AIActivationFunctionsBipolarSigmoid

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

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