问题描述
我正在尝试从条件中绘制一条水平线,然后在前一个条件为真时删除该线,但是如果我将要删除的代码放入 if 语句中,则会出现错误,并且不会如果我将要删除的代码放在 if 语句之外,则绘制任何线条。
r = close > close[1] and close[1] > close[2]
selleverything = if r
l1 := line.new(bar_index[1],price1,bar_index,color=color.red,style=line.style_solid,width=1,extend=dS1 ? extend.right : extend.both)
上面画的线没问题,但如果我添加
line.delete(l1[1])
在 if 语句中我得到一个“void 表达式不能分配给变量”
如果我将 line.delete(l1[1]) 放在 if 语句之外,则不会绘制任何线条。
感谢任何帮助。
解决方法
本例中的 if 语句在变量声明范围内。
selleverything = if r
改为将其移动到全局范围,如下例所示:
//@version=4
study("My Script")
r = close > close[1] and close[1] > close[2]
var line l1 = na
price1 = close
dS1 = true
if r
l1 := line.new(bar_index[1],price1,bar_index,color=color.red,style=line.style_solid,width=1,extend=dS1 ? extend.right : extend.both)
line.delete(l1[1])
- 声明了
price1
和dS1
变量,因为它们丢失了(相应地分配给您的代码) - 预先声明的行
l1
变量。 - 将 if 语句移至全局范围。