Source code for ib_insync.objects

"""Object hierarchy."""

from dataclasses import dataclass, field
from datetime import date as date_, datetime
from typing import ClassVar, List, NamedTuple, Optional, Union

from eventkit import Event

from .contract import Contract, TagValue
from .util import EPOCH, UNSET_DOUBLE, UNSET_INTEGER

__all__ = (
    'SoftDollarTier PriceIncrement Execution CommissionReport '
    'BarList BarDataList RealTimeBarList BarData RealTimeBar '
    'HistogramData NewsProvider DepthMktDataDescription '
    'ScannerSubscription ScanDataList FundamentalRatios '
    'ExecutionFilter PnL PnLSingle AccountValue TickData '
    'TickByTickAllLast TickByTickBidAsk TickByTickMidPoint '
    'HistoricalTick HistoricalTickBidAsk HistoricalTickLast '
    'TickAttrib TickAttribBidAsk TickAttribLast '
    'MktDepthData DOMLevel TradeLogEntry '
    'FamilyCode SmartComponent '
    'PortfolioItem Position Fill OptionComputation OptionChain Dividends '
    'NewsArticle HistoricalNews NewsTick NewsBulletin ConnectionStats'
).split()

nan = float('nan')


[docs]@dataclass class ScannerSubscription: numberOfRows: int = -1 instrument: str = '' locationCode: str = '' scanCode: str = '' abovePrice: float = UNSET_DOUBLE belowPrice: float = UNSET_DOUBLE aboveVolume: int = UNSET_INTEGER marketCapAbove: float = UNSET_DOUBLE marketCapBelow: float = UNSET_DOUBLE moodyRatingAbove: str = '' moodyRatingBelow: str = '' spRatingAbove: str = '' spRatingBelow: str = '' maturityDateAbove: str = '' maturityDateBelow: str = '' couponRateAbove: float = UNSET_DOUBLE couponRateBelow: float = UNSET_DOUBLE excludeConvertible: bool = False averageOptionVolumeAbove: int = UNSET_INTEGER scannerSettingPairs: str = '' stockTypeFilter: str = ''
[docs]@dataclass class SoftDollarTier: name: str = '' val: str = '' displayName: str = '' def __bool__(self): return bool(self.name or self.val or self.displayName)
[docs]@dataclass class Execution: execId: str = '' time: datetime = field(default=EPOCH) acctNumber: str = '' exchange: str = '' side: str = '' shares: float = 0.0 price: float = 0.0 permId: int = 0 clientId: int = 0 orderId: int = 0 liquidation: int = 0 cumQty: float = 0.0 avgPrice: float = 0.0 orderRef: str = '' evRule: str = '' evMultiplier: float = 0.0 modelCode: str = '' lastLiquidity: int = 0
[docs]@dataclass class CommissionReport: execId: str = '' commission: float = 0.0 currency: str = '' realizedPNL: float = 0.0 yield_: float = 0.0 yieldRedemptionDate: int = 0
[docs]@dataclass class ExecutionFilter: clientId: int = 0 acctCode: str = '' time: str = '' symbol: str = '' secType: str = '' exchange: str = '' side: str = ''
[docs]@dataclass class BarData: date: Union[date_, datetime] = EPOCH open: float = 0.0 high: float = 0.0 low: float = 0.0 close: float = 0.0 volume: int = 0 average: float = 0.0 barCount: int = 0
[docs]@dataclass class RealTimeBar: time: datetime = EPOCH endTime: int = -1 open_: float = 0.0 high: float = 0.0 low: float = 0.0 close: float = 0.0 volume: float = 0.0 wap: float = 0.0 count: int = 0
[docs]@dataclass class TickAttrib: canAutoExecute: bool = False pastLimit: bool = False preOpen: bool = False
[docs]@dataclass class TickAttribBidAsk: bidPastLow: bool = False askPastHigh: bool = False
[docs]@dataclass class TickAttribLast: pastLimit: bool = False unreported: bool = False
[docs]@dataclass class HistogramData: price: float = 0.0 count: int = 0
[docs]@dataclass class NewsProvider: code: str = '' name: str = ''
[docs]@dataclass class DepthMktDataDescription: exchange: str = '' secType: str = '' listingExch: str = '' serviceDataType: str = '' aggGroup: int = UNSET_INTEGER
[docs]@dataclass class PnL: account: str = '' modelCode: str = '' dailyPnL: float = nan unrealizedPnL: float = nan realizedPnL: float = nan
[docs]@dataclass class PnLSingle: account: str = '' modelCode: str = '' conId: int = 0 dailyPnL: float = nan unrealizedPnL: float = nan realizedPnL: float = nan position: int = 0 value: float = nan
[docs]class AccountValue(NamedTuple): account: str tag: str value: str currency: str modelCode: str
[docs]class TickData(NamedTuple): time: datetime tickType: int price: float size: int
[docs]class HistoricalTick(NamedTuple): time: datetime price: float size: int
[docs]class HistoricalTickBidAsk(NamedTuple): time: datetime tickAttribBidAsk: TickAttribBidAsk priceBid: float priceAsk: float sizeBid: int sizeAsk: int
[docs]class HistoricalTickLast(NamedTuple): time: datetime tickAttribLast: TickAttribLast price: float size: int exchange: str specialConditions: str
[docs]class TickByTickAllLast(NamedTuple): tickType: int time: datetime price: float size: int tickAttribLast: TickAttribLast exchange: str specialConditions: str
[docs]class TickByTickBidAsk(NamedTuple): time: datetime bidPrice: float askPrice: float bidSize: int askSize: int tickAttribBidAsk: TickAttribBidAsk
[docs]class TickByTickMidPoint(NamedTuple): time: datetime midPoint: float
[docs]class MktDepthData(NamedTuple): time: datetime position: int marketMaker: str operation: int side: int price: float size: int
[docs]class DOMLevel(NamedTuple): price: float size: int marketMaker: str
[docs]class TradeLogEntry(NamedTuple): time: datetime status: str message: str
[docs]class PriceIncrement(NamedTuple): lowEdge: float increment: float
[docs]class PortfolioItem(NamedTuple): contract: Contract position: float marketPrice: float marketValue: float averageCost: float unrealizedPNL: float realizedPNL: float account: str
[docs]class Position(NamedTuple): account: str contract: Contract position: float avgCost: float
[docs]class Fill(NamedTuple): contract: Contract execution: Execution commissionReport: CommissionReport time: datetime
[docs]class OptionComputation(NamedTuple): impliedVol: float delta: float optPrice: float pvDividend: float gamma: float vega: float theta: float undPrice: float
[docs]class OptionChain(NamedTuple): exchange: str underlyingConId: int tradingClass: str multiplier: str expirations: List[str] strikes: List[float]
[docs]class Dividends(NamedTuple): past12Months: Optional[float] next12Months: Optional[float] nextDate: Optional[date_] nextAmount: Optional[float]
[docs]class NewsArticle(NamedTuple): articleType: int articleText: str
[docs]class HistoricalNews(NamedTuple): time: datetime providerCode: str articleId: str headline: str
[docs]class NewsTick(NamedTuple): timeStamp: int providerCode: str articleId: str headline: str extraData: str
[docs]class NewsBulletin(NamedTuple): msgId: int msgType: int message: str origExchange: str
[docs]class FamilyCode(NamedTuple): accountID: str familyCodeStr: str
[docs]class SmartComponent(NamedTuple): bitNumber: int exchange: str exchangeLetter: str
[docs]class ConnectionStats(NamedTuple): startTime: float duration: float numBytesRecv: int numBytesSent: int numMsgRecv: int numMsgSent: int
[docs]class BarList(list): """Base class for bar lists.""" events: ClassVar = ('updateEvent',) def __init__(self, *args): list.__init__(self, *args) self.updateEvent = Event('updateEvent') def __eq__(self, other): return self is other def __hash__(self): return id(self)
[docs]class BarDataList(BarList): """ List of :class:`.BarData` that also stores all request parameters. Events: * ``updateEvent`` (bars: :class:`.BarDataList`, hasNewBar: bool) """ reqId: int contract: Contract endDateTime: Union[datetime, date_, str, None] durationStr: str barSizeSetting: str whatToShow: str useRTH: bool formatDate: int keepUpToDate: bool chartOptions: List[TagValue]
[docs]class RealTimeBarList(BarList): """ List of :class:`.RealTimeBar` that also stores all request parameters. Events: * ``updateEvent`` (bars: :class:`.RealTimeBarList`, hasNewBar: bool) """ reqId: int contract: Contract barSize: int whatToShow: str useRTH: bool realTimeBarsOptions: List[TagValue]
[docs]class ScanDataList(list): """ List of :class:`.ScanData` that also stores all request parameters. Events: * ``updateEvent`` (:class:`.ScanDataList`) """ events: ClassVar = ('updateEvent',) reqId: int subscription: ScannerSubscription scannerSubscriptionOptions: List[TagValue] scannerSubscriptionFilterOptions: List[TagValue] def __init__(self, *args): list.__init__(self, *args) self.updateEvent = Event('updateEvent') def __eq__(self, other): return self is other def __hash__(self): return id(self)
class DynamicObject: def __init__(self, **kwargs): self.__dict__.update(kwargs) def __repr__(self): clsName = self.__class__.__name__ kwargs = ', '.join(f'{k}={v!r}' for k, v in self.__dict__.items()) return f'{clsName}({kwargs})'
[docs]class FundamentalRatios(DynamicObject): """ See: https://interactivebrokers.github.io/tws-api/fundamental_ratios_tags.html """ pass