diff --git a/README.md b/README.md index d178090..1467881 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,10 @@ from ib_fundamental import CompanyFinancials ib = ib_async.IB().connect('localhost',7497) # create your company financials instance -aapl = CompanyFinancials(symbol="AAPL",ib=ib) +aapl = CompanyFinancials(ib=ib,symbol="AAPL") + +# or specify exchange and currency +# aapl = CompanyFinancials(ib=ib,symbol="AAPL",exchange='SMART',currency='USD) # get company info aapl.company_information diff --git a/ib_fundamental/__init__.py b/ib_fundamental/__init__.py index accbd2d..e945494 100644 --- a/ib_fundamental/__init__.py +++ b/ib_fundamental/__init__.py @@ -22,7 +22,7 @@ __copyright__ = "Copyright 2024 Gonzalo Sáenz" __credits__ = ["Gonzalo Sáenz"] __license__ = "Apache 2.0" -__version__ = "0.0.3" +__version__ = "0.0.4" __maintainer__ = "Gonzalo Sáenz" diff --git a/ib_fundamental/fundamental.py b/ib_fundamental/fundamental.py index 94d1916..ea75218 100644 --- a/ib_fundamental/fundamental.py +++ b/ib_fundamental/fundamental.py @@ -62,12 +62,18 @@ class FundamentalData: # pylint: disable=too-many-arguments,too-many-public-methods # pylint: disable=too-many-instance-attributes - def __init__(self, symbol: str, ib: IB) -> None: + def __init__( + self, ib: IB, symbol: str, exchange: str = "SMART", currency: str = "USD" + ) -> None: """Args: - symbol (str): Company symbol/ticker - ib (Optional[IB], optional): IB instance. Defaults to None. + ib (ib_async.IB): ib_async.IB instance + symbol (str): company symbol/ticker + exchange (str, optional): exchange. Defaults to "SMART". + currency (str, optional): currency. Defaults to "USD". """ - self.client = IBClient(symbol=symbol, ib=ib) + self.client = IBClient( + symbol=symbol, ib=ib, exchange=exchange, currency=currency + ) self.symbol = symbol self.contract: Stock = self.client.contract self.ticker: Optional[Ticker] = None @@ -275,14 +281,19 @@ def company_info(self) -> CompanyInfo: class CompanyFinancials: """Company Financials""" - def __init__(self, symbol: str, ib: IB) -> None: - """_summary_ - + def __init__( + self, ib: IB, symbol: str, exchange: str = "SMART", currency: str = "USD" + ) -> None: + """ Args: - symbol (str): company symbol/ticker ib (ib_async.IB): ib_async.IB instance + symbol (str): company symbol/ticker + exchange (str, optional): exchange. Defaults to "SMART". + currency (str, optional): currency. Defaults to "USD". """ - self.data = FundamentalData(symbol=symbol, ib=ib) + self.data = FundamentalData( + symbol=symbol, ib=ib, exchange=exchange, currency=currency + ) def __repr__(self): cls_name = self.__class__.__qualname__ diff --git a/ib_fundamental/ib_client.py b/ib_fundamental/ib_client.py index 3aaaf19..ef81426 100644 --- a/ib_fundamental/ib_client.py +++ b/ib_fundamental/ib_client.py @@ -39,14 +39,22 @@ class IBClient: # pylint: disable=too-many-arguments def __init__( - self, - symbol: str, - ib: IB, + self, ib: IB, symbol: str, exchange: str = "SMART", currency: str = "USD" ): + """_summary_ + Args: + ib (IB): ib async connection + symbol (str): symbol + exchange (str, optional): exchange. Defaults to "SMART". + currency (str, optional): currency. Defaults to "USD". + + Raises: + ValueError: on invalid symbol, IB not connected + """ self.ib = ib if symbol: - self.contract: Stock = self.make_contract(symbol) + self.contract: Stock = self.make_contract(symbol, exchange, currency) self.symbol: str = symbol else: raise ValueError("No symbol defined.") diff --git a/ib_fundamental/xml_parser.py b/ib_fundamental/xml_parser.py index 7e033da..4d410c7 100644 --- a/ib_fundamental/xml_parser.py +++ b/ib_fundamental/xml_parser.py @@ -397,11 +397,11 @@ def get_company_info(self) -> CompanyInfo: "code": r.attrib["Code"] for r in fs.findall("./Issues/Issue/Exchange") } _company_info = CompanyInfo( - ticker=issue_id["Ticker"], - company_name=coids["CompanyName"], - cik=coids["CIKNo"], - exchange_code=exchange_code["code"], - exchange=exchange["Exchange"], - irs=coids["IRSNo"], + ticker=issue_id.get("Ticker"), + company_name=coids.get("CompanyName"), + cik=coids.get("CIKNo"), + exchange_code=exchange_code.get("code"), + exchange=exchange.get("Exchange"), + irs=coids.get("IRSNo"), ) return _company_info diff --git a/tests/conftest.py b/tests/conftest.py index c575fd7..8dd731e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -149,7 +149,7 @@ def tws_client(): def ib_client(tws_client, request): """IBClient fixture""" _symbol = request.param - _ib_client = IBClient(symbol=_symbol, ib=tws_client) + _ib_client = IBClient(ib=tws_client, symbol=_symbol) yield _ib_client del _ib_client diff --git a/tests/ib_client_test.py b/tests/ib_client_test.py index 90a88c1..b1fac34 100644 --- a/tests/ib_client_test.py +++ b/tests/ib_client_test.py @@ -23,6 +23,7 @@ from ib_async import IB, FundamentalRatios from ib_fundamental.ib_client import IBClient, Stock, Ticker +from tests.conftest import DJIA class TestIBClient: @@ -41,6 +42,16 @@ def test_make_contract(self, ib_client): assert isinstance(ib_client.contract, Stock) assert ib_client.contract.symbol == ib_client.symbol + def test_make_contract_custom(self, tws_client): + """test IBClient.make_contract with exchange and currency""" + ib = tws_client + ib_cli = IBClient(ib=ib, symbol=DJIA[0], exchange="SMART", currency="USD") + # assert + assert isinstance(ib_cli.contract, Stock) + assert ib_cli.contract.symbol == ib_cli.symbol + assert ib_cli.contract.exchange == "SMART" + assert ib_cli.contract.currency == "USD" + def test_ratios(self, ib_client): """Test FundamentalRatios""" # act