kurtsmock

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.



Biblioteca Pine

Siguiendo el verdadero espíritu de TradingView, el autor de este código de Pine lo ha publicado como biblioteca de código abierto, para que el resto de programadores de Pine de esta comunidad puedan volver a utilizarlo. ¡Un hurra por el autor!! Puede utilizar esta biblioteca de forma privada o en otras publicaciones de código abierto, pero debe ceñirse a lo establecido en las Normas internas.

Exención de responsabilidad

La información y las publicaciones que ofrecemos, no implican ni constituyen un asesoramiento financiero, ni de inversión, trading o cualquier otro tipo de consejo o recomendación emitida o respaldada por TradingView. Puede obtener información adicional en las Condiciones de uso.

¿Quiere utilizar esta biblioteca?

Copie la siguiente línea y péguela en su script.