コード例 #1
0
 def execute_order(self, order: Order, slippage=0,slippage_rate=0.0, execute_type: ExecuteType = ExecuteType.EXECUTE_ALL_UNITS):
     if order is None or order.trade_unit == 0: return
     # if execute_type == ExecuteType.EXECUTE_ALL_UNITS:
     order.trade_all_unit(slippage=slippage,slippage_rate=slippage_rate)
     # elif execute_type == ExecuteType.EXECUTE_WITH_MAX_VOLUME:
     #     order.trade_with_current_volume(int(self.trading_volume()), slippage)
     # else:
     #     return
     execution_record: pd.Series = order.execution_res
     # calculate margin requirement
     # margin_requirement = 0.0
     margin_requirement = self.get_initial_margin(order.long_short) * execution_record[Util.TRADE_UNIT]
     if self.fee_per_unit is None:
         # 百分比手续费
         transaction_fee = execution_record[Util.TRADE_PRICE] * self.fee_rate * execution_record[
             Util.TRADE_UNIT] * self._multiplier
     else:
         # 每手手续费
         transaction_fee = self.fee_per_unit * execution_record[Util.TRADE_UNIT]
     execution_record[Util.TRANSACTION_COST] += transaction_fee
     transaction_fee_add_to_price = transaction_fee / (execution_record[Util.TRADE_UNIT] * self._multiplier)
     execution_record[Util.TRADE_PRICE] += execution_record[
                                               Util.TRADE_LONG_SHORT].value * transaction_fee_add_to_price
     position_value = order.long_short.value * execution_record[Util.TRADE_PRICE] * execution_record[
         Util.TRADE_UNIT] * self._multiplier
     execution_record[Util.TRADE_BOOK_VALUE] = position_value
     execution_record[Util.TRADE_MARGIN_CAPITAL] = margin_requirement
     execution_record[Util.TRADE_MARKET_VALUE] = position_value
     return execution_record
コード例 #2
0
 def execute_order(self, order: Order, slippage=0, execute_type: ExecuteType = ExecuteType.EXECUTE_ALL_UNITS):
     if order is None: return
     if execute_type == ExecuteType.EXECUTE_ALL_UNITS:
         order.trade_all_unit(slippage)
     elif execute_type == ExecuteType.EXECUTE_WITH_MAX_VOLUME:
         order.trade_with_current_volume(int(self.trading_volume()), slippage)
     else:
         return
     execution_record: pd.Series = order.execution_res
     # calculate margin requirement
     margin_requirement = 0.0
     if self.fee_per_unit is None:
         # 百分比手续费
         transaction_fee = execution_record[Util.TRADE_PRICE] * self.fee_rate * execution_record[
             Util.TRADE_UNIT] * self._multiplier
     else:
         # 每手手续费
         transaction_fee = self.fee_per_unit * execution_record[Util.TRADE_UNIT]
     execution_record[Util.TRANSACTION_COST] += transaction_fee
     transaction_fee_add_to_price = transaction_fee / (execution_record[Util.TRADE_UNIT] * self._multiplier)
     execution_record[Util.TRADE_PRICE] += execution_record[
                                               Util.TRADE_LONG_SHORT].value * transaction_fee_add_to_price
     position_size = order.long_short.value * execution_record[Util.TRADE_PRICE] * execution_record[
         Util.TRADE_UNIT] * self._multiplier
     execution_record[
         Util.TRADE_BOOK_VALUE] = position_size  # 头寸规模(含多空符号),例如,空一手豆粕(3000点,乘数10)得到头寸规模为-30000,而建仓时点头寸市值为0。
     execution_record[Util.TRADE_MARGIN_CAPITAL] = margin_requirement
     # execution_record[
     #     Util.TRADE_MARKET_VALUE] = position_size  # Init value of a future trade is ZERO, except for transaction cost.
     return execution_record
コード例 #3
0
ファイル: base_option.py プロジェクト: wgcgxp/OptionStrategy
 def execute_order(self, order: Order, slippage=0, execute_type: ExecuteType = ExecuteType.EXECUTE_ALL_UNITS) -> pd.Series:
     if order is None or order.trade_unit==0: return
     if execute_type == ExecuteType.EXECUTE_ALL_UNITS:
         order.trade_all_unit(slippage)
     elif execute_type == ExecuteType.EXECUTE_WITH_MAX_VOLUME:
         order.trade_with_current_volume(int(self.trading_volume()), slippage)
     else:
         return
     execution_record: pd.Series = order.execution_res
     if order.long_short == LongShort.LONG:
         # 无保证金交易的情况下,trade_market_value有待从现金账户中全部扣除。
         execution_record[Util.TRADE_MARGIN_CAPITAL] = 0.0
         execution_record[Util.TRADE_MARKET_VALUE] = execution_record[Util.TRADE_UNIT] * \
                                                     execution_record[Util.TRADE_PRICE] * self.multiplier()
     else:
         execution_record[Util.TRADE_MARGIN_CAPITAL] = self.get_initial_margin(order.long_short) * \
                                                       execution_record[Util.TRADE_UNIT]
         execution_record[Util.TRADE_MARKET_VALUE] = 0.0
     if self.fee_per_unit is None:
         # 百分比手续费
         transaction_fee = execution_record[Util.TRADE_PRICE] * self.fee_rate * execution_record[
             Util.TRADE_UNIT] * self.multiplier()
     else:
         # 每手手续费
         transaction_fee = self.fee_per_unit * execution_record[Util.TRADE_UNIT]
     execution_record[Util.TRANSACTION_COST] += transaction_fee
     transaction_fee_add_to_price = transaction_fee / (execution_record[Util.TRADE_UNIT] *
                                                       self.multiplier())
     execution_record[Util.TRADE_PRICE] += execution_record[Util.TRADE_LONG_SHORT].value \
                                           * transaction_fee_add_to_price
     position_size = order.long_short.value * execution_record[Util.TRADE_PRICE] * \
                     execution_record[Util.TRADE_UNIT] * self.multiplier()
     execution_record[
         Util.TRADE_BOOK_VALUE] = position_size  # 头寸规模(含多空符号),例如,空一手豆粕(3000点,乘数10)得到头寸规模为-30000,而建仓时点头寸市值为0。
     return execution_record
コード例 #4
0
    def shift_contract_month(self,
                             account,
                             slippage_rate,
                             cd_price=CdTradePrice.VOLUME_WEIGHTED):
        # 移仓换月: 成交量加权均价
        if self.id_future != self.current_state[Util.ID_FUTURE]:
            for holding in account.dict_holding.values():
                if isinstance(holding, BaseFutureCoutinuous):
                    # close previous contract
                    df = self.df_all_futures_daily[
                        (self.df_all_futures_daily[Util.DT_DATE] ==
                         self.eval_date) & (self.df_all_futures_daily[
                             Util.ID_FUTURE] == self.id_future)]
                    trade_unit = account.trade_book.loc[self.name_code(),
                                                        Util.TRADE_UNIT]
                    position_direction = account.trade_book.loc[
                        self.name_code(), Util.TRADE_LONG_SHORT]
                    if position_direction == LongShort.LONG:
                        long_short = LongShort.SHORT
                    else:
                        long_short = LongShort.LONG
                    if cd_price == CdTradePrice.VOLUME_WEIGHTED:
                        trade_price = df[Util.AMT_TRADING_VALUE].values[
                            0] / df[Util.AMT_TRADING_VOLUME].values[
                                0] / self.multiplier()
                    else:
                        trade_price = df[Util.AMT_CLOSE].values[0]
                    order = Order(holding.eval_date, self.name_code(),
                                  trade_unit, trade_price,
                                  holding.eval_datetime, long_short)
                    record = self.execute_order(order,
                                                slippage_rate=slippage_rate)

                    account.add_record(record, holding)

                    # open
                    trade_price = self.mktprice_volume_weighted()
                    order = Order(holding.eval_date, self.name_code(),
                                  trade_unit, trade_price,
                                  holding.eval_datetime, position_direction)
                    record = self.execute_order(order,
                                                slippage_rate=slippage_rate)
                    account.add_record(record, holding)
            self.id_future = self.current_state[Util.ID_FUTURE]
            return True
        else:
            return False
コード例 #5
0
 def shift_contract_by_VWAP(self, id_c1: str, id_c2: str, hold_unit: int,
                            open_unit: int, long_short: LongShort, slippage,
                            execute_type):
     if long_short == LongShort.LONG:
         close_order_long_short = LongShort.SHORT
     else:
         close_order_long_short = LongShort.LONG
     close_order = Order(dt_trade=self.eval_date,
                         id_instrument=id_c1,
                         trade_unit=hold_unit,
                         trade_price=None,
                         time_signal=self.eval_datetime,
                         long_short=close_order_long_short)
     # TODO: OPEN ORDER UNIT SHOULD BE RECALCULATED BY DELTA.
     open_order = Order(dt_trade=self.eval_date,
                        id_instrument=id_c2,
                        trade_unit=open_unit,
                        trade_price=None,
                        time_signal=self.eval_datetime,
                        long_short=long_short)
     if self.frequency in Util.LOW_FREQUENT:
         return
     else:
         df_c1_today = self.df_all_futures_daily[
             (self.df_all_futures_daily[Util.DT_DATE] == self.eval_date)
             & (self.df_all_futures_daily[Util.ID_INSTRUMENT] == id_c1)]
         total_trade_value_c1 = df_c1_today[
             Util.AMT_TRADING_VALUE].values[0]
         total_volume_c1 = df_c1_today[Util.AMT_TRADING_VOLUME].values[0]
         # volume_weighted_price_c1 = total_trade_value_c1 / (total_volume_c1 * self.multiplier())
         price_c1 = (df_c1_today[Util.AMT_CLOSE].values[0] +
                     df_c1_today[Util.AMT_OPEN].values[0]) / 2.0
         total_trade_value_c2 = 0.0
         total_volume_c2 = 0.0
         while not self.is_last_minute():
             total_trade_value_c2 += self.mktprice_close(
             ) * self.trading_volume() * self.multiplier()
             total_volume_c2 += self.trading_volume()
             self.next()
         total_trade_value_c2 += self.mktprice_close(
         ) * self.trading_volume() * self.multiplier()
         total_volume_c2 += self.trading_volume()
         volume_weighted_price_c2 = total_trade_value_c2 / (
             total_volume_c2 * self.multiplier())
         # close_order.trade_price = volume_weighted_price_c1
         close_order.trade_price = price_c1
         open_order.trade_price = volume_weighted_price_c2
         close_execution_record = self.execute_order(
             close_order, slippage, execute_type)
         open_execution_record = self.execute_order(open_order, slippage,
                                                    execute_type)
         return close_execution_record, open_execution_record
コード例 #6
0
 def execute_order_by_VWAP(
         self,
         order: Order,
         slippage=0,
         execute_type: ExecuteType = ExecuteType.EXECUTE_ALL_UNITS):
     if self.frequency in Util.LOW_FREQUENT:
         return
     else:
         total_trade_value = 0.0
         total_volume_value = 0.0
         while not self.is_last_minute():
             total_trade_value += self.mktprice_close(
             ) * self.trading_volume()
             total_volume_value += self.trading_volume()
             self.next()
         total_trade_value += self.mktprice_close() * self.trading_volume()
         total_volume_value += self.trading_volume()
         volume_weighted_price = total_trade_value / total_volume_value
         order.trade_price = volume_weighted_price
         execution_record = self.execute_order(order, slippage,
                                               execute_type)
         return execution_record
コード例 #7
0
                    (hedging.df_all_futures_daily[c.Util.DT_DATE] ==
                     hedging.eval_date) & (hedging.df_all_futures_daily[
                         c.Util.ID_FUTURE] == id_future)]
                print('移仓:')
                print(df)
                print(id_future, hedging.current_state[c.Util.ID_FUTURE])
                trade_unit = account.trade_book.loc[hedging.name_code(),
                                                    c.Util.TRADE_UNIT]
                if account.trade_book.loc[
                        hedging.name_code(),
                        c.Util.TRADE_LONG_SHORT] == c.LongShort.LONG:
                    long_short = c.LongShort.SHORT
                else:
                    long_short = c.LongShort.LONG
                order = Order(holding.eval_date, hedging.name_code(),
                              trade_unit, df[c.Util.AMT_CLOSE].values[0],
                              holding.eval_datetime, long_short)
                record = hedging.execute_order(order, slippage=slippage)
                account.add_record(record, holding)
        hedging.synthetic_unit = 0
        id_future = hedging.current_state[c.Util.ID_FUTURE]
        flag_hedge = True

    # 触发平仓信号
    if not empty_position:
        moneyness_put = optionset.get_option_moneyness(atm_put)
        moneyness_call = optionset.get_option_moneyness(atm_call)
        if close_signal(optionset.eval_date, maturity1, df_iv_stats):
            for option in account.dict_holding.values():
                order = account.create_close_order(option)
                record = option.execute_order(order, slippage=slippage)
コード例 #8
0
ファイル: base_product.py プロジェクト: wgcgxp/OptionStrategy
 def open_short(self, order: Order) -> bool:  # 空开
     order.trade_type = TradeType.OPEN_SHORT
     return self.execute_order(order)
コード例 #9
0
ファイル: base_product.py プロジェクト: wgcgxp/OptionStrategy
 def open_long(self, order: Order) -> bool:  # 多开
     order.trade_type = TradeType.OPEN_LONG
     return self.execute_order(order)