class TestDataWraper(unittest.TestCase):
    
    def setUp(self):
        self.order_book_id_number = 100
        self.feature_number = 10
        self.toy_data = create_toy_data(order_book_ids_number=order_book_id_number, feature_number=self.feature_number, start="2019-05-01", end="2019-12-12", frequency="D")
        self.env_2d = PortfolioTradingGym(data_df=self.toy_data, sequence_window=1, add_cash=False, mode="numpy")
        
        self.sequence_window =3
        self.env_3d = PortfolioTradingGym(data_df = self.toy_data, sequence_window=self.sequence_window, add_cash=False, mode="numpy")
    
    def test_sequence_window_equal_1(self):
        state = self.env_2d.reset()
        self.assertEqual(state.shape,(self.order_book_id_number, self.feature_number))
        
        random_action = self.env_2d.action_space.sample()
        next_state, reward, done, info = self.env_2d.step(random_action)
        
        self.assertEqual(next_state.shape, (self.order_book_id_number, self.feature_number))
        return next_state
    
    def test_sequence_window_mt_1(self):
        state = self.env_3d.reset()
        self.assertEqual(state.shape,(self.order_book_id_number, self.sequence_window, self.feature_number))
        
        random_action = self.env_3d.action_space.sample()
        next_state, reward, done, info = self.env_3d.step(random_action)
        
        self.assertEqual(next_state.shape, (self.order_book_id_number, self.sequence_window, self.feature_number))
    
    def test_torch_wrapper(self):
示例#2
0
def test_single_number_R():
    order_book_ids = ["000001.XSHE"]
    mock_data = create_mock_data(order_book_ids=order_book_ids,
                                 start_date="2019-01-01",
                                 end_date="2019-01-14")
    sequence_window = 1
    env = PortfolioTradingGym(data_df=mock_data,
                              sequence_window=sequence_window,
                              add_cash=True)
    state = env.reset()
    h_t_list = []
    orderlist = [0.5, 0.8, 0.4, 1.0, 0.0, -0.5, -0.7, -0.4, -1.0, 0.3]
    for i in range(len(orderlist)):
        next_state, reward, done, info = env.step(orderlist[i])
        h_t_list.append(info["h_t"])
    expected_h_t = ([494850,
                     500050], [809848.6,
                               198999.898], [402853.3822,
                                             605369.6309], [1004290.9, 0],
                    [0,
                     1004391.359], [-499734.9212,
                                    1506737.699], [-704690.4898, 1712075.95],
                    [-397473.9834,
                     1410480.594], [-1019895.146,
                                    2026216.001], [304220.9, 704495.1425])
    np.testing.assert_almost_equal(h_t_list, expected_h_t, decimal=1)
示例#3
0
def test_single_number():
    order_book_ids = ["000001.XSHE"]
    mock_data = create_mock_data(order_book_ids=order_book_ids, start_date="2019-01-01", end_date="2019-01-14")
    sequence_window = 1
    env = PortfolioTradingGym(data_df=mock_data, sequence_window=sequence_window, add_cash=True)
    state = env.reset()
    h_t_list = []
    orderlist = [0.5, 0.8, 0.4, 1.0, 0.0]
    for i in range(len(orderlist)):
        next_state, reward, done, info = env.step(orderlist[i])
        h_t_list.append(info["h_t"])
    '''
    stock_reward = [0.0219, -0.0103, 0.0175, -0.0017, -0.0039, 0.0059]
    v_t = portfolio_value = [1000000, 994900.0, 1008848.5, 1008223.0, 1004290.9, 1004391.4]
    expected_portfolio calculate: portfolio_value[i]*weight*mock_data["returns"][i+1] for stock and cash
     000001.XSHE    2019-01-01    0.0219
               2019-01-02   -0.0103
               2019-01-03    0.0175
               2019-01-04   -0.0017
               2019-01-05   -0.0039
               2019-01-06    0.0059
               2019-01-07   -0.0049
               2019-01-08   -0.0003
               2019-01-09   -0.0136
               2019-01-10    0.0068
               2019-01-11    0.0077
               2019-01-12    0.0136
               2019-01-13   -0.0022
               2019-01-14   -0.0012
    '''
    expected_portfolio = ([494850, 500050], [809848.6, 198999.898], [402853.3822, 605369.6309], [1004290.9, 0], [0, 1004391.359])
    np.testing.assert_almost_equal(h_t_list, expected_portfolio, decimal=1)
示例#4
0
def test_positive():
    order_book_ids = ["000001.XSHE", "000002.XSHE"]
    mock_data = create_mock_data(order_book_ids=order_book_ids,
                                 start_date="2019-01-01",
                                 end_date="2019-01-14")
    sequence_window = 1
    env = PortfolioTradingGym(data_df=mock_data,
                              sequence_window=sequence_window,
                              add_cash=True)
    state = env.reset()
    h_t_list = []
    orderlista = [0.5, 0.6, 0.2, 1.0, 0.0, 0.0, 0.4, 0.1]
    orderlistb = [0.5, 0.1, 0.7, 0.0, 1.0, 0.0, 0.6, 0.2]
    for i in range(len(orderlista)):
        next_state, reward, done, info = env.step(
            [orderlista[i], orderlistb[i], 0])
        h_t_list.append(info["h_t"])
    '''
    000001.XSHE    2019-01-01    0.0219
                   2019-01-02   -0.0103
                   2019-01-03    0.0175
                   2019-01-04   -0.0017
                   2019-01-05   -0.0039
                   2019-01-06    0.0059
                   2019-01-07   -0.0049
                   2019-01-08   -0.0003
                   2019-01-09   -0.0136
    000002.XSHE    2019-01-01   -0.0186
                   2019-01-02    0.0098
                   2019-01-03   -0.0030
                   2019-01-04    0.0065
                   2019-01-05    0.0111
                   2019-01-06    0.0006
                   2019-01-07    0.0112
                   2019-01-08   -0.0304
                   2019-01-09   -0.0094
    '''
    expected_h_t = ([494850, 504900,
                     0.0], [610347.375, 99675.075, 299954.9925
                            ], [201652.0962, 711579.5973,
                                101007.84], [1010283.99, 0.0,
                                             0.0], [0.0, 1010890.17, 0.0],
                    [0.0, 0.0,
                     1010991.259], [404275.1847, 588154.2748,
                                    0.0], [97893.241, 196620.0124, 694770.1])
    np.testing.assert_almost_equal(h_t_list, expected_h_t, decimal=1)
示例#5
0
def test_single_array():
    order_book_ids = ["000001.XSHE"]
    mock_data = create_mock_data(order_book_ids=order_book_ids, start_date="2019-01-01", end_date="2019-01-14")
    sequence_window = 1
    env = PortfolioTradingGym(data_df=mock_data, sequence_window=sequence_window, add_cash=True)
    state = env.reset()
    h_t_list = []
    orderlist = [0.5, 0.8, 0.4, 1.0, 0.0]
    for i in range(len(orderlist)):
        next_state, reward, done, info = env.step((orderlist[i], 0))
        h_t_list.append(info["h_t"])
    '''
    Action for "cash" is never used. Whenever add_cash = true, next "cash" is calculated by negative sum of the other 
    trade (in market_simulator.py). Then action for cash can be any number and the results are still right.
    '''
    expected_portfolio = ([494850, 500050], [809848.6, 198999.898], [402853.3822, 605369.6309], [1004290.9, 0], [0, 1004391.359])
    np.testing.assert_almost_equal(h_t_list, expected_portfolio, decimal=1)
示例#6
0
def test_single_cash_false():
    order_book_ids = ["000001.XSHE"]
    mock_data = create_mock_data(order_book_ids=order_book_ids, start_date="2019-01-01", end_date="2019-01-14")
    sequence_window = 1
    env = PortfolioTradingGym(data_df=mock_data, sequence_window=sequence_window, add_cash=False)
    state = env.reset()
    h_t_list = []
    orderlist = [0.5, 0.8, 0.4, 1.0, 0.0]
    for i in range(len(orderlist)):
        next_state, reward, done, info = env.step(orderlist[i])
        h_t_list.append(info["h_t"])
    '''
    Portfolio value becomes stock value incorrectly after every step.
    When portfolio value = 0, self.w_t (h_next / sum(h_next) in portfolio_gym) is NaN, then trigger value error in 
    market simulator. 
    '''
    expected_portfolio = ([494850], [809848.6], [402853.3822], [1004290.9], [0])
    np.testing.assert_almost_equal(h_t_list, expected_portfolio, decimal=1)
示例#7
0
def test_single_cashfalse_R():
    order_book_ids = ["000001.XSHE"]
    mock_data = create_mock_data(order_book_ids=order_book_ids,
                                 start_date="2019-01-01",
                                 end_date="2019-01-14")
    sequence_window = 1
    env = PortfolioTradingGym(data_df=mock_data,
                              sequence_window=sequence_window,
                              add_cash=False)
    state = env.reset()
    h_t_list = []
    orderlist = [0.5, 0.8, 0.4, 1.0, 0.0, -0.5, -0.7, -0.4, -1.0, 0.3]
    for i in range(len(orderlist)):
        next_state, reward, done, info = env.step(orderlist[i])
        h_t_list.append(info["h_t"])
    expected_h_t = ([494850], [809848.6], [402853.3822
                                           ], [1004290.9], [0], [-499734.9212],
                    [-704690.4898], [-397473.9834], [-1019895.146], [304220.9])
    np.testing.assert_almost_equal(h_t_list, expected_h_t, decimal=1)
示例#8
0
def test_mul_R():
    order_book_ids = ["000001.XSHE", "000002.XSHE"]
    mock_data = create_mock_data(order_book_ids=order_book_ids,
                                 start_date="2019-01-01",
                                 end_date="2019-01-14")
    sequence_window = 1
    env = PortfolioTradingGym(data_df=mock_data,
                              sequence_window=sequence_window,
                              add_cash=True)
    state = env.reset()
    h_t_list = []
    orderlista = [0.5, 0.6, -0.2, -0.3, -1.0, 1.0]
    orderlistb = [0.5, -0.2, 0.7, -0.4, 1.0, -1.0]
    for i in range(len(orderlista)):
        next_state, reward, done, info = env.step(
            [orderlista[i], orderlistb[i], 0])
        h_t_list.append(info["h_t"])
    '''
000001.XSHE    2019-01-01    0.0219
               2019-01-02   -0.0103
               2019-01-03    0.0175
               2019-01-04   -0.0017
               2019-01-05   -0.0039
               2019-01-06    0.0059
               2019-01-07   -0.0049
000002.XSHE    2019-01-01   -0.0186
               2019-01-02    0.0098
               2019-01-03   -0.0030
               2019-01-04    0.0065
               2019-01-05    0.0111
               2019-01-06    0.0006
               2019-01-07    0.0112
    '''
    expected_h_t = ([494850, 504900,
                     0.0], [610347.375, -199350.15, 599909.985],
                    [-201837.7335, 712234.6748,
                     505504.15], [-303581.7231, -410871.0374, 1727204.558],
                    [-1018727.033, 1013359.449,
                     1012853.07], [1002548.843, -1018769.3595, 1007586.27])
    print(h_t_list)
    np.testing.assert_almost_equal(h_t_list, expected_h_t, decimal=1)
def test_equal_weighted_portfolio():
    order_book_ids = ["000001.XSHE","600000.XSHG"]
    mock_data = create_mock_data(order_book_ids=order_book_ids, start_date="2019-01-01", end_date="2019-06-11")
    
    sequence_window=3
    env = PortfolioTradingGym(data_df=mock_data, sequence_window=sequence_window, add_cash=True)
    
    state = env.reset()
    print(state)
    action = np.array([0.5, 0.5, 0])
    while True:        
        next_state, reward, done, info = env.step(action)
        if done:
            break
    
    portfolio_reward = np.array(env.experience_buffer["reward"])
    expected_reward = np.array(env.experience_buffer["reward_benchmark"]) 
    
    np.testing.assert_almost_equal(portfolio_reward, expected_reward, decimal=3)
    
    env.render()
    
    return mock_data, env
def test_invest_on_first_stock():
    order_book_ids = ["000001.XSHE","600000.XSHG"]
    mock_data = create_mock_data(order_book_ids=order_book_ids, start_date="2019-01-01", end_date="2019-06-11")
    
    sequence_window=3
    env = PortfolioTradingGym(data_df=mock_data, sequence_window=sequence_window, add_cash=True)
    
    state = env.reset()
    # the first stock buy and hold strategy
    action = np.array([1.0, 0.0, 0.0])
    while True:        
        next_state, reward, done, info = env.step(action)
        if done:
            break
    
    portfolio_reward = np.array(env.experience_buffer["reward"])
    expected_reward = mock_data.xs("000001.XSHE")["returns"].iloc[sequence_window:].values 
    
    #pdb.set_trace()
    np.testing.assert_almost_equal(portfolio_reward, expected_reward, decimal=3)
    
    #env.render()
    
    return mock_data, env
示例#11
0
from sklearn.linear_model import LinearRegression
from trading_gym.utils.data.toy import create_toy_data
from trading_gym.envs.portfolio_gym.portfolio_gym import PortfolioTradingGym

order_book_id_number = 100
toy_data = create_toy_data(order_book_ids_number=order_book_id_number,
                           feature_number=10,
                           start="2019-05-01",
                           end="2019-12-12",
                           frequency="D")

env = PortfolioTradingGym(data_df=toy_data, sequence_window=1, add_cash=False)
state = env.reset()

while True:
    next_state, reward, done, info = env.step(action=None)
    label = info["one_step_fwd_returns"]
    print(state)
    print(label)

    #
    regressor = LinearRegression()
    regressor.fit(state.values, label.values)

    #display and store
    print(regressor.coef_)
    env.experience_buffer["coef"].append(regressor.coef_)
    #
    if done:
        break
    else: