Pine script can';t从pine编辑器中的函数访问值

Pine script can';t从pine编辑器中的函数访问值,pine-script,trading,algorithmic-trading,Pine Script,Trading,Algorithmic Trading,我试图测试RSI-14 DI反转策略,但无法从它们所在的函数中访问DI+或DI-(变量为“正”和“负”)。关于如何访问它们有什么想法吗?代码如下: //@version=4 strategy("RSI-14, DI+, DI- Reversal Strategy", overlay=false) /// DI+ DI- Code /// /// DI+ is the variable called "plus" and DI- is the v

我试图测试RSI-14 DI反转策略,但无法从它们所在的函数中访问DI+或DI-(变量为“正”和“负”)。关于如何访问它们有什么想法吗?代码如下:

    //@version=4
strategy("RSI-14, DI+, DI- Reversal Strategy", overlay=false)


/// DI+ DI- Code /// /// DI+ is the variable called "plus" and DI- is the variable called "minus"
adxlen = input(14, title="ADX Smoothing")
dilen = input(14, title="DI Length")
dirmov(len) =>
    up = change(high)
    down = -change(low)
    plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
    minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
    truerange = rma(tr, len)
    plus = fixnan(100 * rma(plusDM, len) / truerange)
    minus = fixnan(100 * rma(minusDM, len) / truerange)
    [plus, minus]
adx(dilen, adxlen) =>
    [plus, minus] = dirmov(dilen)
    sum = plus + minus
    adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)
sig = adx(dilen, adxlen)
plot(sig, color=color.red, title="ADX")


///// RSI Code /////
rsi = rsi(close,14)
//rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsi_ob = rsi >= 70
rsi_os = rsi <= 30


///// MovAvg Code /////
sma5 = sma(close,5)
sma200 = sma(close,200)


////////// Strategy Entries and Exits ////////// 

t = time(timeframe.period, "0830-1500")
session_open = na(t) ? false : true


plus_ob = plus[1] >= 60 and plus[1] > plus
plus_os = plus <= 15
minus_ob = minus[1] <= 10 and minus[1] < minus
minus_os = minus >= 55

isShortEntry = rsi_ob and plus_ob and minus_ob and close < open
isShortExit = plus_os

if (session_open)
    strategy.entry("Short", strategy.short, 100.0, when = isShortEntry)
    
    strategy.close("Short", when = isShortExit)
else
    strategy.close_all()

bgcolor(session_open ? color.green : na)

plotshape(isLongEntry, style=shape.arrowup, color=color.green, location=location.bottom)
plotshape(isShortEntry, style=shape.arrowdown, color=color.red, location=location.top)
由于第46行的错误,它在第55-64行创建了错误


非常感谢您的帮助,谢谢

您无法访问全局范围内的函数局部变量。

此代码编译并获取全局范围内的
加号
减号
变量

//@version=4
strategy("RSI-14, DI+, DI- Reversal Strategy", overlay=false)

/// DI+ DI- Code /// /// DI+ is the variable called "plus" and DI- is the variable called "minus"
adxlen = input(14, title="ADX Smoothing")
dilen = input(14, title="DI Length")
dirmov(len) =>
    up = change(high)
    down = -change(low)
    plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
    minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
    truerange = rma(tr, len)
    plus = fixnan(100 * rma(plusDM, len) / truerange)
    minus = fixnan(100 * rma(minusDM, len) / truerange)
    [plus, minus]
adx(dilen, adxlen) =>
    [plus, minus] = dirmov(dilen)
    sum = plus + minus
    adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)
sig = adx(dilen, adxlen)
plot(sig, color=color.red, title="ADX")


///// RSI Code /////
rsi = rsi(close,14)
//rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsi_ob = rsi >= 70
rsi_os = rsi <= 30


///// MovAvg Code /////
sma5 = sma(close,5)
sma200 = sma(close,200)


////////// Strategy Entries and Exits ////////// 

t = time(timeframe.period, "0830-1500")
session_open = na(t) ? false : true

// STACKOVERFLOW ADDED CODE - START
var float plus  = na
var float minus = na

[myPlus, myMinus] = dirmov(dilen)

plus  := myPlus
minus := myMinus
// STACKOVERFLOW ADDED CODE - END

plus_ob = plus[1] >= 60 and plus[1] > plus
plus_os = plus <= 15
minus_ob = minus[1] <= 10 and minus[1] < minus
minus_os = minus >= 55

isShortEntry = rsi_ob and plus_ob and minus_ob and close < open
isShortExit = plus_os

if (session_open)
    strategy.entry("Short", strategy.short, 100.0, when = isShortEntry)
    
    strategy.close("Short", when = isShortExit)
else
    strategy.close_all()

bgcolor(session_open ? color.green : na)

// plotshape(isLongEntry, style=shape.arrowup, color=color.green, location=location.bottom) // STACKOVERFLOW: You haven't declared this variable yet, so I commented it out.
plotshape(isShortEntry, style=shape.arrowdown, color=color.red, location=location.top)
/@version=4
策略(“RSI-14,DI+,DI-反转策略”,叠加=假)
///DI+DI-Code/////DI+是名为“加”的变量,DI-是名为“减”的变量
adxlen=输入(14,title=“ADX平滑”)
迪伦=输入(14,title=“DI长度”)
迪莫夫(len)=>
上升=变化(高)
向下=-变化(低)
plusDM=na(向上)?na:(向上>向下和向上>0?向上:0)
minusDM=na(向下)?na:(向下>上下>0?向下:0)
truerange=rma(tr,len)
plus=fixnan(100*rma(plusDM,len)/truerange)
减=固定值(100*rma(最小值,最小值)/truerange)
[加,减]
adx(迪伦,阿德克斯伦)=>
[加,减]=迪尔莫夫(迪伦)
总和=正+负
adx=100*rma(绝对绝对值(加减)/(总和=0?1:总和),adxlen)
sig=adx(迪伦,阿德克斯伦)
绘图(sig,color=color.red,title=“ADX”)
/////RSI码/////
rsi=rsi(收盘,14)
//rsi=下降==0?100:up==0?0:100-(100/(1+上/下))
rsi_ob=rsi>=70
rsi_os=rsi=60加上[1]>加上
加号=加号
//@version=4
strategy("RSI-14, DI+, DI- Reversal Strategy", overlay=false)

/// DI+ DI- Code /// /// DI+ is the variable called "plus" and DI- is the variable called "minus"
adxlen = input(14, title="ADX Smoothing")
dilen = input(14, title="DI Length")
dirmov(len) =>
    up = change(high)
    down = -change(low)
    plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
    minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
    truerange = rma(tr, len)
    plus = fixnan(100 * rma(plusDM, len) / truerange)
    minus = fixnan(100 * rma(minusDM, len) / truerange)
    [plus, minus]
adx(dilen, adxlen) =>
    [plus, minus] = dirmov(dilen)
    sum = plus + minus
    adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)
sig = adx(dilen, adxlen)
plot(sig, color=color.red, title="ADX")


///// RSI Code /////
rsi = rsi(close,14)
//rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsi_ob = rsi >= 70
rsi_os = rsi <= 30


///// MovAvg Code /////
sma5 = sma(close,5)
sma200 = sma(close,200)


////////// Strategy Entries and Exits ////////// 

t = time(timeframe.period, "0830-1500")
session_open = na(t) ? false : true

// STACKOVERFLOW ADDED CODE - START
var float plus  = na
var float minus = na

[myPlus, myMinus] = dirmov(dilen)

plus  := myPlus
minus := myMinus
// STACKOVERFLOW ADDED CODE - END

plus_ob = plus[1] >= 60 and plus[1] > plus
plus_os = plus <= 15
minus_ob = minus[1] <= 10 and minus[1] < minus
minus_os = minus >= 55

isShortEntry = rsi_ob and plus_ob and minus_ob and close < open
isShortExit = plus_os

if (session_open)
    strategy.entry("Short", strategy.short, 100.0, when = isShortEntry)
    
    strategy.close("Short", when = isShortExit)
else
    strategy.close_all()

bgcolor(session_open ? color.green : na)

// plotshape(isLongEntry, style=shape.arrowup, color=color.green, location=location.bottom) // STACKOVERFLOW: You haven't declared this variable yet, so I commented it out.
plotshape(isShortEntry, style=shape.arrowdown, color=color.red, location=location.top)