Skip to content

Commit

Permalink
Support for currency and exchange (#6)
Browse files Browse the repository at this point in the history
* Support for currency and exchange

- add currency and exchange parameters to `CompanyFinancials` and `FundamentalData`
- bump version to `0.0.4`
* update README.md
  • Loading branch information
gnzsnz authored Aug 26, 2024
1 parent 4d3144c commit 144d359
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 22 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ib_fundamental/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"


Expand Down
29 changes: 20 additions & 9 deletions ib_fundamental/fundamental.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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__
Expand Down
16 changes: 12 additions & 4 deletions ib_fundamental/ib_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
12 changes: 6 additions & 6 deletions ib_fundamental/xml_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions tests/ib_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down

0 comments on commit 144d359

Please sign in to comment.