测试策略中未声明的变量

时间:2020-11-03 21:59:46

标签: pine-script

enter image description here我正在尝试将此指标添加到我的测试中,但遇到错误,我熟悉python,但在pinescript中我不明白为什么将e1变量用作e1和e1 {{3 }}在同一行中,并且根据网络,我无法使用控制台甚至看不到e1的值

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © quechon
//@version=4
strategy('Testing indicators', process_orders_on_close=true)

//time to backtest from
start = timestamp(2017, 01, 01, 0, 0, 0)
end = timestamp(2020, 11, 03, 0, 0, 0)

var float e1 = 0

//fxSniper
CCI_Period = 14
T3_Period = 3 //default is 5
b = 0.618
xPrice = close
b2 = b*b
b3 = b2*b
c1 = -b3
c2 = (3*(b2 + b3))
c3 = -3*(2*b2 + b + b3)
c4 = (1 + 3*b + b3 + 3*b2)
nn = iff(T3_Period < 1, 1, T3_Period)
nr = 1 + 0.5*(nn - 1)
w1 = 2 / (nr + 1)
w2 = 1 - w1    
xcci = cci(xPrice, CCI_Period)
e1 = w1*xcci + w2*nz(e1[1])
e2 = w1*e1 + w2*nz(e2[1])
e3 = w1*e2 + w2*nz(e3[1])
e4 = w1*e3 + w2*nz(e4[1])
e5 = w1*e4 + w2*nz(e5[1])
e6 = w1*e5 + w2*nz(e6[1])
xccir = c1*e6 + c2*e5 + c3*e4 + c4*e3  

//buy long when price crosses above dpo
long = xccir > 0
    
// short when price is below dpo
short = xccir < 0

if time >= start and time <= end
    
    
    //strategy to buy according to dpo
    if long
        strategy.close('Short')
        strategy.entry('Long', strategy.long, 10000)
        
    //strategy to go short
    if short
        strategy.close('Long')
        strategy.entry('Short', strategy.short, 10000)

收到此错误后无法弄清楚如何解决

Add to chart operation failed. reason: line 27: Undeclared identifier 'e1'
    

进行了一些搜索,但无法弄清

原始脚本

/////////////////////////////////////////////////////////////
//  Copyright by HPotter v1.0 25/07/2014
// This simple indicator gives you a lot of useful information - when to enter, when to exit
// and how to reduce risks by entering a trade on a double confirmed signal.
// You can use in the xPrice any series: Open, High, Low, Close, HL2, HLC3, OHLC4 and ect...
////////////////////////////////////////////////////////////
study(title="FX Sniper:  T3-CCI", shorttitle="T3-CCI")
CCI_Period = input(14, minval=1)
T3_Period = input(5, minval=1)
b = input(0.618)
hline(0, color=purple, linestyle=line)
xPrice = close
plot(xPrice)
b2 = b*b
b3 = b2*b
c1 = -b3
c2 = (3*(b2 + b3))
c3 = -3*(2*b2 + b + b3)
c4 = (1 + 3*b + b3 + 3*b2)
nn = iff(T3_Period < 1, 1, T3_Period)
nr = 1 + 0.5*(nn - 1)
w1 = 2 / (nr + 1)
w2 = 1 - w1    
xcci = cci(xPrice, CCI_Period)
e1 = w1*xcci + w2*nz(e1[1])
e2 = w1*e1 + w2*nz(e2[1])
e3 = w1*e2 + w2*nz(e3[1])
e4 = w1*e3 + w2*nz(e4[1])
e5 = w1*e4 + w2*nz(e5[1])
e6 = w1*e5 + w2*nz(e6[1])
xccir = c1*e6 + c2*e5 + c3*e4 + c4*e3  
cciHcolor = iff(xccir >= 0 , green,
        iff(xccir < 0, red, black))
plot(xccir, color=blue, title="T3-CCI")
plot(xccir, color=cciHcolor, title="CCIH", style = histogram)

1 个答案:

答案 0 :(得分:0)

请参见Expressions, declarations and statements

您要声明一个可变变量e1,因此必须使用:=为其赋值。 您还应该将变量e2e6声明为可变变量,因为您正在使用e2[1]来引用它们的先前值。
由于您没有显式声明e2,因此它是在首次使用时声明的。
同时,您正在尝试引用它的历史记录,当时还不存在。

这将起作用。
我还修改了一些代码以利用现有功能,并将一些变量转换为输入,因此您可以在指标的设置中更改它们,而无需更改源代码。

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © quechon
//@version=4
strategy('Testing indicators', process_orders_on_close=true)

//time to backtest from
start = timestamp(2017, 01, 01, 0, 0, 0)
end = timestamp(2020, 11, 03, 0, 0, 0)

var float e1 = 0
var float e2 = 0
var float e3 = 0
var float e4 = 0
var float e5 = 0
var float e6 = 0

//fxSniper
var int CCI_Period = input(14, "CCI_Period", minval=0)
var int T3_Period = input(3, "T3_Period", minval=0) //default is 5
b = 0.618
xPrice = input(close, "xPrice", type=input.source)
b2 = pow(b,2)
b3 = pow(b,3)
c1 = -b3
c2 = (3*(b2 + b3))
c3 = -3*(2*b2 + b + b3)
c4 = (1 + 3*b + b3 + 3*b2)
nn = T3_Period < 1 ? 1 : T3_Period
nr = 1 + 0.5*(nn - 1)
w1 = 2 / (nr + 1)
w2 = 1 - w1    
xcci = cci(xPrice, CCI_Period)
e1 := w1*xcci + w2*nz(e1[1])
e2 := w1*e1 + w2*nz(e2[1])
e3 := w1*e2 + w2*nz(e3[1])
e4 := w1*e3 + w2*nz(e4[1])
e5 := w1*e4 + w2*nz(e5[1])
e6 := w1*e5 + w2*nz(e6[1])
xccir = c1*e6 + c2*e5 + c3*e4 + c4*e3  

//buy long when price crosses above dpo
long = crossover(xccir, 0) 
    
// short when price is below dpo
short = crossunder(xccir, 0)

if time >= start and time <= end
    //strategy to buy according to dpo
    if long
        strategy.close('Short')
        strategy.entry('Long', strategy.long, 10000)
        
    //strategy to go short
    if short
        strategy.close('Long')
        strategy.entry('Short', strategy.short, 10000)