交易系统



In [1]:
import os
import h5py
import numpy as np
import pandas as pd
from high2low import high2low

In [2]:
# 交易日历
tradeDays = pd.read_hdf("tradeDays.h5").iloc[22:]
tradeDays.reset_index(drop=True, inplace=True)

# 股票池
dataset_universe = pd.read_hdf("universe_SH50.h5")

In [3]:
universe_list = os.listdir("Access")
dataset_open = {}
dataset_close = {}

for i in universe_list:
    stk_code = i[6:17].replace("_", ".")
    path = "Access/{}".format(i)
    min_quotes = pd.read_hdf(path)
    min_quotes = high2low(min_quotes, "1d")
    dataset_open[stk_code] = min_quotes["open"]
    dataset_close[stk_code] = min_quotes["close"]
    
dataset_open = pd.DataFrame(dataset_open)
dataset_close = pd.DataFrame(dataset_close)
dataset_universe = pd.DataFrame(
    -1, index=dataset_open.index, columns=dataset_open.columns)

In [5]:
dataset_open.to_hdf("dataset_open.h5", "open")

In [6]:
dataset_close.to_hdf("dataset_close.h5", "close")

In [ ]:


In [ ]:


In [4]:
dataset_universe


Out[4]:
600000.XSHG 600010.XSHG 600015.XSHG 600016.XSHG 600018.XSHG 600028.XSHG 600029.XSHG 600030.XSHG 600036.XSHG 600048.XSHG ... 601800.XSHG 601818.XSHG 601857.XSHG 601881.XSHG 601901.XSHG 601919.XSHG 601985.XSHG 601988.XSHG 601989.XSHG 601998.XSHG
2015-12-01 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ... -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
2015-12-02 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ... -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
2015-12-03 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ... -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
2015-12-04 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ... -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
2015-12-07 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ... -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
2015-12-08 -1 -1 -1 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29 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ... -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
2015-12-30 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ... -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
2015-12-31 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ... -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
2016-01-04 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ... -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
2016-01-05 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ... -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
2016-01-06 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ... -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
2016-01-07 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ... -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
2016-01-08 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ... -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
2016-01-11 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ... -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
2016-01-12 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ... -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2017-10-20 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ... -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
2017-10-23 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ... -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
2017-10-24 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ... -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
2017-10-25 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ... -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
2017-10-26 -1 -1 -1 -1 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1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ... -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
2017-11-17 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ... -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
2017-11-20 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ... -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
2017-11-21 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ... -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
2017-11-22 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ... -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
2017-11-23 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ... -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
2017-11-24 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ... -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
2017-11-27 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ... -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
2017-11-28 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ... -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
2017-11-29 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ... -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
2017-11-30 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ... -1 -1 -1 -1 -1 -1 -1 -1 -1 -1

490 rows × 64 columns


In [5]:
dataset_universe.shape


Out[5]:
(490, 64)

In [6]:
buy = np.array([1] * 64)

In [11]:
pr = np.random.randint(0, 1e7, size=64)

In [16]:
np.sign(np.minimum(pr * buy - 1e8 * 0.05, 0))


Out[16]:
array([ 0.,  0., -1.,  0.,  0.,  0., -1., -1.,  0.,  0.,  0., -1.,  0.,
       -1., -1.,  0.,  0., -1., -1.,  0.,  0., -1., -1., -1., -1.,  0.,
        0., -1., -1., -1., -1., -1.,  0.,  0.,  0.,  0., -1., -1.,  0.,
       -1., -1., -1., -1.,  0., -1., -1., -1.,  0.,  0., -1.,  0.,  0.,
        0.,  0., -1.,  0.,  0., -1.,  0., -1., -1.,  0.,  0.,  0.])

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:
pr = np.random.randint

In [ ]:
pr = np.random.rand

In [ ]:
pr = np.random.random

In [ ]:


In [ ]:


In [ ]:


In [ ]:
-

In [ ]:


In [ ]:


In [ ]:


In [ ]:
sell_op.shape

In [ ]:
buy_op

In [ ]:


In [ ]:
np.sign()

In [ ]:
np.array(a)

In [ ]:


In [ ]:


In [ ]:


In [ ]:
# dataset_table = pd.DataFrame(
#     np.zeros_like(dataset_close), 
#     index=dataset_open.index, 
#     columns=dataset_open.columns)

dataset_table = pd.DataFrame(
    np.ones_like(dataset_close) * -1., 
    index=dataset_open.index, 
    columns=dataset_open.columns)

In [ ]:


In [ ]:
A = transaction()
A.reset()
date, universe = A.step(0, 1)

In [ ]:
dataset_table.loc[date, universe] = np.random.normal(size=(50))

In [ ]:
dataset_table.loc[date, universe]

In [ ]:
dataset_mask.loc[date, universe]

In [ ]:
universe

In [ ]:
dataset_table.loc[date, universe]

In [ ]:


In [ ]:
all_stocks = dataset_close.columns.tolist()

In [ ]:
for i in U:
    all_stocks.remove(i)

In [ ]:
all_stocks

In [ ]:
all_stocks - U

In [ ]:
all_stocks

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:
A.portfolio.loc["2016-11-11"]

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:
pre_port = self.portfolio.loc[pre_date]
        
        
#         tradeDay = self.tradeDays[step]
#         universe = self.universe.loc[tradeDay].tolist()
#         open_row = self.open.loc[tradeDay, universe]
#         close_col = self.close.loc[tradeDay, universe]

In [ ]:


In [ ]:
# 交易日历
tradeDays = pd.read_hdf("tradeDays.h5").iloc[23:]
tradeDays.reset_index(drop=True, inplace=True)

# 股票池
dataset_universe = pd.read_hdf("universe_SH50.h5")

In [ ]:
dataset_open.shape

In [ ]:
dataset_open.head()

In [ ]:
pd.DataFrame?

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:
def reset(self):
        self.portfolio = np.zeros_like(self.table_close[26])  # 股票持仓数量
        self.cash = 5e7
        self.valuation = 0  # 持仓估值
        self.total_value = self.cash + self.valuation
        self.buffer_value = []
        self.buffer_reward = []

In [ ]:
class Quotes(object):
    def __init__(self):
        self.table_open = np.array(opens)  # 开盘价
        self.table_close = np.array(closes)  # 收盘价
        self.buy_free = 2.5e-4 + 1e-4
        self.sell_free = 2.5e-4 + 1e-3 + 1e-4
        self.reset()

    def reset(self):
        self.portfolio = np.zeros_like(self.table_close[26])  # 股票持仓数量
        self.cash = 5e7
        self.valuation = 0  # 持仓估值
        self.total_value = self.cash + self.valuation
        self.buffer_value = []
        self.buffer_reward = []

    def buy(self, op, opens):
        cash = self.cash * 0.8  # 可使用资金量
        mask = np.sign(np.maximum(opens - 1, 0))  # 掩码 去掉停盘数据
        op = mask * op
        sum_buy = np.maximum(np.sum(op), 15)
        cash_buy = op * (cash / sum_buy)  # 等资金量
        num_buy = np.round(cash_buy / ((opens + _EPSILON) * 100))  # 手
        self.cash -= np.sum(opens * 100 * num_buy * (1 + self.buy_free))  # 买入股票操作
        self.portfolio += num_buy * 100

    def sell(self, op, opens):
        mask = np.sign(np.maximum(opens - 1, 0))
        num_sell = self.portfolio * op * mask  # 卖出股票数量
        self.cash -= np.sum(opens * num_sell * (1 - self.sell_free))
        self.portfolio += num_sell

    def assess(self, closes):
        total_value = self.cash + np.sum(self.portfolio * closes)
        return total_value

    def step(self, step_counter, op):
        # 获取报价单
        opens = self.table_open[step_counter]
        closes = self.table_close[step_counter]
        # 买卖操作信号
        buy_op = np.maximum(op, 0)
        sell_op = np.minimum(op, 0)
        # 卖买操作
        self.sell(sell_op, opens)
        self.buy(buy_op, opens)
        # 当日估值
        new_value = self.assess(closes)
        reward = np.log(new_value / self.total_value)
        self.total_value = new_value
        self.buffer_value.append(new_value)
        self.buffer_reward.append(reward)

        if step_counter > 465:
            done = True
        elif self.total_value < 4.5e7:
            done = True
        else:
            done = False

        return reward, done