0% found this document useful (0 votes)
439 views5 pages

Kalman+percent Trend

This document contains a Pine Script™ code for a trading indicator called 'Kalman Trend Levels' designed to analyze market trends using Kalman filters and an ultimate smoother. It includes user-defined inputs for customization, calculations for trend detection, and plotting functions for visual representation on trading charts. The script is licensed under Mozilla Public License 2.0 and Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International.

Uploaded by

skabdulafridi53
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
439 views5 pages

Kalman+percent Trend

This document contains a Pine Script™ code for a trading indicator called 'Kalman Trend Levels' designed to analyze market trends using Kalman filters and an ultimate smoother. It includes user-defined inputs for customization, calculations for trend detection, and plotting functions for visual representation on trading charts. The script is licensed under Mozilla Public License 2.0 and Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International.

Uploaded by

skabdulafridi53
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.

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")

color upper_col = input.color(#13bd6e, "up", inline = "colors")


color lower_col = input.color(#af0d4b, "dn", inline = "colors")

// }

// CALCULATION
S――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
――――――――――――{
float atr = ta.atr(200) *0.5

var lower_box = box(na)


var upper_box = box(na)

// Kalman filter function


kalman_filter(src, length, R = 0.01, Q = 0.1) =>
// Initialize variables
var float estimate = na
var float error_est = 1.0
var float error_meas = R * (length)
var float kalman_gain = 0.0
var float prediction = na

// Initialize the estimate with the first value of the source


if na(estimate)
estimate := src[1]

// Prediction step
prediction := estimate

// Update Kalman gain


kalman_gain := error_est / (error_est + error_meas)

// Update estimate with measurement correction


estimate := prediction + kalman_gain * (src - prediction)

// Update error estimates


error_est := (1 - kalman_gain) * error_est + Q / (length) // Adjust process
noise based on length
estimate

float short_kalman = kalman_filter(close, short_len)


float long_kalman = kalman_filter(close, long_len)

bool trend_up = short_kalman > long_kalman

color trend_col = trend_up ? upper_col : lower_col


color trend_col1 = short_kalman > short_kalman[2] ? upper_col : lower_col
color candle_col = candle_color ? (trend_up and short_kalman > short_kalman[2] ?
upper_col : not trend_up and short_kalman < short_kalman[2] ? lower_col :
color.gray) : na

// }

// 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 trend_up[1] and not trend_up


label.new(bar_index, short_kalman, str.tostring(math.round(close,1))+"\n🢃",
color = color(na), textcolor = lower_col, style = label.style_label_down, size =
size.normal)
upper_box := box.new(bar_index, high, bar_index, high-atr, border_color = na,
bgcolor = color.new(lower_col, 60))

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)

if low > lower_box.get_top() and low[1]<= lower_box.get_top()


label.new(bar_index-1, low[1], "+", color = color(na), textcolor =
upper_col, style = label.style_label_up, size = size.normal)

p1 = plot(short_kalman, "Short Kalman", color = trend_col1)


p2 = plot(long_kalman, "Long Kalman", linewidth = 2, color = trend_col)

fill(p1, p2, short_kalman, long_kalman, na, color.new(trend_col, 80))

plotcandle(open, high, low, close, title='Title', color = candle_col,


wickcolor=candle_col, bordercolor = candle_col)
// }
// INPUTS
――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
――――――――――――――――――――――{
int len = input.int(100, "Smoother Length")
int rising = input.int(5, "", inline = "r", group = "Rising/Falling bars")
int falling = input.int(5, "", inline = "r", group = "Rising/Falling bars")
int bars_perc = input.int(20, "Percent points period")
int line_width = input.int(50, "Channel Width", inline = "Channel")
bool show_channel = input.bool(true, "", inline = "Channel")

// 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)

// Initialize counters for up and down trends


var int count_up = 0
var int count_dn = 0
var int count_up_label = 0
var int count_dn_label = 0
// }

// 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

// Initialization of the ultimate smoother variable


var float us = na

// Apply the ultimate smoothing formula


if (bar_index >= 4)
us := (1 - c1) * src + (2 * c1 - c2) * src[1] - (c1 + c3) * src[2] + c2 *
us[1] + c3 * us[2]
else
us := src
us

// Get the previous bar's close


var float prev_close = close

// Calculate percent change


float perc_change = (close - prev_close) / prev_close * 100

// Apply the ultimate smoother


float us = ultimate_smoother(close, len)
bool is_rising = ta.rising(us, rising)
bool is_falling = ta.falling(us, falling)

// Determine the color based on rising or falling trend


if is_rising
col := up_col
if is_falling
col := dn_col

// Handle trend switches and label placements


switch
col == dn_col and col[1] == up_col =>
prev_close := close,
label.new(bar_index-1, us[1], str.tostring(math.round(prev_close, 2)) + "\n▼",
color = color(na),
textcolor = dn_col,
style = label.style_label_down)

col == up_col and col[1] == dn_col =>


prev_close := close,
label.new(bar_index-1, us[1], "▲\n" + str.tostring(math.round(prev_close, 2)),
color = color(na),
textcolor = up_col,
style = label.style_label_up)
// }

// 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))

line.new(bar_index, low, bar_index, low - atr_val * 4, color =


color.new(perc_col_dn, 60))

label.new(bar_index, high, str.tostring(count_dn_label),


color = color(na),
textcolor = chart.fg_color,
style = label.style_label_down)

// Handle percent-based labels and lines during uptrend


if col == up_col
count_up += 1
count_dn := 0
count_dn_label := 0
if count_up % bars_perc == 0
color perc_col_up = perc_change > 0 ? up_col : color.orange
count_up_label += 1
label.new(bar_index, high + atr_val * 4, str.tostring(perc_change,
format.percent),
style = label.style_label_down,
color = color(na),
textcolor = color.new(perc_col_up, 40))

line.new(bar_index, high, bar_index, high + atr_val * 4, color =


color.new(perc_col_up, 60))

label.new(bar_index, low, str.tostring(count_up_label),


color = color(na),
textcolor = chart.fg_color,
style = label.style_label_up)

// Plot the ultimate smoother line and optional channel


plot(us, color = col, linewidth = 2)
plot(show_channel ? us : na, color = color.new(chart.fg_color, 95), linewidth =
line_width)
// }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy