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
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