RSI on an array

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #163713 quote
    Mattias
    Participant
    Senior

    Is it possible to calculate an RSI with values from an array? Something like:

    myRSI = RSI[14]($myArray)
    #163718 quote
    Vonasi
    Moderator
    Master

    So you want to store values in an array and calculate the RSI of the latest 14 of them?

    You would need to use a loop to look at lastset down to lastset-13 values and calculate the RSI using these values. You could not use the RSI instruction as that looks at the last 14 bars values.

    Using arrays is only necessary if you are not storing a new value at every bar.

    Mattias thanked this post
    #163719 quote
    robertogozzi
    Moderator
    Master

    No, because an array is made of several elements, which one should be used?

    You can use only, say, element 12 indirectly:

    Element = $myArray[12]
    myRSI   = RSI[14](Element)
    Mattias thanked this post
    #163738 quote
    Vonasi
    Moderator
    Master

    Here is an example of how we can store values in an array and then do calculations on the last ‘p’ array locations. This example calculates the RSI of the last 14 high fractals. Unfortunately it is just a simple average based RSI and not smoothed as I believe the normal RSI is (I couldn’t figure out the how to do that calculation in the time I had!)

    p = 14
    
    if high < high[1] and high[1] > high[2] then
    a = a + 1
    $myarray[a] = high[1]
    endif
    
    up = 0
    down = 0
    if a >=14 then
    for b = lastset($myarray) downto lastset($myarray)-13
    if $myarray[b] > $myarray[b-1] then
    up = up + ($myarray[b] - $myarray[b-1])
    endif
    
    if $myarray[b] < $myarray[b-1] then
    down = down + ($myarray[b-1] - $myarray[b])
    endif
    next
    
    upavg = up/p
    downavg = down/p
    rs = upavg/downavg
    myrsi = 100-(100/(1+rs))
    endif
    
    return myrsi
    
    Mattias thanked this post
    #163756 quote
    Mattias
    Participant
    Senior

    Thanks a lot, both of you!

    Vonasi, it worked. I had the array code covered (I did an average from it, I believe I learned from your code somewhere from the forum as well), but it saved a lot of time with the RSI-calculation.

    So thanks again.

    #163758 quote
    Vonasi
    Moderator
    Master

    Thanks for the thanks – but it is not an RSI calculation that returns the same result as the built in platform RSI. I tested it using every close value (which would be the same as the built in platform RSI uses) and the result was different because I believe the normal RSI either uses smoothing by doing the following:

    The second, and subsequent, calculations are based on the prior averages and the current gain loss:

    • Average Gain = [(previous Average Gain) x 13 + current Gain] / 14.
    • Average Loss = [(previous Average Loss) x 13 + current Loss] / 14.

     

    Borrowed from:

    https://school.stockcharts.com/doku.php?id=technical_indicators:relative_strength_index_rsi

     

    I’ve got rather a lot on at the moment (like trying to sort out a Greek biometric residency permit due to bloody Brexit) which is taking up a lot of time right now otherwise I would have persisted with the calculations – sorry but hopefully I put you on the right path.

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

RSI on an array


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
Mattias @mattias Participant
Summary

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

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