Indicator value seem wrong
Hello,
I have been trying to get some calculations on stock data done with this library, primarily I am trying to use RSI, DMI-, DMI+ and ADX. Unfortunately I haven't been terribly successful with it. The library works in terms of installation and calling the functions, but the values don't make any sense.
For example I am having the values for Apple on Nasdaq from 1st to 31st May in JSON like this:
{
"2018-05-01": {
"open": 166.41,
"low": 165.27,
"high": 169.20,
"close": 169.10
},...
I then use this code to analyze the values and calculate indicators:
import numpy
import talib
import json
from collections import OrderedDict
cache_file = open('aapl.json', 'r')
value_cache = json.load(cache_file, object_pairs_hook=OrderedDict)
if value_cache:
high_ar = []
low_ar = []
close_ar = []
for date in value_cache:
high_ar.append(float(value_cache[date]['high']))
low_ar.append(float(value_cache[date]['low']))
close_ar.append(float(value_cache[date]['close']))
high_quotes = numpy.array(high_ar)
low_quotes = numpy.array(low_ar)
close_quotes = numpy.array(close_ar)
stock_rsi = talib.RSI(close_quotes, 14).tolist()
stock_minus_dmi = talib.MINUS_DM(high_quotes, low_quotes, 14).tolist()
stock_plus_dmi = talib.PLUS_DM(high_quotes, low_quotes, 14).tolist()
stock_adx = talib.ADX(high_quotes, low_quotes, close_quotes, 14).tolist()
print 'RSI'
for rsi in stock_rsi:
print rsi
print
print 'DMI-'
for dmi in stock_minus_dmi:
print dmi
print
print 'DMI+'
for dmi in stock_plus_dmi:
print dmi
print
print 'ADX'
for adx in stock_adx:
print adx
The ADX is always NaN for every value, while the first 14 values of RSI are NaN which is correct, but for DMI-/DMI+ it is only the first 13 NaN. So the time period seems to mean something else for every indicator, but I couldn't make sense of it from reading the docs.
When I look at the values for 31st May I get this: RSI: 74.4643785734 DMI-: 4.54698475635 DMI+: 15.428528392
Comparing that with tradingview it should be this: RSI: 59.66 DMI-: 26.22 DMI+: 28.30
So the values are completely different but I use the same data to calculate it. When I try other stocks the RSI was sometimes very close to the value on tradingview, but the DMI values were always <0.0 which doesn't make sense at all.
I guess I am using these methods wrong somehow given that the results are so far off, can someone point me in the right direction here?
After some more testing it seems the biggest issue was not enough data, so I presume everything is alright with the library and the issue was with my setup.
what about live trading ? using massive amount of data for live trading will be an issue... I am still not able to find a solution for this wrong indicator values for live trading (I will do the calculation for over 10000 assets each minute) and I know that using a lot of data solve the issue but I wish if we can use previous indicator value to calculate the next value based on just enough bars
There has been some work on trying to make a stateful version of the streaming API, but it's experimental and we can't support it until we take over the C library project.
As it currently stands, the streaming API (meaning, calculate only the "most recent value" of an indicator) will be different for indicators that use infinite window lookbacks, for example EMA, when calculating on large data sets.
I don't think i would necessarily say values are "wrong", it depends on how you define the indicator, but if your indicator is defined as being based on all possible previous values, then even large data sets technically will vary from larger data sets.
I'm curious if your calculation interval is every minute, is it really hard to just calculate full data sets in a timely manner? That seems like a solution for you.
Well they are not wrong but a backtest where the indicator uses previous values of the RSI is always better than a backtest where the RSI calculation is done only with the bars (without previous values) and I am not able to figure out how you guys do the calculation using previous values because I wish I knew just to be able to replicate this in live trading without having to calculate full data sets and storing them because it's just unnecessary work... is it really the only solution ?
Calculating "full RSI" on 1,000 observations of prices for 10,000 stocks appears to take 80 milliseconds on my laptop...
well that's a powerful laptop man, it takes me 10 minutes on mine
Are you using numpy arrays or pandas or polars?Maybe we have a data copy taking place unintentionally and can remove it. Can you make a simple test case?On May 11, 2023, at 11:35 AM, aniss-itaoui @.***> wrote: well that's a powerful laptop man, it takes me 10 minutes on mine
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you commented.Message ID: @.***>
the following script I have a simple function "calculate_rsi" that will return the last value of "indicator_rsi.rsi()" I use 400 bar to get the last value of 200 RSi (just to get the rsi values as close as possible to ninja trader) I use the file with 10 000 observation and it took me 10 minutes approximately to complete the calculation, here is a link to the file I use https://drive.google.com/file/d/1-9G1duJzq5Fbi7n_Ch3yxqdVfYhXvLOX/view?usp=share_link so that you can download it, you can change the path so that the code will work
import pandas as pd
from ta.momentum import RSIIndicator
quatity_of_bars_to_feed_the_indicator = 400
def calculate_rsi(array_of_closes):
df = pd.DataFrame({'Close': array_of_closes})
indicator_rsi = RSIIndicator(close=df["Close"], window=200, fillna=True)
rsi_values = indicator_rsi.rsi()
last_rsi_value = rsi_values[len(rsi_values) - 1]
return last_rsi_value
path = "C:\\Users\\Admin\\Desktop\\aniss\\histo\\higher time frames\\"
crypto = "ETHUSDT.Last - 15.txt"
lines = []
with open(path + crypto) as f:
lines = f.readlines()
for x in range(len(lines)):
line = lines[x]
line = str(line)
line = line.replace("\n", "")
line = line.split(";")
line[1] = float(line[1])
line[2] = float(line[2])
line[3] = float(line[3])
line[4] = float(line[4])
line[5] = int(line[5])
lines[x] = line
look_back_array_rsi = []
for x in range(quatity_of_bars_to_feed_the_indicator):
look_back_array_rsi.append(lines[x][4])
y_rsi = []
for x in range(quatity_of_bars_to_feed_the_indicator, len(lines)):
look_back_array_rsi.pop(0)
look_back_array_rsi.append(lines[x][4])
y_rsi.append(calculate_rsi(look_back_array_rsi))
I give to RSIIndicator a pandas array please note that if I delete the last line "y_rsi.append(calculate_rsi(look_back_array_rsi))" this piece of code will execute in less that 3 seconds and the last line do mainly one thing, executing this line of code RSIIndicator(close=df["Close"], window=200, fillna=True), it means that the TA library code is extremely slow