diff --git a/ibkr/client.py b/ibkr/client.py index f69228b..9a2d514 100644 --- a/ibkr/client.py +++ b/ibkr/client.py @@ -1,7 +1,7 @@ import pandas as pd from datetime import datetime -from ib_insync import ComboLeg, Contract, IB, Index, Option, Order, Trade +from ib_insync import ComboLeg, Contract, IB, Index, Option, Order, Ticker, Trade from ib_insync.util import isNan from random import randint from typing import Callable, List @@ -14,24 +14,24 @@ from .order_action import BUY, SELL class Client: - def __init__(self, host: str = '127.0.0.1', port: int = 7497, client_id = randint(1, 10000)) -> None: + def __init__(self, host: str = '127.0.0.1', port: int = 7497, client_id: int = randint(1, 10000)) -> None: self.ib = IB() self.ib.connect(host, port, clientId = client_id) self.ib.reqMarketDataType(LIVE) - - def get_ticker(self, symbol: str, exchange: str): + + def get_ticker(self, symbol: str, exchange: str) -> Ticker: underlying = Index(symbol, exchange) self.ib.qualifyContracts(underlying) return self.ib.reqTickers(underlying)[0] - - def get_market_data(self, contract: Contract, streaming: bool = False): + + def get_market_data(self, contract: Contract, streaming: bool = False) -> Ticker: market_data = self.ib.reqMktData(contract, '', snapshot = not streaming, regulatorySnapshot = False) while isNan(market_data.bid) or isNan(market_data.ask): # TODO: Add a timeout. self.ib.sleep() return market_data - + def get_option_chain(self, symbol: str, expiration: datetime, sub_symbol: str = None, contract_filter: Callable = None) -> pd.DataFrame: expiration_date = expiration.strftime('%Y%m%d') @@ -78,8 +78,8 @@ class Client: option_contract.tradingClass = option_leg.sub_symbol self.ib.qualifyContracts(option_contract) return option_contract - - def submit_option_order(self, leg: OptionLeg, quantity: int): + + def submit_option_order(self, leg: OptionLeg, quantity: int) -> Trade: option_contract = self.get_option_contract(leg) contract = Contract() contract.conId = option_contract.conId @@ -124,7 +124,7 @@ class Client: combo_order.transmit = True return self.ib.placeOrder(combo_contract, combo_order) - + def submit_stop_loss_order(self, trade: Trade, stop_loss_price: float, limit_price: float = None) -> Trade: stop_loss_order = Order() stop_loss_order.action = SELL if trade.order.action == BUY else BUY @@ -139,6 +139,6 @@ class Client: stop_loss_order.totalQuantity = trade.order.totalQuantity stop_loss_order.transmit = True return self.ib.placeOrder(trade.contract, stop_loss_order) - + def run_event_loop(self): self.ib.run() \ No newline at end of file