简体   繁体   中英

How to convert this pinescript v2 to v4?

I created an algo for tradingview, just stacking various indicators together. I would like to switch one with this keltner strat, but I am pretty bad when it comes to syntax. Could someone be so kind and point out what to change from V2 to V4? I looked into it, but was not able to sort it out :/

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

First, you need to convert from v2 to v3. There are a number of differences between the two versions , but only one is relevant here. In v2, a variable could refer to itself during its declaration; in v3, this is not possible so you have to declare it first and then change the values later on. In this case, you'd have to replace

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

with the following:

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

After that, change the version in the first line from v2 to v3 and you have a v3 script. The subsequent v3 -> v4 conversion can be done automatically in Pine Editor (select Convert to v4 in the More dropdown menu).

The resulting v4 code would look like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM