Is it possible to calculate an RSI with values from an array? Something like:
myRSI = RSI[14]($myArray)
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.
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)
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
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.
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:
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.