在绘图上使用样式变量

Using a style variable on the Plot

我想简单地允许在绘图上进行变量替换,但我一直收到错误。

cr20_50up = cross(d1,d9) and d1 > d9
cr20style = cr20_50up ? 1 : 2
plot(d1, title='%K SMA20', color=cr20_50_color, transp=0,style=cr20style)

但是没用。

line 54: Cannot call `plot` with arguments (series, title=literal string, color=series[color], transp=literal integer, style=series[integer]); available overloads: plot(series, const string, series[color], integer, integer, bool, integer, float, series[integer], bool, series, const bool, const integer, string) => plot; plot(fun_arg__<arg_series_type>, const string, fun_arg__<arg_color_type>, integer, integer, bool, integer, float, series[integer], bool, series, const bool, const integer, string) => plot

有idea吗? 谢谢 斯科特

I'm looking to simply allow a variable replacement on the plot, but I keep getting an error.

您不断收到该代码的 cannot call with arguments error 是因为 plot() 的参数之一不是可接受的格式。

如果我们查看 the plot() function,我们会看到它采用以下值,每个值都有自己的类型:

  • series(系列)
  • title(常量字符串)
  • color(颜色)
  • linewidth(整数)
  • style(整数)
  • transp(整数)
  • trackprice(布尔值)
  • histbase(浮动)
  • offset(整数)
  • join(布尔值)
  • editable (const bool)
  • show_last(常数整数)

下面是您的代码如何调用 plot():

cr20style = cr20_50up ? 1 : 2
plot(d1, title='%K SMA20', color=cr20_50_color, transp=0,style=cr20style)

问题是这里我们设置的style参数不是一个整数,而是一个数列。这是因为 cr20style 有条件地设置为 12。虽然它确实是一系列整数,但一系列仍然不同于 TradingView Pine 中的常规整数。

不幸的是,这也意味着以下内容:您不能有条件地设置 plot() 函数的样式。

可能对您的代码最好的解决方法是创建两个图,每个图都有自己的风格。然后禁用基于 cr20style 的绘图。例如:

plot(cr20style == 1 ? d1 : na, title='%K SMA20', 
     color=cr20_50_color, transp=0,style=1)
plot(cr20style == 2 ? d1 : na, title='%K SMA20', 
     color=cr20_50_color, transp=0,style=2)