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

Categories

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

Error in converting pine scipt from version two to four

enter image description hereI had a version 2 pine script to plot vwaps which I converted to version 4. after having a closer look at the converted script, i noticed something abnormal.(picture for version 4 has been attached,in version 2 the values for green,orange and yellow lines changed on the same date i.e 31st dec unlike the picture below). In the version 2 script, all of the vwaps changed their value on the same day(31st dec) while in the new version they tend to do it on different days(29,30,31st). Codes have been attached below, cant't seem to figure out what's going wrong. These scripts can be added to any asset and the difference between the two is evident during the time of new year.(31st dec 2020) Version 2

study("MTF", overlay=true)


sd = input(true, title="Show Daily VWAP?")
sw = input(true, title="Show Weekly VWAP?")
sm = input(true, title="Show Monthly VWAP?")
sq = input(true, title="Show Qly VWAP?")
sy = input(true, title="Show Yly VWAP?")
spd = input(true, title="Show Previous Day's Daily VWAP?")



startd = security(tickerid, "D", time)
startw = security(tickerid, "W", time)
startm = security(tickerid, "M", time)
startq = security(tickerid, "3M", time)
starty = security(tickerid, "12M", time)


newSessiond = iff(change(startd), 1, 0)
newSessionw = iff(change(startw), 1, 0)
newSessionm = iff(change(startm), 1, 0)
newSessionq = iff(change(startq), 1, 0)
newSessiony = iff(change(starty), 1, 0)

vwapsumd = iff(newSessiond, hl2*volume, vwapsumd[1]+hl2*volume)
vwapsumw = iff(newSessionw, hl2*volume, vwapsumw[1]+hl2*volume)
vwapsumm = iff(newSessionm, hl2*volume, vwapsumm[1]+hl2*volume)
vwapsumq = iff(newSessionq, hl2*volume, vwapsumq[1]+hl2*volume)
vwapsumy = iff(newSessiony, hl2*volume, vwapsumy[1]+hl2*volume)

volumesumd = iff(newSessiond, volume, volumesumd[1]+volume)
volumesumw = iff(newSessionw, volume, volumesumw[1]+volume)
volumesumm = iff(newSessionm, volume, volumesumm[1]+volume)
volumesumq = iff(newSessionq, volume, volumesumq[1]+volume)
volumesumy = iff(newSessiony, volume, volumesumy[1]+volume)

v2sumd = iff(newSessiond, volume*hl2*hl2, v2sumd[1]+volume*hl2*hl2)
v2sumw = iff(newSessionw, volume*hl2*hl2, v2sumw[1]+volume*hl2*hl2)
v2summ = iff(newSessionm, volume*hl2*hl2, v2summ[1]+volume*hl2*hl2)
v2sumq = iff(newSessionq, volume*hl2*hl2, v2sumq[1]+volume*hl2*hl2)
v2sumy = iff(newSessiony, volume*hl2*hl2, v2sumy[1]+volume*hl2*hl2)

myvwapd = vwapsumd/volumesumd
myvwapm = vwapsumm/volumesumm
myvwapq = vwapsumq/volumesumq
myvwapy = vwapsumy/volumesumy
myvwapw = vwapsumw/volumesumw




plot(sd and myvwapd ? myvwapd : na,style=line, title="VWAP_DAILY", color=red)
plot(sw and myvwapw ? myvwapw : na,style=line, title="VWAP_WEEKLY", color=blue)
plot(sm and myvwapm ? myvwapm : na,style=line, title="VWAP_MONTHLY", color=green)
plot(sq and myvwapq ? myvwapq : na,style=line, title="VWAP_QUARTERLY", color=orange)
plot(sy and myvwapy ? myvwapy : na,style=line, title="VWAP_YEARLY", color=yellow)
/// alternative pd vwap code 
///dddd = iff(change(security(tickerid,"D",time)),1,0)
///ddd = valuewhen(dddd,myvwapd[1],0)
///plot(ddd,style=line,color=blue,linewidth = 2)
nsd = iff(change(security(tickerid, "D", time)), 1, 0)

getVWAP(ns) =>
    p = iff(ns, hlc3 * volume, p[1] + hlc3 * volume)
    vol = iff(ns, volume, vol[1] + volume)
    v = p / vol
    Sn = iff(ns, 0, Sn[1] + volume * (hlc3 - v[1]) * (hlc3 - v))
    std = sqrt(Sn / vol)
    [v, std]


[vD, stdevD] = getVWAP(nsd)

//vDplot = plot(vD, title = "Today's VWAP", color = #523584, style = line, transp = 10, linewidth = 2)
plot(spd and valuewhen(nsd, vD[1], 0) ? valuewhen(nsd, vD[1], 0) : na, title = "Previous day VWAP", color = #068E21, style = line, transp = 10, linewidth = 2)

Version 4

//@version=4
[![study("MULTITIMEFRAM", overlay=true)


sd = input(true, title="Show Daily VWAP?")
sw = input(true, title="Show Weekly VWAP?")
sm = input(true, title="Show Monthly VWAP?")
sq = input(true, title="Show Qly VWAP?")
sy = input(true, title="Show Yly VWAP?")
spd = input(true, title="Show Previous Day's Daily VWAP?")

//variables
var float vwapsumd      = na
var float vwapsumw      = na
var float vwapsumm      = na
var float vwapsumq      = na
var float vwapsumy      = na

var float volumesumd     = na
var float volumesumw     = na
var float volumesumm     = na
var float volumesumq     = na
var float volumesumy     = na

var float v2sumd      = na
var float v2sumw      = na
var float v2summ      = na
var float v2sumq      = na
var float v2sumy      = na


startd = security(syminfo.tickerid, "D", time)
startw = security(syminfo.tickerid, "W", time)
startm = security(syminfo.tickerid, "M", time)
startq = security(syminfo.tickerid, "3M", time)
starty = security(syminfo.tickerid, "12M", time)


newSessiond = iff(change(startd), 1, 0)
newSessionw = iff(change(startw), 1, 0)
newSessionm = iff(change(startm), 1, 0)
newSessionq = iff(change(startq), 1, 0)
newSessiony = iff(change(starty), 1, 0)

vwapsumd := iff(newSessiond, hl2*volume, vwapsumd[0]+hl2*volume)
vwapsumw := iff(newSessionw, hl2*volume, vwapsumw[0]+hl2*volume)
vwapsumm := iff(newSessionm, hl2*volume, vwapsumm[0]+hl2*volume)
vwapsumq := iff(newSessionq, hl2*volume, vwapsumq[0]+hl2*volume)
vwapsumy := iff(newSessiony, hl2*volume, vwapsumy[0]+hl2*volume)

volumesumd := iff(newSessiond, volume, volumesumd[0]+volume)
volumesumw := iff(newSessionw, volume, volumesumw[0]+volume)
volumesumm := iff(newSessionm, volume, volumesumm[0]+volume)
volumesumq := iff(newSessionq, volume, volumesumq[0]+volume)
volumesumy := iff(newSessiony, volume, volumesumy[0]+volume)

myvwapd = vwapsumd/volumesumd
myvwapm = vwapsumm/volumesumm
myvwapq = vwapsumq/volumesumq
myvwapy = vwapsumy/volumesumy
myvwapw = vwapsumw/volumesumw




plot(sd and myvwapd ? myvwapd : na, title="VWAP_DAILY", color=color.red)
plot(sw and myvwapw ? myvwapw : na, title="VWAP_WEEKLY", color=color.blue)
plot(sm and myvwapm ? myvwapm : na, title="VWAP_MONTHLY", color=color.green)
plot(sq and myvwapq ? myvwapq : na, title="VWAP_QUARTERLY", color=color.orange)
plot(sy and myvwapy ? myvwapy : na, title="VWAP_YEARLY", color=color.yellow)
/// alternative pd vwap code 
///dddd = iff(change(security(syminfo.tickerid,"D",time)),1,0)
///ddd = valuewhen(dddd,myvwapd[1],0)
///plot(ddd,style=line,color=blue,linewidth = 2)
nsd = iff(change(security(syminfo.tickerid, "D", time)), 1, 0)

getVWAP(ns) =>
    var float p             = na
    var float vol           = na
    var float Sn            = na
    p := iff(ns, hlc3 * volume, p[1] + hlc3 * volume)
    vol := iff(ns, volume, vol[1] + volume)
    v = p / vol
    Sn := iff(ns, 0, Sn[1] + volume * (hlc3 - v[1]) * (hlc3 - v))
    std = sqrt(Sn / vol)
    [v, std]


[vD, stdevD] = getVWAP(nsd)

//vDplot = plot(vD, title = "Today's VWAP", color = #523584, style = line, transp = 10, linewidth = 2)
plot(spd and valuewhen(nsd, vD[1], 0) ? valuewhen(nsd, vD[1], 0) : na, title = "Previous day VWAP", color = color.red,  linewidth = 2)][1]][1]

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

1 Answer

0 votes
by (71.8m points)

The default security() behavior changed between v2 and v3, which causes the difference in the calculations. To make sure they are calculated in the same way, add lookahead=barmerge.lookahead_on to each security call in your v4 script, like this:

startd = security(syminfo.tickerid, "D", time, lookahead=barmerge.lookahead_on)

You can read more about this behavior here in the User Manual.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...