Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for currency and exchange #6

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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