Pine Script v3转v4出现「obv已定义」重分配错误如何解决?
错误原因
Pine Script V4 已内置系统函数obv,你在V3脚本中自定义的同名函数和系统函数冲突,因此触发重定义报错。该问题和变量赋值运算符无关,添加:=无法解决冲突。
解决方法
修改自定义函数的名称,避免和内置函数重名即可,同时适配V4的其他语法规范:
- 自定义
obv函数重命名为calc_obv,对应的调用位置同步修改 - 版本声明改为
//@version=4 - 颜色声明、绘图样式适配V4语法要求
完整可运行V4代码
//@version=4 // // @author LazyBear // // Appreciate a note if you use this code anywhere. // study(title="OBV with Divergence", shorttitle="OBV_Divergence_LB") len = input(20) src = close lbR = input(title="Pivot Lookback Right", defval=5) lbL = input(title="Pivot Lookback Left", defval=5) rangeUpper = input(title="Max of Lookback Range", defval=60) rangeLower = input(title="Min of Lookback Range", defval=5) plotBull = input(title="Plot Bullish", defval=true) plotHiddenBull = input(title="Plot Hidden Bullish", defval=false) plotBear = input(title="Plot Bearish", defval=true) plotHiddenBear = input(title="Plot Hidden Bearish", defval=false) bearColor = color.red bullColor = color.green hiddenBullColor = color.new(color.green, 80) hiddenBearColor = color.new(color.red, 80) textColor = color.white noneColor = color.new(color.white, 100) calc_obv(src) => cum(change(src) > 0 ? volume : change(src) < 0 ? -volume : 0*volume) os=calc_obv(src) obv_osc = (os - ema(os,len)) obc_color=obv_osc > 0 ? color.green : color.red plot(obv_osc, color=obc_color, style=plot.style_line,title="OBV-Points", linewidth=2) plot(obv_osc, color=color.silver, transp=70, title="OBV", style=plot.style_area) hline(0) plFound = na(pivotlow(obv_osc, lbL, lbR)) ? false : true phFound = na(pivothigh(obv_osc, lbL, lbR)) ? false : true _inRange(cond) => bars = barssince(cond == true) rangeLower <= bars and bars <= rangeUpper //------------------------------------------------------------------------------ // Regular Bullish // Osc: Higher Low oscHL = obv_osc[lbR] > valuewhen(plFound, obv_osc[lbR], 1) and _inRange(plFound[1]) // Price: Lower Low priceLL = low[lbR] < valuewhen(plFound, low[lbR], 1) bullCond = plotBull and priceLL and oscHL and plFound plot( plFound ? obv_osc[lbR] : na, offset=-lbR, title="Regular Bullish", linewidth=2, color=(bullCond ? bullColor : noneColor), transp=0 ) plotshape( bullCond ? obv_osc[lbR] : na, offset=-lbR, title="Regular Bullish Label", text=" Bull ", style=shape.labelup, location=location.absolute, color=bullColor, textcolor=textColor, transp=0 ) //------------------------------------------------------------------------------ // Hidden Bullish // Osc: Lower Low oscLL = obv_osc[lbR] < valuewhen(plFound, obv_osc[lbR], 1) and _inRange(plFound[1]) // Price: Higher Low priceHL = low[lbR] > valuewhen(plFound, low[lbR], 1) hiddenBullCond = plotHiddenBull and priceHL and oscLL and plFound plot( plFound ? obv_osc[lbR] : na, offset=-lbR, title="Hidden Bullish", linewidth=2, color=(hiddenBullCond ? hiddenBullColor : noneColor), transp=0 ) plotshape( hiddenBullCond ? obv_osc[lbR] : na, offset=-lbR, title="Hidden Bullish Label", text=" H Bull ", style=shape.labelup, location=location.absolute, color=bullColor, textcolor=textColor, transp=0 ) //------------------------------------------------------------------------------ // Regular Bearish // Osc: Lower High oscLH = obv_osc[lbR] < valuewhen(phFound, obv_osc[lbR], 1) and _inRange(phFound[1]) // Price: Higher High priceHH = high[lbR] > valuewhen(phFound, high[lbR], 1) bearCond = plotBear and priceHH and oscLH and phFound plot( phFound ? obv_osc[lbR] : na, offset=-lbR, title="Regular Bearish", linewidth=2, color=(bearCond ? bearColor : noneColor), transp=0 ) plotshape( bearCond ? obv_osc[lbR] : na, offset=-lbR, title="Regular Bearish Label", text=" Bear ", style=shape.labeldown, location=location.absolute, color=bearColor, textcolor=textColor, transp=0 ) //------------------------------------------------------------------------------ // Hidden Bearish // Osc: Higher High oscHH = obv_osc[lbR] > valuewhen(phFound, obv_osc[lbR], 1) and _inRange(phFound[1]) // Price: Lower High priceLH = high[lbR] < valuewhen(phFound, high[lbR], 1) hiddenBearCond = plotHiddenBear and priceLH and oscHH and phFound plot( phFound ? obv_osc[lbR] : na, offset=-lbR, title="Hidden Bearish", linewidth=2, color=(hiddenBearCond ? hiddenBearColor : noneColor), transp=0 ) plotshape( hiddenBearCond ? obv_osc[lbR] : na, offset=-lbR, title="Hidden Bearish Label", text=" H Bear ", style=shape.labeldown, location=location.absolute, color=bearColor, textcolor=textColor, transp=0 )
内容的提问来源于stack exchange,提问作者HalSkyTrader
相关产品推荐
相关产品推荐

