python pandas 的相对强度指数 [英] Relative Strength Index in python pandas

查看:105
本文介绍了python pandas 的相对强度指数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是熊猫新手.计算熊猫的RSI指标中相对强度部分的最佳方法是什么?到目前为止,我得到以下信息:

I am new to pandas. What is the best way to calculate the relative strength part in the RSI indicator in pandas? So far I got the following:

from pylab import *
import pandas as pd
import numpy as np



def Datapull(Stock):
    try:
        df = (pd.io.data.DataReader(Stock,'yahoo',start='01/01/2010'))
        return df
        print 'Retrieved', Stock
        time.sleep(5)
    except Exception, e:
        print 'Main Loop', str(e)


def RSIfun(price, n=14):
    delta = price['Close'].diff()
    #-----------
    dUp=
    dDown=

    RolUp=pd.rolling_mean(dUp, n)
    RolDown=pd.rolling_mean(dDown, n).abs()

    RS = RolUp / RolDown
    rsi= 100.0 - (100.0 / (1.0 + RS))
    return rsi

Stock='AAPL'
df=Datapull(Stock)
RSIfun(df)

到目前为止,我做得对吗?我在方程的差分部分遇到麻烦,在该差分部分中,您将向上和向下的计算分开了

Am I doing it correctly so far? I am having trouble with the difference part of the equation where you separate out upward and downward calculations

推荐答案

dUp= delta[delta > 0]
dDown= delta[delta < 0]

您还需要以下内容:

RolUp = RolUp.reindex_like(delta, method='ffill')
RolDown = RolDown.reindex_like(delta, method='ffill')

否则RS = RolUp / RolDown不会做您想要的事情

otherwise RS = RolUp / RolDown will not do what you desire

:这似乎是RS计算的一种更准确的方法:

seems this is a more accurate way of RS calculation:

# dUp= delta[delta > 0]
# dDown= delta[delta < 0]

# dUp = dUp.reindex_like(delta, fill_value=0)
# dDown = dDown.reindex_like(delta, fill_value=0)

dUp, dDown = delta.copy(), delta.copy()
dUp[dUp < 0] = 0
dDown[dDown > 0] = 0

RolUp = pd.rolling_mean(dUp, n)
RolDown = pd.rolling_mean(dDown, n).abs()

RS = RolUp / RolDown

这篇关于python pandas 的相对强度指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆