松脚本中的变量范围
Variable scope in pine script
这里我使用一些变量来设置我的退出条件,方法是在 ticks
中定义 profit
或 stop
stopLR = float(floor(stopLoss)) - (3* syminfo.mintick)
targetLR = float(floor(target)) - (3* syminfo.mintick
if(longCondition and strategy.position_size == 0)
//average_long = close
targetLTick := (targetLR - close) / syminfo.mintick
stopLTick := (close - stopLR) / syminfo.mintick
strategy.exit("Long Buy", profit = targetLTick, comment = "Take Profit") //line 460
strategy.exit("Long Buy", loss = stopLTick, comment = "Book Loss") //line 461
Add to Chart operation failed, reason: line 460: Undeclared identifier 'targetLTick';
line 461: Undeclared identifier 'stopLTick'
这里有什么问题?
变量是全局范围的,拼写没有问题,为什么 pine 给我错误?
根据您的代码片段,targetLTick
和 stopLTick
是在本地范围内声明的。
尝试在全局范围内使用 na
值声明它们,然后在本地范围内重新分配,如下例所示:
stopLR = float(floor(stopLoss)) - (3* syminfo.mintick)
targetLR = float(floor(target)) - (3* syminfo.mintick
var float targetLTick = na
var float stopLTick = na
if(longCondition and strategy.position_size == 0)
//average_long = close
targetLTick := (targetLR - close) / syminfo.mintick
stopLTick := (close - stopLR) / syminfo.mintick
strategy.exit("Long Buy", profit = targetLTick, comment = "Take Profit") //line 460
strategy.exit("Long Buy", loss = stopLTick, comment = "Book Loss") //line 461
这里我使用一些变量来设置我的退出条件,方法是在 ticks
中定义profit
或 stop
stopLR = float(floor(stopLoss)) - (3* syminfo.mintick)
targetLR = float(floor(target)) - (3* syminfo.mintick
if(longCondition and strategy.position_size == 0)
//average_long = close
targetLTick := (targetLR - close) / syminfo.mintick
stopLTick := (close - stopLR) / syminfo.mintick
strategy.exit("Long Buy", profit = targetLTick, comment = "Take Profit") //line 460
strategy.exit("Long Buy", loss = stopLTick, comment = "Book Loss") //line 461
Add to Chart operation failed, reason: line 460: Undeclared identifier 'targetLTick';
line 461: Undeclared identifier 'stopLTick'
这里有什么问题? 变量是全局范围的,拼写没有问题,为什么 pine 给我错误?
根据您的代码片段,targetLTick
和 stopLTick
是在本地范围内声明的。
尝试在全局范围内使用 na
值声明它们,然后在本地范围内重新分配,如下例所示:
stopLR = float(floor(stopLoss)) - (3* syminfo.mintick)
targetLR = float(floor(target)) - (3* syminfo.mintick
var float targetLTick = na
var float stopLTick = na
if(longCondition and strategy.position_size == 0)
//average_long = close
targetLTick := (targetLR - close) / syminfo.mintick
stopLTick := (close - stopLR) / syminfo.mintick
strategy.exit("Long Buy", profit = targetLTick, comment = "Take Profit") //line 460
strategy.exit("Long Buy", loss = stopLTick, comment = "Book Loss") //line 461