首页 > 解决方案 > 我可以使用 pine 脚本的三元运算符来指定 plotshape() 中的 style 参数或 plotchar() 中的 char 参数吗?

问题描述

我想我是在要求 pine 脚本做一些它不打算做的事情,但我想我还是会提出这个问题。

我可以使用 pine 脚本的三元运算符来指定 plotshape() 中的 style 参数或 plotchar() 中的 char 参数吗?

例如,像这样:

plotshape(
     volume, 
     style = (close[0] >= close[1]) ? shape.square : shape.xcross,
     color = color.red
     )

或类似的东西:

plotchar(
     volume, 
     char  = (close[0] >= close[1]) ? "#" : "•",
     color = color.red
     )

编辑:添加了label.new()应用于卷窗格的示例 - 但无法正确指定y,y_locstyle:

体积图

标签: plotconditional-operatorpine-script

解决方案


你不能。style的类型是style (input string),这意味着在脚本执行之前必须知道它的值。为了在运行时确定它的值,它需要是 type "series[string]"

如果您尝试编译您正在尝试做的事情,您可以告诉这一点。

您将收到以下错误。

line 7: Cannot call 'plotshape' with arguments (series=series[bool], title=literal string, style=series[string], location=const string); available overloads: plotshape(series[bool], const string, input string, input string, series[color], input integer, series[integer], const string, series[color], const bool, const string, input integer, const integer, string) => void; plotshape(fun_arg__<arg_series_type>, const string, input string, input string, fun_arg__<arg_color_type>, input integer, series[integer], const string, fun_arg__<arg_textcolor_type>, const bool, const string, input integer, const integer, string) => void

在第一个括号中,它告诉您它不能使用 调用此函数style=series[string],这是您尝试通过传递一个在运行时可能具有不同值的变量来执行的操作。


编辑: 您始终可以在series绘图参数中组合您的条件。只有当系列评估为 时,它才会绘图true。因此,两个形状的两个绘图函数。

//@version=4
study("My Script", overlay=true)
mainCond = close > 0
circleCond = close > open
squareCond = open >= close
plotshape(series=mainCond and circleCond, title="Circle", style=shape.circle, location=location.belowbar, color=color.green)
plotshape(series=mainCond and squareCond, title="Square", style=shape.square, location=location.belowbar, color=color.red)

在此处输入图像描述


推荐阅读