Skip to content

Commit

Permalink
feat: hist and pie plot
Browse files Browse the repository at this point in the history
  • Loading branch information
wangshub committed Mar 27, 2020
1 parent 949b812 commit e082c82
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 947 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ stockdata
model
log
.vscode/
*.pkl

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
33 changes: 28 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,37 @@ reward = 1 if reward > 0 else reward = -100

### 验证结果

#### 单只股票测试
**单只股票**

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

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

## 结语
**多只股票**

欢迎指正!
选取 `79` 只股票,进行训练,共计

- 盈利: `44.8%`
- 不亏不赚: `49.0%`
- 亏损:`6.3%`

<img src="img/pie.png" alt="drawing" width="60%"/>

<img src="img/hist.png" alt="drawing" width="60%"/>

## 最后

- 俺完全是股票没入门的新手,难免存在错误,欢迎指正!
- 数据和方法皆来源于网络,无法保证有效性,**Just For Fun**

## 参考资料

- 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.
- [Yuqin Dai, Chris Wang, Iris Wang, Yilun Xu, "Reinforcement Learning for FX trading"](http://stanford.edu/class/msande448/2019/Final_reports/gr2.pdf)
- [Create custom gym environments from scratch — A stock market example](https://towardsdatascience.com/creating-a-custom-openai-gym-environment-for-stock-trading-be532be3910e)
- [notadamking/Stock-Trading-Environment](https://github.com/notadamking/Stock-Trading-Environment)
- Chien Yi Huang. Financial trading as a game: A deep reinforcement learning approach. arXiv preprint arXiv:1807.02787, 2018.
Binary file added img/hist.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/pie.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 18 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import pickle
import pandas as pd
from stable_baselines.common.policies import MlpPolicy
from stable_baselines.common.vec_env import DummyVecEnv
Expand Down Expand Up @@ -40,14 +41,15 @@ def stock_trade(stock_file):


def find_file(path, name):
# print(path, name)
for root, dirs, files in os.walk(path):
for fname in files:
if name in fname:
return os.path.join(root, fname)


def test_a_stock_trade(stock_code):
stock_file = find_file('./stockdata/train', stock_code)
stock_file = find_file('./stockdata/train', str(stock_code))

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


def multi_stock_trade():
pass
start_code = 600000
max_num = 3000

group_result = []

for code in range(start_code, start_code + max_num):
stock_file = find_file('./stockdata/train', str(code))
if stock_file:
profits = stock_trade(stock_file)
group_result.append(profits)

with open(f'code-{start_code}-{start_code + max_num}.pkl', 'wb') as f:
pickle.dump(group_result, f)


if __name__ == '__main__':
test_a_stock_trade('sh.600036')
multi_stock_trade()
# test_a_stock_trade('sh.600036')
# ret = find_file('./stockdata/train', '600036')
# print(ret)

Loading

0 comments on commit e082c82

Please sign in to comment.