Skip to content

Commit e082c82

Browse files
committed
feat: hist and pie plot
1 parent 949b812 commit e082c82

File tree

6 files changed

+110
-947
lines changed

6 files changed

+110
-947
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ stockdata
55
model
66
log
77
.vscode/
8+
*.pkl
89

910
# Byte-compiled / optimized / DLL files
1011
__pycache__/

README.md

+28-5
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,37 @@ reward = 1 if reward > 0 else reward = -100
9999

100100
### 验证结果
101101

102-
#### 单只股票测试
102+
**单只股票**
103103

104-
#### 单只股票多次测试
104+
- 初始本金 `10000`
105+
- 股票代码:`sh.600036`(招商银行)
106+
- 训练集: `stockdata/train/sh.600036.招商银行.csv`
107+
- 测试集: `stockdata/test/sh.600036.招商银行.csv`
108+
- 模拟操作 `20` 天,最终盈利约 `400`
105109

106-
#### 多只股票测试
110+
<img src="img/sh.600036.png" alt="drawing" width="70%"/>
107111

108-
## 结语
112+
**多只股票**
109113

110-
欢迎指正!
114+
选取 `79` 只股票,进行训练,共计
115+
116+
- 盈利: `44.8%`
117+
- 不亏不赚: `49.0%`
118+
- 亏损:`6.3%`
119+
120+
<img src="img/pie.png" alt="drawing" width="60%"/>
121+
122+
<img src="img/hist.png" alt="drawing" width="60%"/>
123+
124+
## 最后
125+
126+
- 俺完全是股票没入门的新手,难免存在错误,欢迎指正!
127+
- 数据和方法皆来源于网络,无法保证有效性,**Just For Fun**
111128

112129
## 参考资料
130+
131+
- Y. Deng, F. Bao, Y. Kong, Z. Ren and Q. Dai, "Deep Direct Reinforcement Learning for Financial Signal Representation and Trading," in IEEE Transactions on Neural Networks and Learning Systems, vol. 28, no. 3, pp. 653-664, March 2017.
132+
- [Yuqin Dai, Chris Wang, Iris Wang, Yilun Xu, "Reinforcement Learning for FX trading"](http://stanford.edu/class/msande448/2019/Final_reports/gr2.pdf)
133+
- [Create custom gym environments from scratch — A stock market example](https://towardsdatascience.com/creating-a-custom-openai-gym-environment-for-stock-trading-be532be3910e)
134+
- [notadamking/Stock-Trading-Environment](https://github.com/notadamking/Stock-Trading-Environment)
135+
- Chien Yi Huang. Financial trading as a game: A deep reinforcement learning approach. arXiv preprint arXiv:1807.02787, 2018.

img/hist.png

6.44 KB
Loading

img/pie.png

13.6 KB
Loading

main.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import pickle
23
import pandas as pd
34
from stable_baselines.common.policies import MlpPolicy
45
from stable_baselines.common.vec_env import DummyVecEnv
@@ -40,14 +41,15 @@ def stock_trade(stock_file):
4041

4142

4243
def find_file(path, name):
44+
# print(path, name)
4345
for root, dirs, files in os.walk(path):
4446
for fname in files:
4547
if name in fname:
4648
return os.path.join(root, fname)
4749

4850

4951
def test_a_stock_trade(stock_code):
50-
stock_file = find_file('./stockdata/train', stock_code)
52+
stock_file = find_file('./stockdata/train', str(stock_code))
5153

5254
daily_profits = stock_trade(stock_file)
5355
fig, ax = plt.subplots()
@@ -61,11 +63,24 @@ def test_a_stock_trade(stock_code):
6163

6264

6365
def multi_stock_trade():
64-
pass
66+
start_code = 600000
67+
max_num = 3000
68+
69+
group_result = []
70+
71+
for code in range(start_code, start_code + max_num):
72+
stock_file = find_file('./stockdata/train', str(code))
73+
if stock_file:
74+
profits = stock_trade(stock_file)
75+
group_result.append(profits)
76+
77+
with open(f'code-{start_code}-{start_code + max_num}.pkl', 'wb') as f:
78+
pickle.dump(group_result, f)
6579

6680

6781
if __name__ == '__main__':
68-
test_a_stock_trade('sh.600036')
82+
multi_stock_trade()
83+
# test_a_stock_trade('sh.600036')
6984
# ret = find_file('./stockdata/train', '600036')
7085
# print(ret)
7186

0 commit comments

Comments
 (0)