# Price Displacement - Candlestick (OHLC) Calculations

アップデート済
A Magical little helper friend for Candle Math.
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:
Points:
pt(_exp) Absolute Point Displacement. Point quantity of given size parameters according to _exp.
vpt(_exp) Vector Point Displacement. Point quantity of given size parameters according to _exp.

Ticks:
tick(_exp) Absolute Tick Displacement. Tick quantity of given size parameters according to _exp.
vtick(_exp) Vector Tick Displacement. Tick quantity of given size parameters according to _exp.

Percentages:
pct(_exp, _prec) Absolute Percent Displacement. (w/rounding overload). Percent quantity of bar range of given size parameters according to _exp.
vpct(_exp, _prec) Vector Percent Displacement (w/rounding overload). Percent quantity of bar range of given size parameters according to _exp.

Expressions You Can Use with Formulas:
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

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.

Additional Notes on the Pct() Only functions:
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 - Non-directional. Absolute Value.
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)

Vector = Directional.
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 = { / (high - low) }

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 "PD"
Price Displacement

pt(_exp) Absolute Point Displacement. Point quantity of given size parameters according to _exp.
Parameters:
_exp: (string) Price Parameter
Returns: Point size of given expression as an absolute value.

vpt(_exp) Vector Point Displacement. Point quantity of given size parameters according to _exp.
Parameters:
_exp: (string) Price Parameter
Returns: Point size of given expression as a vector.

tick(_exp) Absolute Tick Displacement. Tick quantity of given size parameters according to _exp.
Parameters:
_exp: (string) Price Parameter
Returns: Tick size of given expression as an absolute value.

vtick(_exp) Vector Tick Displacement. Tick quantity of given size parameters according to _exp.
Parameters:
_exp: (string) Price Parameter
Returns: Tick size of given expression as a vector.

pct(_exp, _prec) Absolute Percent Displacement (w/rounding overload). Percent quantity of bar range of given size parameters according to _exp.
Parameters:
_exp: (string) Expression
_prec: (int) Overload - Place value precision definition
Returns: Percent size of given expression as decimal.

vpct(_exp, _prec) Vector Percent Displacement (w/rounding overload). Percent quantity of bar range of given size parameters according to _exp.
Parameters:
_exp: (string) Expression
_prec: (int) Overload - Place value precision definition
Returns: Percent size of given expression as decimal.

リリースノート:
v2 - More shortcut bars added. More to come.

overlap(_off)
Overlap
Parameters:
_off: (int) Offset
Returns: Overlap of current bar with prior bar (0.xx)

ibOverlap(_off)
Inside Bar Overlap
Parameters:
_off: (int) Offset
Returns: Percent of Bar that is outside prior bar (0.xx)

ib(_off)
Inside Bar
Parameters:
_off: (int) Offset
Returns: (bool) true if bar is Inside Bar

ibH(_off)
Inside Bar High
Parameters:
_off: (int) Offset
Returns: (bool) true if bar is Inside Bar w/outside high

ibL(_off)
Inside Bar Low
Parameters:
_off: (int) Offset
Returns: (bool) true if bar is Inside Bar w/outside low

ob(_off)
Outside Bar
Parameters:
_off: (int) Offset
Returns: (bool) true if bar is Outside Bar

obR(_off)
Outside Bar - Close in Range
Parameters:
_off: (int) Offset
Returns: (bool) true if bar is Outside Bar but closes in prior bar range

bullbar(_off)
Bull Bar
Parameters:
_off: (int) Offset
Returns: (bool) true if close > open

bearbar(_off)
Bear Bar
Parameters:
_off: (int) Offset
Returns: (bool) true if close < open

pb_CgtH(_off)
close > high
Parameters:
_off: (int) Offset
Returns: (bool) true if close > high

pb_CltH(_off)
close < high
Parameters:
_off: (int) Offset
Returns: (bool) true if close < high

pb_CgtL(_off)
close > low
Parameters:
_off: (int) Offset
Returns: (bool) true if close > low

pb_CltL(_off)
close < low>
Parameters:
_off: (int) Offset
Returns: (bool) true if close < low>

pb_HltO(_off)
high < open
Parameters:
_off: (int) Offset
Returns: (bool) true if high < open

pb_LgtO(_off)
low > open
Parameters:
_off: (int) Offset
Returns: (bool) true if low > open
リリースノート:
v3
Replaced specific "greater than" and "less than" functions for a generic pb_gt() and pb_lt() function. e.g pb_gt(close,open) = close > open | pbe_gt(close,open) = close >= open

pb_gt(_a, _b, _off)
a > b
Parameters:
_a: (float) Queried Attribute
_b: (float) Reference Attribute
_off: (int) Offset
Returns: (bool) true if a > b. Default: offset = 1.

pb_gte(_a, _b, _off)
a >= b
Parameters:
_a: (float) Queried Attribute
_b: (float) Reference Attribute
_off: (int) Offset
Returns: (bool) true if a >= b. Default: offset = 1.

pb_lt(_a, _b, _off)
a < b
Parameters:
_a: (float) Queried Attribute
_b: (float) Reference Attribute
_off: (int) Offset
Returns: (bool) true if a > b. Default: offset = 1.

pb_lte(_a, _b, _off)
a <= b
Parameters:
_a: (float) Queried Attribute
_b: (float) Reference Attribute
_off: (int) Offset
Returns: (bool) true if a <= b. Default: offset = 1.

priceFromSp(_sp)
Price At Scalar Position
Parameters:
_sp: (float) Format: 0.XX Scalar Position of Price
Returns: the price of the corresponding Scalar Price Posiiton on current candle from argument rounded to mintick

Removed:

pb_CgtH(_off)
close > high

pb_CltH(_off)
close < high

pb_CgtL(_off)
close > low

pb_CltL(_off)
close < low>

pb_HltO(_off)
high < open

pb_LgtO(_off)
low > open
リリースノート:
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

gt(_a, _b, _off, _e)
a >= b
Parameters:
_a: (float) Queried Attribute
_b: (float) Reference Attribute
_off: (int) Offset
_e: (bool) (True = >=) | (False = >)
Returns: (bool) true if a > b. Defaults: _off = 1. _e = false

lt(_a, _b, _off, _e)
a <= b
Parameters:
_a: (float) Queried Attribute
_b: (float) Reference Attribute
_off: (int) Offset
_e: (bool) (True = <=) | (False = <)
Returns: (bool) true if a < b. Defaults: _off = 1. _e = false

oGTo(_off)
open > open
Parameters:
_off: (int) Offset
Returns: (bool) true if open > open

oGTh(_off)
open > high
Parameters:
_off: (int) Offset
Returns: (bool) true if open > high

oGTl(_off)
open > low
Parameters:
_off: (int) Offset
Returns: (bool) true if open > low

oGTc(_off)
open > close
Parameters:
_off: (int) Offset
Returns: (bool) true if open > close

oLTo(_off)
open < open
Parameters:
_off: (int) Offset
Returns: (bool) true if open < open

oLTh(_off)
open < high
Parameters:
_off: (int) Offset
Returns: (bool) true if open < high

oLTl(_off)
open < low
Parameters:
_off: (int) Offset
Returns: (bool) true if open < low

oLTc(_off)
open < close
Parameters:
_off: (int) Offset
Returns: (bool) true if open < close

hGTo(_off)
high > open
Parameters:
_off: (int) Offset
Returns: (bool) true if high > open

hGTh(_off)
high > high
Parameters:
_off: (int) Offset
Returns: (bool) true if high > high

hGTl(_off)
high > low
Parameters:
_off: (int) Offset
Returns: (bool) true if high > low

hGTc(_off)
high > close
Parameters:
_off: (int) Offset
Returns: (bool) true if high > close

hLTo(_off)
high < open
Parameters:
_off: (int) Offset
Returns: (bool) true if high < open

hLTh(_off)
high < high
Parameters:
_off: (int) Offset
Returns: (bool) true if high < high

hLTl(_off)
high < low
Parameters:
_off: (int) Offset
Returns: (bool) true if high < low

hLTc(_off)
high < close
Parameters:
_off: (int) Offset
Returns: (bool) true if high < close

lGTo(_off)
low > open
Parameters:
_off: (int) Offset
Returns: (bool) true if low > open

lGTh(_off)
low > high
Parameters:
_off: (int) Offset
Returns: (bool) true if low > high

lGTl(_off)
low > low
Parameters:
_off: (int) Offset
Returns: (bool) true if low > low

lGTc(_off)
low > close
Parameters:
_off: (int) Offset
Returns: (bool) true if low > close

lLTo(_off)
low < open
Parameters:
_off: (int) Offset
Returns: (bool) true if low < open

lLTh(_off)
low < high
Parameters:
_off: (int) Offset
Returns: (bool) true if low < high

lLTl(_off)
low < low
Parameters:
_off: (int) Offset
Returns: (bool) true if low < low

lLTc(_off)
low < close
Parameters:
_off: (int) Offset
Returns: (bool) true if low < close

cGTo(_off)
close > open
Parameters:
_off: (int) Offset
Returns: (bool) true if close > open

cGTh(_off)
close > high
Parameters:
_off: (int) Offset
Returns: (bool) true if close > high

cGTl(_off)
close > low
Parameters:
_off: (int) Offset
Returns: (bool) true if close > low

cGTc(_off)
close > close
Parameters:
_off: (int) Offset
Returns: (bool) true if close > close

cLTo(_off)
close < open
Parameters:
_off: (int) Offset
Returns: (bool) true if close < open

cLTh(_off)
close < high
Parameters:
_off: (int) Offset
Returns: (bool) true if close < high

cLTl(_off)
close < low
Parameters:
_off: (int) Offset
Returns: (bool) true if close < low

cLTc(_off)
close < close
Parameters:
_off: (int) Offset
Returns: (bool) true if close < close

Updated:
priceFromSp(_sp)
Price At Scalar Position of Bar
Parameters:
_sp: (float) Format: 0.XX Scalar Position of Price
Returns: the price of the corresponding Scalar Price Posiiton on current candle from argument rounded to mintick

bullbar(_off)
Bull Bar
Parameters:
_off
Returns: (bool) true if close > open

bearbar(_off)
Bear Bar
Parameters:
_off
Returns: (bool) true if close < open

Removed:
pb_gt(_a, _b, _off)
a > b

pb_gte(_a, _b, _off)
a >= b

pb_lt(_a, _b, _off)
a < b

pb_lte(_a, _b, _off)
a <= b

Pineライブラリ