无法从 pine 编辑器中的函数访问值 [英] can't access value from function in pine editor

查看:96
本文介绍了无法从 pine 编辑器中的函数访问值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试 RSI-14 DI 反转策略,但无法从它们所在的函数访问 DI+ 或 DI-(变量为加"和减").任何想法如何访问它们?代码如下:

I am trying to test out an RSI-14 DI reversal strategy, but am unable to access the DI+ or DI- (variables are "plus" and "minus") from the function that they are in. Any ideas on how to access them? Here is the code:

    //@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)

按添加到图表"后出现这些错误

These errors come up after I press "Add to Chart"

Add to Chart operation failed, reason: line 46: Undeclared identifier 'plus';
line 47: Undeclared identifier 'plus';
line 48: Undeclared identifier 'minus';
line 49: Undeclared identifier 'minus';
line 51: Undeclared identifier 'plus_ob';
line 51: Undeclared identifier 'minus_ob';
line 52: Undeclared identifier 'plus_os';
line 55: Undeclared identifier 'isShortEntry';
line 57: Undeclared identifier 'isShortExit';
line 63: Undeclared identifier 'isLongEntry';
line 64: Undeclared identifier 'isShortEntry'

由于第 46 行的错误,它在第 55-64 行产生了错误.

Because of the error on line 46, it creates the errors on lines 55-64.

感谢任何帮助,谢谢!

推荐答案

不能在全局范围内访问函数局部变量.
请参阅范围脚本

You cannot access function-local variables in the global scope.
See Scopes in the script

此代码编译,并在全局范围内获取您的 plusminus 变量.

This code compiles, and gets your plus and minus variables in the global scope.

//@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)

这篇关于无法从 pine 编辑器中的函数访问值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆