Hi,
I want to implement the following code in PROREALCODE:
vp = Sum(if diff > factor * atr then volume else 0, length);
(The diff, factor and atr are known, The length is a period)
I do following but it seems not the result what I want.
vp=0
for i=1 to length
if diff >atr*factor then
vp = vp + volume[i]
endif
next
Always use the ‘Insert PRT Code’ button when putting code in your posts to make it easier for others to read.
Thank you 🙂
The logic is correct, you can double check it by using a complete sum, then check any difference:
vp=0
vv=0
for i=1 to length
vv = vv + volume[i]
if diff >atr*factor then
vp = vp + volume[i]
endif
next
VP and VV should be different.
Moreover, starting from 1 skips the current candle [0].
Hi Robert,
Thanks for the reply. But it looks different than the original one.
Here are the codes from ThinkOrSwim (see the attached chart):
input length = 30;
input emaLength = 3;
input averageLength = 30;
input factor = 0.1;
input criticalValue = 10;
input averageType = AverageType.EXPONENTIAL;
def atr = WildersAverage(TrueRange(high, close, low), length);
def diff = hlc3 – hlc3[1];
def vp = Sum(if diff > factor * atr then volume else 0, length);
def vn = Sum(if diff < -factor * atr then volume else 0, length);
plot VPN = ExpAverage(100 * (vp – vn) / Sum(volume, length), emaLength);
plot VPNAvg = MovingAverage(averageType, VPN, averageLength);
plot CriticalLevel = criticalValue;
And here are my ProRealTime code:
length = 30
emaLength = 3
averageLength = 30
factor = 0.1
criticalValue = 10
src = customclose
//src = close
MyTR = max(Range,max(abs(high - src[1]),abs(low - src[1])))
IF BarIndex < length THEN
MyATR = MyTR
ELSE
MyATR = ((MyATR[1] * (length - 1)) + MyTR) / length
ENDIF
atr = WilderAverage[length](MyATR)
diff =src - src[1]
at2 = factor * atr
vp=0
for i=1 to length
if diff > factor * atr then
vp = vp + volume[i]
endif
next
vn=0
for i=1 to length
if diff < -1 * (factor * atr) then
vn = vn + volume[i]
endif
next
vpn = ExponentialAverage[emaLength](100*(vp - vn)/summation[length](volume))
RETURN vpn as "VPN", criticalValue as "Thredshold"