-
Notifications
You must be signed in to change notification settings - Fork 8
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
Adapt for docker compose and add ability to update fees #19
base: main
Are you sure you want to change the base?
Changes from all commits
03f9faf
dd939cd
725831c
b44f539
45658ef
c13b2d7
30c6c47
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -7,15 +7,12 @@ | |||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
def get_cash_amount_from_flex(query): | ||||||||||||||||||||||||
print("Getting cash amount") | ||||||||||||||||||||||||
cash = 0 | ||||||||||||||||||||||||
try: | ||||||||||||||||||||||||
cash += query.FlexStatements[0].CashReport[0].endingCash | ||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||
print(e) | ||||||||||||||||||||||||
try: | ||||||||||||||||||||||||
cash += query.FlexStatements[0].CashReport[0].endingCashPaxos | ||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||
print(e) | ||||||||||||||||||||||||
print(e) | ||||||||||||||||||||||||
return cash | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
@@ -55,10 +52,14 @@ def get_diff(old_acts, new_acts): | |||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
class SyncIBKR: | ||||||||||||||||||||||||
IBKRCATEGORY = "9da3a8a7-4795-43e3-a6db-ccb914189737" | ||||||||||||||||||||||||
IBKRNAME = "Interactive Brokers" | ||||||||||||||||||||||||
IBKRCATEGORY = "66b22c82-a96c-4e4f-aaf2-64b4ca41dda2" | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
def __init__(self, ghost_host, ibkrtoken, ibkrquery, ghost_token, ghost_currency): | ||||||||||||||||||||||||
self.ghost_token = ghost_token | ||||||||||||||||||||||||
def __init__(self, ghost_host, ibkrtoken, ibkrquery, ghost_key, ghost_token, ghost_currency): | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (complexity): The addition of the
Consider simplifying the initialization logic by encapsulating the token handling in a separate method or class. This could help reduce the complexity and improve the readability and maintainability of the code. Separating concerns more clearly would also make it easier to test individual components in isolation. |
||||||||||||||||||||||||
if ghost_token == "" and ghost_key != "": | ||||||||||||||||||||||||
self.ghost_token = self.create_ghost_token(ghost_host, ghost_key) | ||||||||||||||||||||||||
else: | ||||||||||||||||||||||||
self.ghost_token = ghost_token | ||||||||||||||||||||||||
self.ghost_host = ghost_host | ||||||||||||||||||||||||
self.ghost_currency = ghost_currency | ||||||||||||||||||||||||
self.ibkrtoken = ibkrtoken | ||||||||||||||||||||||||
|
@@ -87,6 +88,8 @@ def sync_ibkr(self): | |||||||||||||||||||||||
symbol = trade.symbol.replace(".USD-PAXOS", "") + "USD" | ||||||||||||||||||||||||
elif "VUAA" in trade.symbol: | ||||||||||||||||||||||||
symbol = trade.symbol + ".L" | ||||||||||||||||||||||||
elif "V80A" in trade.symbol: | ||||||||||||||||||||||||
symbol = "VNGA80.MI" | ||||||||||||||||||||||||
Comment on lines
+91
to
+92
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (code_refinement): The handling of specific symbols (e.g., 'V80A') with hardcoded replacements ('VNGA80.MI') could lead to maintainability issues as more exceptions are added. Consider creating a mapping or a more flexible solution for symbol transformations.
Suggested change
|
||||||||||||||||||||||||
if trade.buySell == BuySell.BUY: | ||||||||||||||||||||||||
buysell = "BUY" | ||||||||||||||||||||||||
elif trade.buySell == BuySell.SELL: | ||||||||||||||||||||||||
|
@@ -101,7 +104,7 @@ def sync_ibkr(self): | |||||||||||||||||||||||
"currency": trade.currency, | ||||||||||||||||||||||||
"dataSource": "YAHOO", | ||||||||||||||||||||||||
"date": iso_format, | ||||||||||||||||||||||||
"fee": float(0), | ||||||||||||||||||||||||
"fee": abs(float(self.get_fee_for_trade(trade.tradeID,query.FlexStatements[0].UnbundledCommissionDetails))), | ||||||||||||||||||||||||
"quantity": abs(float(trade.quantity)), | ||||||||||||||||||||||||
"symbol": symbol.replace(" ", "-"), | ||||||||||||||||||||||||
"type": buysell, | ||||||||||||||||||||||||
|
@@ -113,18 +116,46 @@ def sync_ibkr(self): | |||||||||||||||||||||||
print("Nothing new to sync") | ||||||||||||||||||||||||
else: | ||||||||||||||||||||||||
self.import_act(diff) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
def get_fee_for_trade(self, trade_id, commission_details): | ||||||||||||||||||||||||
for commission_detail in commission_details: | ||||||||||||||||||||||||
if commission_detail.tradeID == trade_id: | ||||||||||||||||||||||||
return commission_detail.totalCommission | ||||||||||||||||||||||||
return 0 | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
def create_ghost_token(self, ghost_host, ghost_key): | ||||||||||||||||||||||||
print("No bearer token provided, fetching one") | ||||||||||||||||||||||||
token = { | ||||||||||||||||||||||||
'accessToken': ghost_key | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
url = f"{ghost_host}/api/v1/auth/anonymous" | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
payload = json.dumps(token) | ||||||||||||||||||||||||
headers = { | ||||||||||||||||||||||||
'Content-Type': 'application/json' | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
try: | ||||||||||||||||||||||||
response = requests.request("POST", url, headers=headers, data=payload) | ||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||
print(e) | ||||||||||||||||||||||||
return "" | ||||||||||||||||||||||||
if response.status_code == 201: | ||||||||||||||||||||||||
print("Bearer token fetched") | ||||||||||||||||||||||||
return response.json()["authToken"] | ||||||||||||||||||||||||
print("Failed fetching bearer token") | ||||||||||||||||||||||||
return "" | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
def set_cash_to_account(self, account_id, cash): | ||||||||||||||||||||||||
if cash == 0: | ||||||||||||||||||||||||
if cash <= 1: | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question (code_clarification): Changing the condition to 'cash <= 1' for setting cash to an account might not be intuitive. Consider adding a comment to explain the rationale behind using '1' as the threshold, especially if it's related to avoiding setting negligible cash amounts.
Suggested change
|
||||||||||||||||||||||||
print("No cash set, no cash retrieved") | ||||||||||||||||||||||||
return False | ||||||||||||||||||||||||
account = { | ||||||||||||||||||||||||
"accountType": "SECURITIES", | ||||||||||||||||||||||||
"balance": float(cash), | ||||||||||||||||||||||||
"id": account_id, | ||||||||||||||||||||||||
"currency": self.ghost_currency, | ||||||||||||||||||||||||
"isExcluded": False, | ||||||||||||||||||||||||
"name": "IBKR", | ||||||||||||||||||||||||
"name": self.IBKRNAME, | ||||||||||||||||||||||||
"platformId": self.IBKRCATEGORY | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
@@ -206,13 +237,13 @@ def addAct(self, act): | |||||||||||||||||||||||
return response.status_code == 201 | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
def create_ibkr_account(self): | ||||||||||||||||||||||||
print("Creating IBKR account") | ||||||||||||||||||||||||
account = { | ||||||||||||||||||||||||
"accountType": "SECURITIES", | ||||||||||||||||||||||||
"balance": 0, | ||||||||||||||||||||||||
"currency": self.ghost_currency, | ||||||||||||||||||||||||
"isExcluded": False, | ||||||||||||||||||||||||
"name": "IBKR", | ||||||||||||||||||||||||
"platformId": "9da3a8a7-4795-43e3-a6db-ccb914189737" | ||||||||||||||||||||||||
"name": self.IBKRNAME, | ||||||||||||||||||||||||
"platformId": self.IBKRCATEGORY | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
url = f"{self.ghost_host}/api/v1/account" | ||||||||||||||||||||||||
|
@@ -228,11 +259,13 @@ def create_ibkr_account(self): | |||||||||||||||||||||||
print(e) | ||||||||||||||||||||||||
return "" | ||||||||||||||||||||||||
if response.status_code == 201: | ||||||||||||||||||||||||
print("IBKR account: " + response.json()["id"]) | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚨 suggestion (security): Logging the account ID upon creation is useful for tracking and debugging. However, ensure that this does not pose a security risk by inadvertently exposing sensitive information in logs.
Suggested change
|
||||||||||||||||||||||||
return response.json()["id"] | ||||||||||||||||||||||||
print("Failed creating ") | ||||||||||||||||||||||||
return "" | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
def get_account(self): | ||||||||||||||||||||||||
print("Finding IBKR account") | ||||||||||||||||||||||||
url = f"{self.ghost_host}/api/v1/account" | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
payload = {} | ||||||||||||||||||||||||
|
@@ -252,7 +285,8 @@ def get_account(self): | |||||||||||||||||||||||
def create_or_get_IBKR_accountId(self): | ||||||||||||||||||||||||
accounts = self.get_account() | ||||||||||||||||||||||||
for account in accounts: | ||||||||||||||||||||||||
if account["name"] == "IBKR": | ||||||||||||||||||||||||
if account["name"] == self.IBKRNAME: | ||||||||||||||||||||||||
print("IBKR account: ", account["id"]) | ||||||||||||||||||||||||
return account["id"] | ||||||||||||||||||||||||
return self.create_ibkr_account() | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (code_refinement): Consider removing or adjusting the debug print statement for production code to maintain a clean and professional output. If this information is crucial, consider implementing a logging framework with appropriate log levels.