I am using the following code to find the RSI (Relative Strength Index) and DEMA (double exponential moving average) of a stock.
library(quantmod)
library(TTR)
getSymbols("AAPL")
chartSeries(AAPL, TA=NULL)
data=AAPL[,4]
AAPL$rsi = TTR::RSI(data)
AAPL$dema = TTR::DEMA(data)
# object B stores the copy of AAPL object and I save it in a CSV file
B = AAPL
Every day, object AAPL
will have a new line to reflect data of the last closing day.
Each day RSI
and DEMA
functions run on the entire dataset. It seems that it is a wastage of CPU power and time to run RSI
again and again on the last 12+ years data, even though only one new row (for the last trading day) is added to the data.
Is there a way to find RSI
, DEMA
, etc... of only the last day in AAPL
object and add it to the old dataset B
?
I wonder how quant traders might be doing this kind of operation when they get tick data each second and they need to find RSI and few other indicators on new and all the past data. Even with the fastest computer, it will take several minutes to get the indicator data, and the market would have moved by then.
Thanks!
Let's say that yesterday you downloaded all of the relevant data and calculated all of the RSI and DEMA statistics. Below are the data up until March 2, 2021.
Then, you save this result to a CSV:
Now, today you downloaded the data and you've got one new data point. By using the last 200 days numbers, you could generate the same value for the most recent day as using the whole data set. This seems to work for other symbols, too, but you'd want to make sure it generalizes.
You could then take this last row and append it to the previous CSV as @phiver suggested:
The real question is what's to be gained from such a procedure? Looking at the benchmarks for the two different procedures, using the median estimates, executing the RSI operation on the full data is almost 40% slower, though it will not be noticeable if you're doing only a few calls. I didn't print the results here, but the DEMA routine is about 30% slower on the full data set. If you had to do this thousands of times per day, doing it like this might make sense, but if you had to do it 10 times per day, it may not be worth the trouble.