十年前有個(gè)朋友的畢業(yè)論文題目與股票數(shù)據(jù)分析相關(guān)跃须。于是他用程序?qū)崿F(xiàn)了解析通達(dá)信數(shù)據(jù)蚤吹、計(jì)算統(tǒng)計(jì)指標(biāo)千绪、繪圖……結(jié)果時(shí)間過(guò)去大半充易,核心的分析功能還沒(méi)開(kāi)始實(shí)現(xiàn)。
而現(xiàn)在使用Python語(yǔ)言荸型,下載數(shù)據(jù)盹靴、計(jì)算統(tǒng)計(jì)指標(biāo)、繪圖瑞妇,以及數(shù)據(jù)分析都有現(xiàn)成的工具稿静,我們不用再去關(guān)注細(xì)節(jié),可以將主要精力用于組合工具和實(shí)驗(yàn)踪宠。本篇將介紹量化交易相關(guān)的常用三方庫(kù)自赔。
1.常用三方庫(kù)
數(shù)據(jù)分析工具
Pandas:數(shù)據(jù)表工具
Numpy:數(shù)學(xué)計(jì)算工具
Scipy:數(shù)學(xué)計(jì)算工具
Sklearn:機(jī)器學(xué)習(xí)工具下載數(shù)據(jù)
TuShare:讀取國(guó)內(nèi)股票數(shù)據(jù)
Pandas_reader:讀取國(guó)內(nèi)外股票數(shù)據(jù)回溯框架
PyAlgoTrade:離線的分析回溯工具,PyAlgoTrade-cn為A股版本
Zipline:分析回溯工具計(jì)算技術(shù)指標(biāo)
TA-Lib:計(jì)算技術(shù)指標(biāo)柳琢,底層由C語(yǔ)言實(shí)現(xiàn)绍妨,支持150多個(gè)函數(shù)
Pandas_talib:計(jì)算技術(shù)指標(biāo)润脸,由Python語(yǔ)言實(shí)現(xiàn),支持30多個(gè)函數(shù)金融繪圖
Mpl_finance:金融繪圖工具他去,原matplotlib中的子模塊
數(shù)據(jù)分析工具比較通用毙驯,不在股票專題中介紹;上一篇在數(shù)據(jù)下載方法中介紹了Tushare和Pandas_reader軟件的用法灾测,下一篇將講解回溯工具爆价,本篇?jiǎng)t介紹計(jì)算技術(shù)指標(biāo)和金融相關(guān)的繪圖方法。
2.計(jì)算技術(shù)指標(biāo)
a) 技術(shù)指標(biāo)
Ta-lib支持150多個(gè)函數(shù)媳搪,而pandas_talib也加入了一些量?jī)r(jià)結(jié)合的新功能铭段,可以二者配合使用,通過(guò)分析pandas_talib源碼可以看到功能的具體實(shí)現(xiàn)方法秦爆。
Ta-lib從各種均線到蠟燭圖指標(biāo)序愚,能想到的差不多都有,分成以下幾類:
- Overlap Studies(重疊研究類)
- Momentum Indicators(動(dòng)量指標(biāo)類)
- Volume Indicators(成交量指標(biāo)類)
- Volatility Indicators(波動(dòng)性指標(biāo)類)
- Price Transform(價(jià)格指標(biāo)類)
- Cycle Indicators(周期指標(biāo)類)
- Pattern Recognition(形態(tài)識(shí)別類)
- Statistic Functions(統(tǒng)計(jì)函數(shù)類)
- Math Transform(數(shù)學(xué)變換類)
- Math Operators(數(shù)學(xué)運(yùn)算符類)
詳見(jiàn)talib中文文檔:https://github.com/HuaRongSAO/talib-document等限,點(diǎn)開(kāi)每個(gè)指標(biāo)都有中文說(shuō)明及例程爸吮。
b) 直接安裝
由于ta-lib底層依賴C語(yǔ)言實(shí)現(xiàn),在Windows系統(tǒng)中安裝過(guò)程相對(duì)比較麻煩望门,我在ubuntu系統(tǒng)中安裝形娇。
$ sudo pip install Ta-Lib
$ sudo pip install git+git://github.com/femtotrader/pandas_talib.git
如果有以下錯(cuò)誤提示:
talib/_ta_lib.c:526:28: fatal error: ta-lib/ta_defs.h
這是由于沒(méi)有底層依賴C庫(kù)支持,在Ubuntu系統(tǒng)中用以下方法安裝筹误。
$ wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz
$ tar xvzf ta-lib-0.4.0-src.tar.gz
$ cd ta-lib/
$ ./configure
$ sudo make install
$ sudo ldconfig
c) Github下載
$ git clone https://github.com/mrjbq7/ta-lib.git
$ git clone https://github.com/femtotrader/pandas_talib.git
d) 使用方法
輸入數(shù)據(jù)默認(rèn)為Numpy的ndarray格式桐早,而Pandas也是基于Numpy,基本能直接使用纫事,使用方法形如: real = talib.ROC(df.close, timeperiod=10)
3.金融繪圖
從matplotlib2.2.0版本開(kāi)始勘畔,matplotlib.finance已經(jīng)從matplotlib中剝離出來(lái)所灸,名為mpl_finance丽惶,需要單獨(dú)安裝。
a) 安裝
$ sudo pip install https://github.com/matplotlib/mpl_finance/archive/master.zip
b) 使用
下例為使用tushare下載002230爬立,并繪制K線圖:
from matplotlib import pyplot as plt
import mpl_finance as mpf
from matplotlib.pylab import date2num
import pandas as pd
import datetime
import tushare as ts
%matplotlib inline
def draw(stock):
stock['datestr'] = stock['date'].apply(lambda x: date2num(datetime.datetime.strptime(x,'%Y-%m-%d')))
quotes = stock[['datestr','open','high','low','close']]
fig, ax = plt.subplots(figsize=(12,8))
fig.subplots_adjust(bottom=0.1)
ax.xaxis_date()
plt.xticks(rotation=45) #日期顯示的旋轉(zhuǎn)角度
mpf.candlestick_ohlc(ax, quotes.values, width=0.7, colorup='r', colordown='g')
# 格式是:date,open,high,low,close, 格式為list
plt.grid(True)
plt.show()
df = ts.get_hist_data('002230', start='2019-08-01')
df = df.reset_index()
draw(df)
程序運(yùn)行結(jié)果如下圖所示:
學(xué)任何一門復(fù)雜的技術(shù)都需要時(shí)間和精力钾唬,可以說(shuō)無(wú)捷徑可走,操作策略也需要經(jīng)過(guò)反復(fù)驗(yàn)證后再使用侠驯,不懂就做一般都會(huì)被割韭菜抡秆,所以要么花時(shí)間精力,要么交給適合的人吟策,要么別碰儒士。