def publish_trade_fills(self,fills,ammended_orders,verbose=False): for fill,ao in zip(fills,ammended_orders): #send fill to trader message=Message(too=fill.tid,fromm=self.name,subject='Fill',order=fill,time=self.time) self.send(message) if ao is not None: #send accompanying ammendment notice if needed message=Message(too=ao.tid,fromm=self.name,subject='Ammend',order=ao,time=self.time) self.send(message)
def action_converter(self, action_num, auto_cancel=True): #sometimes we want agent to execute more than one order in a period if isinstance(action_num, (tuple, int, np.int64)): action_list = [action_num] elif type(action_num) == list: action_list = action_num else: print('unknown action number type', action_num, type(action_num)) raise AssertionError for action_num in action_list: new_order = self.action_dic[action_num].do(self.lob, auto_cancel=auto_cancel) if new_order is not None: #this informs RL_trader of correct qid, does the bookkeeping. message = Message(too=self.sess.exchange.name, fromm=self.trader.tid, subject='New Exchange Order', order=new_order, time=self.time) self.trader.send(message) #need to refresh lob after each trade self.add_lob(self.sess.exchange.publish_lob())
def cancel_with_exchange(self,order=None,verbose=False): #trader contacts exchange directly to inform of cancellation #if order_to_cancel is None: order_to_cancel=self.orders_dic_hist[oid]['submitted_quotes'][-1] if verbose : print('killing lastquote=%s' % order) message=Message(too=self.exchange.name,fromm=self.tid,subject='Cancel Order',order=order,time=self.time) self.send(message)
def do_dispatch(self, order, cancellations=None, verbose=False): tname = order.tid message = Message(too=tname, fromm=self.name, subject='New Customer Order', order=order, time=self.time) self.send(message) if verbose: print('Customer order: %s %s' % (response[0], order)) return cancellations
def get_orders_from_traders(self): #get the traders with orders this period period_trader=self.picked_traders[self.time] if len(period_trader)>0: #get the noisy signal assigned to those trader noise_signals=self.noise_dic[self.time] #get specific signal for trader noise=noise_signals[period_trader] #send the noise signal and order prompt to trader message=Message(too=period_trader,fromm=self.name,order=noise,time=self.time,subject='Prompt_Order') self.messenger.send(message)
def receive_message(self,message): if message.subject=='New Customer Order': customer_order=message.order (response,oldest_trade_dic)=self.add_order(customer_order,inform_exchange=True,verbose=False) if response=='LOB_Cancel': message=Message(too='SD',fromm=self.tid,subject='Replace',order=oldest_trade_dic,time=self.time) self.send(message) #print(f'receive NCO {message}') elif message.subject=='Get Order': lob=message.order self.getOrderReplace(time=self.time, lob=lob) elif message.subject=='Cancel Customer': cancel_oid=message.order.oid self.del_order( cancel_oid,'cancel') #if there was partial execution send the confirm if cancel_oid in self.blotter: self.send_confirm(self.exec_summary(oid)) elif message.subject=='Confirm': confirm_order=message.order qid=message.order.qid self.add_order_exchange(confirm_order,qid) elif message.subject=='Fill': fill=message.order self.bookkeep(fill) elif message.subject=='Ammend': ammend_order=message.order qid=ammend_order.qid self.add_order_exchange(ammend_order,qid) else: print(f'Unknown message subject {message.subject}') raise UnknownMessageSubject
def simulate_one_period(self, recording=False): new_orders = self.sd.order_dic[self.time] picked_traders = self.traders_picked[self.time] if len(new_orders) > 0: for new_order in new_orders: self.sd.do_dispatch(new_order) #prompt chosen trader for trade for tid in picked_traders: message = Message(fromm=self.name, too=tid, time=self.time, order=self.lob, subject='Get Order') self.messenger.send(message) #get last trade if one happened self._get_last_trade() self.lob = self._traders_respond(self.trade) if recording: self.replay_vars[self.time] = self.lob
def publish_qid(self,order,verbose=False): message=Message(too=order.tid,fromm=self.name,subject='Confirm',order=copy.deepcopy(order),time=self.time) if verbose: print(message) self.send(message)
def send_confirm(self,confirm): message=Message(fromm=self.name,too='SD',subject='Exec Confirm',time=self.time,order=confirm) self.send(message)
def _send_new_order_exchange(self,new_order): assert type(new_order)==Order message=Message(too=self.exchange.name,fromm=self.tid,subject='New Exchange Order',order=new_order,time=self.time) self.send(message)