将版本 2 pinescipt 迁移到版本 4 后,TradingView 指标向右移动

TradingView indicator moves to the right after migrating a version 2 pinescipt to version 4

//@version=2
study(title="Test", shorttitle="Test", overlay=true)
LRG_Channel_TF_mins_D_W_M = input("30")
Range2 = input(1)
SELL = security(tickerid, LRG_Channel_TF_mins_D_W_M, highest(Range2))
BUY = security(tickerid, LRG_Channel_TF_mins_D_W_M, lowest(Range2))
HI = plot(SELL, color=SELL!=SELL[1]?na:red,linewidth=2 )
LO = plot(BUY, color=BUY!=BUY[1]?na:green,linewidth=2 )
fill(HI, LO, color=#E3CAF1, transp=100)

此脚本像“Evan Cabral Binary Strategy”指标一样在图表中创建自动支撑线和阻力线。这是它的截图,应该是这样的: TradingView-chart with pinescript v2

我想将此脚本迁移到 PineScript Verion 4。我已将 tickerid 更改为 syminfo.tickerid,将颜色更改为 color.red,并将 //@version=2 更改为 [=16] =].如果我编译脚本没有错误发生。这是新脚本:

//@version=4
study(title="Test", shorttitle="Test", overlay=true)
LRG_Channel_TF_mins_D_W_M = input("30")
Range2 = input(1)
SELL = security(syminfo.tickerid, LRG_Channel_TF_mins_D_W_M, highest(Range2))
BUY = security(syminfo.tickerid, LRG_Channel_TF_mins_D_W_M, lowest(Range2))
HI = plot(SELL, color=SELL!=SELL[1]?na:color.red,linewidth=2 )
LO = plot(BUY, color=BUY!=BUY[1]?na:color.green,linewidth=2 )
fill(HI, LO, color=#E3CAF1, transp=100)

但是您可以在上面的屏幕截图中看到的支撑线和阻力线向右移动了自己的长度。现在看起来像这样:TradingView-chart with pinescript v4

我看了很多视频教程等等,但我仍然是 PineScript 的初学者,不知道如何解决。有人有想法吗?

您需要将参数 lookahead=barmerge.lookahead_on 添加到您的 security() 调用中。
如果您想深入了解 security() 调用的工作原理,您应该查看 security() revisited by [PineCoders]

//@version=4
study(title="Test", shorttitle="Test", overlay=true)

LRG_Channel_TF_mins_D_W_M = input("30")
Range2 = input(1)

SELL = security(syminfo.tickerid, LRG_Channel_TF_mins_D_W_M, highest(Range2), lookahead=barmerge.lookahead_on)
BUY = security(syminfo.tickerid, LRG_Channel_TF_mins_D_W_M, lowest(Range2), lookahead=barmerge.lookahead_on)

HI = plot(SELL, color=SELL!=SELL[1]?na:color.red,linewidth=2 )
LO = plot(BUY, color=BUY!=BUY[1]?na:color.green,linewidth=2 )

fill(HI, LO, color=#E3CAF1, transp=100)