Volume Based Buy & Sell Momentum

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #242797 quote
    bruces
    Participant
    Junior
    This is a TradingView Pinescript Indicator that I would like to run on ProRealTime in a separate pane and be able to adjust the EMA length please:
    study(title=”Volume Based Buy and Sell Momentum by 2tm”, shorttitle=”VBSM”)
    EMA_Len = input(25, title=”Lenth”, minval=1)
    xROC = roc(close, 1)
    nRes1 = iff(volume < volume[1], nz(nRes1[1], 0) + xROC, nz(nRes1[1], 0))
    nRes2 = iff(volume > volume[1], nz(nRes2[1], 0) + xROC, nz(nRes2[1], 0))
    nRes3 = nRes1 + nRes2
    nResEMA3 = sma(nRes1, EMA_Len) + sma(nRes2, EMA_Len)
    PNVI = plot(nRes3, color=blue, title=”PVI + NVI”)
    PEMA = plot(nResEMA3, color=red, title=”EMA”)
    pCol = nRes3 > nResEMA3 ? blue : red
    fill (PNVI, PEMA, pCol)
    #242845 quote
    Iván González
    Moderator
    Master

    Hi, here you have the code translated:

    // Parameter declaration
    EMALen = 25  // Adjustable EMA length set by the user
    
    // Calculation variables
    xROC = ROC[1](Close)  // Rate of Change of the closing price
    
    // Ensure enough bars exist for EMA calculation
    IF BarIndex > EMALen THEN
    
        // Calculate nRes1 based on decreasing volume
        IF Volume < Volume[1] THEN
            nRes1 = nRes1[1] + xROC 
        ELSE
            nRes1 = nRes1[1]  
        ENDIF
    
        // Calculate nRes2 based on increasing volume
        IF Volume > Volume[1] THEN
            nRes2 = nRes2[1] + xROC 
        ELSE
            nRes2 = nRes2[1]
        ENDIF
    
        // Combine results to compute nRes3
        nRes3 = nRes1 + nRes2  // Combine nRes1 and nRes2 to compute the main indicator
    
        // Calculate EMA (Exponential Moving Average) of the accumulators
        nResEMA1 = Average[EMALen](nRes1)  
        nResEMA2 = Average[EMALen](nRes2)
        nResEMA3 = nResEMA1 + nResEMA2  // Combine the EMAs of nRes1 and nRes2 to get the total EMA
    
        // Determine fill color based on the relationship between nRes3 and nResEMA3
        IF nRes3 > nResEMA3 THEN
            r = 0  
            g = 0
            b = 255
        ELSE
            r = 255
            g = 0
            b = 0
        ENDIF
    
        // Draw the shaded area between nRes3 and nResEMA3 with the calculated color
        ColorBetween(nRes3, nResEMA3, r, g, b, 30)
    
    ENDIF
    
    // Return the results on the chart
    RETURN nRes3 AS "PVI+NVI" COLOURED("blue"), nResEMA3 AS "EMA" COLOURED("red")
    
    #242894 quote
    bruces
    Participant
    Junior

    Ivan, Thank you, Thank you Thank you.

    Perfect – Very grateful

    Would it be possible to include the option to paint the EMA line completely red as it is now OR green if Rate of Change is > zero and Red if ROC is < zero and black if ROC = zero?  Not necessary on the other line, just the EMA.

    Hope that is not pushing the boundaries too much.

    Thyank you

    Bruce

    #242899 quote
    Iván González
    Moderator
    Master

    No problem. Here you have it

    // Parameter declaration
    EMALen = 25  // Adjustable EMA length set by the user
    
    // Calculation variables
    xROC = ROC[1](Close)  // Rate of Change of the closing price
    
    // Ensure enough bars exist for EMA calculation
    IF BarIndex > EMALen THEN
    
    // Calculate nRes1 based on decreasing volume
    IF Volume < Volume[1] THEN
    nRes1 = nRes1[1] + xROC
    ELSE
    nRes1 = nRes1[1]
    ENDIF
    
    // Calculate nRes2 based on increasing volume
    IF Volume > Volume[1] THEN
    nRes2 = nRes2[1] + xROC
    ELSE
    nRes2 = nRes2[1]
    ENDIF
    
    // Combine results to compute nRes3
    nRes3 = nRes1 + nRes2  // Combine nRes1 and nRes2 to compute the main indicator
    
    // Calculate EMA (Exponential Moving Average) of the accumulators
    nResEMA1 = Average[EMALen](nRes1)
    nResEMA2 = Average[EMALen](nRes2)
    nResEMA3 = nResEMA1 + nResEMA2  // Combine the EMAs of nRes1 and nRes2 to get the total EMA
    
    // Determine fill color based on the relationship between nRes3 and nResEMA3
    IF nRes3 > nResEMA3 THEN
    r = 0
    g = 0
    b = 255
    ELSE
    r = 255
    g = 0
    b = 0
    ENDIF
    
    // Draw the shaded area between nRes3 and nResEMA3 with the calculated color
    ColorBetween(nRes3, nResEMA3, r, g, b, 30)
    
    // Determine EMA color based on ROC
    if xROC > 0 then
    r1=0
    g1=255
    b1=0
    elsif xROC < 0 then
    r1=255
    g1=0
    b1=0
    else
    r1=0
    g1=0
    b1=0
    endif
    
    ENDIF
    
    // Return the results on the chart
    RETURN nRes3 AS "PVI+NVI" COLOURED("blue"), nResEMA3 AS "EMA" COLOURED(r1,g1,b1)
    #242908 quote
    bruces
    Participant
    Junior

    Thank you Ivan.  Only issue now is that the colour of the EMA line changes based on the Rate of Change (ROC) of the PVI+NVI Line, not the ROC of the EMA line itself.

    Thank you

    Bruce

    #242925 quote
    JS
    Participant
    Senior

    Hi,

    Do you mean like this…?

    DefParam DrawOnLastBarOnly=true
    // Parameter declaration
    EMALen = 25  // Adjustable EMA length set by the user
     
    // Calculation variables
    xROC = ROC[1](Close)  // Rate of Change of the closing price
     
    // Ensure enough bars exist for EMA calculation
    IF BarIndex > EMALen THEN
     
    // Calculate nRes1 based on decreasing volume
    IF Volume < Volume[1] THEN
    nRes1 = nRes1 + xROC
    ELSE
    nRes1 = nRes1
    ENDIF
     
    // Calculate nRes2 based on increasing volume
    IF Volume > Volume[1] THEN
    nRes2 = nRes2 + xROC
    ELSE
    nRes2 = nRes2
    ENDIF
     
    // Combine results to compute nRes3
    nRes3 = nRes1 + nRes2  // Combine nRes1 and nRes2 to compute the main indicator
     
    // Calculate EMA (Exponential Moving Average) of the accumulators
    nResEMA1 = Average[EMALen,1](nRes1)
    nResEMA2 = Average[EMALen,1](nRes2)
    nResEMA3 = nResEMA1 + nResEMA2  // Combine the EMAs of nRes1 and nRes2 to get the total EMA
     
    // Determine fill color based on the relationship between nRes3 and nResEMA3
    IF nRes3 > nResEMA3 THEN
    r = 0
    g = 0
    b = 255
    ELSE
    r = 255
    g = 0
    b = 0
    ENDIF
     
    // Draw the shaded area between nRes3 and nResEMA3 with the calculated color
    ColorBetween(nRes3, nResEMA3, r, g, b, 30)
    EndIf
    
    EMAROC=ROC[1](nResEMA3)
    
    If EMAROC>0 then
    r=0
    g=255
    b=0
    ElsIf EMAROC<0 then
    r=255
    g=0
    b=0
    Else
    r=0
    g=0
    b=0
    EndIf
     
     
    // Return the results on the chart
    RETURN nRes3 AS "PVI+NVI" COLOURED("blue"), nResEMA3 AS "EMA" COLOURED(r,g,b)
    Iván González thanked this post
    #242973 quote
    bruces
    Participant
    Junior

    Hi Ivan, Apologies for delay in getting back to you.

    I think the green (up trend) and red (down trend) of the EMA is now painting correctly on the EMA line on the chart, based on the slope of the EMA – see Pane 4 on attached chart.  However, the plot of the EMA in the latest iteration of the code doesn’t look correct.

    I’ve attached a screen shot of the PRT chart showing:
    Pane 1 – Price Candles
    Pane 2 – Version 1 of the VBSM converted from Tradingview, without alternate color plots on EMA  (weren’t asked for at the time)
    Pane 3 – Version 2 showing alternate color plots on EMA, but color coding incorrectly based on the trend/slope of the PVI+NVI line and not the EMA line
    Pane 4 – Version 3 (and latest) showing alternate color plots on EMA, correctly based on the slope of the EMA line, but with incorrect EMA plot.

    The plot of the EMA in latest version (Pane 4) is different to the previous two plots.  The plots in Panes 2 and 3 are correct as I have compared them to the plot of the original indicator in Tradingview.

    So grateful for your work to date and wondering if you could have a look at for me please and resolve?  That will be all that is required then thank you.

    Thank you

    Bruce

    #242978 quote
    JS
    Participant
    Senior

    In the first two versions, the “EMA” was calculated as a “Simple Moving Average.”
    See, for example, line 28 in the first and second versions:

     nResEMA1=Average[EMALen](nRes1)

    Here, a “Simple Moving Average” was used for the calculation…

    I changed this in the third version, where the calculation was correctly done using EMA’s instead of SMA’s.

    This explains the difference compared to the first two versions…

    Iván González thanked this post
    #242989 quote
    bruces
    Participant
    Junior

    Thank you so much Ivan.

    Very grateful

    Bruce

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.

Volume Based Buy & Sell Momentum


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
bruces @bruces Participant
Summary

This topic contains 8 replies,
has 3 voices, and was last updated by bruces
1 year ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 01/19/2025
Status: Active
Attachments: 2 files
Logo Logo
Loading...