首页 > 解决方案 > 在绘图上使用样式变量

问题描述

我希望简单地允许在情节上进行变量替换,但我不断收到错误消息。

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

有什么想法吗?谢谢斯科特

标签: pine-script

解决方案


我希望简单地允许在情节上进行变量替换,但我不断收到错误消息。

由于's 参数之一不是可接受的格式,因此您不断收到该代码的cannot call with arguments 错误。plot()

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

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

现在这是您的代码调用方式plot()

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

问题是这里我们将style参数设置为不是整数,而是一个系列。那是因为cr20style有条件地设置为1or 2。虽然它确实是一系列整数,但系列仍然不同于 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)

推荐阅读