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 = OrdersResponse.get_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') order.status = status return order
def parse_order(item, secret_key=None): contract_fields = {} order_fields = {} for key, value in item.items(): if value is None: continue 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') identifier = contract_fields.get('identifier') 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, identifier=identifier) 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')) algo_params = AlgoParams.from_tags(order_fields.get('algo_params')) liquidation = order_fields.get('liquidation') algo_strategy = order_fields.get('algo_strategy') discount = order_fields.get('discount') 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, algo_params=algo_params, liquidation=liquidation, algo_strategy=algo_strategy, discount=discount) 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') if secret_key is not None: order.secret_key = secret_key order.status = status return order