AnyTrading - ビットコイン投資を強化学習で実行 日足編⑥

ビットコインの 1日足 のデータでの投資シミュレーション6回目です。

強化学習のパラメータ

ソースはこれまでの応用なので割愛し、強化学習のパラメータだけを下記に示します。

  • 学習アルゴリズム(前回と同様)
    PPO2

  • 参照する直前データ数(前回と同様)
    50

  • 学習データ
    [2017-07-13 ~ 2018-05-11] 1日足データ(前回と同様)

  • 検証データ
    [2019-09-23 ~ 2020-07-19] 1日足データ(500日分移動)

投資結果

実行結果は以下の通りです。

[コンソール出力]

1
2
3
4
5
6
7
8
9
10
info: {'total_reward': 2554510000.0, 'total_profit': 1.1834550378134772, 'position': 0}
info: {'total_reward': 3187060000.0, 'total_profit': 1.3450211770719407, 'position': 0}
info: {'total_reward': 2907200000.0, 'total_profit': 1.1646436426455773, 'position': 1}
info: {'total_reward': -97260000.0, 'total_profit': 0.900730605758224, 'position': 0}
info: {'total_reward': 593640000.0, 'total_profit': 1.176038814122207, 'position': 1}
info: {'total_reward': 4678830000.0, 'total_profit': 1.3116496658626056, 'position': 1}
info: {'total_reward': -9179640000.0, 'total_profit': 0.6005862765626327, 'position': 1}
info: {'total_reward': 2755750000.0, 'total_profit': 1.1967800058375302, 'position': 1}
info: {'total_reward': 5312840000.0, 'total_profit': 1.42076616228198, 'position': 1}
info: {'total_reward': -6536240000.0, 'total_profit': 0.6448759138878677, 'position': 1}

[出力画像]

実行結果( 1回目)
実行結果( 2回目)
実行結果( 3回目)
実行結果( 4回目)
実行結果( 5回目)
実行結果( 6回目)
実行結果( 7回目)
実行結果( 8回目)
実行結果( 9回目)
実行結果(10回目)


これまでの投資結果(トータル報酬)を表にまとめてみます。

No.トータル報酬(5回前)トータル報酬(4回前)トータル報酬(3回前)トータル報酬(2回前)トータル報酬(前回)トータル報酬(今回)
737,820,000円 1,225,670,000円-2,424,490,000円-129,460,000円 3,543,310,000円 2,554,510,000円
4,451,760,000円 2,672,370,000円 -3,870,060,000円8,007,910,000円 -1,553,380,000円 3,187,060,000円
4,724,240,000円 -996,930,000円 -5,277,570,000円-1,596,410,000円-11,847,060,000円 2,907,200,000円
-3,133,420,000円-5,494,530,000円 -4,064,980,000円-3,328,340,000円-5,097,430,000円 -97,260,000円
7,880,400,000円 7,793,260,000円 -2,953,150,000円-8,788,740,000円4,813,240,000円 593,640,000円
2,833,180,000円 -2,393,360,000円 7,233,760,000円 5,385,110,000円 145,830,000円 4,678,830,000円
2,268,160,000円 -815,410,000円 1,480,400,000円 -2,390,800,000円-6,559,180,000円 -9,179,640,000円
1,437,600,000円 839,630,000円 -5,552,340,000円-3,432,660,000円-6,925,330,000円 2,755,750,000円
-3,185,920,000円2,794,730,000円 4,312,270,000円 1,127,100,000円 3,223,430,000円 5,312,840,000円
-5,817,080,000円-5,354,750,000円 821,240,000円 8,186,140,000円 5,182,330,000円 -6,536,240,000円

10種類の学習済みモデルは、7勝3敗となかなかの成績となりました。

学習モデルごとの結果としては、No⑥、No⑨が5勝1敗となかなかの好成績となっています。

AnyTrading - ビットコイン投資を強化学習で実行 日足編⑤

ビットコインの 1日足 のデータでの投資シミュレーション5回目です。

強化学習のパラメータ

ソースはこれまでの応用なので割愛し、強化学習のパラメータだけを下記に示します。

  • 学習アルゴリズム(前回と同様)
    PPO2

  • 参照する直前データ数(前回と同様)
    50

  • 学習データ
    [2017-07-13 ~ 2018-05-11] 1日足データ(前回と同様)

  • 検証データ
    [2019-06-15 ~ 2020-04-10] 1日足データ(400日分移動)

投資結果

実行結果は以下の通りです。

[コンソール出力]

1
2
3
4
5
6
7
8
9
10
info: {'total_reward': 3543310000.0, 'total_profit': 1.5464109977834202, 'position': 0}
info: {'total_reward': -1553380000.0, 'total_profit': 1.1390683471614786, 'position': 1}
info: {'total_reward': -11847060000.0, 'total_profit': 0.5500536430705991, 'position': 1}
info: {'total_reward': -5097430000.0, 'total_profit': 1.1233650881463042, 'position': 1}
info: {'total_reward': 4813240000.0, 'total_profit': 1.5235928722550143, 'position': 0}
info: {'total_reward': 145830000.0, 'total_profit': 0.9612141186720539, 'position': 0}
info: {'total_reward': -6559180000.0, 'total_profit': 0.7725324354543073, 'position': 0}
info: {'total_reward': -6925330000.0, 'total_profit': 0.677925186974139, 'position': 0}
info: {'total_reward': 3223430000.0, 'total_profit': 1.2687930517984403, 'position': 1}
info: {'total_reward': 5182330000.0, 'total_profit': 1.1655638373126338, 'position': 0}

[出力画像]

実行結果( 1回目)
実行結果( 2回目)
実行結果( 3回目)
実行結果( 4回目)
実行結果( 5回目)
実行結果( 6回目)
実行結果( 7回目)
実行結果( 8回目)
実行結果( 9回目)
実行結果(10回目)


これまでの投資結果(トータル報酬)を表にまとめてみます。

No.トータル報酬(4回前)トータル報酬(3回前)トータル報酬(2回前)トータル報酬(前回)トータル報酬(今回)
737,820,000円 1,225,670,000円-2,424,490,000円-129,460,000円 3,543,310,000円
4,451,760,000円 2,672,370,000円 -3,870,060,000円8,007,910,000円 -1,553,380,000円
4,724,240,000円 -996,930,000円 -5,277,570,000円-1,596,410,000円-11,847,060,000円
-3,133,420,000円-5,494,530,000円 -4,064,980,000円-3,328,340,000円-5,097,430,000円
7,880,400,000円 7,793,260,000円 -2,953,150,000円-8,788,740,000円4,813,240,000円
2,833,180,000円 -2,393,360,000円 7,233,760,000円 5,385,110,000円 145,830,000円
2,268,160,000円 -815,410,000円 1,480,400,000円 -2,390,800,000円-6,559,180,000円
1,437,600,000円 839,630,000円 -5,552,340,000円-3,432,660,000円-6,925,330,000円
-3,185,920,000円2,794,730,000円 4,312,270,000円 1,127,100,000円 3,223,430,000円
-5,817,080,000円-5,354,750,000円 821,240,000円 8,186,140,000円 5,182,330,000円

10種類の学習済みモデルは、5勝5敗とイーブンでした。

学習モデルごとの結果としては、No⑥、No⑨が4勝1敗となかなかの好成績となっています。

AnyTrading - ビットコイン投資を強化学習で実行 日足編④

ビットコインの 1日足 のデータでの投資シミュレーション4回目です。

強化学習のパラメータ

ソースはこれまでの応用なので割愛し、強化学習のパラメータだけを下記に示します。

  • 学習アルゴリズム(前回と同様)
    PPO2

  • 参照する直前データ数(前回と同様)
    50

  • 学習データ
    [2017-07-13 ~ 2018-05-11] 1日足データ(前回と同様)

  • 検証データ
    [2019-03-07 ~ 2020-01-01] 1日足データ(300日分移動)

投資結果

実行結果は以下の通りです。

[コンソール出力]

1
2
3
4
5
6
7
8
9
10
info: {'total_reward': -129460000.0, 'total_profit': 0.7332781444376287, 'position': 0}
info: {'total_reward': 8007910000.0, 'total_profit': 1.1611321501318785, 'position': 0}
info: {'total_reward': -1596410000.0, 'total_profit': 0.6106860051226524, 'position': 1}
info: {'total_reward': -3328340000.0, 'total_profit': 0.6760597155530264, 'position': 1}
info: {'total_reward': -8788740000.0, 'total_profit': 0.45438850615327553, 'position': 0}
info: {'total_reward': 5385110000.0, 'total_profit': 0.8795481277282458, 'position': 0}
info: {'total_reward': -2390800000.0, 'total_profit': 0.7593373128144464, 'position': 1}
info: {'total_reward': -3432660000.0, 'total_profit': 0.6420892635304241, 'position': 0}
info: {'total_reward': 1127100000.0, 'total_profit': 0.8810127355706889, 'position': 0}
info: {'total_reward': 8186140000.0, 'total_profit': 1.060863551413895, 'position': 0}

[出力画像]

実行結果( 1回目)
実行結果( 2回目)
実行結果( 3回目)
実行結果( 4回目)
実行結果( 5回目)
実行結果( 6回目)
実行結果( 7回目)
実行結果( 8回目)
実行結果( 9回目)
実行結果(10回目)


これまでの投資結果(トータル報酬)を表にまとめてみます。

No.トータル報酬(3回前)トータル報酬(2回前)トータル報酬(前回)トータル報酬(今回)
737,820,000円 1,225,670,000円-2,424,490,000円-129,460,000円
4,451,760,000円 2,672,370,000円 -3,870,060,000円8,007,910,000円
4,724,240,000円 -996,930,000円 -5,277,570,000円-1,596,410,000円
-3,133,420,000円-5,494,530,000円 -4,064,980,000円-3,328,340,000円
7,880,400,000円 7,793,260,000円 -2,953,150,000円-8,788,740,000円
2,833,180,000円 -2,393,360,000円 7,233,760,000円 5,385,110,000円
2,268,160,000円 -815,410,000円 1,480,400,000円 -2,390,800,000円
1,437,600,000円 839,630,000円 -5,552,340,000円-3,432,660,000円
-3,185,920,000円2,794,730,000円 4,312,270,000円 1,127,100,000円
-5,817,080,000円-5,354,750,000円 821,240,000円 8,186,140,000円

10種類の学習済みモデルは、4勝6敗と負け越しとなりました。

学習モデルごとの結果としては、No②、No⑥、No⑨が3勝1敗と現状まあまあの投資成績となっています。

AnyTrading - ビットコイン投資を強化学習で実行 日足編③

ビットコインの 1日足 のデータでの投資シミュレーション3回目です。

強化学習のパラメータ

ソースはこれまでの応用なので割愛し、強化学習のパラメータだけを下記に示します。

  • 学習アルゴリズム(前回と同様)
    PPO2

  • 参照する直前データ数(前回と同様)
    50

  • 学習データ
    [2017-07-13 ~ 2018-05-11] 1日足データ(前回と同様)

  • 検証データ
    [2018-11-27 ~ 2019-09-23] 1日足データ(200日分移動)

投資結果

実行結果は以下の通りです。

[コンソール出力]

1
2
3
4
5
6
7
8
9
10
info: {'total_reward': -2424490000.0, 'total_profit': 0.6822839517343141, 'position': 0}
info: {'total_reward': -3870060000.0, 'total_profit': 0.5147784451587587, 'position': 1}
info: {'total_reward': -5277570000.0, 'total_profit': 0.4327079770633398, 'position': 1}
info: {'total_reward': -4064980000.0, 'total_profit': 0.5758555909084516, 'position': 1}
info: {'total_reward': -2953150000.0, 'total_profit': 0.5033038692516506, 'position': 0}
info: {'total_reward': 7233760000.0, 'total_profit': 0.8673161239047598, 'position': 0}
info: {'total_reward': 1480400000.0, 'total_profit': 0.6465792170864173, 'position': 1}
info: {'total_reward': -5552340000.0, 'total_profit': 0.37681486627647476, 'position': 1}
info: {'total_reward': 4312270000.0, 'total_profit': 0.9261066290391465, 'position': 0}
info: {'total_reward': 821240000.0, 'total_profit': 0.7060913589891954, 'position': 1}

[出力画像]

実行結果( 1回目)
実行結果( 2回目)
実行結果( 3回目)
実行結果( 4回目)
実行結果( 5回目)
実行結果( 6回目)
実行結果( 7回目)
実行結果( 8回目)
実行結果( 9回目)
実行結果(10回目)


これまでの投資結果(トータル報酬)を表にまとめてみます。

No.トータル報酬(2回前)トータル報酬(前回)トータル報酬(今回)
737,820,000円 1,225,670,000円-2,424,490,000円
4,451,760,000円 2,672,370,000円 -3,870,060,000円
4,724,240,000円 -996,930,000円 -5,277,570,000円
-3,133,420,000円-5,494,530,000円 -4,064,980,000円
7,880,400,000円 7,793,260,000円 -2,953,150,000円
2,833,180,000円 -2,393,360,000円 7,233,760,000円
2,268,160,000円 -815,410,000円 1,480,400,000円
1,437,600,000円 839,630,000円 -5,552,340,000円
-3,185,920,000円2,794,730,000円 4,312,270,000円
-5,817,080,000円-5,354,750,000円 821,240,000円

10種類の学習済みモデルは、4勝6敗と負け越してしまいました。

3期間の検証結果として全てプラス報酬となる学習済みモデルもなく、現状なかなかにキビシイ結果となっています。。。

AnyTrading - ビットコイン投資を強化学習で実行 日足編②

ビットコインの 1日足 のデータでの投資シミュレーション2回目です。

強化学習のパラメータ

ソースはこれまでの応用なので割愛し、強化学習のパラメータだけを下記に示します。

  • 学習アルゴリズム(前回と同様)
    PPO2

  • 参照する直前データ数(前回と同様)
    50

  • 学習データ
    [2017-07-13 ~ 2018-05-11] 1日足データ(前回と同様)

  • 検証データ
    [2018-08-19 ~ 2019-06-15] 1日足データ(100日分移動)

投資結果

実行結果は以下の通りです。

[コンソール出力]

1
2
3
4
5
6
7
8
9
10
info: {'total_reward': 1225670000.0, 'total_profit': 1.0301971417446745, 'position': 0}
info: {'total_reward': 2672370000.0, 'total_profit': 0.9421943631409182, 'position': 0}
info: {'total_reward': -996930000.0, 'total_profit': 0.6864210748446301, 'position': 1}
info: {'total_reward': -5494530000.0, 'total_profit': 0.5774381110281871, 'position': 1}
info: {'total_reward': 7793260000.0, 'total_profit': 1.902538947650555, 'position': 0}
info: {'total_reward': -2393360000.0, 'total_profit': 0.828989892185724, 'position': 1}
info: {'total_reward': -815410000.0, 'total_profit': 0.7578885784881692, 'position': 0}
info: {'total_reward': 839630000.0, 'total_profit': 0.9834818344678051, 'position': 1}
info: {'total_reward': 2794730000.0, 'total_profit': 1.1658569300925843, 'position': 1}
info: {'total_reward': -5354750000.0, 'total_profit': 0.5246427962755218, 'position': 0}

[出力画像]

実行結果(1回目)
実行結果(2回目)
実行結果(3回目)
実行結果(4回目)
実行結果(5回目)
実行結果(6回目)
実行結果(7回目)
実行結果(8回目)
実行結果(9回目)
実行結果(10回目)


投資結果(トータル報酬)を表にまとめてみます。

No.トータル報酬(前回)トータル報酬(今回)
737,820,000円 1,225,670,000円
4,451,760,000円 2,672,370,000円
4,724,240,000円 -996,930,000円
-3,133,420,000円-5,494,530,000円
7,880,400,000円 7,793,260,000円
2,833,180,000円 -2,393,360,000円
2,268,160,000円 -815,410,000円
1,437,600,000円 839,630,000円
-3,185,920,000円2,794,730,000円
-5,817,080,000円-5,354,750,000円

10種類の学習済みモデルの結果は5勝5敗とイマイチでした。

No.①、No.②、No.⑤、No.⑧の学習済みモデルは2連勝となっています。

AnyTrading - ビットコイン投資を強化学習で実行 日足編①

これまでビットコインの 1時間足 のデータで投資シミュレーションを行ってきましたが、今回からは 1日足 での検証を行ってみます。

強化学習のパラメータ

ソースはこれまでの応用なので割愛し、強化学習のパラメータだけを下記に示します。

  • 学習アルゴリズム
    PPO2
  • 参照する直前データ数
    50
  • 学習データ
    [2017-07-13 ~ 2018-05-11] 1日足データ
  • 検証データ
    [2018-05-12 ~ 2019-03-07] 1日足データ

投資結果

実行結果は以下の通りです。

[コンソール出力]

1
2
3
4
5
6
7
8
9
10
info: {'total_reward': 737820000.0, 'total_profit': 1.5204328969406768, 'position': 0}
info: {'total_reward': 4451760000.0, 'total_profit': 1.9543889662243907, 'position': 0}
info: {'total_reward': 4724240000.0, 'total_profit': 1.8577326597225763, 'position': 1}
info: {'total_reward': -3133420000.0, 'total_profit': 1.0932763909885006, 'position': 1}
info: {'total_reward': 7880400000.0, 'total_profit': 2.791961784381706, 'position': 0}
info: {'total_reward': 2833180000.0, 'total_profit': 1.9502291844926507, 'position': 0}
info: {'total_reward': 2268160000.0, 'total_profit': 1.8259053513012946, 'position': 1}
info: {'total_reward': 1437600000.0, 'total_profit': 1.8368776844043904, 'position': 1}
info: {'total_reward': -3185920000.0, 'total_profit': 1.12174062698247, 'position': 1}
info: {'total_reward': -5817080000.0, 'total_profit': 1.0365691835994815, 'position': 0}

[出力画像]

実行結果(1回目)
実行結果(2回目)
実行結果(3回目)
実行結果(4回目)
実行結果(5回目)
実行結果(6回目)
実行結果(7回目)
実行結果(8回目)
実行結果(9回目)
実行結果(10回目)


投資結果(トータル報酬)を表にまとめてみます。

No.トータル報酬(前回)
737,820,000円
4,451,760,000円
4,724,240,000円
-3,133,420,000円
7,880,400,000円
2,833,180,000円
2,268,160,000円
1,437,600,000円
-3,185,920,000円
-5,817,080,000円

10種類の学習済みモデルの結果は7勝3敗です。

これからは、今回作成した学習済みモデルを使い期間を変えて投資結果を検証していきます。

AnyTrading - ビットコイン投資を強化学習で実行⑨ 10回投資シミュレーション

ビットコインの学習済みモデルを使い、期間を変えて検証してみます。

学習済みモデルを使っての投資シミュレーション

今回は学習を行わないので、学習に関するパラメータは不要となります。

検証データは600データ分後ろに移動したものにします。

以前の処理で、学習済みモデル trading_model0.zip ~ trading_model9.zip が作成されているのでそれを読み込みます。

  • 検証データ
    [2020-07-24 18:00 ~ 2020-08-06 06:00] 1時間足データ

学習済みモデルを読み込んで、投資シミュレーションを行うコードは次のようになります。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os, gym
import datetime
import gym_anytrading
import matplotlib.pyplot as plt
from gym_anytrading.envs import TradingEnv, ForexEnv, StocksEnv, Actions, Positions
from gym_anytrading.datasets import FOREX_EURUSD_1H_ASK, STOCKS_GOOGL
from stable_baselines.common.vec_env import DummyVecEnv
from stable_baselines import PPO2
from stable_baselines import ACKTR
from stable_baselines.bench import Monitor
from stable_baselines.common import set_global_seeds

def simulation(i):
# ログフォルダの生成
log_dir = './logs/'
os.makedirs(log_dir, exist_ok=True)

idx1 = 50
idx2 = 350
# データ数
span = idx2 - idx1

# 環境の生成
env = gym.make('forex-v0', frame_bound=(idx1, idx2), window_size=20)
env = Monitor(env, log_dir, allow_early_resets=True)

# シードの指定
env.seed(0)
set_global_seeds(0)

# ベクトル化環境の生成
env = DummyVecEnv([lambda: env])

# モデルの生成
model = PPO2('MlpPolicy', env, verbose=1)
#model = ACKTR('MlpPolicy', env, verbose=1)

# モデルの読み込み
model = PPO2.load('trading_model{}'.format(i))

# モデルの学習
#model.learn(total_timesteps=128000)

# モデルの保存
#model.save('trading_model{}'.format(i))

# モデルのテスト
env = gym.make('forex-v0', frame_bound=(idx2 + 600, idx2 + span + 600), window_size=20)
env.seed(0)
state = env.reset()
while True:
# 行動の取得
action, _ = model.predict(state)
# 1ステップ実行
state, reward, done, info = env.step(action)
# エピソード完了
if done:
print('info:', info)
break
# グラフのプロット
plt.cla()
env.render_all()
plt.savefig('trading{:%Y%m%d_%H%M%S}.png'.format(datetime.datetime.now()))
#plt.show()

with open('C:/Util/Anaconda3/Lib/site-packages/gym_anytrading/datasets/data/FOREX_EURUSD_1H_ASK.csv', 'r') as f:
lines = f.readlines()
s1 = lines[idx1].split(',')[0]
s2 = lines[idx2-1].split(',')[0]
print(s1,s2, (idx2 - idx1))

s3 = lines[idx2].split(',')[0]
s4 = lines[idx2+span].split(',')[0]
print(s3,s4, (idx2+span - idx2))

for i in range(10):
simulation(i)

48行目 で検証データの位置を600データ分後ろに移動しています。

10回投資シミュレーションを実行

上記コードを実行すると次のような結果になりました。

[コンソール出力]

1
2
3
4
5
6
7
8
9
10
info: {'total_reward': 421760000.0, 'total_profit': 1.0042685135763472, 'position': 0}
info: {'total_reward': 905030000.0, 'total_profit': 1.0256242377301583, 'position': 0}
info: {'total_reward': 189400000.0, 'total_profit': 0.9938455506325421, 'position': 0}
info: {'total_reward': -317860000.0, 'total_profit': 0.9730433876393387, 'position': 1}
info: {'total_reward': 63950000.0, 'total_profit': 0.9891720025304946, 'position': 0}
info: {'total_reward': 554080000.0, 'total_profit': 1.0072603146242594, 'position': 0}
info: {'total_reward': -350570000.0, 'total_profit': 0.9735053325752616, 'position': 0}
info: {'total_reward': 712880000.0, 'total_profit': 1.0150636299213376, 'position': 1}
info: {'total_reward': 647480000.0, 'total_profit': 1.01115320849058, 'position': 1}
info: {'total_reward': -291820000.0, 'total_profit': 0.975244543874682, 'position': 1}

実行結果(1回目)
実行結果(2回目)
実行結果(3回目)
実行結果(4回目)
実行結果(5回目)
実行結果(6回目)
実行結果(7回目)
実行結果(8回目)
実行結果(9回目)
実行結果(10回目)


これまでの投資結果(トータル報酬)を表にまとめてみます。

No.トータル報酬(今回)
421,760,000円
905,030,000円
189,400,000円
-317,860,000円
63,950,000円
554,080,000円
-350,570,000円
712,880,000円
647,480,000円
-291,820,000円
No.トータル報酬(6回前)トータル報酬(5回前)トータル報酬(4回前)トータル報酬(3回前)トータル報酬(2回前)トータル報酬(前回)
13,300,000円-800,660,000円1,116,260,000円-973,390,000円1,150,100,000円887,510,000円
-127,340,000円564,920,000円239,580,000円1,158,820,000円-1,389,460,000円1,375,120,000円
429,940,000円-1,556,660,000円-806,620,000円1,102,640,000円210,810,000円-816,060,000円
-472,090,000円1,235,520,000円738,540,000円180,224,0000円2,223,310,000円-1,133,470,000円
40,030,000円486,750,000円2,096,110,000円1,061,140,000円1,336,600,000円-354,990,000円
-260,510,000円1,682,380,000円1,493,810,000円919,960,000円-1,016,860,000円-1,834,340,000円
88,920,000円-513,180,000円-936,250,000円915,500,000円173,250,000円-447,420,000円
25,500,000円588,740,000円1,198,470,000円331,960,000円1,847,910,000円-401,270,000円
405,860,000円-58,280,000円-2,274,570,000円1,668,460,000円-1,364,720,000円1,434,680,000円
88,940,000円-602,670,000円-382,560,000円511,840,000円1,651,120,000円-1,423,540,000円

10種類の学習済みモデルの結果は全体で7勝3敗とまあまあでした。
前回初めてマイナスに転じたNo⑤とNo⑧も今回はトータル報酬プラスとなり、これまでの全7回の検証で6勝1敗とかなり優秀な投資成績をおさめています。

AnyTrading - ビットコイン投資を強化学習で実行⑧ 10回投資シミュレーション

学習済みモデルを使い、期間を変えて検証してみます。

学習済みモデルを使っての投資シミュレーション

今回は学習を行わないので、学習に関するパラメータは不要となります。

検証データは500データ分後ろに移動したものにします。

以前の処理で、学習済みモデル trading_model0.zip ~ trading_model9.zip が作成されているのでそれを読み込みます。

  • 検証データ
    [2020-07-20 12:00 ~ 2020-08-02 02:00] 1時間足データ

学習済みモデルを読み込んで、投資シミュレーションを行うコードは次のようになります。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os, gym
import datetime
import gym_anytrading
import matplotlib.pyplot as plt
from gym_anytrading.envs import TradingEnv, ForexEnv, StocksEnv, Actions, Positions
from gym_anytrading.datasets import FOREX_EURUSD_1H_ASK, STOCKS_GOOGL
from stable_baselines.common.vec_env import DummyVecEnv
from stable_baselines import PPO2
from stable_baselines import ACKTR
from stable_baselines.bench import Monitor
from stable_baselines.common import set_global_seeds

def simulation(i):
# ログフォルダの生成
log_dir = './logs/'
os.makedirs(log_dir, exist_ok=True)

idx1 = 50
idx2 = 350
# データ数
span = idx2 - idx1

# 環境の生成
env = gym.make('forex-v0', frame_bound=(idx1, idx2), window_size=20)
env = Monitor(env, log_dir, allow_early_resets=True)

# シードの指定
env.seed(0)
set_global_seeds(0)

# ベクトル化環境の生成
env = DummyVecEnv([lambda: env])

# モデルの生成
model = PPO2('MlpPolicy', env, verbose=1)
#model = ACKTR('MlpPolicy', env, verbose=1)

# モデルの読み込み
model = PPO2.load('trading_model{}'.format(i))

# モデルの学習
#model.learn(total_timesteps=128000)

# モデルの保存
#model.save('trading_model{}'.format(i))

# モデルのテスト
env = gym.make('forex-v0', frame_bound=(idx2 + 500, idx2 + span + 500), window_size=20)
env.seed(0)
state = env.reset()
while True:
# 行動の取得
action, _ = model.predict(state)
# 1ステップ実行
state, reward, done, info = env.step(action)
# エピソード完了
if done:
print('info:', info)
break
# グラフのプロット
plt.cla()
env.render_all()
plt.savefig('trading{:%Y%m%d_%H%M%S}.png'.format(datetime.datetime.now()))
#plt.show()

with open('C:/Util/Anaconda3/Lib/site-packages/gym_anytrading/datasets/data/FOREX_EURUSD_1H_ASK.csv', 'r') as f:
lines = f.readlines()
s1 = lines[idx1].split(',')[0]
s2 = lines[idx2-1].split(',')[0]
print(s1,s2, (idx2 - idx1))

s3 = lines[idx2].split(',')[0]
s4 = lines[idx2+span].split(',')[0]
print(s3,s4, (idx2+span - idx2))

for i in range(10):
simulation(i)

48行目 で検証データの位置を500データ分後ろに移動しています。

10回投資シミュレーションを実行

上記コードを実行すると次のような結果になりました。

[コンソール出力]

1
2
3
4
5
6
7
8
9
10
info: {'total_reward': 887510000.0, 'total_profit': 1.0515069207233156, 'position': 1}
info: {'total_reward': 1375120000.0, 'total_profit': 1.0715165066463221, 'position': 0}
info: {'total_reward': -816060000.0, 'total_profit': 0.9809841824700954, 'position': 1}
info: {'total_reward': -1133470000.0, 'total_profit': 0.969451859987788, 'position': 0}
info: {'total_reward': -354990000.0, 'total_profit': 1.0015367861956161, 'position': 0}
info: {'total_reward': -1834340000.0, 'total_profit': 0.9423721198652031, 'position': 0}
info: {'total_reward': -447420000.0, 'total_profit': 0.9982399586170433, 'position': 1}
info: {'total_reward': -401270000.0, 'total_profit': 1.0002387833236033, 'position': 0}
info: {'total_reward': 1434680000.0, 'total_profit': 1.0787498460037899, 'position': 1}
info: {'total_reward': -1423540000.0, 'total_profit': 0.9582216476939388, 'position': 0}

実行結果(1回目)
実行結果(2回目)
実行結果(3回目)
実行結果(4回目)
実行結果(5回目)
実行結果(6回目)
実行結果(7回目)
実行結果(8回目)
実行結果(9回目)
実行結果(10回目)


これまでの投資結果(トータル報酬)を表にまとめてみます。

No.トータル報酬(5回前)トータル報酬(4回前)トータル報酬(3回前)トータル報酬(2回前)トータル報酬(前回)トータル報酬(今回)
13,300,000円-800,660,000円1,116,260,000円-973,390,000円1,150,100,000円887,510,000円
-127,340,000円564,920,000円239,580,000円1,158,820,000円-1,389,460,000円1,375,120,000円
429,940,000円-1,556,660,000円-806,620,000円1,102,640,000円210,810,000円-816,060,000円
-472,090,000円1,235,520,000円738,540,000円180,224,0000円2,223,310,000円-1,133,470,000円
40,030,000円486,750,000円2,096,110,000円1,061,140,000円1,336,600,000円-354,990,000円
-260,510,000円1,682,380,000円1,493,810,000円919,960,000円-1,016,860,000円-1,834,340,000円
88,920,000円-513,180,000円-936,250,000円915,500,000円173,250,000円-447,420,000円
25,500,000円588,740,000円1,198,470,000円331,960,000円1,847,910,000円-401,270,000円
405,860,000円-58,280,000円-2,274,570,000円1,668,460,000円-1,364,720,000円1,434,680,000円
88,940,000円-602,670,000円-382,560,000円511,840,000円1,651,120,000円-1,423,540,000円

今回は全体として3勝7敗と成績となりました。

これまで全勝だったNo⑤とNo⑧もマイナスとなってしまいました。

今回はデータ位置20あたりの急激な下げが厳しかったのでしょうか。。。

AnyTrading - ビットコイン投資を強化学習で実行⑦ 10回投資シミュレーション

学習済みモデルを使い、期間を変えて検証してみます。

学習済みモデルを使っての投資シミュレーション

今回は学習を行わないので、学習に関するパラメータは不要となります。

検証データは400データ分後ろに移動したものにします。

以前の処理で、学習済みモデル trading_model0.zip ~ trading_model9.zip が作成されているのでそれを読み込みます。

  • 検証データ
    [2020-07-16 08:00 ~ 2020-07-28 22:00] 1時間足データ

学習済みモデルを読み込んで、投資シミュレーションを行うコードは次のようになります。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os, gym
import datetime
import gym_anytrading
import matplotlib.pyplot as plt
from gym_anytrading.envs import TradingEnv, ForexEnv, StocksEnv, Actions, Positions
from gym_anytrading.datasets import FOREX_EURUSD_1H_ASK, STOCKS_GOOGL
from stable_baselines.common.vec_env import DummyVecEnv
from stable_baselines import PPO2
from stable_baselines import ACKTR
from stable_baselines.bench import Monitor
from stable_baselines.common import set_global_seeds

def simulation(i):
# ログフォルダの生成
log_dir = './logs/'
os.makedirs(log_dir, exist_ok=True)

idx1 = 50
idx2 = 350
# データ数
span = idx2 - idx1

# 環境の生成
env = gym.make('forex-v0', frame_bound=(idx1, idx2), window_size=20)
env = Monitor(env, log_dir, allow_early_resets=True)

# シードの指定
env.seed(0)
set_global_seeds(0)

# ベクトル化環境の生成
env = DummyVecEnv([lambda: env])

# モデルの生成
model = PPO2('MlpPolicy', env, verbose=1)
#model = ACKTR('MlpPolicy', env, verbose=1)

# モデルの読み込み
model = PPO2.load('trading_model{}'.format(i))

# モデルの学習
#model.learn(total_timesteps=128000)

# モデルの保存
#model.save('trading_model{}'.format(i))

# モデルのテスト
env = gym.make('forex-v0', frame_bound=(idx2 + 400, idx2 + span + 400), window_size=20)
env.seed(0)
state = env.reset()
while True:
# 行動の取得
action, _ = model.predict(state)
# 1ステップ実行
state, reward, done, info = env.step(action)
# エピソード完了
if done:
print('info:', info)
break
# グラフのプロット
plt.cla()
env.render_all()
plt.savefig('trading{:%Y%m%d_%H%M%S}.png'.format(datetime.datetime.now()))
#plt.show()

with open('C:/Util/Anaconda3/Lib/site-packages/gym_anytrading/datasets/data/FOREX_EURUSD_1H_ASK.csv', 'r') as f:
lines = f.readlines()
s1 = lines[idx1].split(',')[0]
s2 = lines[idx2-1].split(',')[0]
print(s1,s2, (idx2 - idx1))

s3 = lines[idx2].split(',')[0]
s4 = lines[idx2+span].split(',')[0]
print(s3,s4, (idx2+span - idx2))

for i in range(10):
simulation(i)

48行目 で検証データの位置を400データ分後ろに移動しています。

10回投資シミュレーションを実行

上記コードを実行すると次のような結果になりました。

[コンソール出力]

1
2
3
4
5
6
7
8
9
10
info: {'total_reward': 1150100000.0, 'total_profit': 1.0178212241892242, 'position': 1}
info: {'total_reward': -1389460000.0, 'total_profit': 0.9164807726700638, 'position': 0}
info: {'total_reward': 210810000.0, 'total_profit': 0.9784756700954919, 'position': 1}
info: {'total_reward': 2223310000.0, 'total_profit': 1.062583600131417, 'position': 0}
info: {'total_reward': 1336600000.0, 'total_profit': 1.0230497130609701, 'position': 0}
info: {'total_reward': -1016860000.0, 'total_profit': 0.9293458811263876, 'position': 0}
info: {'total_reward': 173250000.0, 'total_profit': 0.9765900444415986, 'position': 0}
info: {'total_reward': 1847910000.0, 'total_profit': 1.0463837241671654, 'position': 0}
info: {'total_reward': -1364720000.0, 'total_profit': 0.9163062921618682, 'position': 1}
info: {'total_reward': 1651120000.0, 'total_profit': 1.0366399288852488, 'position': 1}

実行結果(1回目)
実行結果(2回目)
実行結果(3回目)
実行結果(4回目)
実行結果(5回目)
実行結果(6回目)
実行結果(7回目)
実行結果(8回目)
実行結果(9回目)
実行結果(10回目)


これまでの投資結果(トータル報酬)を表にまとめてみます。

No.トータル報酬(4回前)トータル報酬(3回前)トータル報酬(2回前)トータル報酬(前回)トータル報酬(今回)
13,300,000円-800,660,000円1,116,260,000円-973,390,000円1,150,100,000円
-127,340,000円564,920,000円239,580,000円1,158,820,000円-1,389,460,000円
429,940,000円-1,556,660,000円-806,620,000円1,102,640,000円210,810,000円
-472,090,000円1,235,520,000円738,540,000円180,224,0000円2,223,310,000円
40,030,000円486,750,000円2,096,110,000円1,061,140,000円1,336,600,000円
-260,510,000円1,682,380,000円1,493,810,000円919,960,000円-1,016,860,000円
88,920,000円-513,180,000円-936,250,000円915,500,000円173,250,000円
25,500,000円588,740,000円1,198,470,000円331,960,000円1,847,910,000円
405,860,000円-58,280,000円-2,274,570,000円1,668,460,000円-1,364,720,000円
88,940,000円-602,670,000円-382,560,000円511,840,000円1,651,120,000円

今回は全体として7勝3敗と成績となりました。

No⑤とNo⑧はずっとプラス報酬となっています。ここまで安定した成績ですと、実運用したい衝動にかられます。(-_☆)キラーン

AnyTrading - ビットコイン投資を強化学習で実行⑥ 10回投資シミュレーション

学習済みモデルを使い、期間を変えて検証してみます。

学習済みモデルを使っての投資シミュレーション

今回は学習を行わないので、学習に関するパラメータは不要となります。

検証データは300データ分後ろに移動したものにします。

前前前回の処理で、学習済みモデル trading_model0.zip ~ trading_model9.zip が作成されているのでそれを読み込みます。

  • 検証データ
    [2020-07-24 18:00 ~ 2020-08-06 06:00] 1時間足データ

学習済みモデルを読み込んで、投資シミュレーションを行うコードは次のようになります。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os, gym
import datetime
import gym_anytrading
import matplotlib.pyplot as plt
from gym_anytrading.envs import TradingEnv, ForexEnv, StocksEnv, Actions, Positions
from gym_anytrading.datasets import FOREX_EURUSD_1H_ASK, STOCKS_GOOGL
from stable_baselines.common.vec_env import DummyVecEnv
from stable_baselines import PPO2
from stable_baselines import ACKTR
from stable_baselines.bench import Monitor
from stable_baselines.common import set_global_seeds

def simulation(i):
# ログフォルダの生成
log_dir = './logs/'
os.makedirs(log_dir, exist_ok=True)

idx1 = 50
idx2 = 350
# データ数
span = idx2 - idx1

# 環境の生成
env = gym.make('forex-v0', frame_bound=(idx1, idx2), window_size=20)
env = Monitor(env, log_dir, allow_early_resets=True)

# シードの指定
env.seed(0)
set_global_seeds(0)

# ベクトル化環境の生成
env = DummyVecEnv([lambda: env])

# モデルの生成
model = PPO2('MlpPolicy', env, verbose=1)
#model = ACKTR('MlpPolicy', env, verbose=1)

# モデルの読み込み
model = PPO2.load('trading_model{}'.format(i))

# モデルの学習
#model.learn(total_timesteps=128000)

# モデルの保存
#model.save('trading_model{}'.format(i))

# モデルのテスト
env = gym.make('forex-v0', frame_bound=(idx2 + 300, idx2 + span + 300), window_size=20)
env.seed(0)
state = env.reset()
while True:
# 行動の取得
action, _ = model.predict(state)
# 1ステップ実行
state, reward, done, info = env.step(action)
# エピソード完了
if done:
print('info:', info)
break
# グラフのプロット
plt.cla()
env.render_all()
plt.savefig('trading{:%Y%m%d_%H%M%S}.png'.format(datetime.datetime.now()))
#plt.show()

with open('C:/Util/Anaconda3/Lib/site-packages/gym_anytrading/datasets/data/FOREX_EURUSD_1H_ASK.csv', 'r') as f:
lines = f.readlines()
s1 = lines[idx1].split(',')[0]
s2 = lines[idx2-1].split(',')[0]
print(s1,s2, (idx2 - idx1))

s3 = lines[idx2].split(',')[0]
s4 = lines[idx2+span].split(',')[0]
print(s3,s4, (idx2+span - idx2))

for i in range(10):
simulation(i)

48行目 で検証データの位置を300データ分後ろに移動しています。

10回投資シミュレーションを実行

上記コードを実行すると次のような結果になりました。

[コンソール出力]

1
2
3
4
5
6
7
8
9
10
info: {'total_reward': -973390000.0, 'total_profit': 0.8735152498195379, 'position': 1}
info: {'total_reward': 1158820000.0, 'total_profit': 0.9521246437616637, 'position': 1}
info: {'total_reward': 1102640000.0, 'total_profit': 0.9497657824164452, 'position': 0}
info: {'total_reward': 1802240000.0, 'total_profit': 0.9864694966967853, 'position': 0}
info: {'total_reward': 1061140000.0, 'total_profit': 0.9462835420395067, 'position': 1}
info: {'total_reward': 919960000.0, 'total_profit': 0.9478856902017828, 'position': 1}
info: {'total_reward': 915500000.0, 'total_profit': 0.9498915585082796, 'position': 0}
info: {'total_reward': 331960000.0, 'total_profit': 0.923191270124819, 'position': 0}
info: {'total_reward': 1668460000.0, 'total_profit': 0.9798416175778797, 'position': 1}
info: {'total_reward': 511840000.0, 'total_profit': 0.9307596938562549, 'position': 1}

実行結果(1回目)
実行結果(2回目)
実行結果(3回目)
実行結果(4回目)
実行結果(5回目)
実行結果(6回目)
実行結果(7回目)
実行結果(8回目)
実行結果(9回目)
実行結果(10回目)


これまでの投資結果(トータル報酬)を表にまとめてみます。

No.トータル報酬(前前前回)トータル報酬(前前回)トータル報酬(前回)トータル報酬(今回)
13,300,000円-800,660,000円1,116,260,000円-973,390,000円
-127,340,000円564,920,000円239,580,000円1,158,820,000円
429,940,000円-1,556,660,000円-806,620,000円1,102,640,000円
-472,090,000円1,235,520,000円738,540,000円180,224,0000円
40,030,000円486,750,000円2,096,110,000円1,061,140,000円
-260,510,000円1,682,380,000円1,493,810,000円919,960,000円
88,920,000円-513,180,000円-936,250,000円915,500,000円
25,500,000円588,740,000円1,198,470,000円331,960,000円
405,860,000円-58,280,000円-2,274,570,000円1,668,460,000円
88,940,000円-602,670,000円-382,560,000円511,840,000円

今回は9勝1敗と全体的に好成績となりました。

これまでずっとプラス報酬となっていたNo⑤とNo⑧も相変わらずプラス報酬のままです。