def log_message(message, mapping): try: del (mapping['self']) except (KeyError, ): pass items = list(mapping.items()) items.sort() log.debug(('### %s' % (message, ))) for k, v in items: log.debug((' %s:%s' % (k, v)))
def openOrder(self, order_id, contract, order, state): self.open_orders[order_id] = _method_params_to_dict(vars()) log.debug( "Order-{order_id} {status}: " "{order_action} {order_count} {symbol} with {order_type} order. " "limit_price={limit_price} stop_price={stop_price}".format( order_id=order_id, status=state.status, order_action=order.action, order_count=order.totalQuantity, symbol=contract.symbol, order_type=order.orderType, limit_price=order.lmtPrice, stop_price=order.auxPrice))
def subscribe_to_market_data(self, asset): if asset not in self.subscribed_assets: ib_symbol = self._asset_symbol(asset) self._tws.subscribe_to_market_data(ib_symbol) self._subscribed_assets.append(asset) try: polling.poll( lambda: ib_symbol in self._tws.bars, timeout=_max_wait_subscribe, step=_poll_frequency) except polling.TimeoutException as te: log.warning('Cannot subscribe market data for %s.' % ib_symbol) else: log.debug("Subscription completed")
def orderStatus(self, order_id, status, filled, remaining, avg_fill_price, perm_id, parent_id, last_fill_price, client_id, why_held, mkt_cap): self.order_statuses[order_id] = _method_params_to_dict(vars()) log.debug( "Order-{order_id} {status}: " "filled={filled} remaining={remaining} " "avg_fill_price={avg_fill_price} " "last_fill_price={last_fill_price} ".format( order_id=order_id, status=self.order_statuses[order_id]['status'], filled=self.order_statuses[order_id]['filled'], remaining=self.order_statuses[order_id]['remaining'], avg_fill_price=self.order_statuses[order_id]['avg_fill_price'], last_fill_price=self.order_statuses[order_id]['last_fill_price']))
def commissionReport(self, commission_report): exec_id = commission_report.execId order_id = self._execution_to_order_id[commission_report.execId] self.commissions[order_id][exec_id] = commission_report log.debug( "Order-{order_id} report: " "realized_pnl: ${realized_pnl} " "commission: ${commission} yield: {yield_} " "exec_id: {exec_id}".format( order_id=order_id, exec_id=commission_report.execId, realized_pnl=commission_report.realizedPNL if commission_report.realizedPNL != sys.float_info.max else 0, commission=commission_report.commission, yield_=commission_report.yield_ if commission_report.yield_ != sys.float_info.max else 0))
def subscribe_to_market_data(self, asset): if asset not in self.subscribed_assets: ib_symbol = self._asset_symbol(asset) exchange = 'SMART' primaryExchange = 'ISLAND' secType = 'STK' currency = 'USD' self._tws.subscribe_to_market_data(ib_symbol, exchange, primaryExchange, secType, currency) self._subscribed_assets.append(asset) try: polling.poll(lambda: ib_symbol in self._tws.bars, timeout=_max_wait_subscribe, step=_poll_frequency) except polling.TimeoutException as te: log.warning('Cannot subscribe market data for %s.' % ib_symbol) else: log.debug("Subscription completed")
def load_raw_arrays(self, fields, start_dt, end_dt, sids): start_day = self._fmt_date(start_dt) end_day = self._fmt_date(end_dt) sessions = self.trading_calendar.sessions_in_range(start_dt, end_dt) log.debug("Loading raw arrays for %d assets (%s)." % (len(sids), type(sids))) if any(not isinstance(x, (int, np.integer)) for x in sids): sids = [x.sid for x in sids] raw_arrays = [] with sqlite3.connect(self._filename) as conn: for field in fields: query = "SELECT date, sid, %s FROM prices WHERE sid in (%s) and date >= '%s' AND date <= '%s';" \ % (field, ",".join(map(str, sids)), str(start_day), str(end_day)) df = pd.read_sql_query(query, conn) result = df.pivot(index='date', columns='sid', values=field) result = result.reindex(index=list(map(self._fmt_date, sessions)), columns=sids) raw_arrays.append(result.values) return raw_arrays
def execDetailsEnd(self, req_id): log.debug("Execution details completed for request {req_id}".format( req_id=req_id))