深入淺出 Python 量化交易實戰

段小手

  • 深入淺出 Python 量化交易實戰-preview-1
  • 深入淺出 Python 量化交易實戰-preview-2
  • 深入淺出 Python 量化交易實戰-preview-3
深入淺出 Python 量化交易實戰-preview-1

買這商品的人也買了...

相關主題

商品描述

本書主要以國內A股市場為例,借助第三方量化交易平臺,講述了KNN、線性模型、決策樹、支持向量機、樸素貝葉斯等常見機器學習算法在交易策略中的應用,同時展示瞭如何對策略進行回測,以便讓讀者能夠有效評估自己的策略。 另外,本書還講解了自然語言處理(NLP)技術在量化交易領域的發展趨勢,並使用時下熱門的深度學習技術,向讀者介紹了多層感知機、捲積神經網絡,以及長短期記憶網絡在量化交易方面的前瞻性應用。 本書沒有從Python基礎語法講起,對於傳統交易策略也只是一帶而過,直接將讀者帶入機器學習的世界。本書適合對Python語言有一定瞭解且對量化交易感興趣的讀者閱讀。

目錄大綱

VII

 

目 錄

 

第1章 小瓦的故事—從零開始

 

1.1 何以解憂,“小富”也行 ···············1

 

1.1.1 那些年,那些交易 ···························2

 

1.1.2 自動化交易和高頻交易 ·····················2

 

1.1.3 因子投資悄然興起 ···························3

 

1.2 機器學習崛起 ······························4

 

1.2.1 量化投資風生水起 ···························4

 

1.2.2 沒有數據是不行的 ···························5

 

1.2.3 交易策略和阿爾法因子 ·····················5

 

1.3 要想富,先配庫 ···························6

 

1.3.1 Anaconda的下載和安裝 ·····················6

 

1.3.2 Jupyter Notebook的基本使用方法 ········8

 

1.3.3 用真實股票數據練練手 ···················11

 

1.4 小結 ········································15

 

第2章 小瓦的策略靠譜嗎—回測與經典策略

 

2.1 對小瓦的策略進行簡單回測 ··········16

 

2.1.1 下載數據並創建交易信號 ················16

 

2.1.2 對交易策略進行簡單回測 ················18

 

2.1.3 關於回測,你還需要知道的 ·············20

 

2.2 經典策略之移動平均策略 ·············21

 

2.2.1 單一移動平均指標 ·························21

 

2.2.2 雙移動平均策略的實現 ···················23

 

2.2.3 對雙移動平均策略進行回測 ·············26

 

2.3 經典策略之海龜策略 ···················28

 

2.3.1 使用海龜策略生成交易信號 ·············28

 

2.3.2 根據交易信號和倉位進行下單 ··········29

 

2.3.3 對海龜策略進行回測 ······················31

 

2.4 小結 ········································34

 

第3章 AI來了—機器學習在交易中的簡單應用

 

3.1 機器學習的基本概念 ···················35

 

3.1.1 有監督學習和無監督學習 ················35

 

 

 

深入淺出Python量化交易實戰

 

 

VIII

 

3.1.2 分類和回歸···································37

 

3.1.3 模型性能的評估·····························37

 

3.2 機器學習工具的基本使用方法 ·······37

 

3.2.1 KNN算法的基本原理 ·····················38

 

3.2.2 KNN算法用於分類 ························38

 

3.2.3 KNN算法用於回歸 ························43

 

3.3 基於機器學習的簡單交易策略 ·······47

 

3.3.1 獲取股票數據································47

 

3.3.2 創建交易條件································49

 

3.3.3 使用分類算法制定交易策略 ·············50

 

3.4 小結 ········································54

 

第4章 多來點數據—借助量化交易平臺

 

4.1 數據不夠,平臺來湊 ···················55

 

4.1.1 選擇量化交易平臺 ·························56

 

4.1.2 量化交易平臺的研究環境 ················57

 

4.1.3 在研究環境中運行代碼 ···················58

 

4.2 借助財務數據篩選股票 ················59

 

4.2.1 獲取股票的概況·····························60

 

4.2.2 獲取股票的財務數據 ······················62

 

4.2.3 通過財務指標進行選股 ···················64

 

4.3 誰是幕後“大佬” ······················65

 

4.3.1 找到最大的股東·····························66

 

4.3.2 大股東們增持了還是減持了 ·············67

 

4.3.3 資金凈流入還是凈流出 ···················69

 

4.4 小結 ········································71

 

第5章 因子來了—基本原理和用法

 

5.1 “瓦氏因子”瞭解一下 ················72

 

5.1.1 獲取主力資金流向數據 ···················73

 

5.1.2 簡易特徵工程································74

 

5.1.3 “瓦氏因子”的計算 ······················75

 

5.1.4 用添加“瓦氏因子”的數據訓練模型 ······76

 

5.1.5 “因子”都能乾啥 ·························77

 

5.2 股票不知道怎麽選?因子來幫忙 ····78

 

5.2.1 確定股票池···································78

 

5.2.2 獲取滬深兩市的全部指數 ················79

 

5.2.3 獲取股票的市值因子 ······················80

 

5.2.4 獲取股票的現金流因子 ···················81

 

5.2.5 獲取股票的凈利率因子 ···················82

 

5.2.6 獲取股票的凈利潤增長率因子 ··········83

 

5.3 把諸多因子“打個包” ················84

 

5.3.1 將4個因子存入一個DataFrame ········84

 

5.3.2 使用PCA提取主成分 ·····················85

 

5.3.3 找到主成分數值最高的股票 ·············86

 

5.4 小結 ········································87

 

 

 

目錄

 

 

IX

 

第6章 因子好用嗎—有些事需要你知道

 

6.1 針對投資組合獲取因子值 ·············88

 

6.1.1 建立投資組合並設定日期 ················88

 

6.1.2 獲取一個情緒因子 ·························90

 

6.1.3 獲取全部的因子分析結果 ················91

 

6.2 因子收益分析 ····························92

 

6.2.1 因子各分位統計·····························92

 

6.2.2 因子加權多空組合累計收益 ·············94

 

6.2.3 做多最大分位做空最小分位收益 ·······96

 

6.2.4 分位數累計收益對比 ······················97

 

6.3 因子IC分析 ·····························98

 

6.3.1 因子IC分析概況 ···························99

 

6.3.2 因子IC時間序列圖 ························99

 

6.3.3 因子IC正態分佈Q-Q圖和月度均值 ·····101

 

6.4  因子換手率、因子自相關性和因子預 

測能力分析 ······························102

 

6.4.1 因子換手率分析····························103

 

6.4.2 因子自相關性分析 ························104

 

6.4.3 因子預測能力分析 ························106

 

6.5 小結 ·······································107

 

第7章 當因子遇上線性模型

 

7.1 什麽是線性模型 ························108

 

7.1.1 準備用於演示的數據 ·····················108

 

7.1.2 來試試最簡單的線性回歸 ···············110

 

7.1.3 使用正則化的線性模型 ··················113

 

7.2 用線性模型搞搞交易策略 ············115

 

7.2.1 準備因子·····································115

 

7.2.2 訓練模型·····································117

 

7.2.3 基於模型的預測進行選股 ···············118

 

7.3 能不能賺到錢 ···························119

 

7.3.1 平臺的策略回測功能 ·····················120

 

7.3.2 把研究成果寫成策略 ·····················121

 

7.3.3 回測···········································124

 

7.4 小結 ·······································126

 

第8章 因子遇到決策樹與隨機森林

 

8.1 什麽是決策樹和隨機森林 ············127

 

8.1.1 線性模型不適用的數據樣本 ············127

 

8.1.2 決策樹的用法和原理 ·····················129

 

8.1.3 隨機森林的用法和原理 ··················130

 

8.2 哪些因子重要,決策樹能告訴你 ·····132

 

 8.2.1 多來點因子··································132

 

8.2.2 設定目標並訓練模型 ·····················135

 

8.2.3 哪些因子重要·······························137

 

8.3 用重要因子和隨機森林來制訂 

策略 ·······································138

 

 

 

深入淺出Python量化交易實戰

 

 

8.3.1 回測函數的初始化 ························138

 

8.3.2 盤前的準備工作····························139

 

8.3.3 策略中的機器學習部分 ··················141

 

8.3.4 定義買入股票和賣出股票的列表 ······142

 

8.3.5 定義買入操作和賣出操作 ···············144

 

8.3.6 對策略進行回測····························145

 

8.4 小結 ·······································146

 

第9章 因子遇到支持向量機

 

9.1 什麽是支持向量機 ·····················147

 

9.1.1 支持向量機的基本原理 ··················147

 

9.1.2 線性內核有時“很著急” ···············149

 

9.1.3 RBF內核“閃亮登場” ··················150

 

9.2 動態因子選擇策略 ·····················152

 

9.2.1 設置回測環境·······························152

 

9.2.2 開盤前準備··································153

 

9.2.3 機器學習的部分····························155

 

9.2.4 買入和賣出的操作 ························157

 

9.3 策略的回測詳情 ························158

 

9.3.1 策略收益概述·······························159

 

9.3.2 策略交易詳情·······························159

 

9.3.3 持倉和收益詳情····························161

 

9.4 使用策略進行模擬交易 ···············162

 

9.4.1 模擬交易·····································163

 

9.4.2 查看模擬交易詳情 ························164

 

9.4.3 模擬交易的持倉與下單 ··················165

 

9.5 小結 ·······································166

 

第10章 初識自然語言處理技術

 

10.1我們的想法是否靠譜 ··················167

 

10.1.1 思考幾個問題 ·····························167

 

10.1.2 參考一下“大佬”們的做法 ···········168

 

10.1.3 說了那麽多,什麽是NLP ··············169

 

10.2 獲取文本數據並簡單清洗 ··········170

 

10.2.1 獲取新聞聯播文本數據 ·················170

 

10.2.2 對文本數據進行簡單清洗 ··············172

 

10.3 中文分詞,“結巴”來幫忙 ·······173

 

10.3.1 使用“結巴”進行分詞 ·················174

 

10.3.2 使用“結巴”進行列表分詞 ···········174

 

10.3.3 建立停用詞表 ·····························175

 

10.3.4 去掉文本中的停用詞 ····················176

 

10.3.5 使用“結巴”提取關鍵詞 ··············178

 

10.4 小結 ·····································180

 

第11章 新聞文本向量化和話題建模

 

11.1 讓機器“讀懂”新聞 ················181

 

11.1.1 準備文本數據 ·····························181

 

 

 

目錄

 

 

11.1.2 使用CountVectorizer將文本轉化為 

向量 ··········································183

 

11.1.3 使用TfidfVectorizer將文本轉化為 

向量 ··········································185

 

11.2 讓機器告訴我們新聞說了啥 ·······186

 

11.2.1 什麽是話題建模 ··························186

 

11.2.2 什麽是LDA模型 ························187

 

11.3 話題建模實戰 ·························188

 

11.3.1 加載數據並進行分詞 ····················188

 

11.3.2 將分詞結果合並保存 ····················190

 

11.3.3 使用LDA進行話題建模 ···············191

 

11.3.4 對模型進行改進 ··························192

 

11.4 小結 ·····································194

 

第12章 股評數據情感分析

 

12.1 機器懂我們的情感嗎 ················195

 

12.1.1 瞭解分好類的語料 ·······················196

 

12.1.2 將文件上傳到量化交易平臺 ···········197

 

12.2 用語料製作數據集 ···················198

 

12.2.1 將正面情緒語料存儲為列表 ···········198

 

12.2.2 將負面情緒語料存儲為列表 ···········200

 

12.2.3 給數據“打上標簽” ····················201

 

12.2.4 合並正負面情緒語料 ····················202

 

12.3 隆重推出“樸素貝葉斯” ··········203

 

12.3.1 “樸素貝葉斯”又是什麽 ··············204

 

12.3.2 為貝葉斯模型準備數據 ·················205

 

12.3.3 開始訓練貝葉斯模型並評估其性能 ·····206

 

12.4 小結 ·····································208

 

第13章 咱也“潮”一把—深度學習來了

 

13.1 開始研究前的準備 ···················209

 

13.1.1 翻翻工具箱,看看有什麽 ··············210

 

13.1.2 為神經網絡準備數據 ····················211

 

13.2 使用Keras對文本進行預處理 ·····213

 

13.2.1 使用Tokenizer提取特徵 ················213

 

13.2.2 將文本轉化為序列 ·······················214

 

13.2.3 填充序列與轉化矩陣 ····················216

 

13.3 使用Keras構建簡單神經網絡 ·····217

 

13.3.1 先動手“擼”一個多層感知機 ········217

 

13.3.2 念叨一下多層感知機的原理 ···········218

 

13.3.3 再來說說激活函數 ·······················220

 

13.3.4 Dropout層又是乾嗎的 ··················221

 

13.3.5 訓練一下,看看效果如何 ··············222

 

13.4 小結 ·····································224

 

 

 

深入淺出Python量化交易實戰

 

 

第14章 再進一步—CNN和LSTM

 

14.1 先動手“擼”一個捲積神經 

網絡 ·····································225

 

14.1.1 準備好庫和數據集 ·······················225

 

14.1.2 處理數據與搭建模型 ····················227

 

14.2 捲積神經網絡模型詳解 ·············229

 

14.2.1 嵌入層是乾啥用的 ·······················230

 

14.2.2 捲積層是乾啥用的 ·······················231

 

14.2.3 最大池化層是乾啥用的 ·················233

 

14.2.4 訓練模型看看效果 ·······················234

 

14.3 長短期記憶網絡 ······················236

 

14.3.1 搭建一個簡單的長短期記憶網絡 ·····236

 

14.3.2 關於長短期記憶網絡 ····················237

 

14.3.3 訓練模型及評估 ··························238

 

14.3.4 保存模型並在回測中調用 ··············240

 

14.4 小結 ·····································241

 

第15章 寫在最後—小瓦的徵程

 

15.1 可以一夜暴富了嗎 ···················242

 

15.1.1 使用第三方量化平臺是個好主 

意嗎 ··········································243

 

15.1.2 機器學習到底有沒有用 ·················243

 

15.1.3 要“弔死”在A股“這棵樹” 

上嗎 ··········································244

 

15.2 將來要做什麽 ·························245

 

15.2.1 學習一些數據庫知識 ····················245

 

15.2.2 多看看不同的投資標的 ·················247

 

15.2.3 打開國際化的視野 ·······················249

 

15.3 小結 ·····································252