テクノロジー

読書感想 Pythonからはじめるアルゴリズムトレード

handa's icon'
  • handa
  • 2023/10/27 05:12

概要

Content image
表紙

書名 Pythonからはじめるアルゴリズムトレード

著者 イーブ・ヒルピシュ

訳者 村上振一郎

発行 オライリー・ジャパン

ページ数 347

ジャンル 技術

同じオライリーの「Pythonによるファイナンス」の著者が、コンピュータプログラムによるトレードについて踏み込んで書いた本。私は前書は読んでないが、本書は前書をさらに発展させたものと理解している。

 

機械学習

私はトレードbotを作っているが、これまで作った機械学習を取り入れたbotでうまく成果を出すことができなかった。単純な価格予測ではだめらしい。何かヒントはないかと思い、本書を手に取った。

本書はPythonを独りで学んだ者として、「こんな書き方もできるの」と驚かされるコードがよく見られて勉強になった。機械学習において、特徴量に何を選ぶかは肝だが、本書では「ラグ」という手法を紹介していた。ラグというのは、「将来の価格は、これまでの価格が最も影響する」という考えにもとづき、

 

次の価格(これを予測したい)

最新の価格

1期前の価格

2期前の価格

 

このように、過去のデータの履歴を横に展開して利用することである。

1期前の価格, 2期前の価格, ..., N期前の価格

 

一番私にとってためになったのは10章のトレードオペレーションの自動化の部分。機械学習を用いたトレード戦略についての記載があった。基本部分は本の通りで、特徴量の評価処理を追加したソースを以下に示す。

 

データ

まず使用したデータ。最新でなくてすみませんが、bitFlyerにおける2015年~2022年のBTC FXの日足データ。

※長くて恐縮です。

 

プログラム

このデータを読んで、どれくらい儲けられるかシミュレーションするプログラム。用意した特徴量のどれがどれくらい重要かを表示する部分を追加している。

 

実行結果1

表示されたグラフ。

returnはベンチマークとなる対数リターンで、資産価値の相対的な変化を表す。

strategyは取引コスト差し引き前の利益。

strategy_tcは取引コスト差し引き後の利益。

Content image
今回の機械学習によってどれだけ利益が得られたかのシミュレーション結果。

実行結果2

プログラムのログ出力。

**_lag_** となっている部分がラグ。

==== 比例取引コストの平均 ====
0.0006170877391277697
==== 列の一覧 ====
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2748 entries, 0 to 2747
Data columns (total 48 columns):
#   Column        Non-Null Count  Dtype
---  ------        --------------  -----
0   Timestamp     2748 non-null   datetime64[ns]
1   Open          2748 non-null   int64
2   High          2748 non-null   int64
3   Low           2748 non-null   int64
4   Close         2748 non-null   int64
5   Volume        2748 non-null   float64
6   return        2747 non-null   float64
7   vol           2728 non-null   float64
8   mom           2728 non-null   float64
9   sma           2729 non-null   float64
10  min           2729 non-null   float64
11  max           2729 non-null   float64
12  return_lag_1  2746 non-null   float64
13  return_lag_2  2745 non-null   float64
14  return_lag_3  2744 non-null   float64
15  return_lag_4  2743 non-null   float64
16  return_lag_5  2742 non-null   float64
17  return_lag_6  2741 non-null   float64
18  vol_lag_1     2727 non-null   float64
19  vol_lag_2     2726 non-null   float64
20  vol_lag_3     2725 non-null   float64
21  vol_lag_4     2724 non-null   float64
22  vol_lag_5     2723 non-null   float64
23  vol_lag_6     2722 non-null   float64
24  mom_lag_1     2727 non-null   float64
25  mom_lag_2     2726 non-null   float64
26  mom_lag_3     2725 non-null   float64
27  mom_lag_4     2724 non-null   float64
28  mom_lag_5     2723 non-null   float64
29  mom_lag_6     2722 non-null   float64
30  sma_lag_1     2728 non-null   float64
31  sma_lag_2     2727 non-null   float64
32  sma_lag_3     2726 non-null   float64
33  sma_lag_4     2725 non-null   float64
34  sma_lag_5     2724 non-null   float64
35  sma_lag_6     2723 non-null   float64
36  min_lag_1     2728 non-null   float64
37  min_lag_2     2727 non-null   float64
38  min_lag_3     2726 non-null   float64
39  min_lag_4     2725 non-null   float64
40  min_lag_5     2724 non-null   float64
41  min_lag_6     2723 non-null   float64
42  max_lag_1     2728 non-null   float64
43  max_lag_2     2727 non-null   float64
44  max_lag_3     2726 non-null   float64
45  max_lag_4     2725 non-null   float64
46  max_lag_5     2724 non-null   float64
47  max_lag_6     2723 non-null   float64
dtypes: datetime64[ns](1), float64(43), int64(4)
memory usage: 1.0 MB
None
==== 訓練用特徴量データセット ====
       Volume       vol       mom       sma       min  ...  max_lag_2  max_lag_3  max_lag_4  max_lag_5  max_lag_6
1926 -0.671648 -0.755209  0.846987  1.286698  1.520306  ...   1.042335   1.043423   1.044512   1.045603   1.046694
1927 -0.994194 -0.762574  0.846987  1.288272  1.520306  ...   1.042335   1.043423   1.044512   1.045603   1.046694
1928 -0.919207 -0.800337 -1.180036  1.288052  1.520306  ...   1.042335   1.043423   1.044512   1.045603   1.046694
1929 -0.803131 -0.795174  0.846987  1.288338  1.520306  ...   1.042335   1.043423   1.044512   1.045603   1.046694
1930 -0.771288 -0.788118 -1.180036  1.284928  1.520306  ...   1.042335   1.043423   1.044512   1.045603   1.046694

[5 rows x 42 columns]
==== 訓練データによる正解率 ====
0.6708661417322834
==== テスト用特徴量データセット ====
       Volume       vol       mom       sma       min  ...  max_lag_2  max_lag_3  max_lag_4  max_lag_5  max_lag_6
2743 -0.152830 -1.878558 -1.063848 -1.215784 -1.086178  ...  -1.287786  -1.284963  -1.282152  -1.279354  -1.276567
2744 -0.349370 -1.877162 -1.063848 -1.220109 -1.086178  ...  -1.287786  -1.284963  -1.282152  -1.279354  -1.276567
2745 -0.052071 -1.869754 -1.063848 -1.224840 -1.100550  ...  -1.287786  -1.284963  -1.282152  -1.279354  -1.276567
2746 -0.396140 -1.867855 -1.063848 -1.230304 -1.115037  ...  -1.287786  -1.284963  -1.282152  -1.279354  -1.276567
2747 -0.805779 -1.918505 -1.063848 -1.236765 -1.115037  ...  -1.287786  -1.284963  -1.282152  -1.279354  -1.276567

[5 rows x 42 columns]
==== テストデータによる正解率 ====
0.5263157894736842
==== 評価 ====
トレード回数= 318
return          1.934099
strategy       19.386365
strategy_tc    15.932102
dtype: float64
==== 特徴量ごとの重要度 ====
Volume 0.13154534914677252
vol 0.01866909617987705
mom 0.03551734859436794
sma 0.06666666666666667
min 0.0
max 0.0
return_lag_1 0.10202126399658863
return_lag_2 0.11386773016437449
return_lag_3 0.02505128688703455
return_lag_4 0.02780584883050535
return_lag_5 0.06940690496953793
return_lag_6 0.04408735234076049
vol_lag_1 0.019491344768601013
vol_lag_2 0.06164157444196529
vol_lag_3 0.05781774895104607
vol_lag_4 0.0
vol_lag_5 0.0
vol_lag_6 0.0480403504763769
mom_lag_1 0.031149318072298728
mom_lag_2 0.0
mom_lag_3 0.0
mom_lag_4 0.0
mom_lag_5 0.0
mom_lag_6 0.0
sma_lag_1 0.0
sma_lag_2 0.04114135659187682
sma_lag_3 0.0
sma_lag_4 0.034002108408267305
sma_lag_5 0.0
sma_lag_6 0.010559746545988383
min_lag_1 0.014965563528801459
min_lag_2 0.0
min_lag_3 0.0
min_lag_4 0.0
min_lag_5 0.023709978992553876
min_lag_6 0.0
max_lag_1 0.0
max_lag_2 0.0
max_lag_3 0.0
max_lag_4 0.0
max_lag_5 0.0
max_lag_6 0.022842061445738564
重要度の合計:  0.9999999999999999

 

特徴量の工夫により、本書に掲載されているプログラムそのままの時より結果を良くすることができた。importance(重要度)が0.0だからといってその特徴量を取り払うと、評価が低くなってしまうことが多い。どういうことなのか...。機械の考えることはわからない。

 

今後

今までは、価格を予測しようとしてうまく行かなかった。本書の上記プログラムは上がるか下がるかを予測しており、これを応用して儲けられるプログラムにしたい。

 

以上

Content image
サムネイル用

 

Supporter profile iconSupporter profile iconSupporter profile icon
Article tip 3人がサポートしています
獲得ALIS: Article like 115.57 ALIS Article tip 115.87 ALIS
handa's icon'
  • handa
  • @handa
コンピュータシステムの開発、子供向けプログラミング教室などを行っています。

投稿者の人気記事
コメントする
コメントする
こちらもおすすめ!
Eye catch
クリプト

ジョークコインとして出発したDogecoin(ドージコイン)の誕生から現在まで。注目される非証券性🐶

Like token Tip token
38.31 ALIS
Eye catch
クリプト

約2年間ブロックチェ-ンゲームをして

Like token Tip token
61.20 ALIS
Eye catch
テクノロジー

iOS15 配信開始!!

Like token Tip token
7.20 ALIS
Eye catch
クリプト

スーパーコンピュータ「京」でマイニングしたら

Like token Tip token
1.06k ALIS
Eye catch
クリプト

ブロックチェーンの51%攻撃ってなに

Like token Tip token
0.00 ALIS
Eye catch
ゲーム

ドラクエで学ぶオーバフロー

Like token Tip token
30.10 ALIS
Eye catch
クリプト

「ハッシュ」とは何なのか、必ず理解させます

Like token Tip token
0.10 ALIS
Eye catch
テクノロジー

なぜ、素人エンジニアの私が60日間でブロックチェーンゲームを制作できたのか、について語ってみた

Like token Tip token
270.93 ALIS
Eye catch
テクノロジー

オープンソースプロジェクトに参加して自己肯定感を高める

Like token Tip token
85.05 ALIS
Eye catch
クリプト

Bitcoin史 〜0.00076ドルから6万ドルへの歩み〜

Like token Tip token
947.13 ALIS
Eye catch
他カテゴリ

ALISのシステム概観

Like token Tip token
5.00 ALIS
Eye catch
テクノロジー

彼女でも分かるように解説:ディープフェイク

Like token Tip token
32.10 ALIS