Beispiel #1
0
    def _handle_position_data(self, account_id: str, message: IncomingMessage):
        instrument = Instrument.get_instance_from(message)

        instrument.contract_id = message.read(int)
        instrument.symbol = message.read(str)
        instrument.security_type = message.read(str)
        instrument.last_trade_date = instrument.contract_month = message.read(
            datetime.date)
        instrument.strike = message.read(float)
        instrument.right = message.read(str)
        instrument.multiplier = message.read(int)
        instrument.exchange = message.read(str)
        instrument.currency = message.read(str)
        instrument.local_symbol = message.read(str)
        if message.message_version >= 2:
            instrument.trading_class = message.read(str)

        try:
            account = self.accounts[account_id]
        except KeyError:
            account = self.accounts[account_id] = Account(account_id)

        size = account.positions[instrument] = message.read(float)
        if message.message_version >= 3:
            average_cost = account.avg_position_cost[
                instrument] = message.read(float)
        else:
            average_cost = None

        self.on_position(PositionEvent(account, instrument, size,
                                       average_cost))
Beispiel #2
0
    def _handle_contract_data(self, request_id: RequestId,
                              message: IncomingMessage):
        # fast forward to instrument id position, so that we avoid making new contracts when existing ones can be
        # reused. This is required for proper event routing elsewhere

        instrument = self._pending_instrument_updates.get(request_id)

        if not instrument:
            message.idx += 10
            instrument = Instrument.get_instance_from(message)
            self._pending_instrument_updates[request_id] = instrument
            message.idx -= 10

        instrument.symbol = message.read(str)
        instrument.security_type = message.read(SecurityType)
        instrument.last_trade_date = message.read(datetime.datetime)
        instrument.strike = message.read(float)
        instrument.right = message.read(str)
        instrument.exchange = message.read(str)
        instrument.currency = message.read(str)
        instrument.local_symbol = message.read(str)
        instrument.market_name = message.read(str)
        instrument.trading_class = message.read(str)
        instrument.contract_id = message.read(int)
        instrument.minimum_tick = message.read(float)

        instrument.market_data_size_multiplier = message.read(
            str, min_version=ProtocolVersion.MD_SIZE_MULTIPLIER)

        instrument.multiplier = message.read(str)
        instrument.order_types = message.read(str).split(',')
        instrument.valid_exchanges = message.read(str).split(',')
        instrument.price_magnifier = message.read(int)
        instrument.underlying_contract_id = message.read(int)
        instrument.long_name = message.read(str)
        instrument.primary_exchange = message.read(str)
        instrument.contract_month = message.read(str)
        instrument.industry = message.read(str)
        instrument.category = message.read(str)
        instrument.subcategory = message.read(str)
        instrument.time_zone = message.read(str)
        instrument.trading_hours = message.read(str)
        instrument.liquid_hours = message.read(str)
        instrument.ev_rule = message.read(str)
        instrument.ev_multiplier = message.read(str)
        instrument.security_ids = message.read(
            typing.Dict[SecurityIdentifierType, str])

        instrument.aggregated_group = message.read(
            str, min_version=ProtocolVersion.AGG_GROUP)

        instrument.underlying_symbol = message.read(
            str, min_version=ProtocolVersion.UNDERLYING_INFO)
        instrument.underlying_security_type = message.read(
            SecurityType, min_version=ProtocolVersion.UNDERLYING_INFO)

        instrument.market_rule_ids = message.read(
            str, min_version=ProtocolVersion.MARKET_RULES)
        instrument.real_expiration_date = message.read(
            datetime.datetime,
            min_version=ProtocolVersion.REAL_EXPIRATION_DATE)

        self.resolve_future(request_id, instrument)