def close_tradepos(self, idx, tradepos, price): valid_time = self.agent.tick_id + self.trade_valid_time insts = tradepos.insts nAsset = len(insts) trade_vol = [-v * tradepos.pos for v in tradepos.volumes] order_type = [self.order_type] * nAsset if (self.order_type == OPT_LIMIT_ORDER) and (nAsset > 1): order_type[-1] = OPT_MARKET_ORDER conv_f = [self.agent.instruments[inst].multiple for inst in insts] etrade = order.ETrade( insts, trade_vol, order_type, -price*tradepos.direction, [self.num_tick] * nAsset, \ valid_time, self.name, self.agent.name, conv_f[-1]*abs(tradepos.pos), conv_f) tradepos.exit_tradeid = etrade.id self.submitted_trades[idx].append(etrade) return
def open_tradepos(self, idx, direction, price): valid_time = self.agent.tick_id + self.trade_valid_time insts = self.underliers[idx] nAsset = len(insts) trade_vol = [ v * self.trade_unit[idx] * direction for v in self.volumes[idx] ] order_type = [self.order_type] * nAsset if (self.order_type == OPT_LIMIT_ORDER) and (nAsset > 1): order_type[-1] = OPT_MARKET_ORDER conv_f = [ self.agent.instruments[inst].multiple for inst in insts ] etrade = order.ETrade( insts, trade_vol, order_type, price * direction, [self.num_tick] * nAsset, \ valid_time, self.name, self.agent.name, conv_f[-1]*self.trade_unit[idx], conv_f) tradepos = self.pos_class(insts, self.volumes[idx], direction * self.trade_unit[idx], \ price, price, conv_f[-1]*self.trade_unit[idx], **self.pos_args) tradepos.entry_tradeid = etrade.id self.submitted_trades[idx].append(etrade) self.positions[idx].append(tradepos) return
def on_trade(self, etrade): save_status = False under_key = '_'.join(sorted(etrade.instIDs)) idx = self.under2idx[under_key] entry_ids = [tp.entry_tradeid for tp in self.positions[idx]] exit_ids = [tp.exit_tradeid for tp in self.positions[idx]] i = 0 if etrade.id in entry_ids: i = entry_ids.index(etrade.id) is_entry = True elif etrade.id in exit_ids: i = exit_ids.index(etrade.id) is_entry = False else: self.logger.warning( 'the trade %s is in status = %s but not found in the strat=%s tradepos table' % (etrade.id, etrade.status, self.name)) etrade.status = order.ETradeStatus.StratConfirm return tradepos = self.positions[idx][i] if etrade.status == order.ETradeStatus.Done: traded_price = etrade.final_price() if is_entry: tradepos.open(traded_price, datetime.datetime.now()) self.logger.info( 'strat %s successfully opened a position on %s after tradeid=%s is done, trade status is changed to confirmed' % (self.name, '_'.join(sorted(tradepos.insts)), etrade.id)) etrade.status = order.ETradeStatus.StratConfirm self.num_entries[idx] += 1 else: tradepos.close(traded_price, datetime.datetime.now()) self.save_closed_pos(tradepos) self.logger.info( 'strat %s successfully closed a position on %s after tradeid=%s is done, the closed trade position is saved' % (self.name, '_'.join(sorted(tradepos.insts)), etrade.id)) etrade.status = order.ETradeStatus.StratConfirm self.num_exits[idx] += 1 elif etrade.status == order.ETradeStatus.Cancelled: if self.pos_exec_flag == STRAT_POS_CANCEL: if is_entry: tradepos.cancel_open() self.logger.info( 'strat %s cancelled an open position on %s after tradeid=%s is cancelled. Both the trade and the position will be removed.' % (self.name, '_'.join(sorted( tradepos.insts)), etrade.id)) etrade.status = order.ETradeStatus.StratConfirm else: tradepos.cancel_close() self.logger.info( 'strat %s cancelled closing a position on %s after tradeid=%s is cancelled. The position is still open.' % (self.name, '_'.join(sorted( tradepos.insts)), etrade.id)) etrade.status = order.ETradeStatus.StratConfirm if self.pos_exec_flag == STRAT_POS_MUST_EXEC: etrade.status = order.ETradeStatus.StratConfirm valid_time = self.agent.tick_id + self.trade_valid_time new_trade = order.ETrade( etrade.instIDs, etrade.volumes, etrade.order_types, etrade.limit_price, etrade.slip_ticks, \ valid_time, self.name, self.agent.name, etrade.price_unit, etrade.conv_f) if is_entry: tradepos.entry_tradeid = new_trade.id else: tradepos.exit_tradeid = new_trade.id self.submitted_trades[idx].append(new_trade) self.positions[idx] = [ tradepos for tradepos in self.positions[idx] if not tradepos.is_closed ] self.submitted_trades[idx] = [ etrade for etrade in self.submitted_trades[idx] if etrade.status != order.ETradeStatus.StratConfirm ] self.save_state()