PineScript 从 v3 到 v4 - "is already defined" 重新分配问题
PineScript from v3 to v4 - "is already defined" Reassignment problem
我正在尝试将下面的脚本从 Pine Script 版本 3 转换为版本 4。但是,我收到一条错误消息,提示编译失败,第 28:30 行:“obv”已定义。我试图在分配函数 (=>) 之前在 obv(src) 上添加重新分配运算符 (:=),但它没有用。有人知道如何解决这个问题吗?
谢谢
//@version=3
//
// @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 = red
bullColor = green
hiddenBullColor = color(green, 80)
hiddenBearColor = color(red, 80)
textColor = white
noneColor = color(white, 100)
obv(src) => cum(change(src) > 0 ? volume : change(src) < 0 ? -volume : 0*volume)
os=obv(src)
obv_osc = (os - ema(os,len))
obc_color=obv_osc > 0 ? green : red
plot(obv_osc, color=obc_color, style=line,title="OBV-Points", linewidth=2)
plot(obv_osc, color=silver, transp=70, title="OBV", 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
)
Pine V4 有自己的内置函数 obv()。因此,您必须将 obv()
重命名为 obv_f()
,或者删除此函数并使用内置的
//@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)
obv_f(src) =>
change_1 = change(src)
cum(change(src) > 0 ? volume : change_1 < 0 ? -volume : 0 * volume)
os = obv_f(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)
将其转换为 V5 也很有意义。
我正在尝试将下面的脚本从 Pine Script 版本 3 转换为版本 4。但是,我收到一条错误消息,提示编译失败,第 28:30 行:“obv”已定义。我试图在分配函数 (=>) 之前在 obv(src) 上添加重新分配运算符 (:=),但它没有用。有人知道如何解决这个问题吗?
谢谢
//@version=3
//
// @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 = red
bullColor = green
hiddenBullColor = color(green, 80)
hiddenBearColor = color(red, 80)
textColor = white
noneColor = color(white, 100)
obv(src) => cum(change(src) > 0 ? volume : change(src) < 0 ? -volume : 0*volume)
os=obv(src)
obv_osc = (os - ema(os,len))
obc_color=obv_osc > 0 ? green : red
plot(obv_osc, color=obc_color, style=line,title="OBV-Points", linewidth=2)
plot(obv_osc, color=silver, transp=70, title="OBV", 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
)
Pine V4 有自己的内置函数 obv()。因此,您必须将 obv()
重命名为 obv_f()
,或者删除此函数并使用内置的
//@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)
obv_f(src) =>
change_1 = change(src)
cum(change(src) > 0 ? volume : change_1 < 0 ? -volume : 0 * volume)
os = obv_f(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)
将其转换为 V5 也很有意义。