Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

pine script - Having problems with converting pinescript v2 into v3

I want to convert this fragment of code from v2 to version3 or 4 in Pinescript. I try to follow some tutorials but I didn't find any information how to deal with conversion between versions

//@version=3

strategy(title = "Strategy_upgrade_try", shorttitle = "Symphony 1.1", overlay = false, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, pyramiding = 20)

//Settings

//needlong = input(true, defval = true, title = "Long")
//needshort = input(true, defval = true, title = "Short")

capital = input(100, defval = 100, minval = 1, maxval = 10000, title = "Lot, %")
usersi1 = input(true, defval = true, title = "Use RSI 1")
rsiperiod1 = input(4, defval = 4, minval = 2, maxval = 100, title = "RSI 1 Period")
rsilimit1 = input(20, defval = 20, minval = 2, maxval = 50, title = "RSI 1 Limit")
usersi2 = input(true, defval = true, title = "Use RSI 2")
rsiperiod2 = input(7, defval = 7, minval = 2, maxval = 100, title = "RSI 2 Period")
rsilimit2 = input(25, defval = 25, minval = 2, maxval = 50, title = "RSI 2 Limit")
usersi3 = input(true, defval = true, title = "Use RSI 3")
rsiperiod3 = input(14, defval = 14, minval = 2, maxval = 100, title = "RSI 3 Period")
rsilimit3 = input(30, defval = 30, minval = 2, maxval = 50, title = "RSI 3 Limit")
usersi4 = input(false, defval = false, title = "Use RSI 4")
rsiperiod4 = input(21, defval = 21, minval = 2, maxval = 100, title = "RSI 4 Period")
rsilimit4 = input(35, defval = 35, minval = 2, maxval = 50, title = "RSI 4 Limit")
usersi5 = input(false, defval = false, title = "Use RSI 5")
rsiperiod5 = input(28, defval = 28, minval = 2, maxval = 100, title = "RSI 5 Period")
rsilimit5 = input(40, defval = 40, minval = 2, maxval = 50, title = "RSI 5 Limit")
cf = input(false, defval = false, title = "Use color filter")
fromyear = input(1900, defval = 1900, minval = 1900, maxval = 2100, title = "From Year")
toyear = input(2100, defval = 2100, minval = 1900, maxval = 2100, title = "To Year")
frommonth = input(01, defval = 01, minval = 01, maxval = 12, title = "From Month")
tomonth = input(12, defval = 12, minval = 01, maxval = 12, title = "To Month")
fromday = input(01, defval = 01, minval = 01, maxval = 31, title = "From Day")
today = input(31, defval = 31, minval = 01, maxval = 31, title = "To Day")

//RSI
rsi1 = rsi(close, rsiperiod1)
rsi2 = rsi(close, rsiperiod2)
rsi3 = rsi(close, rsiperiod3)
rsi4 = rsi(close, rsiperiod4)
rsi5 = rsi(close, rsiperiod5)

//Signals
up1 = rsi1 < rsilimit1 and usersi1  
up2 = rsi2 < rsilimit2 and usersi2
up3 = rsi3 < rsilimit3 and usersi3
up4 = rsi4 < rsilimit4 and usersi4
up5 = rsi5 < rsilimit5 and usersi5

str = up5 ? 5 : up4 ? 4 : up3 ? 3 : up2 ? 2 : up1 ? 1 : str[1]
up = up1 or up2 or up3 or up4 or up5
exit = (rsi1 > rsilimit1 and str == 1) or (rsi2 > rsilimit2 and str == 2) or (rsi3 > rsilimit3 and str == 3) or (rsi4 > rsilimit4 and str == 4) or (rsi5 > rsilimit5 and str == 5)
lot = strategy.position_size == 0 ? strategy.equity / close * capital / 100 : lot[1]

//Background
col = up ? lime : na
bgcolor(col, transp = 0)

//Trading
if up and (close < open or cf == false)
    strategy.entry("Long", strategy.long, lot, when=(time > timestamp(fromyear, frommonth, fromday, 00, 00) and time < timestamp(toyear, tomonth, today, 23, 59)))
 
if time > timestamp(toyear, tomonth, today, 23, 59) or exit
    strategy.close_all()

In console I am getting:

Processing script...
line 48: Undeclared identifier `str`;
line 50: Undeclared identifier `str`;
line 51: Undeclared identifier `lot`;

I am totally new to this, I will appreciate any help.

question from:https://stackoverflow.com/questions/66050817/having-problems-with-converting-pinescript-v2-into-v3

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I've converted it to version 4.
Problem was that you were trying to use a historical value [1] on str and lot that did not exist yet. So you have to define them first, like this:

var int     str = na
var float   lot = na

Then you need to assign a value to them with := instead of =

Also, I've removed the defval sections of your input functions, because the first parameter is already the defval. No need to define defval twice, because the compiler sees that as an error.

This will work:

//@version=4

strategy(title = "Strategy_upgrade_try", shorttitle = "Symphony 1.1", overlay = false, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, pyramiding = 20)

//Settings

//needlong = input(true, defval = true, title = "Long")
//needshort = input(true, defval = true, title = "Short")

capital     = input(100, minval = 1, maxval = 10000, title = "Lot, %")
usersi1     = input(true, title = "Use RSI 1")
rsiperiod1  = input(4, minval = 2, maxval = 100, title = "RSI 1 Period")
rsilimit1   = input(20, minval = 2, maxval = 50, title = "RSI 1 Limit")
usersi2     = input(true, title = "Use RSI 2")
rsiperiod2  = input(7, minval = 2, maxval = 100, title = "RSI 2 Period")
rsilimit2   = input(25, minval = 2, maxval = 50, title = "RSI 2 Limit")
usersi3     = input(true, title = "Use RSI 3")
rsiperiod3  = input(14, minval = 2, maxval = 100, title = "RSI 3 Period")
rsilimit3   = input(30, minval = 2, maxval = 50, title = "RSI 3 Limit")
usersi4     = input(false, title = "Use RSI 4")
rsiperiod4  = input(21, minval = 2, maxval = 100, title = "RSI 4 Period")
rsilimit4   = input(35, minval = 2, maxval = 50, title = "RSI 4 Limit")
usersi5     = input(false, title = "Use RSI 5")
rsiperiod5  = input(28, minval = 2, maxval = 100, title = "RSI 5 Period")
rsilimit5   = input(40, minval = 2, maxval = 50, title = "RSI 5 Limit")
cf          = input(false, title = "Use color filter")
fromyear    = input(1900, minval = 1900, maxval = 2100, title = "From Year")
toyear      = input(2100, minval = 1900, maxval = 2100, title = "To Year")
frommonth   = input(01, minval = 01, maxval = 12, title = "From Month")
tomonth     = input(12, minval = 01, maxval = 12, title = "To Month")
fromday     = input(01, minval = 01, maxval = 31, title = "From Day")
today       = input(31, minval = 01, maxval = 31, title = "To Day")

var int     str = na
var float   lot = na

//RSI
rsi1 = rsi(close, rsiperiod1)
rsi2 = rsi(close, rsiperiod2)
rsi3 = rsi(close, rsiperiod3)
rsi4 = rsi(close, rsiperiod4)
rsi5 = rsi(close, rsiperiod5)

//Signals
up1 = rsi1 < rsilimit1 and usersi1  
up2 = rsi2 < rsilimit2 and usersi2
up3 = rsi3 < rsilimit3 and usersi3
up4 = rsi4 < rsilimit4 and usersi4
up5 = rsi5 < rsilimit5 and usersi5

str     := up5 ? 5 : up4 ? 4 : up3 ? 3 : up2 ? 2 : up1 ? 1 : str[1]
up      = up1 or up2 or up3 or up4 or up5
exit    = (rsi1 > rsilimit1 and str == 1) or (rsi2 > rsilimit2 and str == 2) or (rsi3 > rsilimit3 and str == 3) or (rsi4 > rsilimit4 and str == 4) or (rsi5 > rsilimit5 and str == 5)
lot     := strategy.position_size == 0 ? strategy.equity / close * capital / 100 : lot[1]

//Background
col = up ? color.lime : na
bgcolor(col, transp = 0)

//Trading
if up and (close < open or cf == false)
    strategy.entry("Long", strategy.long, lot, when=(time > timestamp(fromyear, frommonth, fromday, 00, 00) and time < timestamp(toyear, tomonth, today, 23, 59)))
 
if time > timestamp(toyear, tomonth, today, 23, 59) or exit
    strategy.close_all()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...