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_response_content(self, response_content): response = super(ContractsResponse, self).parse_response_content(response_content) if 'is_success' in response: self._is_success = response['is_success'] if self.data: data_json = json.loads(self.data) if 'items' in data_json: for item in data_json['items']: contract_fields = {} for key, value in item.items(): if value is None: continue if isinstance(value, six.string_types): value = get_string(value) tag = CONTRACT_FIELD_MAPPINGS[key] if key in CONTRACT_FIELD_MAPPINGS else key if tag in CONTRACT_FIELDS: contract_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') right = 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, right=right, multiplier=multiplier) self.contracts.append(contract)
def parse_response_content(self, response_content): response = super(ContractsResponse, self).parse_response_content(response_content) if 'is_success' in response: self._is_success = response['is_success'] if self.data: data_json = self.data if isinstance(self.data, dict) else json.loads(self.data) if 'items' in data_json: for item in data_json['items']: contract_fields = {} for key, value in item.items(): if value is None: continue tag = CONTRACT_FIELD_MAPPINGS[key] if key in CONTRACT_FIELD_MAPPINGS else key if tag in CONTRACT_FIELDS: contract_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') name = contract_fields.get('name') short_margin = contract_fields.get('short_margin') short_fee_rate = contract_fields.get('short_fee_rate') shortable = contract_fields.get('shortable') long_initial_margin = contract_fields.get('long_initial_margin') long_maintenance_margin = contract_fields.get('long_maintenance_margin') contract_month = contract_fields.get('contract_month') identifier = contract_fields.get('identifier') primary_exchange = contract_fields.get('primary_exchange') market = contract_fields.get('market') min_tick = contract_fields.get('min_tick') trading_class = contract_fields.get('trading_class') status = contract_fields.get('status') continuous = contract_fields.get('continuous') trade = contract_fields.get('trade') last_trading_date = contract_fields.get('last_trading_date') first_notice_date = contract_fields.get('first_notice_date') last_bidding_close_time = contract_fields.get('last_bidding_close_time') 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, name=name, short_margin=short_margin, short_fee_rate=short_fee_rate, shortable=shortable, long_initial_margin=long_initial_margin, contract_month=contract_month, long_maintenance_margin=long_maintenance_margin, identifier=identifier, primary_exchange=primary_exchange, market=market, min_tick=min_tick, trading_class=trading_class, status=status, continuous=continuous, trade=trade, last_trading_date=last_trading_date, first_notice_date=first_notice_date, last_bidding_close_time=last_bidding_close_time) self.contracts.append(contract)
def future_contract(symbol, currency, expiry, multiplier=None, local_symbol=None): return Contract(symbol, currency, sec_type='FUT', expiry=expiry, multiplier=multiplier, local_symbol=local_symbol)
def option_contract(identifier, multiplier=100, currency='USD'): symbol, expiry, put_call, strike = extract_option_info(identifier) if expiry and '-' in expiry: expiry = expiry.replace('-', '') return Contract(symbol, currency, sec_type='OPT', expiry=expiry, strike=strike, put_call=put_call, multiplier=multiplier)
def stock_contract(symbol, currency, local_symbol=None, exchange=None, contract_id=None): return Contract(symbol, currency, sec_type='STK', local_symbol=local_symbol, exchange=exchange, contract_id=contract_id)
def _parse_transactions(self, item_dict): trans = Transaction() contract = Contract() for k, v in item_dict.items(): map_k = FIELD_MAPPINGS.get(k, k) if map_k in CONTRACT_FIELDS: setattr(contract, map_k, v) else: setattr(trans, map_k, v) trans.contract = contract return trans
def parse_response_content(self, response_content): response = super(PositionsResponse, self).parse_response_content(response_content) if 'is_success' in response: self._is_success = response['is_success'] if self.data: data_json = json.loads(self.data) if 'items' in data_json: for item in data_json['items']: contract_fields = {} position_fields = {} for key, value in item.items(): if value is None: continue if isinstance(value, six.string_types): value = get_string(value) tag = POSITION_FIELD_MAPPINGS[key] if key in POSITION_FIELD_MAPPINGS else key if tag in CONTRACT_FIELDS: contract_fields[tag] = value else: position_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') right = 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, right=right, multiplier=multiplier) account = position_fields.get('account') quantity = position_fields.get('quantity') average_cost = position_fields.get('average_cost') market_price = position_fields.get('market_price') market_value = position_fields.get('market_value') realized_pnl = position_fields.get('realized_pnl') unrealized_pnl = position_fields.get('unrealized_pnl') position = Position(account, contract, quantity, average_cost=average_cost, market_price=market_price, market_value=market_value, realized_pnl=realized_pnl, unrealized_pnl=unrealized_pnl) self.positions.append(position)
def future_option_contract(symbol, currency, expiry, strike, put_call, multiplier=None, local_symbol=None, contract_id=None): return Contract(symbol, currency, sec_type='FOP', expiry=expiry, strike=strike, put_call=put_call, multiplier=multiplier, local_symbol=local_symbol, contract_id=contract_id)
def option_contract_by_symbol(symbol, expiry, strike, put_call, currency, multiplier=100, local_symbol=None, contract_id=None): return Contract(symbol, currency, sec_type='OPT', expiry=expiry, strike=strike, put_call=put_call, multiplier=multiplier, local_symbol=local_symbol, contract_id=contract_id)
def parse_response_content(self, response_content): response = super(ContractsResponse, self).parse_response_content(response_content) if 'is_success' in response: self._is_success = response['is_success'] if self.data: data_json = self.data if isinstance(self.data, dict) else json.loads(self.data) if 'items' in data_json: for item in data_json['items']: contract_fields = {} for key, value in item.items(): if value is None: continue if isinstance(value, six.string_types): value = get_string(value) tag = CONTRACT_FIELD_MAPPINGS[key] if key in CONTRACT_FIELD_MAPPINGS else key if tag in CONTRACT_FIELDS: contract_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') name = contract_fields.get('name') short_margin = contract_fields.get('short_margin') short_fee_rate = contract_fields.get('short_fee_rate') shortable = contract_fields.get('shortable') long_initial_margin = contract_fields.get('long_initial_margin') long_maintenance_margin = contract_fields.get('long_maintenance_margin') contract_month = contract_fields.get('contract_month') 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, name=name, short_margin=short_margin, short_fee_rate=short_fee_rate, shortable=shortable, long_initial_margin=long_initial_margin, contract_month=contract_month, long_maintenance_margin=long_maintenance_margin, identifier=identifier) self.contracts.append(contract)
def cash_contract(symbol, currency, local_symbol=None): return Contract(symbol, currency, sec_type='CASH', local_symbol=local_symbol)
def future_contract(symbol, currency, expiry=None, exchange=None, contract_month=None, multiplier=None, local_symbol=None): return Contract(symbol, currency, sec_type='FUT', expiry=expiry, exchange=exchange, contract_month=contract_month, multiplier=multiplier, local_symbol=local_symbol)
def option_contract(symbol, currency, expiry, strike, right, multiplier=100, local_symbol=None, contract_id=None): return Contract(symbol, currency, sec_type='OPT', expiry=expiry, strike=strike, right=right, multiplier=multiplier, local_symbol=local_symbol, contract_id=contract_id)
def iopt_contract_by_symbol(symbol, expiry, strike, put_call, local_symbol, multiplier=100, currency='HKD', contract_id=None): """港股牛熊证""" return Contract(symbol, currency=currency, sec_type=SecurityType.IOPT.value, expiry=expiry, strike=strike, put_call=put_call, local_symbol=local_symbol, multiplier=multiplier, contract_id=contract_id)
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