forked from lukaszbinden/rsi_tradingview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stoch_rsi.py
32 lines (26 loc) · 1.1 KB
/
stoch_rsi.py
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
import pandas as pd
from rsi import rsi_tradingview
def stoch_rsi_tradingview(ohlc: pd.DataFrame, period=14, smoothK=3, smoothD=3):
""" Calculating Stochastic RSI (gives the same values as TradingView as of March 20, 2021.
smoothK = input(3, "K", minval=1)
smoothD = input(3, "D", minval=1)
lengthRSI = input(14, "RSI Length", minval=1)
lengthStoch = input(14, "Stochastic Length", minval=1)
src = input(close, title="RSI Source")
rsi1 = rsi(src, lengthRSI)
k = sma(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = sma(k, smoothD)
:param ohlc:
:param period:
:param smoothK:
:param smoothD:
:return:
"""
# Calculate RSI
rsi = rsi_tradingview(ohlc, period=period, round_rsi=False)
# Calculate StochRSI
rsi = pd.Series(rsi)
stochrsi = (rsi - rsi.rolling(period).min()) / (rsi.rolling(period).max() - rsi.rolling(period).min())
stochrsi_K = stochrsi.rolling(smoothK).mean()
stochrsi_D = stochrsi_K.rolling(smoothD).mean()
return round(rsi, 2), round(stochrsi_K * 100, 2), round(stochrsi_D * 100, 2)