从复杂函数中提取颜色:“无法修改函数中的全局变量‘cColor’。”

问题描述 投票:0回答:1

我想从此函数中提取“col”颜色值,用于绘制绘图或蜡烛颜色。但我尝试的一切都会产生一个或另一个错误。我检查了脚本参考。难道不应该有某种方法来“返回”一个值吗,就像大多数函数通常的情况一样?

lset(l,x1,y1,x2,y2,col)=>
    line.set_xy1(l,x1,y1)
    line.set_xy2(l,x2,y2)
    line.set_width(l,5)
    line.set_style(l, line.style_solid)
    line.set_color(l,y2 > y1 ? #ff1100 : #39ff14)  //red : green
    temp = line.get_price(l,bar_index) // another value to extract
function global-variables pine-script pine-script-v5
1个回答
0
投票

文档显示如下:

line.new(x1, y1, x2, y2, xloc, extend, color, style, width) → series line

因此,在您的代码中,它看起来有所不同,并且还缺少“新”。 在链接页面上向上滚动一点显示确实存在检索线条对象的某些属性的方法:

Lines are managed using built-in functions in the line namespace. They include: line.new() to create them. line.set_*() functions to modify the properties of an line. line.get_*() functions to read the properties of an existing line. line.copy() to clone them. line.delete() to delete them. The line.all array which always contains the IDs of all the visible lines on the chart. The array’s size will depend on the maximum line count for your script and how many of those you have drawn. aray.size(line.all) will return the array’s size.
最简单的用法是直接用正确的值实例化一个线对象,如下所示:

//@version=5 indicator("Price path projection", "PPP", true, max_lines_count = 100) qtyOfLinesInput = input.int(10, minval = 1) y2Increment = (close - open) / qtyOfLinesInput // Starting point of the fan in y. lineY1 = math.avg(close[1], open[1]) // Loop creating the fan of lines on each bar. for i = 0 to qtyOfLinesInput // End point in y if line stopped at current bar. lineY2 = open + (y2Increment * i) // Extrapolate necessary y position to the next bar because we extend lines one bar in the future. lineY2 := lineY2 + (lineY2 - lineY1) lineColor = lineY2 > lineY1 ? color.lime : color.fuchsia line.new(bar_index - 1, lineY1, bar_index + 1, lineY2, color = lineColor)
从外部获取线条颜色是困难或不可能的,因为从来不存在检索它的方法,而对于其他属性则存在这些方法。
因此,最简单的方法是创建相同的功能,获取线条对象内部、外部或仅外部存在的颜色。

currentLineColor = y2 > y1 ? #ff1100 : #39ff14
您可以尝试以某种方式扩展线对象,如下所示:

line.prototype.get_color = function() { return this.color; }; console.log(line.get_color())
我不确定

原型方法是否有效,但如果您需要的话值得尝试。

© www.soinside.com 2019 - 2024. All rights reserved.