'How can I get last value of STOCHRSI with Ta-Lib?

I implemented it but it prints all.

print(ta.STOCHRSI(df["close"], 14, 5, 3, 0)[-1])
2022-04-20 17:00:00          NaN
2022-04-20 18:00:00          NaN
2022-04-20 19:00:00          NaN
2022-04-20 20:00:00          NaN
2022-04-20 21:00:00          NaN
                         ...    
2022-04-28 20:00:00    79.700101
2022-04-28 21:00:00     0.000000
2022-04-28 22:00:00     0.000000
2022-04-28 23:00:00    44.877738
2022-04-29 00:00:00    65.792554
Length: 200, dtype: float64

I just want to get recent value of STOCHRSI, just one float value. How can I get it?

or if I want to get the avg of recent 3 values, How can I implement it?



Solution 1:[1]

If you really mean the library TA-Lib.enter link description here

As far as I know, the syntax there is different from yours.

Streaming API:"An experimental Streaming API was added that allows users to compute the latest value of an indicator. This can be faster than using the Function API, for example in an application that receives streaming data, and wants to know just the most recent updated indicator value Streaming API

This works with 'SMA', but fails with 'STOCHRSI' if I make a difference less than 5 in 'assert'. And to calculate the indicator, you need a quote history. You probably saw that the first values are empty, since there is no data required by the indicator period.

You can try the following: determine how much data is needed for the correct calculation of the indicator. And then feed only this array length.

If resources allow you, then you can calculate all the values and save their variable and take only the last of the variable fastk[-1].

import talib
from talib import stream

sma = talib.SMA(df["close"], timeperiod=14)
latest = stream.SMA(df["close"], timeperiod=14)
assert (sma[-1] - latest) < 0.00001

print(sma[-1], latest)#1.6180066666666686 1.6180066666666668

fastk, fastd = talib.STOCHRSI(df["close"], timeperiod=14, fastk_period=5, fastd_period=3, fastd_matype=0)
f, fd = stream.STOCHRSI(df["close"], timeperiod=14, fastk_period=5, fastd_period=3, fastd_matype=0)
print(fastk[-1], f)
assert (fastk[-1] - f) < 5#64.32089013974793 59.52628987038199

print(fastk[-1], f)

Use the condition of crossing the main signal line from bottom to top.

if fastd[100] < fastk[100] and fastd[101] > fastk[101]:
    pass#pass replace your code

I also drew an indicator under the main chart to show what it looks like.

import matplotlib.pyplot as plt
import pandas as pd
import talib

date = df.iloc[:, 0].index.date
x = len(df)
fig, ax = plt.subplots(2)
ax[0].plot(date[x-100:], df.iloc[x-100:, 3])
ax[1].plot(date[x-100:], fastk[x-100:])
ax[1].plot(date[x-100:], fastd[x-100:])

fig.autofmt_xdate()
plt.show()

enter image description here

I made a code to determine the minimum size of the data length for the correct calculation of the indicator.

x = len(df["C"])
fastk, fastd = talib.STOCHRSI(df["C"].values, timeperiod=14, fastk_period=5, fastd_period=3, fastd_matype=0)
fk = np.round(fastk[x - 3:], 5)
fd = np.round(fastd[x - 3:], 5)
print('fk', fk, 'fd', fd)

Output

fk [100.       32.52114   0.     ] fd [43.27353 54.11391 44.17371]

Next, we find the desired length of the array.

for depth in range(10, 250, 5):
    fastk, fastd = talib.STOCHRSI(df["C"].values[x - depth:], timeperiod=14, fastk_period=5, fastd_period=3,
                                  fastd_matype=0)
    if (fk == np.round(fastk[depth - 3:], 5)).all() and (fd == np.round(fastd[depth - 3:], 5)).all():
        print('fastk[depth-3:]', fastk[depth - 3:], 'fastd[depth-3:]', fastd[depth - 3:])
        print('stop iteration required length',  depth)
        break

Output

fastk[depth-3:] [100.          32.52113882   0.        ] fastd[depth-3:] [43.27353345 54.11391306 44.17371294]
stop iteration required length 190

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1