def new_order_message(client, **kwargs): """ Generates a new order message. Arguments: Returns: Raises: ValueError """ # Required parameters for sym in ['symbol', 'side', 'order_type']: if sym not in kwargs: raise ValueError("{0} must have a value".format(sym)) # optional parameters extra_tags = kwargs.get('extra_tags', []) return FIXMessage(source=[ (35, FIX.NEWORDER_SINGLE), (49, client.sender_compid), (56, client.target_compid), (11, client.get_next_orderid()), (21, '1'), # handlInst (55, kwargs['symbol']), (54, kwargs['side']), (60, format_time(datetime.datetime.now())), (40, kwargs['order_type']), ] + extra_tags)
def prepare_send_message(self, message): """ Sends a message via the transport. """ self._send_seqno += 1 send_time = datetime.datetime.now() # update some of the required fields for sending message[8] = self.link_config['protocol_version'] message[34] = self._send_seqno message[52] = format_time(send_time) # apply any common fields if 'common_fields' in self.link_config: for tag, value in self.link_config['common_fields']: message[tag] = value # verify required tags for tag in self.link_config['required_fields']: # these two tags are updated when generating the binary if tag in {9, 10}: continue if tag not in message or len(str(message[tag])) == 0: raise FIXDataError(tag, 'missing field: id:{0}'.format(tag)) self._last_send_time = send_time return message
def on_timer_tick_received(self): """ This is the Twisted timer callback when the heartbeat interval has elapsed. """ if self.heartbeat <= 0: return now = datetime.datetime.now() # Have we received a testrequest response before we timed out? if (self._testrequest_id is not None and (now - self._testrequest_time).seconds > 2*self.heartbeat): raise FIXTimeoutError('testrequest response timeout') # if heartbeat seconds/2 have elapsed since the last time # a message was sent, send a heartbeat if (now - self._last_send_time).seconds > (self.heartbeat/2): self.transport.send_message( FIXMessage(source=[(35, FIX.HEARTBEAT), (49, self.link_config['sender_compid']), (56, self.link_config['target_compid'])])) # if heartbeat seconds + "some transmission time" have elapsed # since a message was received, send a TestRequest if (now - self._last_received_time).seconds > self.heartbeat: testrequest_id = "TR{0}".format(format_time(now)) self._testrequest_time = now self._testrequest_id = testrequest_id self.transport.send_message( FIXMessage(source=[(35, FIX.TEST_REQUEST), (112, testrequest_id), (49, self.link_config['sender_compid']), (56, self.link_config['target_compid'])]))