我在Github上尝试了许多库,但是它们都没有为TradingView产生匹配的结果,因此我按照此链接上的公式计算RSI指标。我使用Excel进行了计算,并使用TradingView整理了结果。我知道这是绝对正确的,但是我没有找到使用Pandas进行计算的方法。
100
RSI = 100 - --------
1 + RS
RS = Average Gain / Average Loss
The very first calculations for average gain and average loss are simple
14-period averages:
First Average Gain = Sum of Gains over the past 14 periods / 14.
First Average Loss = Sum of Losses over the past 14 periods / 14
The second, and …Run Code Online (Sandbox Code Playgroud) #df_1h has been imported before
import time
n = 14
pd.options.display.max_columns = 8
display("df_1h's Shape {} rows x {} columns".format(df_1h.shape[0], df_1h.shape[1]))
close = df_1h['close']
start = time.time()
df_1h['sma_14_pandas'] = close.rolling(14).mean()
end = time.time()
display('pandas: {}'.format(end - start))
start = time.time()
df_1h['sma_14_loop'] = np.nan
for i in range(n-1, df_1h.shape[0]):
df_1h['sma_14_loop'][i] = close[i-n+1:i+1].mean()
end = time.time()
display('loop: {}'.format(end - start))
display(df_1h.tail())
Run Code Online (Sandbox Code Playgroud)
"df_1h's Shape 16598 rows x 15 columns" …Run Code Online (Sandbox Code Playgroud)