Summation with IF inside…

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #167703 quote
    thomas2004ch
    Participant
    Average

    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
    #167706 quote
    robertogozzi
    Moderator
    Master

    Always use the ‘Insert PRT Code’ button when putting code in your posts to make it easier for others to read.

    Thank you 🙂

    #167707 quote
    robertogozzi
    Moderator
    Master

    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].

    #167730 quote
    thomas2004ch
    Participant
    Average

    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"
    
    VPN.png VPN.png
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.

Summation with IF inside…


ProBuilder: Indicators & Custom Tools

New Reply
Author
Summary

This topic contains 3 replies,
has 2 voices, and was last updated by thomas2004ch
4 years, 9 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 04/21/2021
Status: Active
Attachments: 1 files
Logo Logo
Loading...