Esempio n. 1
0
def _merge_order_book_deltas(all_deltas: List[OrderBookDeltas]):
    per_instrument_deltas = defaultdict(list)
    level = one(set(deltas.level for deltas in all_deltas))
    timestamp_ns = one(set(deltas.timestamp_ns for deltas in all_deltas))

    for deltas in all_deltas:
        per_instrument_deltas[deltas.instrument_id].extend(deltas.deltas)
    return [
        OrderBookDeltas(
            instrument_id=instrument_id,
            deltas=deltas,
            level=level,
            timestamp_ns=timestamp_ns,
        ) for instrument_id, deltas in per_instrument_deltas.items()
    ]
Esempio n. 2
0
    async def load(self, build_options_chain=False, option_kwargs=None, **kwargs):
        """
        Search and load the instrument for the given symbol, exchange and (optional) kwargs

        Parameters
        ----------
        build_options_chain: bool (default: False)
            Search for full option chain
        option_kwargs: str (default: False)
            JSON string for options filtering, available fields: min_expiry, max_expiry, min_strike, max_strike, kind
        kwargs: **kwargs
            Optional extra kwargs to search for, examples:
                secType, conId, symbol, lastTradeDateOrContractMonth, strike, right, multiplier, exchange,
                primaryExchange, currency, localSymbol, tradingClass, includeExpired, secIdType, secId,
                comboLegsDescrip, comboLegs,  deltaNeutralContract
        """
        contract = self._parse_contract(**kwargs)
        qualified = await self._client.qualifyContractsAsync(contract)
        qualified = one(qualified)
        contract_details: List[ContractDetails] = await self.get_contract_details(
            qualified, build_options_chain=build_options_chain, option_kwargs=option_kwargs
        )
        if not contract_details:
            raise ValueError(f"No contract details found for the given kwargs ({kwargs})")

        for details in contract_details:
            instrument: Instrument = parse_instrument(
                contract_details=details,
            )
            self.add(instrument)
            self.contract_details[instrument.id] = details
            self.contract_id_to_instrument_id[details.contract.conId] = instrument.id
Esempio n. 3
0
def _merge_order_book_deltas(all_deltas: List[OrderBookDeltas]):
    per_instrument_deltas = defaultdict(list)
    book_type = one(set(deltas.book_type for deltas in all_deltas))
    ts_event = one(set(deltas.ts_event for deltas in all_deltas))
    ts_init = one(set(deltas.ts_init for deltas in all_deltas))

    for deltas in all_deltas:
        per_instrument_deltas[deltas.instrument_id].extend(deltas.deltas)
    return [
        OrderBookDeltas(
            instrument_id=instrument_id,
            deltas=deltas,
            book_type=book_type,
            ts_event=ts_event,
            ts_init=ts_init,
        ) for instrument_id, deltas in per_instrument_deltas.items()
    ]
Esempio n. 4
0
    async def get_option_chain_details(
        self,
        underlying: Contract,
        min_expiry=None,
        max_expiry=None,
        min_strike=None,
        max_strike=None,
        kind=None,
        exchange=None,
    ) -> List[ContractDetails]:
        chains = await self._client.reqSecDefOptParamsAsync(
            underlying.symbol, "", underlying.secType, underlying.conId
        )

        chain = one(chains)

        strikes = [
            strike
            for strike in chain.strikes
            if (min_strike or -np.inf) <= strike <= (max_strike or np.inf)
        ]
        expirations = sorted(
            exp
            for exp in chain.expirations
            if (pd.Timestamp(min_expiry or pd.Timestamp.min) <= pd.Timestamp(exp))
            and (pd.Timestamp(exp) <= pd.Timestamp(max_expiry or pd.Timestamp.max))
        )
        rights = [kind] if kind is not None else ["P", "C"]

        contracts = [
            ib_insync.Option(
                underlying.symbol,
                expiration,
                strike,
                right,
                exchange or "SMART",
            )
            for right in rights
            for expiration in expirations
            for strike in strikes
        ]
        qualified = await self._client.qualifyContractsAsync(*contracts)
        details = await asyncio.gather(
            *[self._client.reqContractDetailsAsync(contract=c) for c in qualified]
        )
        return [x for d in details for x in d]