怎么把pinescript v2转换成v4?

时间:2020-11-10 09:16:48

标签: pine-script

我创建了一个用于交易视图的算法,只是将各种指标堆叠在一起。我想用这个keltner策略切换一个,但是在语法方面我很糟糕。有人会这么友善,指出从V2更改为V4的方法吗?我调查了一下,但无法将其整理出来:/

//@version=2
////////////////////////////////////////////////////////////
//  Copyright by HPotter v1.0 30/08/2018
// The Keltner Channel, a classic indicator 
// of technical analysis developed by Chester Keltner in 1960. 
// The indicator is a bit like Bollinger Bands and Envelopes.
//
// You can change long to short in the Input Settings
// WARNING:
// - For purpose educate only
// - This script to change bars colors.
////////////////////////////////////////////////////////////
strategy(title="Keltner Channel Backtest", overlay = true)
nPeriod = input(title="Period", type=integer, defval=10, minval=1)
xPrice = sma(hlc3, nPeriod)
xMove = sma(high - low, nPeriod)
reverse = input(false, title="Trade reverse")
xUpper = xPrice + xMove
xLower = xPrice - xMove
pos = iff(close < xLower, -1,
       iff(close > xUpper, 1, nz(pos[1], 0))) 
possig = iff(reverse and pos == 1, -1,
          iff(reverse and pos == -1, 1, pos))       
if (possig == 1) 
    strategy.entry("Long", strategy.long)
if (possig == -1)
    strategy.entry("Short", strategy.short)         
barcolor(possig == -1 ? red: possig == 1 ? green : blue ) 
plot(xPrice, color=blue, title="KSmid")
plot(xUpper, color=red, title="KSup")
plot(xLower, color=green, title="KSdn")

1 个答案:

答案 0 :(得分:0)

首先,您需要从v2转换为v3。 There are a number of differences between the two versions,但此处只涉及一个。在v2中,变量可以在声明期间引用自身;在v3中,这是不可能的,因此您必须先声明它,然后再更改值。在这种情况下,您必须替换

pos = iff(close < xLower, -1,
       iff(close > xUpper, 1, nz(pos[1], 0))) 

具有以下内容:

pos = 0.0
pos := iff(close < xLower, -1,
       iff(close > xUpper, 1, nz(pos[1], 0))) 

然后,将第一行中的版本从v2更改为v3,然后您便拥有了v3脚本。后续的v3-> v4转换可以在Pine Editor中自动完成(在Convert to v4下拉菜单中选择More)。

生成的v4代码如下所示:

//@version=4
////////////////////////////////////////////////////////////
//  Copyright by HPotter v1.0 30/08/2018
// The Keltner Channel, a classic indicator 
// of technical analysis developed by Chester Keltner in 1960. 
// The indicator is a bit like Bollinger Bands and Envelopes.
//
// You can change long to short in the Input Settings
// WARNING:
// - For purpose educate only
// - This script to change bars colors.
////////////////////////////////////////////////////////////
strategy(title="Keltner Channel Backtest", overlay=true)
nPeriod = input(title="Period", type=input.integer, defval=10, minval=1)
xPrice = sma(hlc3, nPeriod)
xMove = sma(high - low, nPeriod)
reverse = input(false, title="Trade reverse")
xUpper = xPrice + xMove
xLower = xPrice - xMove
pos = 0.0
pos := iff(close < xLower, -1, iff(close > xUpper, 1, nz(pos[1], 0)))
possig = iff(reverse and pos == 1, -1, iff(reverse and pos == -1, 1, pos))
if possig == 1
    strategy.entry("Long", strategy.long)
if possig == -1
    strategy.entry("Short", strategy.short)
barcolor(possig == -1 ? color.red : possig == 1 ? color.green : color.blue)
plot(xPrice, color=color.blue, title="KSmid")
plot(xUpper, color=color.red, title="KSup")
plot(xLower, color=color.green, title="KSdn")