Skip to main content

The Python implementation of an stock analysis and visualization tool.

Project description

StockInsider

Package Version Contributions welcome License build

这是一个用来获取中国上市股票价格相关的信息,计算交易指标,并将交易指标绘出的Python工具。指标的计算大多是根据这个博客里 列出的技术分析指标总结: [By woojean](http://www.woojean.com/2018/03/09/%E6%8A%80%E6%9C%AF%E5%88%86%E6%9E%90%E6%8C%87%E6%A0%87%E6%80%BB%E7%BB%93/) 以及同花顺给出的公式。

This is a Python tool to calculate the trading indicators for analysis and visualize stock price and indicator lines. This tool provides a simple API for the collection stock information from Chinese stock market, but you could also use it to get the stock data from your saved stock information in CSV format.

How to use the tool (如何使用这个工具)

Installation (安装)

You can choose to either download it from PyPI through: (你可以选择从PyPI上下载)

pip install StockInsider

or to install the latest version: (也可以安装最新的Github版本)

pip install -U git+https://github.com/charlesdong1991/StockInsider

or fork and clone the repo to your local device, and install the dependencies through: (或者克隆这个项目到你本地电脑,然后安装所需包)

git clone git@github.com:charlesdong1991/StockInsider.git
cd StockInsider
pip install -r requirements.txt

Tool Overview (工具简介)

主要工具是StockInsider,它需要两个参数:必要参数是code,它代表股票在交易 所的代码,另一个是可选参数ktype,它代表股票数据类型。

首先,值得注意的是你需要输入完整的股票代码,我只试过上海和深圳股票交易所的代码,所以 可以肯定的是这两个交易所上市的公司的股票信息是支持的,比如sz002156或者sh603019, 目前的工具只支持股票代码以sh或者sz开头。

其次,股票数据类型是以何种频率股票价格等信息会汇总,默认是D,表示是日线。也可以选择 W(周线),M(月线)。

值得注意的是,所有的指标都是以日线为基准的。

The main tool is called StockInsider, and it takes in two parameters: code which is mandatory and an optional ktype argument. Once the tool is initiated, the data is being stored under the hood.

First, you need to explicitly type the full code of the stock you would like to analyze, e.g. sz002156 or sh603019. Be aware that only stocks traded in Shanghai and Shenzhen Stock Exchange can be collected, thus the code to put has to start with either sh or sz.

ktype is the frequency of the aggregated stock price, default is D which refers to daily stock price, and other allowed values are W (weekly) and M (monthly).

from insider import StockInsider
si = StockInsider("sz002516")

Calculate and Plot Trading Indicators on your Saved Stock Data (利用你已有的股票数据来计算和绘出曲线)

如果你想利用这个工具来计算和绘出其他的(非中国)股票数据,可以利用以下来初始化工具.

Other than calculating and plotting trading indicators for Chinese stock data (default), you could also use this tool to do the same for any of your saved stock data in CSV. You could initialize your StockInsider by from_external_csv_data, and then use the initialized tool to do the rest.

from insider import StockInsider
fpath = "path/to/your/stock/data/in/CSV"
si = StockInsider.from_external_csv_data(fapth=fpath, code="my stock")

Stock Price Visualization (股票价格图)

如果想用这个工具单纯的画出股票K线,只需要输入 si.plot(), 就会得到K线走势。

这也有一些选择,比如可以选择head来决定只绘出近N日的股票价格而不是所有的,也可以 选择开始和截止日期内的股票价格;如果想画出向同花顺软件类似的MA曲线,可以选择verbose=True来 做到。

Once initiation, you could use the tool to plot stock price and the corresponding indicators over time.

In order to plot the stock price, you could simply do:

si.plot()

And there are other options for you:

  • You could to select only certain number of recent trading days to plot by changing the head parameters, default is 90.
  • You could choose a start/end date of stock prices to plot by defining start_date and end_date.
  • If you would like to only have the k-lines hidden in the plot, you can set verbose to False.

Stock Trading Indicator Visualization (股票交易指标图)

Currently, there are many commonly used indicators supported in this tool (目前,这个工具支持画出以下的指标):

  • MA (Moving Average, 移动平均线): si.plot_ma()
  • MD (Moving Deviation, 移动标准差曲线): si.plot_md()
  • EMA (Exponential Moving Average, 指数移动平均线): si.plot_ema()
  • MACD (Moving Average Convergence/Divergence,指数平滑移动均线): si.plot_macd()
  • KDJ (随机指标): si.plot_kdj
  • RSI (Relative Strength Index,相对强弱指标): si.plot_rsi()
  • VRSI (Volumn Relative Strength Index,量相对强弱指标): si.plot_vrsi()
  • Volumn change(量变指标): si.plot_volumn()
  • VMA (Volumn Moving Average,量移动平均线): si.plot_vma()
  • VSTD(成交量标准差): si.plot_vstd()
  • VMACD (Volumn Moving Average Convergence/Divergence,量指数平滑异同平均线): si.plot_vmacd()
  • ENV(轨道线): si.plot_env()
  • VOSC (Volumn Oscillator,量震荡指标): si.plot_vosc()
  • MI (Momentum Indicator,动力指标): si.plot_mi()
  • MIKE(麦克指标): si.plot_mike()
  • ADTM(动态买卖气指标): si.plot_adtm()
  • OBV (On Balance Volumn,能量潮曲线): si.plot_obv()
  • RC (Price rate of Change,变化率指标): si.plot_rc()
  • BOLL (BOLL line,布林线): si.plot_boll()
  • BBIBOLL (BBI BOLL line,多空布林线): si.plot_bbiboll()
  • ATR (Average True Ranger,真实波幅): si.plot_atr()
  • CDP (Contrarian Operation,逆势操作指标): si.plot_cdp()
  • SAR (Stop And Reverse,停损点转向指标): si.plot_sar()
  • MTM (Momentum Index,动量指标): si.plot_mtm()
  • DMI (Directional Movement Index, 动向指标): si.plot_dmi()

And there are several options to tune with(一些可以其他的选项):

  • Choose to plot with the stock price by defining verbose to True (是否将股票价格一并绘出)
  • Choose the number of recent trading days of stock information to plot via setting head. (选择最近N日的股票价格来绘出)
  • Choose which indicators are included in the plot, e.g. only plot 5-day and 60-day line of MA indicator by setting ns to [5, 60]. (当有多条指标线绘出时,可以选择 只绘出其中的指定的线而不是全部绘出)

And some other more for specific indicators, please check out the docstrings (每个指标可能都有 一些自己独有的参数,可以查看公式注释).

Get the original stock data in Pandas DataFrame (以Pandas DataFrame的形式得到原始数据)

如果你想利用原始数据自己做一些有意思的项目,可以选择si.show_data()来得到原始股票数据,这些数据将以 Pandas DataFrame的形式返回。

If you would love to play with the data and do some other more fancy stuff, you could also use the tool as a scrapper. Simply use si.show_data() could return the data in Pandas DataFrame.

Get the calculated indicator data in Pandas DataFrame (以Pandas DataFrame的形式得到计算出来的指标数据)

你也可以得到计算好的指标数据,你只需要用指标名称即可,而不需要绘出,比如si.macd()

If you do not like the plot, and want to do other fancy analysis or visualization, it is certainly okay! And you just need to call the indicator name, e.g. si.vosc(), or si.macd(), then it will return the subset with those indicators in it.

Gallery (样例)

  • Example1
si = StockInsider("sh603019")
si.plot(head=120, verbose=True)

Example 1

  • Example2
si = StockInsider("sz002156")
si.plot_ma(head=90, verbose=False)

Example 2

  • Example3
si = StockInsider("sz002156")
si.plot_ema(ns=[5, 20, 60], verbose=True)

Example 3

  • Example4
si = StockInsider("sz002156")
si.plot_macd()

Example 4

  • Example5
si = StockInsider("sh603019")
si.plot_kdj(head=90, smooth_type="sma", n=7)

Example 5

  • Example6
si = StockInsider("sh603019")
si.plot_rsi(head=80, ns=[6, 12])

Example 6

  • Example7
si = StockInsider("sh603019")
si.plot_vma(verbose=True)

Example 7

  • Example8
si = StockInsider("sh603019")
si.plot_vmacd(head=60)

Example 8

  • Example9
si = StockInsider("sh603019")
si.plot_env(verbose=True, head=60)

Example 9

  • Example10
si = StockInsider("sh603019")
si.plot_mike(head=60, ns=["ws", "wr"], verbose=True)

Example 10

  • Example11
si = StockInsider("sh603019")
si.plot_adtm(head=120)

Example 11

  • Example12
si = StockInsider("sh603019")
si.plot_bbiboll(head=80, n=11, m=9, verbose=True)

Example 12

  • Example13
si = StockInsider("sh603019")
si.plot_cdp(verbose=False)

Example 13

  • Example14
si = StockInsider("sh603019")
si.plot_sar(head=90, verbose=True)

Example 14

  • Example15
si = StockInsider("sh603019")
si.plot_dmi()

Example 15

Donation (打赏)

It takes time and efforts to make the tool, and if you like it, you could also support the author by donation via Wechat Pay :)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

StockInsider-0.2.0.tar.gz (19.4 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page