Answer the question
In order to leave comments, you need to log in
pandas, python. There is a snippet of the code for the RSI indicator using pandas, is there a list of where to tweak it?
Help me please! There is a code taken on the Internet, its essence is "if I understand correctly" that it analyzes a list of numbers and displays the RSI value (relative strength index).
At first, for two days I tried to write this indicator myself using formulas from the Internet, but I constantly got the wrong result and compared it on the tradingview website. I came across a code here that seems to perform the actions I need, but I don't know Pandas.
The bottom line is, I have a list of numbers [1,2,3...n], it is stored in a json file, how can I pass this list to this code? Where to put a variable with a list to make it work?
I will accept any help, and I will be very grateful, henceforth I promise to learn pandas.
import pandas as pd
import numpy as np
def rsi_tradingview(ohlc: pd.DataFrame, period: int = 14, round_rsi: bool = True):
""" Implements the RSI indicator as defined by TradingView on March 15, 2021.
The TradingView code is as follows:
//@version=4
study(title="Relative Strength Index", shorttitle="RSI", format=format.price, precision=2, resolution="")
len = input(14, minval=1, title="Length")
src = input(close, "Source", type = input.source)
up = rma(max(change(src), 0), len)
down = rma(-min(change(src), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
plot(rsi, "RSI", color=#8E1599)
band1 = hline(70, "Upper Band", color=#C0C0C0)
band0 = hline(30, "Lower Band", color=#C0C0C0)
fill(band1, band0, color=#9915FF, transp=90, title="Background")
:param ohlc:
:param period:
:param round_rsi:
:return: an array with the RSI indicator values
"""
delta = ohlc["close"].diff()
up = delta.copy()
up[up < 0] = 0
up = pd.Series.ewm(up, alpha=1/period).mean()
down = delta.copy()
down[down > 0] = 0
down *= -1
down = pd.Series.ewm(down, alpha=1/period).mean()
rsi = np.where(up == 0, 0, np.where(down == 0, 100, 100 - (100 / (1 + up / down))))
return np.round(rsi, 2) if round_rsi else rsi
Answer the question
In order to leave comments, you need to log in
Without going into mathematics, I sketched an example for working with the above function. I'm not sure if it produces what is expected, but it does produce something.
import json
import numpy as np
import pandas as pd
from pathlib import Path
# тут я формирую тестовый файл json со списком длиной 50
n = 50
list_for_json = [i for i in range(n)]
my_json_file = Path('myjson.json')
with open(my_json_file, 'w') as myfile:
json.dump(fp=myfile, obj=list_for_json)
# тут открываю этот файл, создаю из него dataframe Pandas и именую колонку с данными 'close',
# так как такое имя используется в приведенной вами функции.
with open(my_json_file, 'r') as myfile:
df = pd.read_json(myfile)
df.rename(columns={0: 'close'}, inplace=True)
print(df.head())
# собственно функция без изменений
def rsi_tradingview(ohlc: pd.DataFrame, period: int = 14, round_rsi: bool = True):
""" Implements the RSI indicator as defined by TradingView on March 15, 2021.
The TradingView code is as follows:
//@version=4
study(title="Relative Strength Index", shorttitle="RSI", format=format.price, precision=2, resolution="")
len = input(14, minval=1, title="Length")
src = input(close, "Source", type = input.source)
up = rma(max(change(src), 0), len)
down = rma(-min(change(src), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
plot(rsi, "RSI", color=#8E1599)
band1 = hline(70, "Upper Band", color=#C0C0C0)
band0 = hline(30, "Lower Band", color=#C0C0C0)
fill(band1, band0, color=#9915FF, transp=90, title="Background")
:param ohlc:
:param period:
:param round_rsi:
:return: an array with the RSI indicator values
"""
delta = ohlc["close"].diff()
up = delta.copy()
up[up < 0] = 0
up = pd.Series.ewm(up, alpha=1/period).mean()
down = delta.copy()
down[down > 0] = 0
down *= -1
down = pd.Series.ewm(down, alpha=1/period).mean()
rsi = np.where(up == 0, 0, np.where(down == 0, 100, 100 - (100 / (1 + up / down))))
return np.round(rsi, 2) if round_rsi else rsi
# ну а дальше просто вызываю функцию и передаю в неё dataframe и печатаю, что она возвращает. # Насколько это вменяемо внутри функции, не знаю.
print(rsi_tradingview(ohlc=df))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question