π LIBRARY OHLC
π· Introduction
This library is a custom library designed to work with real-time bars. It allows to easily calculate OHLC values for any source.Personally, I use this library to accurately display the highest and lowest values on visual indicators such as my progress bars.
π· How to Use
βΌ 1. Import the OHLC library into your TradingView script:
- or -
Instead of the library namespace, you can define a custom namespace as alias.
import cryptolinx/OHLC/1 as src
βΌ 2. Create a new OHLC source using the `new()` function.
varip mySrc = OHLC.new() // It is required to use the `varip` keyword to init your `<OHLC.src>`
- or -
If you has set up an alias before.
===
In that case, your `<OHLC.src>` needs to be `na`, define your object like that
varip <OHLC.src> mySrc = na
βΌ 3. Call the `hydrateOHLC()` method on your OHLC source to update its values:
Basic
float rsi = ta.rsi(close, 14)
mySrc.hydrateOHLC(rsi)
- or -
Inline
rsi = ta.rsi(close, 14).hydrateOHLC(mySrc)
βΌ 4. The data is accessible under their corresponding names.
mySrc.open
mySrc.high
mySrc.low
mySrc.close
π· Note: This library only works with real-time bars and will not work with historical bars.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Β© cryptolinx - jango_blockchained - open π source
//@version=5
// βͺ ββββ LIBRARY
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// @description
// This library provides a simple way to calculate OHLC values for any source.
// ___
// **Limitations:** This library only works with real-time bars. It will not work
// with historical bars,
// ___
library('OHLC', overlay = false)
// >>
// -- OHLC Source {
//
// @type A source that can be used to calculate OHLC values.
// @field close The last close value.
// @field open The last open value.
// @field high The last high value.
// @field low The last low value.
// @field length The number of bars since the last open.
export type src
// --
float close
float open
float high
float low
int length = 0
// }
// -- Build-In Overloaded/Expanded Functions {
//
method nz(float _src, float _target) => na(_src) ? _target : _src
method nz(src _src, src _target) => na(_src) ? _target : _src
// }
// -- OHLC Source Methods {
//
// @function Hydrates the OHLC source with a new value.
// ___
// @param this The source to hydrate.
// @param _src The new value.
// @returns float The hydrated source.
export method hydrateOHLC(src this, float _src) =>
if not na(_src)
this.close := _src
if barstate.isnew
this.open := this.close
this.low := this.close
this.high := this.close
this.length := 0
this.low := math.min(this.low.nz(this.close), this.close)
this.high := math.max(this.high.nz(this.close), this.close)
this.length += 1
// >>
this
// --
//
// @note The "method overloading" and "argument flipping" pattern used in this
// code can also be manually applied to any custom function inside your script.
// This can be useful for creating more flexible and versatile functions that
// can be used in different contexts and with different argument orders.
export method hydrateOHLC(float _src, src this) => this.hydrateOHLC(_src), _src
// }
// -- New OHLC Source {
//
// @function Creates a new OHLC source.
// ___
// [](https://www.tradingview.com/pine-script-docs/en/v5/language/Variable_declarations.html?highlight=varip#varip)
// ___
// **NOTE:**
// Needs to be initialized by using the `varip` keyword.
// @returns src A new blank OHLC source object.
export new() => src.new()
// }
// βͺ ββββ EXAMPLES
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// -- Example #1 {
//
// @variable rsi Example RSI calculation
// @variable mySrc Example OHLC source.
rsi = ta.rsi(close, 14)
varip mySrc2 = src.new()
mySrc2.hydrateOHLC(rsi) // equals to rsi.hydrateOHLC(mySrc)
// }
// -- Example #2 {
//
// @variable mySrc2 Example OHLC source
// @variable rsi2 Example RSI calculation
varip mySrc = src.new()
rsi2 = ta.rsi(close, 5).hydrateOHLC(mySrc) // equals to mySrc2.hydrateOHLC(rsi)
// }
// -- Plotting {
//
// @variable __PLOT_CANDLE
var __PLOT_CANDLE = input.bool(true, title='Plot Candle')
// --
// @variable rsiPlot
rsiPlot = plot(not __PLOT_CANDLE or not barstate.islast ? mySrc.close : na, color = mySrc.close > mySrc.open ? color.green : color.red)
// --
fill(
plot1 = plot(not __PLOT_CANDLE and barstate.islast ? mySrc.high : na, color = color.new(color.white, 85)),
plot2 = rsiPlot,
bottom_value = 0,
top_value = 100,
bottom_color = #00000000,
top_color = color.new(color.red, 90))
// --
fill(
plot1 = rsiPlot,
plot2 = plot(not __PLOT_CANDLE and barstate.islast ? mySrc.low : na, color = color.new(color.white, 85)),
bottom_value = 0,
top_value = 100,
bottom_color = color.new(color.green, 90),
top_color = #00000000)
// --
plotcandle(
open = __PLOT_CANDLE and barstate.islast ? mySrc.open : na,
high = __PLOT_CANDLE and barstate.islast ? mySrc.high : na,
low = __PLOT_CANDLE and barstate.islast ? mySrc.low : na,
close = __PLOT_CANDLE and barstate.islast ? mySrc.close : na,
title = 'RSI Candle',
color = mySrc.open > mySrc.close ? color.red : color.green,
wickcolor = mySrc.open > mySrc.close ? color.red : color.green,
bordercolor = mySrc.open > mySrc.close ? color.red : color.green)
// }
// #EOF
cryptolinx - jango_blockchained β¨