def risk_parity_strategy(state: pd.DataFrame) -> list:
    volatility = state["returns"].groupby(level=0).std()
    volatility_reverse = 1 / volatility
    weight = volatility_reverse / volatility_reverse.sum()
    weight_dict = weight.to_dict()

    #call in-built trade API function
    action = order_target_weights(weight_dict)
    return action
def pick_stocks_by_prediction(state: pd.DataFrame, top_number=50) -> list:
    sorted_prediction = state["prediction"].sort_values(ascending=False)
    to_target = sorted_prediction[:top_number].droplevel(1)
    # equal weight

    target_weight = to_target.map(lambda x: 1 / top_number)
    target_weight_dict = target_weight.to_dict()
    print(target_weight_dict)
    #call in-built trade API function
    action = order_target_weights(target_weight_dict)
    return action
def your_strategy(state):
    """
    here is a random strategy, only trade the first stock with a random target percent
    """

    target_percent_of_postion = round(random.uniform(0, 1), 2)
    # call trade API
    target_pososition_dict = {the_first_stock_id: target_percent_of_postion}
    print("the target portfolio is to be: {}".format(target_pososition_dict))
    # call trade API
    action = order_target_weights(target_pososition_dict)
    return action
Beispiel #4
0
    def test_senond_step_sell(self):
        to_submit_orders = order_target_weights({self.order_book_id: 0.2})
        state, reward, is_done, info = self.env.step(action=to_submit_orders)

        order = to_submit_orders[0]
        # this is a sell trade
        #pdb.set_trace()
        self.assertEqual(first=order.side, second=SIDE.SELL)
        #
        second_trading_dt = self.trading_dts[1]
        expect_deal_price = self.data_source.get_last_price(
            order_book_id=self.order_book_id, dt=second_trading_dt)
        expect_deal_money = expect_deal_price * order.quantity

        expect_commission_fee = expect_deal_money * self.commission_rate * self.commission_multiplier
        expect_tax = expect_deal_money * self.tax_rate * self.tax_multiplier  # no tax rate when buy
        expect_transaction_cost = expect_commission_fee + expect_tax
        print("expect_transaction_cost: {}".format(expect_transaction_cost))
        second_trade = Context.get_instance().tracker._trades[1]
        print(second_trade)
        self.assertEqual(first=second_trade["order_book_id"],
                         second=self.order_book_id)
        self.assertEqual(
            first=second_trade["trading_datetime"],
            second=second_trading_dt.strftime("%Y-%m-%d %H:%M:%S"))
        self.assertEqual(first=second_trade["last_price"],
                         second=expect_deal_price)
        self.assertEqual(first=second_trade["commission"],
                         second=expect_commission_fee)
        self.assertEqual(first=second_trade["tax"], second=expect_tax)
        self.assertEqual(first=second_trade["transaction_cost"],
                         second=expect_transaction_cost)

        expect_cash_stock_settlement = self.expect_cash_stock_settlement + expect_deal_money - expect_transaction_cost

        next_trading_dt = self.trading_dts[2]
        expect_settlement_price = self.data_source.get_last_price(
            order_book_id=self.order_book_id, dt=next_trading_dt)
        expect_remaining_market_value_stock_settlement = (
            self.first_trade["last_quantity"] -
            second_trade["last_quantity"]) * expect_settlement_price

        self.expect_total_value_stock_settlement = expect_cash_stock_settlement + expect_remaining_market_value_stock_settlement

        self.expect_total_value_future_settlement = self.expect_total_value_future

        expect_total_value_settlement = self.expect_total_value_stock_settlement + self.expect_total_value_future_settlement

        expect_reward = (expect_total_value_settlement -
                         self.expect_total_value_settlement
                         ) / self.expect_total_value_settlement
        #pdb.set_trace()
        self.assertAlmostEqual(first=reward, second=expect_reward)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from sharpe.utils.mock_data import create_toy_feature
from sharpe.data.data_source import DataSource
from sharpe.environment import TradingEnv

feature_df, price_s = create_toy_feature(order_book_ids_number=2,
                                         feature_number=3,
                                         random_seed=111)
data_source = DataSource(feature_df=feature_df, price_s=price_s)

env = TradingEnv(data_source=data_source, look_backward_window=2)
print('--------------------------------------------')
from sharpe.core.context import Context
print("current context \n", Context.get_instance().__dict__)

from sharpe.mod.sys_account.api import order_target_weights
to_submit_orders = order_target_weights({"000001.XSHE": 0.5})

state, reward, is_done, info = env.step(action=to_submit_orders)
print(state, reward, is_done)
# to_submit_orders2 = order_target_portfolio({"000001.XSHE":0.2})
# state, reward, is_done, info = env.step(action=to_submit_orders2)
# print(state, reward, is_done)
Beispiel #6
0
    def test_first_step_buy_and_then_sell(self):
        feature_df, price_s = create_toy_feature(order_book_ids_number=1,
                                                 feature_number=3,
                                                 random_seed=111)
        data_source = DataSource(feature_df=feature_df, price_s=price_s)
        order_book_ids = data_source.get_available_order_book_ids()

        STOCK_INIT_CASH = 1000000
        FUTURE_INIT_CASH = 10000
        starting_cash = {"STOCK": STOCK_INIT_CASH, "FUTURE": FUTURE_INIT_CASH}
        commission_rate = 0.0005  #default value in environment
        tax_rate = 0.001  #default value in environment

        commission_multiplier = 2
        min_commission = 5
        tax_multiplier = 1
        """
         
        MANUALly check
        
        first date: 
            target weight: 0.5
            price at end: 77.53
            
            target quantity: 6400
            deal_money = 6400 * 77.53 = 496192
            commition_fee_total = 496.192
            availabel_cash_stock = 1000000 - 496192 - 496.192 = 503311.808
            total_value_account_stock = 496192 + 503311.808 = 999503.808
            total_value_account = 999503.808 + 10000 = 1009503.808
            returns = 1009503.808/1010000  -1 = -0.0004912792079208028
        
        second date:
            target weight: 0.2
            price at end: 69.78
            
            stock_account_at_this_time: 6400*69.78 + 503311.808 =  949903.808
            target quantity: 2700
            to_trade_quantity: 3700
            deal_money = 3700 * 69.78 = 258186
            commition_fee_total = 258186 * (0.001 + 0.001) = 516.372
            
            available_cash_stock = 503311.808 + 258186 - 516.372 = 760981.436
            
            total_value_account_stock = 760981.436 + 2700*69.78 = 949387.436
            
            total_value_account = 949387.436 + 10000 =959387.436
            
            return = 959387.436/1009503.808 - 1 = -0.04964455963696568
        
        """

        env = TradingEnv(data_source=data_source,
                         look_backward_window=2,
                         mode="non-rl",
                         starting_cash=starting_cash,
                         commission_multiplier=commission_multiplier,
                         min_commission=min_commission,
                         tax_multiplier=tax_multiplier)

        print('--------------------------------------------')
        #print("current context \n",Context.get_instance().__dict__)
        context = Context.get_instance()

        #stock account
        expect_market_value_stock = 0
        expect_cash_stock = starting_cash["STOCK"]
        expect_total_value_stock = expect_cash_stock + expect_market_value_stock

        #future account
        expect_market_value_future = 0
        expect_cash_future = starting_cash["FUTURE"]
        expect_total_value_future = expect_cash_future + expect_market_value_future
        expect_total_value_future = expect_total_value_future

        #portfolio
        expect_total_value_portfolio = expect_total_value_stock + expect_total_value_future

        trading_dts = context.get_available_trading_dts()
        first_trading_dt = trading_dts[0]
        order_book_id = order_book_ids[0]

        #
        target_weight = 0.5
        print(
            "------------the first trading date: {}--------------------------".
            format(env.trading_dt))
        to_submit_orders = order_target_weights({order_book_id: target_weight})

        submit_order = to_submit_orders[0]
        # ============================================================= #
        # step1: test trade correctness                                 #
        # ============================================================= #
        expect_deal_price1 = data_source.get_last_price(
            order_book_id=order_book_id, dt=first_trading_dt)
        print("fisrt date end price: {}".format(expect_deal_price1))
        expect_quantity1 = (STOCK_INIT_CASH *
                            target_weight) / expect_deal_price1
        expect_quantity1 = int(round(expect_quantity1 / 100) * 100)

        print((submit_order.quantity, expect_quantity1))

        expect_deal_money = expect_deal_price1 * expect_quantity1
        expect_commission_fee = expect_deal_money * commission_rate * commission_multiplier
        expect_tax = 0  # no tax rate when buy
        expect_transaction_cost = expect_commission_fee + expect_tax

        state, reward, is_done, info = env.step(action=to_submit_orders)

        true_order = context.tracker._orders[0]
        print(true_order)
        first_trade = context.tracker._trades[0]

        #first_trade = first_trade
        self.assertEqual(first=first_trade["order_book_id"],
                         second=order_book_id)
        self.assertEqual(first=first_trade["trading_datetime"],
                         second=first_trading_dt.strftime("%Y-%m-%d %H:%M:%S"))
        self.assertEqual(first=first_trade["last_price"],
                         second=expect_deal_price1)
        self.assertEqual(first=first_trade["commission"],
                         second=expect_commission_fee)
        self.assertEqual(first=first_trade["tax"], second=expect_tax)
        self.assertEqual(first=first_trade["transaction_cost"],
                         second=expect_transaction_cost)

        # =========================================== #
        # step2.1 test position                       #
        # step2.2 test account                        #
        # step2.3 test portfolio                      #
        # =========================================== #

        # portfolio and accounts change after trading
        #stock account
        expect_market_value_stock = expect_deal_money  #496192
        expect_cash_stock = starting_cash[
            "STOCK"] - expect_market_value_stock - expect_transaction_cost  #502448.2768
        expect_total_value_stock = expect_cash_stock + expect_market_value_stock

        portfolio = Context.get_instance().portfolio
        stock_account = portfolio.stock_account
        position1 = stock_account.get_position(order_book_id,
                                               POSITION_DIRECTION.LONG)
        print(expect_market_value_stock, position1.market_value)
        #pdb.set_trace()
        assert expect_market_value_stock == position1.market_value
        print(expect_market_value_stock, stock_account.market_value)
        print(expect_cash_stock, stock_account.total_cash)
        print(expect_total_value_stock, stock_account.total_value)

        self.assertEqual(expect_market_value_stock, position1.market_value)
        self.assertEqual(expect_market_value_stock, stock_account.market_value)
        self.assertEqual(expect_cash_stock, stock_account.total_cash)
        self.assertEqual(expect_total_value_stock, stock_account.total_value)

        # #future account
        expect_market_value_future = 0
        expect_cash_future = starting_cash["FUTURE"]
        expect_total_value_future = expect_cash_future + expect_market_value_future

        # #portfolio
        expect_cash_portfolio = expect_cash_stock + expect_cash_future
        expect_market_value = expect_market_value_stock + expect_market_value_future
        expect_total_value_portfolio_new = expect_total_value_stock + expect_total_value_future
        print(expect_cash_portfolio, portfolio.cash)
        print(expect_market_value, portfolio.market_value)
        print(expect_total_value_portfolio_new, portfolio.total_value)
        portfolio_record = Context.get_instance().tracker._total_portfolio[0]
        #print(portfolio_record)
        expect_reward = (expect_total_value_portfolio_new /
                         expect_total_value_portfolio) - 1
        print(expect_reward, reward, portfolio.daily_returns)
        #pdb.set_trace()
        self.assertEqual(expect_cash_portfolio, portfolio.cash)
        self.assertEqual(expect_market_value, portfolio.market_value)
        self.assertEqual(expect_total_value_portfolio_new,
                         portfolio.total_value)
        self.assertEqual(expect_reward, reward)

        # ============================================================== #
        # the next dt trade                                              #
        # ============================================================== #
        print(
            "------------the second trading date: {}--------------------------"
            .format(env.trading_dt))
        target_weight = 0.2

        second_trading_dt = trading_dts[1]
        print(second_trading_dt, env.trading_dt)
        self.assertEqual(second_trading_dt, env.trading_dt)

        to_submit_orders = order_target_weights({order_book_id: target_weight})
        order = to_submit_orders[0]
        #pdb.set_trace()

        expect_deal_price = data_source.get_last_price(
            order_book_id=order_book_id, dt=second_trading_dt)
        print("second date end price: {}".format(expect_deal_price))
        #here is the point: we need to get the updated account total value
        expect_deal_price = data_source.get_last_price(
            order_book_id=order_book_id, dt=second_trading_dt)  #69.78

        expect_market_value_stock = expect_quantity1 * expect_deal_price
        expect_cash_stock = expect_cash_stock
        expect_total_value_stock = expect_cash_stock + expect_market_value_stock

        expect_quantity2 = (expect_total_value_stock *
                            target_weight) / expect_deal_price
        expect_quantity2 = int(round(expect_quantity2 / 100) * 100)

        to_trade_quantity = abs(expect_quantity2 - expect_quantity1)
        #expect_quantity = true_order.quantity  #allow not 100 times
        #print((, expetrue_orderct_quantity))
        expect_deal_money = expect_deal_price * to_trade_quantity

        expect_commission_fee = expect_deal_money * commission_rate * commission_multiplier
        expect_tax = expect_deal_money * tax_rate * tax_multiplier  # no tax rate when buy, but charge when sell
        expect_transaction_cost = expect_commission_fee + expect_tax
        #pdb.set_trace()

        state, reward, is_done, info = env.step(action=to_submit_orders)

        true_order = context.tracker._orders[1]
        # if sell, allow the quantity is not 100 times
        print(true_order)
        second_trade = context.tracker._trades[1]
        print(second_trade["order_book_id"], order_book_id)
        print(second_trade["trading_datetime"],
              second_trading_dt.strftime("%Y-%m-%d %H:%M:%S"))
        print(second_trade["last_price"], expect_deal_price)
        print(second_trade["commission"], expect_commission_fee)
        print(second_trade["tax"], expect_tax)
        print(second_trade["transaction_cost"], expect_transaction_cost)
        self.assertEqual(first=second_trade["order_book_id"],
                         second=order_book_id)
        self.assertEqual(
            first=second_trade["trading_datetime"],
            second=second_trading_dt.strftime("%Y-%m-%d %H:%M:%S"))
        self.assertEqual(first=second_trade["last_price"],
                         second=expect_deal_price)
        self.assertEqual(first=second_trade["commission"],
                         second=expect_commission_fee)
        self.assertEqual(first=second_trade["tax"], second=expect_tax)
        self.assertEqual(first=second_trade["transaction_cost"],
                         second=expect_transaction_cost)
        #pdb.set_trace()

        # here is important and think why use settlement price rather the trade price
        # the special case. when sell at the end time of the trading time. the settlement price is equal to the trade price
        expect_cash_stock_settlement = expect_cash_stock + expect_deal_money - expect_transaction_cost
        expect_settlement_price = data_source.get_last_price(
            order_book_id=order_book_id, dt=second_trading_dt)

        position1 = stock_account.get_position(order_book_id,
                                               POSITION_DIRECTION.LONG)
        print(position1)
        #expect_remaining_market_value_stock_settlement = (first_trade["last_quantity"] - second_trade["last_quantity"]) * expect_settlement_price
        expect_market_value_stock_settlement = position1.quantity * expect_settlement_price
        expect_total_value_stock_settlement = expect_cash_stock_settlement + expect_market_value_stock_settlement

        portfolio = Context.get_instance().portfolio
        stock_account = portfolio.stock_account
        position1 = stock_account.get_position(order_book_id,
                                               POSITION_DIRECTION.LONG)
        #expect_reward = (expect_total_value_settlement - self.expect_total_value_settlement) / self.expect_total_value_settlement

        print(expect_market_value_stock_settlement, position1.market_value)
        print(expect_market_value_stock_settlement, stock_account.market_value)
        print(expect_cash_stock_settlement, stock_account.total_cash)
        print(expect_total_value_stock_settlement, stock_account.total_value)
        #pdb.set_trace()
        self.assertEqual(expect_market_value_stock_settlement,
                         position1.market_value)
        self.assertEqual(expect_market_value_stock_settlement,
                         stock_account.market_value)
        self.assertEqual(expect_cash_stock_settlement,
                         stock_account.total_cash)
        self.assertEqual(expect_total_value_stock_settlement,
                         stock_account.total_value)

        expect_cash_future_settlement = starting_cash["FUTURE"]
        expect_market_value_future_settlement = 0
        expect_total_value_future_settlement = expect_cash_future_settlement + expect_market_value_future_settlement

        expect_cash_portfolio_settlement = expect_cash_stock_settlement + expect_cash_future_settlement
        expect_market_value_portfolio_settlement = expect_market_value_stock_settlement + expect_market_value_future_settlement
        expect_total_value_settlement_portfolio_2 = expect_total_value_stock_settlement + expect_total_value_future_settlement

        #pdb.set_trace()
        print(expect_cash_portfolio_settlement, portfolio.cash)
        print(expect_market_value_portfolio_settlement, portfolio.market_value)
        print(expect_total_value_settlement_portfolio_2, portfolio.total_value)
        expect_reward = (expect_total_value_settlement_portfolio_2 /
                         expect_total_value_portfolio_new) - 1
        print(expect_reward, reward, portfolio.daily_returns)
        #pdb.set_trace()
        self.assertEqual(expect_cash_portfolio_settlement, portfolio.cash)
        self.assertEqual(expect_market_value_portfolio_settlement,
                         portfolio.market_value)
        self.assertEqual(expect_total_value_settlement_portfolio_2,
                         portfolio.total_value)
        self.assertEqual(expect_reward, reward)

        print(
            "------------the third trading date: {}--------------------------".
            format(env.trading_dt))
        third_trading_dt = trading_dts[2]
        print(second_trading_dt, env.trading_dt)

        target_weight = 0.0
        to_submit_orders = order_target_weights({order_book_id: target_weight})

        expect_deal_price = data_source.get_last_price(
            order_book_id=order_book_id, dt=third_trading_dt)
        print("third date end price: {}".format(expect_deal_price))  #72.5
        expect_quantity = 0  #allow not 100 times
        to_trade_quantity = expect_quantity2
        # #print((, expetrue_orderct_quantity))
        expect_deal_money = expect_deal_price * to_trade_quantity

        expect_commission_fee = expect_deal_money * commission_rate * commission_multiplier
        expect_tax = expect_deal_money * tax_rate * tax_multiplier  # no tax rate when buy
        expect_transaction_cost = expect_commission_fee + expect_tax

        state, reward, is_done, info = env.step(action=to_submit_orders)

        order = to_submit_orders[0]
        true_order = context.tracker._orders[2]
        # if sell, allow the quantity is not 100 times
        print(true_order)

        third_trade = context.tracker._trades[2]
        print(third_trade["order_book_id"], order_book_id)
        print(third_trade["trading_datetime"],
              second_trading_dt.strftime("%Y-%m-%d %H:%M:%S"))
        print(third_trade["last_price"], expect_deal_price)
        print(third_trade["commission"], expect_commission_fee)
        print(third_trade["tax"], expect_tax)
        print(third_trade["transaction_cost"], expect_transaction_cost)
        #pdb.set_trace()
        self.assertEqual(first=third_trade["order_book_id"],
                         second=order_book_id)
        self.assertEqual(first=third_trade["trading_datetime"],
                         second=third_trading_dt.strftime("%Y-%m-%d %H:%M:%S"))
        self.assertEqual(first=third_trade["last_price"],
                         second=expect_deal_price)
        self.assertEqual(first=third_trade["commission"],
                         second=expect_commission_fee)
        self.assertEqual(first=third_trade["tax"], second=expect_tax)
        self.assertEqual(first=third_trade["transaction_cost"],
                         second=expect_transaction_cost)

        # # here is important and think why use settlement price rather the trade price
        # # the special case. when sell at the end time of the trading time. the settlement price is equal to the trade price
        expect_cash_stock_settlement = expect_cash_stock_settlement + expect_deal_money - expect_transaction_cost
        expect_settlement_price = data_source.get_last_price(
            order_book_id=order_book_id, dt=third_trading_dt)

        position = stock_account.get_position(order_book_id,
                                              POSITION_DIRECTION.LONG)
        print(position)
        expect_market_value_stock_settlement = position.quantity * expect_settlement_price
        print("expect_market_value_stock_settlement: {}".format(
            expect_market_value_stock_settlement))
        #expect_remaining_market_value_stock_settlement = (first_trade["last_quantity"] - second_trade["last_quantity"]) * expect_settlement_price
        expect_total_value_stock_settlement = expect_cash_stock_settlement + expect_market_value_stock_settlement

        portfolio = Context.get_instance().portfolio
        stock_account = portfolio.stock_account
        position1 = stock_account.get_position(order_book_id,
                                               POSITION_DIRECTION.LONG)

        print(expect_market_value_stock_settlement, position1.market_value)
        print(expect_market_value_stock_settlement, stock_account.market_value)
        print("---------------------")
        print(expect_cash_stock_settlement, stock_account.total_cash)
        print(expect_total_value_stock_settlement, stock_account.total_value)
        #pdb.set_trace()
        self.assertEqual(expect_market_value_stock_settlement,
                         position1.market_value)
        self.assertEqual(expect_market_value_stock_settlement,
                         stock_account.market_value)
        self.assertEqual(expect_cash_stock_settlement,
                         stock_account.total_cash)
        self.assertEqual(expect_total_value_stock_settlement,
                         stock_account.total_value)

        expect_cash_future_settlement = starting_cash["FUTURE"]
        expect_market_value_future_settlement = 0
        expect_total_value_future_settlement = expect_cash_future_settlement + expect_market_value_future_settlement

        expect_cash_portfolio_settlement = expect_cash_stock_settlement + expect_cash_future_settlement
        expect_market_value_portfolio_settlement = expect_market_value_stock_settlement + expect_market_value_future_settlement
        expect_total_value_settlement_portfolio = expect_total_value_stock_settlement + expect_total_value_future_settlement

        print(expect_cash_portfolio_settlement, portfolio.cash)
        print(expect_market_value_portfolio_settlement, portfolio.market_value)
        print(expect_total_value_settlement_portfolio, portfolio.total_value)
        expect_reward = (expect_total_value_settlement_portfolio /
                         expect_total_value_settlement_portfolio_2) - 1
        print(expect_reward, reward, portfolio.daily_returns)
        print(portfolio.cash, portfolio.market_value, portfolio.total_value)
        #pdb.set_trace()
        self.assertEqual(expect_cash_portfolio_settlement, portfolio.cash)
        self.assertEqual(expect_market_value_portfolio_settlement,
                         portfolio.market_value)
        self.assertEqual(expect_total_value_settlement_portfolio,
                         portfolio.total_value)
        self.assertEqual(expect_reward, reward)
Beispiel #7
0
    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)
        feature_df, price_s = create_toy_feature(order_book_ids_number=1,
                                                 feature_number=3,
                                                 random_seed=111)
        data_source = DataSource(feature_df=feature_df, price_s=price_s)
        #order_book_ids  = data_source.get_available_order_book_ids()

        self.data_source = DataSource(feature_df=feature_df, price_s=price_s)

        self.order_book_ids = self.data_source.get_available_order_book_ids()

        self.look_backward_window = 2

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

        self.commission_rate = 0.0005
        self.tax_rate = 0.001

        self.commission_multiplier = 1
        self.min_commission = 5
        self.tax_multiplier = 1

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

        context = Context.get_instance()

        #stock account
        expect_market_value_stock = 0
        expect_cash_stock = self.starting_cash["STOCK"]
        expect_total_value_stock = expect_cash_stock + expect_market_value_stock

        #future account
        expect_market_value_future = 0
        expect_cash_future = self.starting_cash["FUTURE"]
        expect_total_value_future = expect_cash_future + expect_market_value_future
        self.expect_total_value_future = expect_total_value_future
        #portfolio
        expect_total_value = expect_total_value_stock + expect_total_value_future

        self.trading_dts = context.get_available_trading_dts()
        first_trading_dt = self.trading_dts[0]
        self.order_book_id = self.order_book_ids[0]

        print("-------------first_trading_dt: {}----------".format(
            context.trading_dt))
        to_submit_orders = order_target_weights({self.order_book_id: 0.5})
        order = to_submit_orders[0]
        expect_deal_price = self.data_source.get_last_price(
            order_book_id=self.order_book_id, dt=first_trading_dt)
        expect_deal_money = expect_deal_price * order.quantity

        expect_commission_fee = expect_deal_money * self.commission_rate * self.commission_multiplier
        expect_tax = 0  # no tax rate when buy
        expect_transaction_cost = expect_commission_fee + expect_tax

        #pdb.set_trace()
        state, reward, is_done, info = self.env.step(action=to_submit_orders)

        first_trade = context.tracker._trades[0]

        #pdb.set_trace()
        self.first_trade = first_trade
        self.assertEqual(first=first_trade["order_book_id"],
                         second=self.order_book_id)
        self.assertEqual(first=first_trade["trading_datetime"],
                         second=first_trading_dt.strftime("%Y-%m-%d %H:%M:%S"))
        self.assertEqual(first=first_trade["last_price"],
                         second=expect_deal_price)
        self.assertEqual(first=first_trade["commission"],
                         second=expect_commission_fee)
        self.assertEqual(first=first_trade["tax"], second=expect_tax)
        self.assertEqual(first=first_trade["transaction_cost"],
                         second=expect_transaction_cost)

        # portfolio and accounts change after trading
        #stock account
        expect_market_value_stock = expect_deal_money
        expect_cash_stock = self.starting_cash[
            "STOCK"] - expect_market_value_stock - expect_transaction_cost  #502448.2768
        #expect_total_value_stock = expect_cash_stock + expect_market_value_stock

        #future account
        #expect_market_value_future = 0
        #expect_cash_future = self.starting_cash["FUTURE"]
        #expect_total_value_future = expect_cash_future + expect_market_value_future

        #portfolio
        #expect_total_value = expect_total_value_stock + expect_total_value_future

        # settlement on the next bar
        next_trading_dt = self.trading_dts[1]
        expect_settlement_price = self.data_source.get_last_price(
            order_book_id=self.order_book_id, dt=next_trading_dt)
        expect_market_value_stock_settlement = first_trade[
            "last_quantity"] * expect_settlement_price
        self.expect_cash_stock_settlement = expect_cash_stock
        self.expect_total_value_stock_settlement = self.expect_cash_stock_settlement + expect_market_value_stock_settlement

        self.expect_total_value_future_settlement = expect_total_value_future

        self.expect_total_value_settlement = self.expect_total_value_stock_settlement + self.expect_total_value_future_settlement

        expect_reward = (self.expect_total_value_settlement -
                         expect_total_value) / expect_total_value
        self.assertAlmostEqual(first=reward, second=expect_reward)
    init_allocation = 1 / order_book_ids_num
    target_weight_dict = {x: init_allocation for x in order_book_ids}
    return target_weight_dict


NOT_TRADE_FLAG = True

while True:
    print("the current trading_dt is: {}".format(env.trading_dt))
    #action = risk_parity_strategy(state)
    if NOT_TRADE_FLAG:
        order_book_ids = state.index.get_level_values(0).drop_duplicates()
        target_weight_dict = equal_weight_buy_and_hold_strategy(
            list(order_book_ids))
        print(target_weight_dict)
        action = order_target_weights(target_weight_dict)

        next_state, reward, done, info = env.step(action)

        NOT_TRADE_FLAG = False
    else:
        # the action can be set to None, means keep the previous position unchange and do nothing.
        next_state, reward, done, info = env.step(action=None)
    print("the reward of this action: {}".format(reward))
    print("the next state is \n {}".format(next_state))
    if done:
        break
    else:
        state = next_state
env.render(auto_open=False)