PINE LIBRARY

Updated

When composing scripts, it is often necessary to manipulate the math around the OHLC. At times, you want a scalar (absolute) value others you want a vector (+/-). Sometimes you want the open - close and sometimes you want just the positive number of the body size. You might want it in ticks or you might want it in points or you might want in percentages. And every time you try to put it together you waste precious time and brain power trying to think about how to properly structure what you're looking for. Not to mention it's normally not that aesthetically pleasing to look at in the code.

So, this fixes all of that.

Using this library. A function like 'pd.pt(_exp)' can call any kind of candlestick math you need. The function returns the candlestick math you define using particular expressions.

Candle Math Functions Include:

The expressions are simple (simple strings that is) and I did my best to make them sensible, generally using just the ohlc abreviations. I also included uw, lw, bd, and rg for when you're just trying to pull a candle component out. That way you don't have to think about which of the ohlc you're trying to get just use pd.tick("uw") and now the variable is assigned the length of the upper wick, absolute value, in ticks. If you wanted the vector in pts its pd.vpt("uw"). It also makes changing things easy too as I write it out.

Expression List:

Combinations

"oh" = open - high

"ol" = open - low

"oc" = open - close

"ho" = high - open

"hl" = high - low

"hc" = high - close

"lo" = low - open

"lh" = low - high

"lc" = low - close

"co" = close - open

"ch" = close - high

"cl" = close - low

Candle Components

"uw" = Upper Wick

"bd" = Body

"lw" = Lower Wick

"rg" = Range [Pt() Tick() Only]

Pct() Only

"scp" = Scalar Close Position

"sop" = Scalar Open Position

"vcp" = Vector Close Position

"vop" = Vector Open Position

The attributes are going to be available in the pop up dialogue when you mouse over the function, so you don't really have to remember them. I tried to make that look as efficient as possible. You'll notice it follows the OHLC pattern. Thus, "oh" precedes "ho" (heyo) because "O" would be first in the OHLC. Its a way to help find the expression you're looking for quickly. Like looking through an alphabetized list for traders.

There is a copy/paste console friendly helper list in the script itself.

This is the original reason I started writing this. These concepts place a rating/value on the bar based on candle attributes in one number. These formulas put a open or close value in a percentile of the bar relative to another aspect of the bar.

Scalar Position: The position of the price attribute relative to the scale of the bar range (high - low)

- Example: high = 100. low = 0. close = 25.
- (A) Measure price distance C-L. How high above the low did the candle close (e.g. close - low = 25)
- (B) Divide by bar range (high - low). 25 / (100 - 0) = .25
- Explaination: The candle closed at the 25th percentile of the bar range given the bar range low = 0 and bar range high = 100.
- Formula: scp = (close - low) / (high - low)
*[For sop, substitute open for close.]*

Vector Position: The position of the price attribute relative to the scale of the bar midpoint (Vector Position at hl2 = 0)

- Example: high = 100. low = 0. close = 25.
- (A) Measure Price distance C-L: How high above the low did the candle close (e.g. close - low = 25)
- (B) Measure Price distance H-C: How far below the high did the candle close (e.g. high - close = 75)
- (C) Take Difference: A - B = C = -50
- (D) Divide by bar range (high - low). -50 / (100 - 0) = -0.50
- Explaination: Candle close at the midpoint between hl2 and the low.
- Formula: vcp = { [(close - low) - (high - close)] / (high - low) }
*[For vop, substitute open for close.]*

Thank you for checking this out. I hope no one else has already done this (because it took half the day) and I hope you find value in it. Be well. Trade well.

Library

Price Displacement

Parameters:

Returns: Point size of given expression as an absolute value.

Parameters:

Returns: Point size of given expression as a vector.

Parameters:

Returns: Tick size of given expression as an absolute value.

Parameters:

Returns: Tick size of given expression as a vector.

Parameters:

Returns: Percent size of given expression as decimal.

Parameters:

Returns: Percent size of given expression as decimal.

Release Notes

v2 - More shortcut bars added. More to come.Added:

Overlap

Parameters:

Returns: Overlap of current bar with prior bar (0.xx)

Inside Bar Overlap

Parameters:

Returns: Percent of Bar that is outside prior bar (0.xx)

Inside Bar

Parameters:

Returns: (bool) true if bar is Inside Bar

Inside Bar High

Parameters:

Returns: (bool) true if bar is Inside Bar w/outside high

Inside Bar Low

Parameters:

Returns: (bool) true if bar is Inside Bar w/outside low

Outside Bar

Parameters:

Returns: (bool) true if bar is Outside Bar

Outside Bar - Close in Range

Parameters:

Returns: (bool) true if bar is Outside Bar but closes in prior bar range

Bull Bar

Parameters:

Returns: (bool) true if close > open

Bear Bar

Parameters:

Returns: (bool) true if close < open

close > high[off]

Parameters:

Returns: (bool) true if close > high[off]

close < high[off]

Parameters:

Returns: (bool) true if close < high[off]

close > low[off]

Parameters:

Returns: (bool) true if close > low[off]

close < low>[off]

Parameters:

Returns: (bool) true if close < low>[off]

high < open[off]

Parameters:

Returns: (bool) true if high < open[off]

low > open[off]

Parameters:

Returns: (bool) true if low > open[off]

Release Notes

v3Replaced specific "greater than" and "less than" functions for a generic pb_gt() and pb_lt() function. e.g pb_gt(close,open) = close > open[1] | pbe_gt(close,open) = close >= open[1]

Added:

a > b[off]

Parameters:

Returns: (bool) true if a > b[off]. Default: offset = 1.

a >= b[off]

Parameters:

Returns: (bool) true if a >= b[off]. Default: offset = 1.

a < b[off]

Parameters:

Returns: (bool) true if a > b[off]. Default: offset = 1.

a <= b[off]

Parameters:

Returns: (bool) true if a <= b[off]. Default: offset = 1.

Price At Scalar Position

Parameters:

Returns: the price of the corresponding Scalar Price Posiiton on current candle from argument rounded to mintick

Removed:

close > high[off]

close < high[off]

close > low[off]

close < low>[off]

high < open[off]

low > open[off]

Release Notes

I made some changes to simplify things. Make there less to remember. Also added greater than (or equal to)/less than (or equal to) functions to easily compare prices of prior candlesticks wherein the distance is fixed. Default is prior bar Added:

a >= b[off]

Parameters:

Returns: (bool) true if a > b[off]. Defaults: _off = 1. _e = false

a <= b[off]

Parameters:

Returns: (bool) true if a < b[off]. Defaults: _off = 1. _e = false

open > open[off]

Parameters:

Returns: (bool) true if open > open[off]

open > high[off]

Parameters:

Returns: (bool) true if open > high[off]

open > low[off]

Parameters:

Returns: (bool) true if open > low[off]

open > close[off]

Parameters:

Returns: (bool) true if open > close[off]

open < open[off]

Parameters:

Returns: (bool) true if open < open[off]

open < high[off]

Parameters:

Returns: (bool) true if open < high[off]

open < low[off]

Parameters:

Returns: (bool) true if open < low[off]

open < close[off]

Parameters:

Returns: (bool) true if open < close[off]

high > open[off]

Parameters:

Returns: (bool) true if high > open[off]

high > high[off]

Parameters:

Returns: (bool) true if high > high[off]

high > low[off]

Parameters:

Returns: (bool) true if high > low[off]

high > close[off]

Parameters:

Returns: (bool) true if high > close[off]

high < open[off]

Parameters:

Returns: (bool) true if high < open[off]

high < high[off]

Parameters:

Returns: (bool) true if high < high[off]

high < low[off]

Parameters:

Returns: (bool) true if high < low[off]

high < close[off]

Parameters:

Returns: (bool) true if high < close[off]

low > open[off]

Parameters:

Returns: (bool) true if low > open[off]

low > high[off]

Parameters:

Returns: (bool) true if low > high[off]

low > low[off]

Parameters:

Returns: (bool) true if low > low[off]

low > close[off]

Parameters:

Returns: (bool) true if low > close[off]

low < open[off]

Parameters:

Returns: (bool) true if low < open[off]

low < high[off]

Parameters:

Returns: (bool) true if low < high[off]

low < low[off]

Parameters:

Returns: (bool) true if low < low[off]

low < close[off]

Parameters:

Returns: (bool) true if low < close[off]

close > open[off]

Parameters:

Returns: (bool) true if close > open[off]

close > high[off]

Parameters:

Returns: (bool) true if close > high[off]

close > low[off]

Parameters:

Returns: (bool) true if close > low[off]

close > close[off]

Parameters:

Returns: (bool) true if close > close[off]

close < open[off]

Parameters:

Returns: (bool) true if close < open[off]

close < high[off]

Parameters:

Returns: (bool) true if close < high[off]

close < low[off]

Parameters:

Returns: (bool) true if close < low[off]

close < close[off]

Parameters:

Returns: (bool) true if close < close[off]

Updated:

Price At Scalar Position of Bar

Parameters:

Returns: the price of the corresponding Scalar Price Posiiton on current candle from argument rounded to mintick

Bull Bar

Parameters:

Returns: (bool) true if close > open

Bear Bar

Parameters:

Returns: (bool) true if close < open

Removed:

a > b[off]

a >= b[off]

a < b[off]

a <= b[off]

Release Notes

v5Small update to clean some things up. There's not astronomical benefit to v5 over v4. I would imagine it would be less error prone. That said, the other add is the "index." What I've found is that I need to remember what correct arguments are and my indicators are too large for the popups to work. So I have to pull up the library and look, which is an inefficient game of Seek and Find (it's not really hiding, so can't be hide and seek). So, I'm adding indices to the top of my libraries. Then, when you pull up the library inline (technically overlaid), then you immediately see a list of available functions and their required parameters. It adds lines but It saves time.

Updated:

Absolute Point Displacement. Point quantity of given size parameters according to _exp.

Parameters:

Returns: Point size of given expression as an absolute value. Expressions - (open-high="oh") (open-low="ol") (open-close="oc") (high-open="ho") (high-low="hl") (high-close="hc") (low-open="lo") (low-high="lh") (low-close="lc") (close-open="co") (close-high="ch") (close-low="cl") (Upper Wick="uw") (Body="bd") (Lower Wick="lw") (Range="rg")

Vector Point Displacement. Point quantity of given size parameters according to _exp.

Parameters:

Returns: Point size of given expression as a vector. Expressions - (open-high="oh") (open-low="ol") (open-close="oc") (high-open="ho") (high-low="hl") (high-close="hc") (low-open="lo") (low-high="lh") (low-close="lc") (close-open="co") (close-high="ch") (close-low="cl") (Upper Wick="uw") (Body="bd") (Lower Wick="lw") (Range="rg")

Absolute Tick Displacement. Tick quantity of given size parameters according to _exp.

Parameters:

Returns: Tick size of given expression as an absolute value. Expressions - (open-high="oh") (open-low="ol") (open-close="oc") (high-open="ho") (high-low="hl") (high-close="hc") (low-open="lo") (low-high="lh") (low-close="lc") (close-open="co") (close-high="ch") (close-low="cl") (Upper Wick="uw") (Body="bd") (Lower Wick="lw") (Range="rg")

Vector Tick Displacement. Tick quantity of given size parameters according to _exp.

Parameters:

Returns: Tick size of given expression as a vector. Expressions -(open-high="oh") (open-low="ol") (open-close="oc") (high-open="ho") (high-low="hl") (high-close="hc") (low-open="lo") (low-high="lh") (low-close="lc") (close-open="co") (close-high="ch") (close-low="cl") (Upper Wick="uw") (Body="bd") (Lower Wick="lw") (Range="rg")

Absolute Percent Displacement (w/rounding overload). Percent quantity of bar range of given size parameters according to _exp. (See Library Notes for additional definitions).

Parameters:

Returns: Percent size of given expression as decimal. Expressions - (open-high="oh") (open-low="ol") (open-close="oc") (high-open="ho") (high-low="hl") (high-close="hc") (low-open="lo") (low-high="lh") (low-close="lc") (close-open="co") (close-high="ch") (close-low="cl") (Upper Wick="uw") (Body="bd") (Lower Wick="lw") (Scalar Close Loc="scp") (Vector Close Loc="vcp") (Scalar Open Loc ="sop") (Vector Open Loc="vop")

Vector Percent Displacement (w/rounding overload). Percent quantity of bar range of given size parameters according to _exp.(See Library Notes for additional definitions).

Parameters:

Returns: Percent size of given expression as decimal. Expressions - (open-high="oh") (open-low="ol") (open-close="oc") (high-open="ho") (high-low="hl") (high-close="hc") (low-open="lo") (low-high="lh") (low-close="lc") (close-open="co") (close-high="ch") (close-low="cl") (Upper Wick="uw") (Body="bd") (Lower Wick="lw") (Scalar Close Loc="scp") (Vector Close Loc="vcp") (Scalar Open Loc ="sop") (Vector Open Loc="vop")

Price At Scalar Percentage of Range

Parameters:

Returns: the price of the corresponding Scalar Price Posiiton on current candle from argument rounded to mintick

In true TradingView spirit, the author has published this Pine code as an open-source library so that other Pine programmers from our community can reuse it. Cheers to the author! You may use this library privately or in other open-source publications, but reuse of this code in a publication is governed by House rules.

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.