Loop to calculate historical volatility

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #121195 quote
    pascaltmn
    Participant
    New

    Hello,

    I’m writing a script to measure a long chunk of volatility to find out if the current volatility is high or low.

    With one twist: At “1”, the indicator should be showing that we are at 100% normal historical volatility.

    I have managed to calculate historical volatility, but I can’t make the indicator display the data in the form of “1” = 100% of normal historical volatility.

    Here’s my ProRealTime code:

    b = summation[24](TR(close)) 
    c = 0.0
    
    FOR i = 24 TO 1200 DO
    c = b[i] + c
    NEXT
    
    d = c / 50
    volat = b / d

    Also, not sure if it helps, but I did manage to make it work properly in “PineScript”.

    Here is the working snippet in PineScript:

    b = sum(atr(1),24)
    c = 0.0
    for i = 24 to 1200 by +24
        c := b[i] + c
    
    d = c / 50
    vol = b / d

    I would appreciate your help A LOT to make the ProRealTime code version work.

    Thank you.

    #121199 quote
    robertogozzi
    Moderator
    Master

    Line 1 in Pinescript code reads ATR (AverageTrueRange in PRT), while you wrote TR which is True Range.

    Can this be the source of your problem?

    #121200 quote
    pascaltmn
    Participant
    New

    Already tried that, and it didn’t fix the issue, unfortunately.

    My guess is that the issue has something to do with the “By” element in the loop, which is present in the PineScript version, but not in my PRT code.

    I could not find any “By” equivalent for PRT…

    #121201 quote
    Nicolas
    Keymaster
    Master

    Did you know that ProBuilder has an historical volatility instruction? It might help!

    #121203 quote
    pascaltmn
    Participant
    New

    Yeah, I’ve tried it. But I couldn’t figure out how to use it for readings in the form that I need them.

    I need an indicator that shows where volatility currently is in comparison to the historical average.

    e.g. if the indicator gives a “0.5” reading, it should mean that volatility is currently 50% below historical average. If the indicator gives a “1.0” reading, it should mean that volatility is currently at the exact historical average. And so on…

    Do you know how I could do this?

    #121206 quote
    Vonasi
    Moderator
    Master

    My guess – not tested.

    b = 0
    for i = 1 to 24
    b = b + averagetruerange[i](close)
    next
    
    c = 0
    FOR i = 1 TO 50
    ii= i*24
    c = b[ii] + c
    NEXT
     
    d = c / 50
    volat = b / d
    Nicolas and pascaltmn thanked this post
    #121207 quote
    Nicolas
    Keymaster
    Master

    You have to normalize the value in order to make a fixed scale.

    #121226 quote
    pascaltmn
    Participant
    New

    Thanks Vonasi, that worked!

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

Loop to calculate historical volatility


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
pascaltmn @pascaltmn Participant
Summary

This topic contains 7 replies,
has 4 voices, and was last updated by pascaltmn
5 years, 11 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 03/05/2020
Status: Active
Attachments: No files
Logo Logo
Loading...