Created
April 8, 2025 14:10
-
-
Save yunyu950908/cb706a9160d880549ada0e9b1a39d70b to your computer and use it in GitHub Desktop.
Realized Price Oscillator [InvestorUnknown]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ | |
// © InvestorUnknown | TRW: @Andrej S. | |
// {||} | |
// , {||} | |
// ,,,,, {||} | |
// ,,,,, , ,, {||} | |
// , ,,,, , ,, ,,, {||} | |
// . , , ,,,, , ,,,, .,, {||} ╔╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╗ | |
// ,, , ,, ,,,,,,, , , , {||} ╠╬╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╬╣ | |
// ,, ,, , ,, ,,,,,, ,,,, , , {||} ╠╣ /$$$$$$ /$$ ╠╣ | |
// ., , ,,, ,,,,,,,,,,,,,, ,, ,, , , ,, {||} ╠╣ |_ $$_/ | $$ ╠╣ | |
// , . ,, ,,,,,,,,,,,,, , ,,, , ,, , , {||} ╠╣ | $$ /$$$$$$$ /$$ /$$ /$$$$$$ /$$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$ ╠╣ | |
// ,, ,,, ,,,,,,,,,,,,,,,,,,,,,, ,,, ,, {||} ╠╣ | $$ | $$__ $$| $$ /$$//$$__ $$ /$$_____/|_ $$_/ /$$__ $$ /$$__ $$ ╠╣ | |
// , , ,,, .,,,,,,,,,,,, ,,, ,,,,,,,, ,,, ,, {||} ╠╣ | $$ | $$ \ $$ \ $$/$$/| $$$$$$$$| $$$$$$ | $$ | $$ \ $$| $$ \__/ ╠╣ | |
// ., , ,, ,, ,,, ,,,,,,, ,, ,,, ,,,,, ,,, , ,, ,, {||} ╠╣ | $$ | $$ | $$ \ $$$/ | $$_____/ \____ $$ | $$ /$$| $$ | $$| $$ ╠╣ | |
// , ,,,,, , ,,,, ,, , ,,,,,,,,,,,,,,,,,,,,,, ,, ,, {||} ╠╣ /$$$$$$| $$ | $$ \ $/ | $$$$$$$ /$$$$$$$/ | $$$$/| $$$$$$/| $$ ╠╣ | |
// . //./ /// ,,,,,,,,,,,,,,,. ,,,,,,,,,,,,,,,,,, {||} ╠╣ |______/|__/ |__/ \_/ \_______/|_______/ \___/ \______/ |__/ ╠╣ | |
// , / ,., ,,,,,,,,,,, ,,,,,,, ,,,,,,, {||} ╠╣ ╠╣ | |
// . ,,, ,/ ///./ ,,,.,,,,,,,,,,,,,,, ,, , , {||} ╠╣ ╠╣ | |
// ,,,,,, //./ , / .,,.,,, ,,,,,, ,. ,,,,,,, {||} ╠╣ ╠╣ | |
// ,,,, // *, / / ,,,,,,,,,,,, ,, ,,,,, {||} ╠╣ /$$ /$$ /$$ ╠╣ | |
// ,, // ////.*/// / ,.,,,,,.,, ,, ,,,, ,,,,,, {||} ╠╣ | $$ | $$ | $$ ╠╣ | |
// , ///// // , ,,,,,, ,,,, ,,,,, ,,, / /. {||} ╠╣ | $$ | $$ /$$$$$$$ | $$ /$$ /$$$$$$$ /$$$$$$ /$$ /$$ /$$ /$$$$$$$ ╠╣ | |
// ,, , ////// ,,,,,,,,, ,,,,,,,,/ /// / // {||} ╠╣ | $$ | $$| $$__ $$| $$ /$$/| $$__ $$ /$$__ $$| $$ | $$ | $$| $$__ $$ ╠╣ | |
// ///// .// ,,,,,, ,, ,,,, ,,, ///* //*/// {||} ╠╣ | $$ | $$| $$ \ $$| $$$$$$/ | $$ \ $$| $$ \ $$| $$ | $$ | $$| $$ \ $$ ╠╣ | |
// // . ,, .// ,, ///, /// {||} ╠╣ | $$ | $$| $$ | $$| $$_ $$ | $$ | $$| $$ | $$| $$ | $$ | $$| $$ | $$ ╠╣ | |
// ////// ,,,, ///// ,. , {||} ╠╣ | $$$$$$/| $$ | $$| $$ \ $$| $$ | $$| $$$$$$/| $$$$$/$$$$/| $$ | $$ ╠╣ | |
// *///////. // / */////* {||} ╠╣ \______/ |__/ |__/|__/ \__/|__/ |__/ \______/ \_____/\___/ |__/ |__/ ╠╣ | |
// .,, // ,,,,,,,,,, //* ,,, ////// {||} ╠╬╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╦╬╣ | |
// ,,,,, ,,,,,, ,.,,,,,,, {||} ╚╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╝ | |
// ,,,,,,,,,,,, ,, {||} | |
// ,,,,,,,,, {||} | |
// {||} | |
// {||} | |
//@version=5 | |
indicator("Realized Price Oscillator [InvestorUnknown]", "Realized Price Oscillator", overlay = false) | |
// - - - - - STRINGS - - - - - //{ | |
var string G1 = "Calculation and Scaling Settings" | |
var string G2 = "DCA Signal Settings" | |
var string lim_tooltip = "Sets limit for scaling of the values, can be useful if you need higher decimal precision of the data." | |
var string neg_tooltip = "Allow negative values on the scale. If turned off, values will be displayed in range of \"Limit\" and 0." | |
var string decay_tooltip = "Decay function reduces the value of Max/Min by multiplying it with \"Decay\" value on every bar." | |
var string threshold_tooltip = "Represents the distance between midline and the TOP/BOTTOM values - determined by \"Limit\" input." | |
//} | |
// - - - - - INPUTS - - - - - //{ | |
lim = input.float(1.0, "Limit", group = G1, tooltip = lim_tooltip, inline = "A1") | |
neg = input.bool (true, "Allow Negatives", group = G1, tooltip = neg_tooltip, inline = "A1") | |
decay = input.float(0.9999, "Decay", group = G1, tooltip = decay_tooltip, step = 0.00001, maxval = 1) | |
plot_signal = input.bool (true, "Plot Dollar-Cost Average Signals", group = G2) | |
dca_in = input.float(0.5, "DCA IN threshold (%)", group = G2, tooltip = threshold_tooltip, maxval = 0.99, minval = 0.01, step = 0.01) | |
dca_out = input.float(0.5, "DCA OUT threshold (%)", group = G2, tooltip = threshold_tooltip, maxval = 0.99, minval = 0.01, step = 0.01) | |
normalization_start_date = timestamp(2012,1,1) | |
plot_start_date = timestamp(2012,3,1) | |
//} | |
// - - - - - FUNCTIONS - - - - - //{ | |
f_rescale(float value, float min, float max, float limit, bool negatives) => | |
((limit * (negatives ? 2 : 1)) * (value - min) / (max - min)) - (negatives ? limit : 0) | |
f_realized_price_oscillator(float realized_price) => | |
rpo_o = math.log(open / realized_price) | |
rpo_h = math.log(high / realized_price) | |
rpo_l = math.log(low / realized_price) | |
rpo_c = math.log(close / realized_price) | |
[rpo_o, rpo_h, rpo_l, rpo_c] | |
normalized_oscillator(float x, float b) => | |
float oscillator = b | |
var float min = na | |
var float max = na | |
if (oscillator > max or na(max)) and time >= normalization_start_date | |
max := oscillator | |
if (min > oscillator or na(min)) and time >= normalization_start_date | |
min := oscillator | |
if time >= normalization_start_date | |
max := max * decay | |
min := min * decay | |
normalized_oscillator = f_rescale(x, min, max, lim, neg) | |
//} | |
// - - - - - CALCULATIONS - - - - - //{ | |
float RC = request.security("COINMETRICS:BTC_MARKETCAPREAL", "D", close) | |
float SP = request.security("GLASSNODE:BTC_SUPPLY","D", close) | |
float Realiized_price = RC / SP | |
[rpo_o, rpo_h, rpo_l, rpo_c] = f_realized_price_oscillator(Realiized_price) | |
float norm_o = normalized_oscillator(rpo_o, rpo_c) | |
float norm_h = normalized_oscillator(rpo_h, rpo_c) | |
float norm_l = normalized_oscillator(rpo_l, rpo_c) | |
float norm_c = normalized_oscillator(rpo_c, rpo_c) | |
//} | |
// - - - - - VISUALIZATION - - - - - //{ | |
color col = color.from_gradient(norm_c, neg ? -lim : 0, lim, color.green,color.red) | |
plotcandle(time >= plot_start_date ? norm_o : na, | |
time >= plot_start_date ? norm_h : na, | |
time >= plot_start_date ? norm_l : na, | |
time >= plot_start_date ? norm_c : na, | |
"rpo", col, wickcolor = col, bordercolor = col) | |
bgcol = norm_h > (dca_out * lim) ? color.new(color.red,70) : norm_l < (dca_in* (neg ? -lim : 0)) ? color.new(color.green,70) : na | |
barcol = norm_h > (dca_out * lim) ? color.new(color.red,0) : norm_l < (dca_in * (neg ? -lim : 0)) ? color.new(color.green,0) : na | |
bgcolor(time >= plot_start_date and plot_signal ? bgcol : na) | |
barcolor(time >= plot_start_date and plot_signal ? barcol : na) | |
//} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.tradingview.com/v/UxC7meAg/