博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python 股市指标模块_Stockstats –适用于各种股市指标的Python模块
阅读量:2518 次
发布时间:2019-05-11

本文共 3856 字,大约阅读时间需要 12 分钟。

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 and the  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。 发生这种情况时,我必须去寻找各种算法来计算所需的各种指标/统计数据。 当发生这种情况时,我通常最终会犯错误和/或花费比我真正需要的更长的时间,而这些错误和/或花费的时间比让我将这些算法构建为要在项目中使用的时间长。 当然,我会尽可能地重用我可以使用的内容,但是很多次我都忘记了我过去建立了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 .

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

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 pdimport pandas.io.data as webfrom 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中的数据。

SPY historical data - stock market indicators
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”的列。

SPY Historical Data with RSI  - stock market indicators
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”重命名为“关闭”)。

翻译自:

python 股市指标模块

转载地址:http://lwhwd.baihongyu.com/

你可能感兴趣的文章
git使用——分支
查看>>
selenium之多线程启动grid分布式测试框架封装(一)
查看>>
突然的伤感
查看>>
Objective-C 链式编程思想
查看>>
iOS开发之XMPP即时通讯简单实现
查看>>
Java连接各类数据库
查看>>
Multiload-ng
查看>>
Java7并发编程实战(一) 线程的等待
查看>>
mysql update语句添加表关联查询
查看>>
开通了一个微信公众账号,主要想分享一些自己对于行业、技术和产品的思考以及收录精彩内容给读者...
查看>>
留言板
查看>>
c#$用法
查看>>
js通过as完成socket通信
查看>>
关于我书里提到的“挂多个类,使用类的组合”的一些补充
查看>>
JavaScrip中的循环语句
查看>>
IntelliJ IDEA和Webstorm 的下载安装、注册码(永久有效)
查看>>
停止使用非版本控制的可执行代码
查看>>
Php5.3的lambda函数以及closure(闭包)
查看>>
动画优化之window.requestAnimationFrame()
查看>>
用GDB调试程序(一)
查看>>