手把手教你如何用Python和數(shù)據(jù)科學(xué)賺錢钾腺?

金融領(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>

image

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è)人需求爬立。

祝大家賺錢開心钾唬!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子抡秆,更是在濱河造成了極大的恐慌奕巍,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,183評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件儒士,死亡現(xiàn)場離奇詭異的止,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)着撩,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評論 3 399
  • 文/潘曉璐 我一進(jìn)店門诅福,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人拖叙,你說我怎么就攤上這事氓润。” “怎么了薯鳍?”我有些...
    開封第一講書人閱讀 168,766評論 0 361
  • 文/不壞的土叔 我叫張陵咖气,是天一觀的道長。 經(jīng)常有香客問我挖滤,道長崩溪,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,854評論 1 299
  • 正文 為了忘掉前任斩松,我火速辦了婚禮伶唯,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘砸民。我一直安慰自己抵怎,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,871評論 6 398
  • 文/花漫 我一把揭開白布岭参。 她就那樣靜靜地躺著反惕,像睡著了一般。 火紅的嫁衣襯著肌膚如雪演侯。 梳的紋絲不亂的頭發(fā)上姿染,一...
    開封第一講書人閱讀 52,457評論 1 311
  • 那天,我揣著相機(jī)與錄音秒际,去河邊找鬼悬赏。 笑死,一個(gè)胖子當(dāng)著我的面吹牛娄徊,可吹牛的內(nèi)容都是我干的闽颇。 我是一名探鬼主播,決...
    沈念sama閱讀 40,999評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼寄锐,長吁一口氣:“原來是場噩夢啊……” “哼兵多!你這毒婦竟也來了尖啡?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,914評論 0 277
  • 序言:老撾萬榮一對情侶失蹤剩膘,失蹤者是張志新(化名)和其女友劉穎衅斩,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體怠褐,經(jīng)...
    沈念sama閱讀 46,465評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡畏梆,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,543評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了奈懒。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片奠涌。...
    茶點(diǎn)故事閱讀 40,675評論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖筐赔,靈堂內(nèi)的尸體忽然破棺而出铣猩,到底是詐尸還是另有隱情揖铜,我是刑警寧澤茴丰,帶...
    沈念sama閱讀 36,354評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站天吓,受9級特大地震影響贿肩,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜龄寞,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,029評論 3 335
  • 文/蒙蒙 一汰规、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧物邑,春花似錦溜哮、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,514評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至科阎,卻和暖如春述吸,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背锣笨。 一陣腳步聲響...
    開封第一講書人閱讀 33,616評論 1 274
  • 我被黑心中介騙來泰國打工蝌矛, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人错英。 一個(gè)月前我還...
    沈念sama閱讀 49,091評論 3 378
  • 正文 我出身青樓入撒,卻偏偏與公主長得像,于是被迫代替她去往敵國和親椭岩。 傳聞我的和親對象是個(gè)殘疾皇子茅逮,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,685評論 2 360

推薦閱讀更多精彩內(nèi)容