线性回归通道绘制选项



我正在使用"线性回归通道";作为我的覆盖层之一,我正在使用集成在一个松木脚本中的所有覆盖层。现在,我需要在";线性回归通道";绘制或不绘制,这样我就可以根据需要打开和关闭它。我认为可以使用pine脚本中的input.bool函数来完成。但我想不出应该在哪一行添加选项。我的代码如下-

//Linear Regression Channel
src = input(defval = close, title = "Source")
len = input.int(defval = 100, title = "Length", minval = 10)
devlen = input.float(defval = 2., title = "Deviation", minval = 0.1, step = 0.1)
lrcPlot = input.bool(title = "Plot LRC?", defval=false)
extendit = input(defval = true, title = "Extend Lines")
showfibo = input(defval = false, title = "Show Fibonacci Levels")
showbroken = input(defval = false, title = "Show Broken Channel", inline = "brk")
brokencol = input(defval = color.blue, title = "", inline = "brk")
upcol = input(defval = color.lime, title = "Up/Down Trend Colors", inline = "trcols")
dncol = input(defval = color.red, title = "", inline = "trcols")
widt = input(defval = 1, title = "Line Width")
var fibo_ratios = array.new_float(0)
var colors = array.new_color(2)
if barstate.isfirst
array.unshift(colors, upcol)
array.unshift(colors, dncol)
array.push(fibo_ratios, 0.236)
array.push(fibo_ratios, 0.382)
array.push(fibo_ratios, 0.618)
array.push(fibo_ratios, 0.786)

get_channel(src, len)=>
mid = math.sum(src, len) / len
slope = ta.linreg(src, len, 0) - ta.linreg(src, len, 1)
intercept  = mid - slope * math.floor(len / 2) + ((1 - (len % 2)) / 2) * slope
endy = intercept  + slope * (len - 1) 
dev = 0.0
for x = 0 to len - 1
dev := dev + math.pow(src[x] - (slope * (len - x) + intercept), 2)
dev := math.sqrt(dev/len)
[intercept, endy, dev, slope]
[y1_, y2_, dev, slope] = get_channel(src, len)
outofchannel = (slope > 0 and close < y2_ - dev * devlen) ? 0 : (slope < 0 and close > y2_ + dev * devlen) ? 2 : -1
var reglines = array.new_line(3)
var fibolines = array.new_line(4)
for x = 0 to 2
if not showbroken or outofchannel != x or nz(outofchannel[1], -1) != -1
line.delete(array.get(reglines, x))
else
line.set_color(array.get(reglines, x), color = brokencol)
line.set_width(array.get(reglines, x), width = 2)
line.set_style(array.get(reglines, x), style = line.style_dotted)
line.set_extend(array.get(reglines, x), extend = extend.none)

array.set(reglines, x, 
line.new(x1 = bar_index - (len - 1), 
y1 = y1_ + dev * devlen * (x - 1), 
x2 = bar_index, 
y2 = y2_ + dev * devlen * (x - 1),
color = array.get(colors, math.round(math.max(math.sign(slope), 0))),
style =  x % 2 == 1 ? line.style_solid : line.style_dashed,
width = widt,
extend = extendit ? extend.right : extend.none))
if showfibo
for x = 0 to 3
line.delete(array.get(fibolines, x))
array.set(fibolines, x, 
line.new(x1 = bar_index - (len - 1), 
y1 = y1_ - dev * devlen + dev * devlen * 2 * array.get(fibo_ratios, x), 
x2 = bar_index, 
y2 = y2_ - dev * devlen + dev * devlen * 2 * array.get(fibo_ratios, x),
color = array.get(colors, math.round(math.max(math.sign(slope), 0))),
style =  line.style_dotted,
width = widt,
extend = extendit ? extend.right : extend.none))

var label sidelab = label.new(x = bar_index - (len - 1), y = y1_, text = "S",  size = size.large)
txt = slope > 0 ? slope > slope[1] ? "⇑" : "⇗" : slope < 0 ? slope < slope[1] ? "⇓" : "⇘" : "⇒"
stl = slope > 0 ? slope > slope[1] ? label.style_label_up : label.style_label_upper_right : slope < 0 ? slope < slope[1] ? label.style_label_down :  label.style_label_lower_right : label.style_label_right
label.set_style(sidelab, stl)
label.set_text(sidelab, txt)
label.set_x(sidelab, bar_index - (len - 1))
label.set_y(sidelab, slope > 0 ? y1_ - dev * devlen : slope < 0 ? y1_ + dev * devlen : y1_)
label.set_color(sidelab, slope > 0 ? upcol : slope < 0 ? dncol : color.blue)
alertcondition(outofchannel, title='Channel Broken', message='Channel Broken')
// direction
trendisup = math.sign(slope) != math.sign(slope[1]) and slope > 0
trendisdown = math.sign(slope) != math.sign(slope[1]) and slope < 0
alertcondition(trendisup, title='Up trend', message='Up trend')
alertcondition(trendisdown, title='Down trend', message='Down trend')

请帮我解决这个问题。谢谢你抽出时间。当做

您可以使用类似plotreression=input.bool(true,"Plot Regression"(的输入语句,然后在绘制线之前检查输入变量。下的示例

//@version=5
//Linear Regression Channel
indicator("test",overlay=true)
src = input(defval = close, title = "Source")
len = input.int(defval = 100, title = "Length", minval = 10)
devlen = input.float(defval = 2., title = "Deviation", minval = 0.1, step = 0.1)
lrcPlot = input.bool(title = "Plot LRC?", defval=false)
extendit = input(defval = true, title = "Extend Lines")
showfibo = input(defval = false, title = "Show Fibonacci Levels")
showbroken = input(defval = false, title = "Show Broken Channel", inline = "brk")
brokencol = input(defval = color.blue, title = "", inline = "brk")
upcol = input(defval = color.lime, title = "Up/Down Trend Colors", inline = "trcols")
dncol = input(defval = color.red, title = "", inline = "trcols")
widt = input(defval = 1, title = "Line Width")
plotregression=input.bool(true,"Plot Regression")
var fibo_ratios = array.new_float(0)
var colors = array.new_color(2)
if barstate.isfirst
array.unshift(colors, upcol)
array.unshift(colors, dncol)
array.push(fibo_ratios, 0.236)
array.push(fibo_ratios, 0.382)
array.push(fibo_ratios, 0.618)
array.push(fibo_ratios, 0.786)

get_channel(src, len)=>
mid = math.sum(src, len) / len
slope = ta.linreg(src, len, 0) - ta.linreg(src, len, 1)
intercept  = mid - slope * math.floor(len / 2) + ((1 - (len % 2)) / 2) * slope
endy = intercept  + slope * (len - 1) 
dev = 0.0
for x = 0 to len - 1
dev := dev + math.pow(src[x] - (slope * (len - x) + intercept), 2)
dev := math.sqrt(dev/len)
[intercept, endy, dev, slope]
[y1_, y2_, dev, slope] = get_channel(src, len)
outofchannel = (slope > 0 and close < y2_ - dev * devlen) ? 0 : (slope < 0 and close > y2_ + dev * devlen) ? 2 : -1
var reglines = array.new_line(3)
var fibolines = array.new_line(4)
for x = 0 to 2
if plotregression
if not showbroken or outofchannel != x or nz(outofchannel[1], -1) != -1
line.delete(array.get(reglines, x))
else
line.set_color(array.get(reglines, x), color = brokencol)
line.set_width(array.get(reglines, x), width = 2)
line.set_style(array.get(reglines, x), style = line.style_dotted)
line.set_extend(array.get(reglines, x), extend = extend.none)

array.set(reglines, x, 
line.new(x1 = bar_index - (len - 1), 
y1 = y1_ + dev * devlen * (x - 1), 
x2 = bar_index, 
y2 = y2_ + dev * devlen * (x - 1),
color = array.get(colors, math.round(math.max(math.sign(slope), 0))),
style =  x % 2 == 1 ? line.style_solid : line.style_dashed,
width = widt,
extend = extendit ? extend.right : extend.none))
if showfibo
for x = 0 to 3
line.delete(array.get(fibolines, x))
array.set(fibolines, x, 
line.new(x1 = bar_index - (len - 1), 
y1 = y1_ - dev * devlen + dev * devlen * 2 * array.get(fibo_ratios, x), 
x2 = bar_index, 
y2 = y2_ - dev * devlen + dev * devlen * 2 * array.get(fibo_ratios, x),
color = array.get(colors, math.round(math.max(math.sign(slope), 0))),
style =  line.style_dotted,
width = widt,
extend = extendit ? extend.right : extend.none))

var label sidelab = label.new(x = bar_index - (len - 1), y = y1_, text = "S",  size = size.large)
txt = slope > 0 ? slope > slope[1] ? "⇑" : "⇗" : slope < 0 ? slope < slope[1] ? "⇓" : "⇘" : "⇒"
stl = slope > 0 ? slope > slope[1] ? label.style_label_up : label.style_label_upper_right : slope < 0 ? slope < slope[1] ? label.style_label_down :  label.style_label_lower_right : label.style_label_right
label.set_style(sidelab, stl)
label.set_text(sidelab, txt)
label.set_x(sidelab, bar_index - (len - 1))
label.set_y(sidelab, slope > 0 ? y1_ - dev * devlen : slope < 0 ? y1_ + dev * devlen : y1_)
label.set_color(sidelab, slope > 0 ? upcol : slope < 0 ? dncol : color.blue)
alertcondition(outofchannel, title='Channel Broken', message='Channel Broken')
// direction
trendisup = math.sign(slope) != math.sign(slope[1]) and slope > 0
trendisdown = math.sign(slope) != math.sign(slope[1]) and slope < 0
alertcondition(trendisup, title='Up trend', message='Up trend')
alertcondition(trendisdown, title='Down trend', message='Down trend')
for x = 0 to 2
if plotregression

之前的if

if plotregression
for x = 0 to 2

最新更新