首页 > 解决方案 > 如何在 Pinescript 中为系列字符串输入添加复选框选项

问题描述

我想弄清楚如何允许以交叉样式格式绘制系列字符串,但作为复选框功能。不幸的是,我找不到任何提供此类用例示例的资源,所以让我们创建一个。

首先,这是一个工作标准输入字符串的示例,它在绘制时可以作为选项启用。

testfunction = sma(rsi, 7)
show_testfunction = input(false, "Show testfunction", type=input.bool)

plot(show_testfunction ? testfunction : na, "Test Function", color.white, transp=30, linewidth=2)

但是,当想要为系列字符串使用相同类型的复选框选项时,如果它允许style=plot.cross_style,我会遇到以下错误:

Syntax error at input 'series'.

我编写以下代码来解决问题:

Testfunctionseries = sma (rsi, 3)

show_testfunctionseries = input(false, "Show series testfunction", type=input.bool)

plot(show_testfunctionseries ? series=testfunctionseries : na, "Test Function Series With Cross Style",  style=plot.style_cross, color.white, transp=30, linewidth=2)

以下是在没有复选框选项的情况下绘制同一行代码的工作方式:

plot(series=testfunctionseries, style=plot.style_cross, color=color.purple, title="Testfunction Series Cross", transp=70, linewidth=2)

另外,我希望这个帖子对不了解这些类型功能的 pine 开发人员有所帮助。

标签: pine-script

解决方案


您错误地使用了参数名称series=。应该将整个表达式show_testfunctionseries ? testfunctionseries : na传递给series=您的代码才能工作。以下是代码应该如何寻找它以正确编译:

//@version=4
study("My Script")
rsi = rsi(close, 14)
testfunctionseries = sma(rsi, 3)
show_testfunctionseries = input(false, "Show series testfunction", type=input.bool)
plot(series = show_testfunctionseries ? testfunctionseries : na, title = "Test Function Series With Cross Style",  style=plot.style_cross, color=color.white, transp=30, linewidth=2)

请注意,我还在那里添加了title=color=。一旦您使用关键字来引用参数(在您的情况下为series=),您就不能再在同一个函数调用中使用位置参数,您必须使用关键字来编译代码。只有当所有位置参数都在第一个关键字参数之前时,您才能同时使用位置参数和关键字参数:

plot(show_testfunctionseries ? testfunctionseries : na, "Test Function Series With Cross Style",  style=plot.style_cross, color=color.white, transp=30, linewidth=2)

推荐阅读