def parse_order(item): contract_fields = {} order_fields = {} for key, value in item.items(): if value is None: continue if isinstance(value, six.string_types): value = get_string(value) tag = ORDER_FIELD_MAPPINGS[ key] if key in ORDER_FIELD_MAPPINGS else key if tag in CONTRACT_FIELDS: contract_fields[tag] = value else: order_fields[tag] = value contract_id = contract_fields.get('contract_id') symbol = contract_fields.get('symbol') currency = contract_fields.get('currency') sec_type = contract_fields.get('sec_type') exchange = contract_fields.get('exchange') origin_symbol = contract_fields.get('origin_symbol') local_symbol = contract_fields.get('local_symbol') expiry = contract_fields.get('expiry') strike = contract_fields.get('strike') put_call = contract_fields.get('right') multiplier = contract_fields.get('multiplier') contract = Contract(symbol, currency, contract_id=contract_id, sec_type=sec_type, exchange=exchange, origin_symbol=origin_symbol, local_symbol=local_symbol, expiry=expiry, strike=strike, put_call=put_call, multiplier=multiplier) account = order_fields.get('account') action = order_fields.get('action') order_type = order_fields.get('order_type') quantity = order_fields.get('quantity') limit_price = order_fields.get('limit_price') aux_price = order_fields.get('aux_price') trail_stop_price = order_fields.get('trail_stop_price') trailing_percent = order_fields.get('trailing_percent') percent_offset = order_fields.get('percent_offset') time_in_force = order_fields.get('time_in_force') outside_rth = order_fields.get('outside_rth') filled = order_fields.get('filled') avg_fill_price = order_fields.get('avg_fill_price') commission = order_fields.get('commission') realized_pnl = order_fields.get('realized_pnl') id = order_fields.get('id') order_id = order_fields.get('order_id') parent_id = order_fields.get('parent_id') status = get_order_status(order_fields.get('status')) order = Order(account, contract, action, order_type, quantity, limit_price=limit_price, aux_price=aux_price, trail_stop_price=trail_stop_price, trailing_percent=trailing_percent, percent_offset=percent_offset, time_in_force=time_in_force, outside_rth=outside_rth, filled=filled, avg_fill_price=avg_fill_price, commission=commission, realized_pnl=realized_pnl, id=id, order_id=order_id, parent_id=parent_id) if 'order_time' in order_fields: order.order_time = order_fields.get('order_time') if 'trade_time' in order_fields: order.trade_time = order_fields.get('trade_time') if 'reason' in order_fields: order.reason = order_fields.get('reason') order.status = status return order
def on_message(self, headers, body): """ Called by the STOMP connection when a MESSAGE frame is received. :param dict headers: a dictionary containing all headers sent by the server as key/value pairs. :param body: the frame's payload - the message body. """ try: response_type = headers.get('ret-type') if response_type == str(ResponseType.GET_SUB_SYMBOLS_END.value): if self.subscribed_symbols: data = json.loads(body) limit = data.get('limit') symbols = data.get('subscribedSymbols') used = data.get('used') symbol_focus_keys = data.get('symbolFocusKeys') focus_keys = dict() for sym, keys in symbol_focus_keys.items(): keys = set( QUOTE_KEYS_MAPPINGS.get(key, key) for key in keys) focus_keys[sym] = list(keys) self.subscribed_symbols(symbols, focus_keys, limit, used) elif response_type == str(ResponseType.GET_QUOTE_CHANGE_END.value): if self.quote_changed: data = json.loads(body) hour_trading = False if 'hourTradingLatestPrice' in data: hour_trading = True if 'symbol' in data: symbol = data.get('symbol') offset = data.get('offset', 0) items = [] # 期货行情推送的价格都乘了 10 的 offset 次方变成了整数, 需要除回去变为正常单位的价格 if offset: for key, value in data.items(): if (key == 'latestTime' or key == 'hourTradingLatestTime') and \ isinstance(value, six.string_types): continue if key in QUOTE_KEYS_MAPPINGS: key = QUOTE_KEYS_MAPPINGS.get(key) if key in PRICE_FIELDS: value /= 10**offset elif key == 'minute': minute_item = dict() for m_key, m_value in value.items(): if m_key in {'p', 'h', 'l'}: m_value /= 10**offset minute_item[m_key] = m_value value = minute_item items.append((key, value)) else: for key, value in data.items(): if (key == 'latestTime' or key == 'hourTradingLatestTime') and \ isinstance(value, six.string_types): continue if key in QUOTE_KEYS_MAPPINGS: key = QUOTE_KEYS_MAPPINGS.get(key) items.append((key, value)) if items: self.quote_changed(symbol, items, hour_trading) elif response_type == str(ResponseType.SUBSCRIBE_ASSET.value): if self.asset_changed: data = json.loads(body) if 'account' in data: account = data.get('account') items = [] for key, value in data.items(): if key in ASSET_KEYS_MAPPINGS: items.append( (ASSET_KEYS_MAPPINGS.get(key), value)) if items: self.asset_changed(account, items) elif response_type == str(ResponseType.SUBSCRIBE_POSITION.value): if self.position_changed: data = json.loads(body) if 'account' in data: account = data.get('account') items = [] for key, value in data.items(): if key in POSITION_KEYS_MAPPINGS: items.append( (POSITION_KEYS_MAPPINGS.get(key), value)) if items: self.position_changed(account, items) elif response_type == str( ResponseType.SUBSCRIBE_ORDER_STATUS.value): if self.order_changed: data = json.loads(body) if 'account' in data: account = data.get('account') items = [] for key, value in data.items(): if key in ORDER_KEYS_MAPPINGS: if key == 'status': value = get_order_status(value) # 部分成交 (服务端推送 'Submitted' 状态) if value == OrderStatus.HELD and data.get( 'filledQuantity'): value = OrderStatus.PARTIALLY_FILLED items.append( (ORDER_KEYS_MAPPINGS.get(key), value)) if items: self.order_changed(account, items) except Exception as e: logging.error(e, exc_info=True)
def on_message(self, headers, body): """ Called by the STOMP connection when a MESSAGE frame is received. :param dict headers: a dictionary containing all headers sent by the server as key/value pairs. :param body: the frame's payload - the message body. """ try: response_type = headers.get('ret-type') if response_type == str(ResponseType.GET_SUB_SYMBOLS_END.value): if self.subscribed_symbols: data = json.loads(body) limit = data.get('limit') symbols = data.get('subscribedSymbols') used = data.get('used') symbol_focus_keys = data.get('symbolFocusKeys') focus_keys = dict() for sym, keys in symbol_focus_keys.items(): keys = [ QUOTE_KEYS_MAPPINGS.get(key, key) for key in keys ] focus_keys[sym] = keys self.subscribed_symbols(symbols, focus_keys, limit, used) elif response_type == str(ResponseType.GET_QUOTE_CHANGE_END.value): if self.quote_changed: data = json.loads(body) hour_trading = False if 'hourTradingLatestPrice' in data: hour_trading = True if 'symbol' in data: symbol = data.get('symbol') items = [] for key, value in data.items(): if key.startswith('hourTrading'): key = key[11:] if key == 'latestTime' and isinstance( value, six.string_types): continue if key in QUOTE_KEYS_MAPPINGS: items.append( (QUOTE_KEYS_MAPPINGS.get(key), value)) if items: self.quote_changed(symbol, items, hour_trading) elif response_type == str(ResponseType.SUBSCRIBE_ASSET.value): if self.asset_changed: data = json.loads(body) if 'account' in data: account = data.get('account') items = [] for key, value in data.items(): if key in ASSET_KEYS_MAPPINGS: items.append( (ASSET_KEYS_MAPPINGS.get(key), value)) if items: self.asset_changed(account, items) elif response_type == str(ResponseType.SUBSCRIBE_POSITION.value): if self.position_changed: data = json.loads(body) if 'account' in data: account = data.get('account') items = [] for key, value in data.items(): if key in POSITION_KEYS_MAPPINGS: items.append( (POSITION_KEYS_MAPPINGS.get(key), value)) if items: self.position_changed(account, items) elif response_type == str( ResponseType.SUBSCRIBE_ORDER_STATUS.value): if self.order_changed: data = json.loads(body) if 'account' in data: account = data.get('account') items = [] for key, value in data.items(): if key in ORDER_KEYS_MAPPINGS: if key == 'status': value = get_order_status(value) items.append( (ORDER_KEYS_MAPPINGS.get(key), value)) if items: self.order_changed(account, items) except Exception as e: logging.error(e, exc_info=True)