金融領(lǐng)域或許是數(shù)據(jù)科學(xué)應(yīng)用場景中最充滿想象力的部分垮庐,畢竟它跟財(cái)富結(jié)合地?zé)o比緊密坞琴。
不管是否是經(jīng)濟(jì)達(dá)人剧辐,數(shù)據(jù)科學(xué)都是一種幫你了解一支股票的高效方式荧关。本文作者把數(shù)據(jù)科學(xué)和機(jī)器學(xué)習(xí)技術(shù)應(yīng)用到金融領(lǐng)域中忍啤,向你展示如何通過數(shù)據(jù)分析的方式馳騁股市,搭建自己的金融模型鳄梅!
讓我們先了解一些基本定義戴尸。
定義和假設(shè)
什么是交易算法孙蒙?
Quantopian定義:
交易算法是一種計(jì)算機(jī)程序,它定義了一套買賣資產(chǎn)的規(guī)則香追。大多數(shù)交易算法基于研究歷史數(shù)據(jù)得出的數(shù)學(xué)或統(tǒng)計(jì)模型來做出決策翅阵。
我們使用什么平臺掷匠?
我使用Anaconda, Jupyter Notebooks, 和 PyCharm實(shí)現(xiàn)Python建模岖圈,使用這些工具非常容易蜂科。但是导匣,你也可以使用Quantopian平臺內(nèi)置內(nèi)核工具贡定,或者甚至可以根據(jù)需要將代碼修改為R或者其他語言。
我使用Mac系統(tǒng)蚓耽,并將全程分享所用的UNIX命令步悠。Windows用戶請自行搜索答案瘫镇!
我們關(guān)注哪些資產(chǎn)铣除?
Apple蘋果(AAPL)是一支很好的股票通孽,因?yàn)槟壳盀橹梗?018年9月)它已經(jīng)是世界上價(jià)值最高的公司背苦,不僅擁有相對穩(wěn)定的股票價(jià)格,而且擁有足夠多與品牌相關(guān)的體量秕噪、新聞和人氣腌巾。
需要提醒:此處涵蓋的原則對于較小的公司股本澈蝙,或不同的行業(yè)等的適用性有所不同灯荧。
環(huán)境搭建
要在本地電腦上獲取Quantopian平臺逗载,請?jiān)诮K端執(zhí)行以下命令:
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; caret-color: rgb(51, 51, 51); color: rgb(51, 51, 51); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; font-size: 16px; overflow-x: auto;">
create conda py35 since that's the newest version that works
conda create -n py35 python=3.5
conda install -c quantopian/label/ci -c quantopian zipline
</pre>
為了確保Quandl正常運(yùn)行厉斟,請根據(jù)賬號創(chuàng)建說明和API文檔加載金融數(shù)據(jù)擦秽。另外漩勤,請保存好你的API key锯七,因?yàn)樾枰玫剿鼇砑虞d所有重要數(shù)據(jù)眉尸。
加載數(shù)據(jù)
讓我們開始使用代碼庫:
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; caret-color: rgb(51, 51, 51); color: rgb(51, 51, 51); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; font-size: 16px; overflow-x: auto;">
import pandas as pd
import numpy as np
import patsy
pd.core.common.is_list_like = pd.api.types.is_list_like
from pandas_datareader import data
import quandl
quandl.ApiConfig.api_key = "##############"
</pre>
現(xiàn)在讓我們來拉取些Apple股票數(shù)據(jù):
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; caret-color: rgb(51, 51, 51); color: rgb(51, 51, 51); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; font-size: 16px; overflow-x: auto;">
df = quandl.get("WIKI/" + 'AAPL', start_date="2014-01-01")
</pre>
注意觀察這些列霉祸,注意其中一個(gè)名為“分割比例”的列袱蜡。這是一個(gè)非常重要的指標(biāo)坪蚁;它標(biāo)志著股票拆分發(fā)生。在2014年缅茉,Apple決定采用7:1進(jìn)行股票分割男摧,我們可以使用Python 和pandas 來查詢發(fā)生的日期:
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; caret-color: rgb(51, 51, 51); color: rgb(51, 51, 51); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; font-size: 16px; overflow-x: auto;">
len(df)
df['Split Ratio'].value_counts()
df[df['Split Ratio'] == 7.0]
</pre>
我們從而找到了2014-06-09拇颅。讓我們拉取這個(gè)日期后的股票價(jià)格信息:
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; caret-color: rgb(51, 51, 51); color: rgb(51, 51, 51); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; font-size: 16px; overflow-x: auto;">
aapl_split = quandl.get("WIKI/" + 'AAPL', start_date="2014-06-10")
aapl_split.head()
</pre>
順便說一句蔬蕊,我在GitHub上找到了所有財(cái)富500強(qiáng)的股票代碼清單岸夯。如果你想將自己的分析擴(kuò)展到股票集,可以像這樣將它們加載到列表中:
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; caret-color: rgb(51, 51, 51); color: rgb(51, 51, 51); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; font-size: 16px; overflow-x: auto;">
f500 = pd.read_csv('https://raw.githubusercontent.com/datasets/s-and-p-500-companies/master/data/constituents.csv')
tickers = f500.Symbol.tolist()
</pre>
關(guān)鍵統(tǒng)計(jì)數(shù)據(jù)
增廣迪基-福勒檢驗(yàn)(Augmented Dickey-Fuller test),簡稱ADF檢驗(yàn)们妥。
我們需要檢驗(yàn)單位根是否存在猜扮,可以使用ADF測試完成檢驗(yàn)。簡而言之监婶,單位根存在則預(yù)示存在驅(qū)動AAPL的潛在趨勢旅赢,從而我們可以提取模式并用于預(yù)測。
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; caret-color: rgb(51, 51, 51); color: rgb(51, 51, 51); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; font-size: 16px; overflow-x: auto;">
run ADF to determine unit root
import statsmodels.tsa.stattools as ts
cadf = ts.adfuller(aapl_split.Close)
print('Augmented Dickey Fuller:')
print('Test Statistic =',cadf[0])
print('p-value =',cadf[1])
print('Critical Values =',cadf[4])
Augmented Dickey Fuller:
Test Statistic = -0.731194982176
p-value = 0.838503045276
Critical Values = {‘1%’: -3.4372231474483499, ‘5%’: -2.8645743628401763, ‘10%’: -2.5683856650361054}
</pre>
我們將上面的測試統(tǒng)計(jì)值與臨界值進(jìn)行比較惑惶;如果它低于我們選擇的閾值煮盼,則拒絕存在單位根的零假設(shè)带污。正如你所見僵控,p-value比較大,所以我們必須接受原假設(shè)(Ho):即AAPL存在單位根鱼冀。這個(gè)結(jié)果很好报破,因?yàn)槲覀兛梢岳脻撛诘内厔莺湍J竭M(jìn)行預(yù)測。
與其他股票的相關(guān)性
Apple被認(rèn)為是一個(gè)巨頭技術(shù)品牌千绪。假如我們能夠計(jì)算與其他股票的強(qiáng)相關(guān)性會怎么樣充易?
請注意相關(guān)性并不意味著因果關(guān)系,并且可能存在著哪個(gè)股票是先行者的問題荸型,但是模式和關(guān)系對于提高模型性能總是一件好事盹靴。
我建議你查看三支股票,以及AAPL如何與它們關(guān)聯(lián):
微軟Microsoft (MSFT)
因特爾Intel (INTC)
蒂芙尼Tiffany & Co. (TIF)
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; caret-color: rgb(51, 51, 51); color: rgb(51, 51, 51); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; font-size: 16px; overflow-x: auto;">
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
MSFT = quandl.get("WIKI/" + 'MSFT', start_date="2014-06-10")
INTC = quandl.get("WIKI/" + 'INTC', start_date="2014-06-10")
TIF = quandl.get("WIKI/" + 'TIF', start_date="2014-06-10")
</pre>
時(shí)間緣故,我們在這里只關(guān)注Intel數(shù)據(jù)稿静;讓我們繪制AAPL與INTC的收盤價(jià):
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; caret-color: rgb(51, 51, 51); color: rgb(51, 51, 51); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; font-size: 16px; overflow-x: auto;">
sns.jointplot(INTC.Close, aapl_split.Close, kind="reg");
</pre>
Intel vs. Apple
我們還可以看看相關(guān)值數(shù)據(jù)( correlation value ):
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; caret-color: rgb(51, 51, 51); color: rgb(51, 51, 51); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; font-size: 16px; overflow-x: auto;">
np.corrcoef(INTC.Close, aapl_split.Close)
</pre>
我們注意到r-value為0.7434梭冠;就預(yù)測來說不錯(cuò),但我們需要記住一個(gè)重要的事實(shí):如果我們知道INTC的收盤價(jià)自赔,我們也可以看查AAPL的收盤價(jià)妈嘹。所以讓我們查看下INTC七天前的收盤價(jià)的相關(guān)性柳琢,來獲得更可行的指標(biāo):
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; caret-color: rgb(51, 51, 51); color: rgb(51, 51, 51); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; font-size: 16px; overflow-x: auto;">
seven day lead
np.corrcoef(INTC.Close[:-7], aapl_split.Close[7:])
</pre>
這次我們得到r-value為0.7332绍妨;還是很不錯(cuò)的!
谷歌趨勢(Google Trends)
我們可以比較Twitter和其他社交網(wǎng)絡(luò)人氣數(shù)據(jù)如何影響股價(jià)〖砹常現(xiàn)在讓我們看看Google Trends是否與AAPL相關(guān)他去。請確保為指定時(shí)間范圍,或使用此鏈接(https://trends.google.com/trends/explore?date=2014-06-10%202018-04-02&q=%2Fm%2F0k8z)來進(jìn)行準(zhǔn)確搜索(注意我在四月多添加了幾天來處理半周問題)倒堕,然后將CSV加載到Python中:
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; caret-color: rgb(51, 51, 51); color: rgb(51, 51, 51); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; font-size: 16px; overflow-x: auto;">
Google Trends
aapl_trends = pd.read_csv('/Users/jb/Desktop/multiTimeline.csv', header=1)
aapl_trends.tail()
</pre>
注意每周的數(shù)據(jù)格式灾测,因此我們需要使用 pandas.resample() 轉(zhuǎn)換我們的股票價(jià)格數(shù)據(jù)集:
API鏈接:
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.resample.html
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; caret-color: rgb(51, 51, 51); color: rgb(51, 51, 51); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; font-size: 16px; overflow-x: auto;">
aapl_split_week = aapl_split.resample('W', convention='end').last()
</pre>
現(xiàn)在讓我們檢查相關(guān)性并繪制給定周的Google搜索請求數(shù)據(jù)總和圖表,以及該周最后一個(gè)工作日的收盤價(jià)格:
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; caret-color: rgb(51, 51, 51); color: rgb(51, 51, 51); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.5440000295639038px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; font-size: 16px; overflow-x: auto;">
trend and price corr
np.corrcoef(aapl_trends['Apple: (Worldwide)'], aapl_split_week.Close)
</pre>
哎呀垦巴!我們得到了一個(gè)微不足道的0.0454媳搪,這個(gè)數(shù)據(jù)有些道理,我們可以想一下:AAPL相關(guān)的新聞/活動/閑談并不是影響股票價(jià)格的積極因素骤宣。像人氣這種對其有重要影響應(yīng)該能提供更強(qiáng)的信號秦爆,但我們在下次再做討論。
結(jié)語
我們只是淺顯的討論了下部分EDA( Exploratory Data Analysis)可以做的金融分析憔披,但是在下一篇文章中等限,我們將過渡到建立預(yù)測模型并通過高級軟件包來為我們實(shí)現(xiàn)繁重的工作。
我們希望這篇文章對你有幫助芬膝,并且很樂意在評論中聽到你的意見:
運(yùn)行代碼是否遇到任何問題望门?有時(shí)候環(huán)境和版本會搞砸一切……
你使用什么包和技術(shù)?
那些可視化工具有助于了解股票價(jià)格的變動锰霜?
你認(rèn)為哪些因素會最大化模型預(yù)測效果筹误?
最后,如果你恰好知道一種持續(xù)賺大錢的建模技術(shù)癣缅,請直接告訴我們 ! 如果你們喜歡這個(gè)系列纫事,請持續(xù)關(guān)注大數(shù)據(jù)文摘的后續(xù)文章。
最后的最后所灸,本文提供的信息和隨附材料僅供參考丽惶。本文不應(yīng)被當(dāng)做法律或財(cái)務(wù)建議。你應(yīng)咨詢律師或其他專業(yè)人士來確定什么最適合你的個(gè)人需求爬立。
祝大家賺錢開心钾唬!