首页 > 解决方案 > 未知标识符错误,我的策略不会添加到图表中,因为 pine 说它需要输出函数

问题描述

因此,我正在使用 pine 脚本创建一个交易系统,即使我已经创建了变量,我也不断得到一个未声明的标识符,然后第二个问题是我的策略不会添加到图表中,因为它说我需要一个输出函数,但我有strategy.entry 函数,所以我很困惑,有人可以看看我的脚本并告诉我如何解决它吗?

  strategy(title ="My Long term strategy", overlay=true)

//DEFINITIONS
shortMa = ema(close,8)
longMa = ema(close, 14)
//MACD indicator
macdShort = input(12, minval=1, title ="MACD Fast length")
macdLong = input(26, minval=1, title = "MACD Slow Length")
macdSignal = input(9, minval=1, title = "MACD Signal Length")
mac= macd(close,macdShort, macdLong, macdSignal)
MACDLine= (macdShort - macdLong) 
//SignalLine= 9
//MACDHistogram: MACDLine - SignalLine

//Stochastic indicator
Length = input(14, minval = 1, title = "stochastic Length")
k= input(3,minval = 1, title= "stochastic%k")
d= input(3,minval = 1, title= "stochastic%d")
sto = stoch(close,highest(Length),lowest(Length), Length)
stoUppper = hline(80, title="Upper Limit",color=color.red, linestyle=hline.style_dotted)
stoLower = hline(20, title="Lower Limit", color=color.lime, linestyle=hline.style_dotted)

//VWAP indicator
v = vwap(hlc3)

// RSI indicator
src = close 
RSIlen = input(14, minval=1,title="RSI length")
EMAlen =input(10, minval=1 , title = "RSI EMA Length")
Up = rma(max(change(src),0), RSIlen)
Down = rma(-min(change(src), 0), RSIlen)
rsi= Down == 0 ? 100: Up == 0 ? 0:100 -(100/(1+Up/Down))
RSIEMA = ema(rsi,EMAlen)
band1 = hline(70, color=color.red, title = "Upper Line", linestyle=hline.style_solid)
band0 = hline(30, color=color.lime, title = "Lower Line", linestyle=hline.style_solid)
 
 //LOGIC
 longsignal = crossover(shortMa, longMa)
 macdlong = crossover(macdline, macdSignal)
 stolong = crossover(k and d, lowerlimit)
 vlong = crossover(close,v)
 RSIlong = crossover(rsi and RSIEMA, band0)
 
 //POSITIONS
strategy.entry(id = "longPosition", long=true, when=longsignal and macdlong and stolong and vlong and RSIlong)

标签: pine-script

解决方案


您的脚本存在结构性问题:

  1. 它在第一行没有//@version=4编译器指令来告诉编译器您正在使用哪个版本的 Pine。请参阅:https ://www.tradingview.com/pine-script-docs/en/v4/language/Versions.html
  2. 您有以空格开头的行。全局范围内的代码必须从第 0 列开始:https ://www.tradingview.com/pine-script-docs/en/v4/language/Structure_of_the_script.html
  3. 正如编译器错误消息告诉您的那样,您指的是未定义的变量名称。例如,第 40 行表示macdline尚未在该引用之前定义它。您MACDLine在第 12 行定义了一个变量;如果那是您要使用的那个,请以相同的方式拼写。Pine 区分变量名的大小写。

你还有其他问题。例如:

stolong = crossover(k and d, lowerlimit)

正如 refmancrossover()提到的那样,它的参数必须是“float”类型,但您正在尝试传递它k and d,这是一个基于两个“float”变量的布尔表达式本身,这是没有意义的。

你有很多工作要做,我们不能为你做这一切。这将使您至少从一个格式良好的脚本开始:

//@version=4
strategy(title ="My Long term strategy", overlay=true)

//DEFINITIONS
shortMa = ema(close,8)
longMa = ema(close, 14)
//MACD indicator
macdShort = input(12, minval=1, title ="MACD Fast length")
macdLong = input(26, minval=1, title = "MACD Slow Length")
macdSignal = input(9, minval=1, title = "MACD Signal Length")
mac= macd(close,macdShort, macdLong, macdSignal)
MACDLine= (macdShort - macdLong) 
//SignalLine= 9
//MACDHistogram: MACDLine - SignalLine

//Stochastic indicator
Length = input(14, minval = 1, title = "stochastic Length")
k= input(3,minval = 1, title= "stochastic%k")
d= input(3,minval = 1, title= "stochastic%d")
sto = stoch(close,highest(Length),lowest(Length), Length)
stoUppper = hline(80, title="Upper Limit",color=color.red, linestyle=hline.style_dotted)
stoLower = hline(20, title="Lower Limit", color=color.lime, linestyle=hline.style_dotted)

//VWAP indicator
v = vwap(hlc3)

// RSI indicator
src = close 
RSIlen = input(14, minval=1,title="RSI length")
EMAlen =input(10, minval=1 , title = "RSI EMA Length")
Up = rma(max(change(src),0), RSIlen)
Down = rma(-min(change(src), 0), RSIlen)
rsi= Down == 0 ? 100: Up == 0 ? 0:100 -(100/(1+Up/Down))
RSIEMA = ema(rsi,EMAlen)
band1 = hline(70, color=color.red, title = "Upper Line", linestyle=hline.style_solid)
band0 = hline(30, color=color.lime, title = "Lower Line", linestyle=hline.style_solid)

//LOGIC
longsignal = crossover(shortMa, longMa)
macdlong = crossover(macdline, macdSignal)
stolong = crossover(k and d, lowerlimit)
vlong = crossover(close,v)
RSIlong = crossover(rsi and RSIEMA, band0)
 
 //POSITIONS
strategy.entry(id = "longPosition", long=true, when=longsignal and macdlong and stolong and vlong and RSIlong)

推荐阅读