I’m curious about the VPN indicator by Markos Katsanos, but I’m struggling to convert the code. Does anyone know how to code this?
“The VPN indicator calculates the difference of up day volume and down day volume, divided by the total volume. A day is an up day when its typical price – the average of high, low, and close – is higher than yesterday’s typical price plus 1/10 ATR. The opposite is true for down days. The result is multiplied by 100 and smoothed with a 3-day EMA.”
This is my poor attempt, would appreciate if someone could point me in the right direction.
// VPN INDICATOR
//The VPN indicator calculates the difference of up day volume and down day volume, divided by the total volume. A day is an up day when its typical price - the average of high, low, and close - is higher than yesterday’s typical price plus 1/10 ATR. The opposite is true for down days. The result is multiplied by 100 and smoothed with a 3-day EMA. The C code of this indicator:
hlc = (Dhigh(0) + Dlow(0) + Dclose(0)) / 3
//should it be? hlc = (high + low + close) / 3
once Periods = 30
dist = 0.1 * AverageTrueRange
once vp = 0.0
once vn = 0.0
for i = 0 to periods-1 do
if (hlc[i] > hlc[i+1] + dist) then
vp = volume[i]
elsif (hlc[i] < hlc[i+1] - dist) then
vn = volume[i]
vtot = volume[i]
endif
next
result = 100*((vp-vn)/vtot)
VPN = ExponentialAverage(result)[3]
DRAWHLINE(0)coloured(0,0,0)
return VPN coloured(225,0,0)
Not correct, but you were close! 🙂 Pinescript is somehow similar to Python with indentation.
Try that version instead:
once period=30
once iATR = .1
dist = AverageTrueRange[period](close) * iATR
vp = 0.0
vn = 0.0
vtot = 0.0
for i = 0 to period - 1
hlc3 = (Dhigh(i) + Dlow(i) + Dclose(i)) / 3
hlc31 = (Dhigh(i+1) + Dlow(i+1) + Dclose(i+1)) / 3
if (hlc3 > (hlc31 + dist)) then
vp = vp+volume[i]
elsif (hlc3 < (hlc31 - dist)) then
vn = vn+volume[i]
endif
vtot = vtot+ volume[i]
next
vpn = (((vp - vn) / (vtot / period)) / period) * 100
return vpn