示例#1
0
    def test_data_from_data_utils(self):
        feature_df, price_s = create_toy_feature(order_book_ids_number=2,
                                                 feature_number=3,
                                                 start="2020-01-01",
                                                 end="2020-01-11",
                                                 random_seed=555)
        print(price_s)
        data_source = DataSource(feature_df=feature_df, price_s=price_s)

        #availabel_dts
        availabel_dts = data_source.get_available_trading_dts()

        first_dt_expected = pd.Timestamp("2020-01-01")
        first_dt = availabel_dts[0]
        self.assertEqual(first_dt, first_dt_expected)

        end_dt_expected = pd.Timestamp("2020-01-11")
        end_dt = availabel_dts[-1]
        self.assertEqual(end_dt, end_dt_expected)

        #get_availabel_order_book_ids
        order_book_ids = data_source.get_available_order_book_ids()

        forth_dt_expected = pd.Timestamp("2020-01-04")
        forth_dt = availabel_dts[3]
        self.assertEqual(forth_dt, forth_dt_expected)

        #get state
        #state = data_source.history_bars(order_book_ids=order_book_ids, dt=availabel_dts[3], bar_count=2)

        #get last price
        last_price = data_source.get_last_price(
            order_book_id=order_book_ids[0], dt=forth_dt_expected)
        last_price_expected = 20.11
        self.assertEqual(last_price, last_price_expected)
class TestOneObjectImplmentCorrection(unittest.TestCase):
    def setUp(self):
        feature_df, price_s = create_toy_feature(order_book_ids_number=2,
                                                 feature_number=3,
                                                 start="2020-01-01",
                                                 end="2020-01-11",
                                                 random_seed=111)
        self.data_source = DataSource(feature_df=feature_df, price_s=price_s)

        self.look_backward_window = 2

        self.starting_cash = {"STOCK": 1000000, "FUTURE": 10000}

        self.env = TradingEnv(data_source=self.data_source,
                              look_backward_window=self.look_backward_window,
                              mode="rl",
                              starting_cash=self.starting_cash)

    def test_portfolio_setup(self):
        portfolio = Context.get_instance().portfolio
        expected_total_value = sum(self.starting_cash.values())
        actual_total_value = portfolio.total_value
        self.assertEqual(actual_total_value, expected_total_value)

    def test_trading_dts_setup(self):
        #test available trading dts
        all_trading_dts = self.data_source.get_available_trading_dts()
        expected_available_trading_dts = all_trading_dts[self.
                                                         look_backward_window -
                                                         1:]
        actual_available_trading_dts = Context.get_instance(
        ).get_available_trading_dts()
        self.assertListEqual(list(actual_available_trading_dts),
                             list(expected_available_trading_dts))

        #test the first trading dt
        expected_first_trading_dt = expected_available_trading_dts[0]
        actual_first_trading_dt = Context.get_instance().trading_dt
        self.assertEqual(actual_first_trading_dt, expected_first_trading_dt)

        order_book_ids = self.data_source.get_available_order_book_ids()

        expected_first_last_price = self.data_source.get_last_price(
            order_book_id=order_book_ids[0], dt=expected_first_trading_dt)
        actual_first_last_price = Context.get_instance().get_last_price(
            order_book_id=order_book_ids[0])
        self.assertEqual(actual_first_last_price, expected_first_last_price)