-
Notifications
You must be signed in to change notification settings - Fork 0
/
stock_market_trade.py
64 lines (57 loc) · 2.28 KB
/
stock_market_trade.py
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
from agent import agent
from trade_env import StockTradingEnv
import numpy as np
from datetime import datetime
import matplotlib.pyplot as plt
if __name__=='__main__':
"""
流通股本1亿,初始价格10元
100个agent,每个agent 20万现金,100万股
"""
agent_count = 100
ep = 20
env = StockTradingEnv(1e9,10,0.1,0.01,0.01,6,4)
agent_list = []
price_l=[]
for i in range(agent_count):
agent_list.append(agent(i,2e5,1e6)) #初始化100个agent
for t in range(ep): #假设进行100次交易
print('*'*70)
print('第{}次交易循环'.format(t))
print('*' * 70)
for i in range(100): #每次交易每个agent各自进行操作
now_price = env.market_price()
action = agent_list[i].trade(now_price)
if action==None:
print('agent{}持仓不动'.format(agent_list[i].id))
continue
print('agent{}执行交易,买/卖{}手,挂单价格为{:.2f}'.format(action[2], action[0], action[1]))
now_price, list_of_sell, list_of_buy=env.step(action)
if now_price==None:
continue
k=0
del_sell=[]
del_buy = []
for sell,buy in zip(list_of_sell,list_of_buy):
if sell[0]==0:
agent_id = int(sell[2])
agent_list[agent_id].update(sell[3]) #更新agent状态
del_sell.append(k)
if buy[0]==0:
agent_id = int(buy[2])
agent_list[agent_id].update(buy[3]) #更新agent状态
del_buy.append(k)
k+=1
list_of_sell = np.delete(list_of_sell, del_sell, axis=0)
list_of_buy = np.delete(list_of_buy,del_buy, axis=0) # 删除买单
env._update_list(list_of_sell,list_of_buy)
print('{}市场价格为{:.2f}'.format(datetime.now().strftime('%H:%M:%S'),now_price))
price_l.append(now_price)
index_ = list(range(len(price_l)))
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.plot(index_,price_l)
plt.xlabel("序列")
plt.ylabel("价格")
plt.title('买卖均势')
plt.show()
plt.savefig('{}agent-{}ep—balance.png'.format(agent_count,ep))