Multi Kernel Regression Babak
Multi Kernel Regression Babak
float current_price = na
float previous_price = na
float previous_price_delta = na
float std_dev = na
float upper_1 = na
float lower_1 = na
float upper_2 = na
float lower_2 = na
line estimate = na
line dev_upper = na
line dev_lower = na
label bullish = na
label bearish = na
if not repaint
float sum = 0.0
float sumw = 0.0
float sumsq = 0.0
for i = 0 to bandwidth - 1
j = math.pow(i, 2) / (math.pow(bandwidth, 2))
weight = kernel(j, 1, style)
sum += source[i] * weight
sumw += weight
mean = sum / sumw
direction = mean - mean[1] > 0
direction_color = direction ? bullish_color : bearish_color
for i = 0 to bandwidth - 1
sumsq += math.pow(source[i] - mean[i], 2)
stdev = math.sqrt(sumsq / (bandwidth - 1)) * deviations
array.unshift(estimate_array, line.new(bar_index, mean, bar_index - 1,
mean[1], xloc.bar_index, extend.none, direction_color, line_style, 3))
if enable
array.unshift(dev_upper_array, line.new(bar_index, mean + stdev,
bar_index - 1, mean[1] + stdev[1], xloc.bar_index, extend.none, direction_color,
line_style, 3))
array.unshift(dev_lower_array, line.new(bar_index, mean - stdev,
bar_index - 1, mean[1] - stdev[1], xloc.bar_index, extend.none, direction_color,
line_style, 3))
if lables
if direction and not direction[1]
array.unshift(up_labels, label.new(bar_index, source, "Up", yloc =
yloc.belowbar, color = bullish_color, style = label.style_label_up, textcolor =
text_color, size = size))
if not direction and direction[1]
array.unshift(down_labels, label.new(bar_index, source, "Down",
yloc = yloc.abovebar, color = bearish_color, style = label.style_label_down,
textcolor = text_color, size = size))
else
if barstate.isfirst
for i = 500 to 0
array.set(estimate_array, i, line.new(na, na, na, na))
if enable
array.set(dev_upper_array, i, line.new(na, na, na, na))
array.set(dev_lower_array, i, line.new(na, na, na, na))
if lables
array.set(up_labels, i, label.new(na, na))
array.set(down_labels,i, label.new(na, na))
if barstate.islast
for i = 0 to math.min(bar_index - 1, 500)
float sum = 0
float sumw = 0
float sumsq = 0
if enable
std_dev := math.sqrt(math.max(sumsq / sumw - sq(current_price),
0))
upper_2 := current_price + deviations * std_dev
lower_2 := current_price - deviations * std_dev
estimate := array.get(estimate_array, i)
if enable
dev_upper := array.get(dev_upper_array, i)
dev_lower := array.get(dev_lower_array, i)
if enable
line.set_xy1(dev_upper, bar_index - i + 1, upper_1)
line.set_xy2(dev_upper, bar_index - i , upper_2)
line.set_style(dev_upper, line_style)
line.set_color(dev_upper, current_price > previous_price ?
bearish_color : bullish_color)
line.set_width(dev_upper, 3)
line.set_xy1(dev_lower, bar_index - i + 1, lower_1)
line.set_xy2(dev_lower, bar_index - i , lower_2)
line.set_style(dev_lower, line_style)
line.set_color(dev_lower, current_price > previous_price ?
bearish_color : bullish_color)
line.set_width(dev_lower, 3)
if lables
bullish := array.get(up_labels, i)
bearish := array.get(down_labels, i)
previous_price := current_price
upper_1 := upper_2
lower_1 := lower_2
previous_price_delta := delta
if barstate.isconfirmed
for i = array.size(up_labels) - 1 to 0
label.set_xy(array.get(up_labels, i), na, na)
for i = array.size(down_labels) - 1 to 0
label.set_xy(array.get(down_labels, i), na, na)