Kalman+percent Trend
Kalman+percent Trend
0
at https://mozilla.org/MPL/2.0/
// This work is licensed under Creative Commons Attribution-NonCommercial-
ShareAlike 4.0 International
// https://creativecommons.org/licenses/by-nc-sa/4.0/
// © BigBeluga
//@version=5
indicator("Kalman Trend Levels [BigBeluga]+ percent trend", overlay = true,
max_labels_count = 500, max_boxes_count = 500)
// INPUTS
――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
――――――――――――――――――――――{
int short_len = input.int(50)
int long_len = input.int(150)
bool retest_sig = input.bool(false, "Retest Signals")
bool candle_color = input.bool(true, "Candle Color")
// }
// CALCULATION
S――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
――――――――――――{
float atr = ta.atr(200) *0.5
// Prediction step
prediction := estimate
// }
// PLOT
――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
――――――――――――――――――――――――――{
if trend_up and not trend_up[1]
label.new(bar_index, short_kalman, "🡹\n" + str.tostring(math.round(close,1)),
color = color(na), textcolor = upper_col, style = label.style_label_up, size =
size.normal)
lower_box := box.new(bar_index, low+atr, bar_index, low, border_color = na,
bgcolor = color.new(upper_col, 60))
if not ta.change(trend_up)
lower_box.set_right(bar_index)
if not ta.change(trend_up)
upper_box.set_right(bar_index)
if retest_sig
if high < upper_box.get_bottom() and high[1]>= upper_box.get_bottom() //or high
< lower_box.get_bottom() and high[1]>= lower_box.get_bottom()
label.new(bar_index-1, high[1], "x", color = color(na), textcolor =
lower_col, style = label.style_label_down, size = size.normal)
// Color inputs
float atr_val = ta.atr(200)
color up_col = input.color(#23cc99, "Up", group = "Colors", inline = "C")
color dn_col = input.color(#cc2323, "Dn", group = "Colors", inline = "C")
var color col = color(na)
// CALCULATION
S――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
――――――――――――{
// Ultimate Smoother function based on John Ehlers' formula
ultimate_smoother(src, period) =>
float coeff = math.sqrt(2)
float step = 2.0 * math.pi / period
float a1 = math.exp(-coeff * math.pi / period)
float b1 = 2 * a1 * math.cos(coeff * step / period)
float c2 = b1
float c3 = -a1 * a1
float c1 = (1 + c2 - c3) / 4
// PLOT
――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
――――――――――――――――――――――――――{
// Handle percent-based labels and lines during downtrend
if col == dn_col
count_up := 0
count_dn += 1
count_up_label := 0
if count_dn % bars_perc == 0
color perc_col_dn = perc_change < 0 ? dn_col : color.orange
count_dn_label += 1
label.new(bar_index, low - atr_val * 4, str.tostring(perc_change,
format.percent),
style = label.style_label_up,
color = color(na),
textcolor = color.new(perc_col_dn, 40))