How To Calculate Relative Strength Index (RSI) in Lua?

Member

by stephon , in category: SEO Tools , 4 months ago

How To Calculate Relative Strength Index (RSI) in Lua?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

2 answers

by ervin.williamson , 4 months ago

@stephon Here's an example of how you can implement this calculation in Lua:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
function calculateRSI(period, closingPrices)
    local gains = {}
    local losses = {}
    
    for i=2, #closingPrices do
        local priceChange = closingPrices[i] - closingPrices[i-1]
        
        if priceChange > 0 then
            table.insert(gains, priceChange)
            table.insert(losses, 0)
        else
            table.insert(losses, math.abs(priceChange))
            table.insert(gains, 0)
        end
    end
    
    local averageGain = 0
    local averageLoss = 0
    
    for i=1, period do
        averageGain = averageGain + gains[i]
        averageLoss = averageLoss + losses[i]
    end
    
    averageGain = averageGain / period
    averageLoss = averageLoss / period
    
    local RS = averageGain / averageLoss
    local RSI = 100 - (100 / (1 + RS))
    
    return RSI
end

-- Example usage
local closingPrices = {100, 105, 110, 108, 115, 112, 120, 118, 122, 125, 130, 128, 135, 132}
local period = 14

local rsi = calculateRSI(period, closingPrices)
print("RSI: " .. rsi)


This code calculates the RSI for a given set of closing prices and a chosen period. You can adjust the closingPrices and period variables in the example usage section to test the calculation with different data.

Member

by info , 3 months ago

@stephon You can find RSI of any stock on https://www.tradingview.com/ or https://finquota.com/. For example if you go to AAPL and click on "Add Indicator" then you can specify Window size and Overbought/Oversold threshold and etc...


or do you build your own app?