1 Star 0 Fork 12

rizheng213/stock_robot

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
test.py 4.60 KB
一键复制 编辑 原始数据 按行查看 历史
邹吉华 提交于 2023-04-12 16:27 . 1.6
import pandas as pd
import cvt_helper as cvt
from stock_holder import StockHolder
from stock_info import StockSceneInfo
from stock_info import StockPredictInfo
import const as cst
from data_center import DataCenter
from stable_baselines3 import SAC
import const as cst
class StockPortfolioTest():
def __init__(self,model_path,is_safe):
self.current_step = cst.HISTORY_DATA_COUNT
self.is_safe = is_safe
self.model = SAC.load(model_path)
self.data_center = DataCenter()
def run(self,start_date,zs_type):
#self.protfolie_stock = self.data_center.query_concern_stock(start_date,zs_type)
#中证500 10大成分股
self.protfolie_stock = ["sh.600522","sh.601615","sh.600256","sh.600157","sz.002407","sz.000009","sz.002180","sz.002384"]
#沪深300 10大成分股
#self.protfolie_stock = ["sh.600519","sh.601318","sh.600036","sh.601012","sz.000858","sz.002594","sz.000333","sh.600900","sh.601166"]
#上证 10大成分股
#self.protfolie_stock = ["sh.600519","sh.601398","sh.601288","sh.601857","sh.600036","sh.601988","sh.601628","sh.600900","sh.601088","sh.601012"]
#方差最小10大股份
#self.protfolie_stock = ["sh.601880","sz.002936","sh.600022","sh.600567","sh.600871","sh.601860","sh.601005","sz.002958","sz.002948","sh.601992"]
if self.protfolie_stock is None :
print('run error : protfolie_stock is none')
return
total_delta_monty = 0
win_count = 0
max_win_money = 0
max_lose_money = 0
for it in self.protfolie_stock :
protfolie_stock = [it]
self.current_step = 0
self.stock_holder = StockHolder(self.is_safe,True)
self.all_stock = self.data_center.get_protfolie_info(protfolie_stock,start_date)
while not self._is_done():
self._step()
delta_monty = self.stock_holder.get_fortune()-cst.INITIAL_CAPITAL
if delta_monty > 0:
win_count += 1
if delta_monty > max_win_money:
max_win_money = delta_monty
else:
if delta_monty < -max_lose_money:
max_lose_money = -delta_monty
total_delta_monty+=delta_monty
total_num = len(self.protfolie_stock)
return win_count/total_num,max_win_money/cst.INITIAL_CAPITAL,max_lose_money/cst.INITIAL_CAPITAL,total_delta_monty/(total_num*cst.INITIAL_CAPITAL)
def _step(self):
for key,value in self.all_stock.items () :
frame_stock_data,last_info = self._get_frame_data(value)
self.stock_holder.update(key,last_info)
obs = cvt.get_obs(frame_stock_data)
action, _ = self.model.predict(obs,deterministic=True)
buy_price,sell_price = cvt.parse_action(last_info.Standard,action)
if key in self.stock_holder.stock_info :
self.stock_holder.sell(key,sell_price)
if last_info is not None:
self.stock_holder.buy(key,last_info,buy_price)
self.current_step += 1
def _print(self,stock_code):
print(f'{stock_code} Money : {self.stock_holder.get_fortune()},Safe Money : {self.stock_holder.safe_money}')
pass
def _is_done(self):
max_step = 0
for value in self.all_stock.values () :
if max_step < len(value)-1:
max_step = len(value)-1
return self.current_step>=max_step
def _get_frame_data(self,current_stock_data):
scene_info = []
for i in range(cst.HISTORY_DATA_COUNT):
scene : StockSceneInfo = current_stock_data[self.current_step-cst.HISTORY_DATA_COUNT+i]
scene_info.append(scene)
last_info:StockSceneInfo = current_stock_data[self.current_step]
return scene_info,last_info
if __name__ == "__main__":
model_name = "best_model_all_128c_2000_4"
test = StockPortfolioTest("./model/"+model_name,False)
result = []
#result.append(test.run("2022-01-01","sz50"))
result.append(test.run("2022-01-01","zz500"))
#result.append(test.run("2022-01-01","hs300"))
'''
result.append(test.run("2021-08-01","sz50"))
result.append(test.run("2021-08-01","zz500"))
result.append(test.run("2021-08-01","hs300"))
result.append(test.run("2020-08-01","sz50"))
result.append(test.run("2020-08-01","zz500"))
result.append(test.run("2020-08-01","hs300"))
'''
result = pd.DataFrame(result)
print(f"{model_name} test result")
print(result)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/rizheng213/stock_robot.git
git@gitee.com:rizheng213/stock_robot.git
rizheng213
stock_robot
stock_robot
master

搜索帮助