PINE LIBRARY

Price Displacement - Candlestick (OHLC) Calculations

Cập nhật
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 [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.

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) [For sop, substitute open for close.]


Vector = Directional. [-100% : 100%]
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 "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.


Phát hành các Ghi chú
v2 - More shortcut bars added. More to come.

Added:
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[off]
  Parameters:
    _off: (int) Offset
  Returns: (bool) true if close > high[off]

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

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

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

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

pb_LgtO(_off)
  low > open[off]
  Parameters:
    _off: (int) Offset
  Returns: (bool) true if low > open[off]
Phát hành các Ghi chú
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[1] | pbe_gt(close,open) = close >= open[1]
Added:

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

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

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

pb_lte(_a, _b, _off)
  a <= b[off]
  Parameters:
    _a: (float) Queried Attribute
    _b: (float) Reference Attribute
    _off: (int) Offset
  Returns: (bool) true if a <= b[off]. 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[off]

pb_CltH(_off)
  close < high[off]

pb_CgtL(_off)
  close > low[off]

pb_CltL(_off)
  close < low>[off]

pb_HltO(_off)
  high < open[off]

pb_LgtO(_off)
  low > open[off]
Phát hành các Ghi chú
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:
gt(_a, _b, _off, _e)
  a >= b[off]
  Parameters:
    _a: (float) Queried Attribute
    _b: (float) Reference Attribute
    _off: (int) Offset
    _e: (bool) (True = >=) | (False = >)
  Returns: (bool) true if a > b[off]. Defaults: _off = 1. _e = false

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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[off]

pb_gte(_a, _b, _off)
  a >= b[off]

pb_lt(_a, _b, _off)
  a < b[off]

pb_lte(_a, _b, _off)
  a <= b[off]
Phát hành các Ghi chú
v5
Small 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:
pt(_exp)
  Absolute Point Displacement. Point quantity of given size parameters according to _exp.
  Parameters:
    _exp (string): (string) Price Parameter
  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")

vpt(_exp)
  Vector Point Displacement. Point quantity of given size parameters according to _exp.
  Parameters:
    _exp (string): (string) Price Parameter
  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")

tick(_exp)
  Absolute Tick Displacement. Tick quantity of given size parameters according to _exp.
  Parameters:
    _exp (string): (string) Price Parameter
  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")

vtick(_exp)
  Vector Tick Displacement. Tick quantity of given size parameters according to _exp.
  Parameters:
    _exp (string): (string) Price Parameter
  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")

pct(_exp, _prec)
  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:
    _exp (string): (string)
    _prec (int): (int) Overload - Place value precision definition
  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")

vpct(_exp, _prec)
  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:
    _exp (string): (string) Price Parameter
    _prec (int)
  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")

priceFromSp(_sp)
  Price At Scalar Percentage of Range
  Parameters:
    _sp (float): (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
candlesticmathfunctionhelperinputsMATHohlc

Thư viện Pine

Theo tinh thần TradingView thực sự, tác giả đã xuất bản mã Pine này dưới dạng thư viện nguồn mở để các lập trình viên Pine khác trong cộng đồng của chúng tôi có thể sử dụng lại. Chúc mừng tác giả! Bạn có thể sử dụng thư viện này riêng tư hoặc trong các bài đăng nguồn mở khác. Tuy nhiên, bạn cần sử dụng lại mã này theo Quy tắc nội bộ.


Ngoài ra, trên:

Thông báo miễn trừ trách nhiệm