python 股市指标模块_Stockstats –适用于各种股市指标的Python模块

python 股市指标模块

I’m always working with stock market data and stock market indicators. During this work, there’s times that I need to calculate things like Relative Strength Index (RSI), Average True Range (ATR), Commodity Channel Index (CCI) and other various indicators and stats.

我一直在处理股市数据和股市指标。 在这项工作中,有时我需要计算相对强度指数(RSI),平均真实范围(ATR),商品渠道指数(CCI)和其他各种指标和统计数据。

My go-to for this type of work is TA-Lib and the python wrapper for TA-Lib but there’s times when I can’t install and configure TA-Lib on a computer. When this occurs, I then have to go find the various algorithms to calculate the various indicators / stats that I need.  When this happens, I usually end up making mistakes and/or taking longer than I really should to get these algo’s built to use in a project.  Of course I re-use what I can when I can but many times I’ve forgotten that I built an RSI function in the past and recreate.

对于这类工作,我最喜欢的是TA-Lib和TA-Lib的python包装器,但是有时候我无法在计算机上安装和配置TA-Lib。 发生这种情况时,我必须去寻找各种算法来计算所需的各种指标/统计数据。 当发生这种情况时,我通常最终会犯错误和/或花费比我真正需要的更长的时间,而这些错误和/或花费的时间比让我将这些算法构建为要在项目中使用的时间长。 当然,我会尽可能地重用我可以使用的内容,但是很多次我都忘记了我过去建立了RSI函数并重新创建它。

I found myself in this situation today. I need an RSI calculation for some work I’m doing.  I couldn’t get TA-Lib installed and working on the machine I was working on (no clue what was wrong either) so I decided to write my own indicator.  While looking around the web for a good algorithm to use, I ran across a new module that I hadn’t see before called stockstats.

我今天发现自己处在这种情况下。 我需要做一些工作的RSI计算。 我无法安装TA-Lib并在正在使用的机器上工作(也不知道有什么问题),所以我决定编写自己的指示器。 在网上寻找一种可以使用的好的算法时,我遇到了一个之前从未见过的新模块,叫做stockstats 。

Stockstats is a wrapper for pandas dataframes and provides the ability to calculate many different stock market indicators / statistics.  The fact that it is a simple wrapper around pandas is ideal since I do 99% of my work within pandas.

Stockstats是熊猫数据框的包装,并提供了计算许多不同的股市指标/统计数据的功能。 因为我在熊猫中完成了99%的工作,所以它是围绕熊猫的简单包装是很理想的。

To use stockstats, you simply to to ‘convert’ a pandas dataframe to a stockstats dataframe. This can be done like so:

要使用stockstats,您只需要将“熊猫”数据框“转换”为stockstats数据框。 可以这样完成:

stockstats_df = StockDataFrame.retype(df)

Then, to calculate the RSI for this dataframe, all you need to do is pass a command into the stockstats dataframe.

然后,要计算此数据帧的RSI,您要做的就是将命令传递到stockstats数据帧中。

stock['rsi_14']

The above calculates the 14-day RSI for the entire dataframe.

上面计算了整个数据帧的14天RSI。

Let’s look at a full example using data from yahoo.

让我们来看一个使用yahoo数据的完整示例。

First, import the modules we’ll need:

首先,导入我们需要的模块:

import pandas as pd
import pandas.io.data as web
from stockstats import StockDataFrame as Sdf

Pull down all the historical data for the S&P 500 ETF (SPY):

下拉所有S&P 500 ETF(SPY)的历史数据:

data = web.get_data_yahoo('SPY')

Taking a look at the ‘tail’ of the data gives us something like the data in Table 1.

看一下数据的“尾部”,我们得到了类似于表1中的数据。

python 股市指标模块_Stockstats –适用于各种股市指标的Python模块_第1张图片
Table 1: SPY Historical Data 表1:SPY历史数据

To calculate RSI, retype the pandas dataframe into a stockstats dataframe and then calculate the 14-day RSI.

要计算RSI,请将熊猫数据框重新输入到stockstats数据框,然后计算14天的RSI。

stock_df = Sdf.retype(data)
data['rsi']=stock_df['rsi_14']

With this approach, you end up with some extra columns in your dataframe. These can easily be removed with the ‘del’ command.

使用这种方法,最终会在数据框中添加一些额外的列。 这些可以很容易地通过“ del”命令删除。

del data['close_-1_s']
del data['close_-1_d']
del data['rs_14']
del data['rsi_14']

With these extra columns removed, you now have the 14-day RSI values a column titled “rsi”.

除去这些额外的列之后,您现在具有14天RSI值的标题为“ rsi”的列。

python 股市指标模块_Stockstats –适用于各种股市指标的Python模块_第2张图片
Table 2: SPY Historical Data with RSI 表2:带有RSI的SPY历史数据

One caveat on this approach – stockstats seems to take the ‘close’ column. This might or might not be an issue for you if you are wanting to use the Adj Close column provided by yahoo. This is a simple fix (delete the ‘close’ and rename ‘adj close’ to ‘close’).

关于此方法的一个警告-股票统计数据似乎处于“关闭”列。 如果您要使用yahoo提供的“调整关闭”列,则可能对您来说不是问题。 这是一个简单的修复方法(删除“关闭”并将“ adj close”重命名为“关闭”)。

翻译自: https://www.pybloggers.com/2016/12/stockstats-python-module-for-various-stock-market-indicators/

python 股市指标模块

你可能感兴趣的:(算法,python,大数据,java,人工智能)