def get_executions(args): """Gets all (filtered) executions from last 24hrs """ client = connection.get_client() if client is None: return g.error_resp[-2] elif client.isConnected() is False: return g.error_resp[-1] g.executions_resp = dict(execDetailsEnd=False, execDetails=[], commissionReport=dict()) log.debug('Requesting executions for filter {}'.format(args)) filter = ExecutionFilter() for attr in dir(filter): if attr[:2] == 'm_' and attr[2:] in args: setattr(filter, attr, args[attr[2:]]) log.debug('Filter: {}'.format(filter.__dict__)) filter.m_clientId = 0 client.reqExecutions(1, filter) timeout = g.timeout / 2 while g.executions_resp['execDetailsEnd'] is False and client.isConnected( ) is True and timeout > 0: # log.debug("Waiting for responses on client {}...".format(client.clientId)) time.sleep(.25) timeout -= 1 log.debug('Current executions {}'.format(g.executions_resp)) connection.close_client(client) return g.executions_resp
def exec_filter(client_id): contract = make_contract('NVDA') filt = ExecutionFilter() filt.m_clientId = client_id filt.m_symbol = contract.m_symbol filt.m_secType = contract.m_secType filt.m_exchange = contract.m_exchange return filt
def _download_account_details(self): exec_filter = ExecutionFilter() exec_filter.m_clientId = self.client_id self.reqExecutions(self.next_request_id, exec_filter) self.reqManagedAccts() while self.managed_accounts is None: sleep(_poll_frequency) for account in self.managed_accounts: self.reqAccountUpdates(subscribe=True, acctCode=account) while self.accounts_download_complete is False: sleep(_poll_frequency)
def _download_account_details(self): exec_filter = ExecutionFilter() exec_filter.m_clientId = self.client_id self.reqExecutions(self.next_request_id, exec_filter) self.reqManagedAccts() while self.managed_accounts is None: sleep(_poll_frequency) for account in self.managed_accounts: self.reqAccountUpdates(subscribe=True, acctCode=account) while self.accounts_download_complete is False: sleep(_poll_frequency)
def testExecutions(): print 'testing executions' f = ExecutionFilter() #f.m_clientId = 101 f.m_time = '20110901-00:00:00' f.m_symbol = 'SPY' f.m_secType = 'STK' f.m_exchange = 'SMART' #f.m_side = 'BUY' con.reqExecutions(f) sleep(2)
def get_transactions(self, event, orders): import time time.sleep(1) efilter = ExecutionFilter() efilter.m_symbol = event.sid for order in orders: # Todo: I need to refactor how executions are summoned, this is currently a huge bottleneck # cycle through all executions matching the event sid for execution in self.executions(efilter): prior_execution = None # further filter out any executions not matching the order.id if execution.m_orderRef == order.id: # prevent processing of duplicate executions if execution != prior_execution: order_status_vals = (0, 0) # cycle through the order status messages to get transaction details for status in self.order_status(execution.m_orderId): # filter out duplicate transaction messages if (status['remaining'], status['filled']) != order_status_vals: # get execution date date = dt.datetime.strptime( execution.m_time, '%Y%m%d %H:%M:%S').replace(tzinfo=pytz.utc) amount = status['filled'] - order_status_vals[1] txn = { 'sid': event.sid, 'amount': int(amount), 'dt': date, 'price': status['lastFillPrice'], 'order_id': order.id } transaction = Transaction(**txn) order_status_vals = (status['remaining'], status['filled']) #TODO: pretty sure there is still transactions are being duplicated still if order.status == ORDER_STATUS.OPEN: yield order, transaction prior_execution = execution
def get_transactions(self, event, orders): import time time.sleep(1) efilter = ExecutionFilter() efilter.m_symbol = event.sid for order in orders: # Todo: I need to refactor how executions are summoned, this is currently a huge bottleneck # cycle through all executions matching the event sid for execution in self.executions(efilter): prior_execution = None # further filter out any executions not matching the order.id if execution.m_orderRef == order.id: # prevent processing of duplicate executions if execution != prior_execution: order_status_vals = (0, 0) # cycle through the order status messages to get transaction details for status in self.order_status(execution.m_orderId): # filter out duplicate transaction messages if (status["remaining"], status["filled"]) != order_status_vals: # get execution date date = dt.datetime.strptime(execution.m_time, "%Y%m%d %H:%M:%S").replace( tzinfo=pytz.utc ) amount = status["filled"] - order_status_vals[1] txn = { "sid": event.sid, "amount": int(amount), "dt": date, "price": status["lastFillPrice"], "order_id": order.id, } transaction = Transaction(**txn) order_status_vals = (status["remaining"], status["filled"]) # TODO: pretty sure there is still transactions are being duplicated still if order.status == ORDER_STATUS.OPEN: yield order, transaction prior_execution = execution
def get_execution(self, executionFilter_=ExecutionFilter()): """ Getting the details of the execution within the past 24 hours. Commission reports as well An executionFilter needs to be passed in """ # First step is to define a dictionary will will be populated by IB API con = ibConnection(port=4001, clientId=999) # Registering Handlers #con.registerAll(self.watcher) con.register(self.my_execution_handler, message.execDetails) con.register(self.my_commission_handler, message.commissionReport) con.register(self.my_execution_end, message.execDetailsEnd) # Setting up the connection con.connect() # Cleaning up the this_execution first if not self.this_execution.empty: self.this_execution.drop(range(self.this_execution.shape[0]), inplace=True) # Cleaning up the this_execution first if not self.this_commission.empty: self.this_commission.drop(range(self.this_commission.shape[0]), inplace=True) # Asking the execution details #filter_in = self.make_exec_filter(secType = 'BAG') con.reqExecutions(1, executionFilter_) sleep(5) # Disconnecting print('Disconnected', con.disconnect())
def update_orders(self, orders): requestId = self._get_next_request_id() exf = ExecutionFilter() distribution = {} self._connection.reqExecutions(requestId, exf) # while not self._wrapper.isExecutionRequestFinished(requestId): err = self._wrapper.getError(requestId) if err is not None: raise Exception(err) very_short_sleep() executions = self._wrapper.getExecutions(requestId) for order in orders: price = 0 shares = 0 if executions is not None: for execution in executions: if execution.m_shares > 0 and execution.m_orderId == order.Order_Id and not execution.m_acctNumber.startswith('DF'): price = execution.m_price if order.Symbol not in distribution: distribution[order.Symbol] = {} if execution.m_acctNumber not in distribution[order.Symbol]: distribution[order.Symbol][execution.m_acctNumber] = 0 distribution[order.Symbol][execution.m_acctNumber] += execution.m_shares shares += execution.m_shares if price != 0: order.setFills(price, shares) return distribution
def get_executions(self, efilter=None): if not efilter: efilter = ExecutionFilter() ref = self.request_reference_id(integer=True) self.connection.reqExecutions(reqId=ref, filter=efilter) sleep(3) # Todo: This is a ridiculous bottleneck return ref
def reqExecutions(self, value): try: filt = ExecutionFilter( ) if value == '' else ExecutionFilterHelper.kvstring2object( value, ExecutionFilter) self.connection.reqExecutions(0, filt) except: logging.error(traceback.format_exc())
def request_executions(self, callback): if not self.execution_callbacks: self.executions = {} filter = ExecutionFilter() id = self.next_id() self.tws_conn.reqExecutions(id, filter) self.execution_callbacks.append( TWS_Callback(self, 0, 'executions', callback))
def exec_filter(self, client_id, accountName, contract): filt = ExecutionFilter() filt.m_clientId = client_id filt.m_acctCode = accountName #filt.m_time = "20160122-00:00:00" filt.m_symbol = contract.m_symbol filt.m_secType = contract.m_secType filt.m_exchange = contract.m_exchange return filt
def _subscribe(self): self._register() self.connection.reqExecutions(2, ExecutionFilter()) self.connection.reqAccountSummary(1, 'All', 'NetLiquidation') self.connection.reqIds(1) # in theory this should help to avoid errors on connection break/restore in the middle of # trading, but needs some testing logger.debug('Connection: waiting for next valid ID..') self.next_id_event.wait() logger.debug('Obtained next ID, connection is completed') self.next_id_event.clear()
def exec_filter(client_id): contract = make_contract('NVDA') filt = ExecutionFilter() filt.m_clientId = client_id filt.m_symbol = contract.m_symbol filt.m_secType = contract.m_secType filt.m_exchange = contract.m_exchange return filt
def exec_filter(self, client_id, accountName, contract): filt = ExecutionFilter() filt.m_clientId = client_id filt.m_acctCode = accountName #filt.m_time = "20160122-00:00:00" filt.m_symbol = contract.m_symbol filt.m_secType = contract.m_secType filt.m_exchange = contract.m_exchange return filt
def testExecutions(): print 'testing executions' f = ExecutionFilter() #f.m_clientId = 101 f.m_time = '20110901-00:00:00' f.m_symbol = 'SPY' f.m_secType = 'STK' f.m_exchange = 'SMART' #f.m_side = 'BUY' con.reqExecutions(f) sleep(2)
def make_exec_filter(self, **kwargs): """ This function helps us to filter a executio based on its attributes m_acctCode - String m_exchange - String m_secType - String m_side - String m_symbol - String m_time - String 'yyyymmdd-hh:mm:ss' m_clientId - int """ ex_filter = ExecutionFilter() for name, value in kwargs.items(): if not isinstance(name, basestring) or not isinstance( value, basestring): raise ( 'Contract keywords are not set right! It should start with m_. They should both be strings as well.' ) setattr(ex_filter, 'm_' + name, value) return ex_filter
TWS.sync_order_id() # In[ ]: def req_position(msg): print(dir(msg)) TWS.con.reqAllOpenOrders() TWS.con.reqOpenOrders() subscribe = False acctCode = 'U9015509' TWS.con.reqAccountUpdates(subscribe, acctCode) E_filter = ExecutionFilter() reqId = 0 TWS.con.reqExecutions(reqId, E_filter) # In[ ]: a_order = TWS.create_order('LMT', 9985, 1, "BUY") # In[ ]: TWS.place_order(a_order) #TWS.con.reqPositions() # In[ ]: TWS.con.cancelOrder(TWS.Id)
def makeExecFilter(): filter = ExecutionFilter() return filter
def reqExecutions(self): filt = ExecutionFilter() self.connection.reqExecutions(0, filt)
con.connect() while initialized == 0: sleep(0.1) print "--> Initialization complete!" ##con.reqAccountUpdates(1, '') print "-----" ##con.reqAllOpenOrders() ticker_id = gen_tick_id() contract = make_contract('SLV') con.reqMktData(ticker_id, contract, [], False) ##con.cancelMktData(ticker_id) ### Place sell orders exFilter = ExecutionFilter() con.reqExecutions(exFilter) #insertBids() #order = make_order(1, 25, 'BUY') #contract = make_contract('SLV') #con.placeOrder(id=next_order_id(), contract=contract, order=order) # Order confirmation feed #https://mail.google.com/mail/feed/atom/$-in-interactivebrokers/ print "--> Looping on console..." while True: line = sys.stdin.readline()[0:-1] if line == "quit" or line == "exit":