-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathSyncIBKR.py
445 lines (380 loc) · 15.9 KB
/
SyncIBKR.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import json
import re
from datetime import datetime
from typing import Optional
import requests
import yaml
from ibflex import client, parser, FlexQueryResponse, BuySell, FlexStatement, Trade
# Create logger
import logging
logger = logging.getLogger(__name__)
def get_cash_amount_from_flex(account_statement: FlexStatement) -> dict:
logger.info("Getting cash amount")
base_currency = account_statement.AccountInformation.currency
logger.info("Base currency: %s", base_currency)
cash = {}
for cash_report_currency in account_statement.CashReport:
if cash_report_currency.currency == "BASE_SUMMARY":
try:
cash[base_currency] = float(cash_report_currency.endingCash)
logger.info("Cash amount: %s", cash[base_currency])
return cash
except Exception as e:
logger.info(e)
return cash
def generate_chunks(lst, n):
for i in range(0, len(lst), n):
yield lst[i:i + n]
def format_existing_act(act: dict, symbol_type: str = "symbol") -> dict:
symbol = act.get("SymbolProfile", {symbol_type: ""}).get(symbol_type)
if symbol is None or len(symbol) == 0:
logger.warning("Could not find nested symbol type %s for activity %s: %s",
symbol_type, act["id"], act.get("SymbolProfile"))
symbol = act.get("symbol", "")
return {
"accountId": act["accountId"],
"date": act["date"][0:18],
"fee": abs(float(act["fee"])),
"quantity": abs(float(act["quantity"])),
"symbol": symbol,
"type": act["type"],
"unitPrice": act["unitPrice"]
}
def format_new_act(act: dict, symbol_type: str = "symbol") -> dict:
return {
"accountId": act["accountId"],
"date": act["date"][0:18],
"fee": abs(float(act["fee"])),
"quantity": abs(float(act["quantity"])),
"symbol": act.get(symbol_type, ""),
"type": act["type"],
"unitPrice": act["unitPrice"]
}
def is_act_present(new_act, existing_acts, synced_acts_ids: set):
# Precise comparison using the IBKR trade id
comment = new_act["comment"]
if comment is not None:
# Extract the tradeID from the comment using regular expressions
match = re.search(r"tradeID=(\d+)", comment)
if match:
trade_id = match.group(1)
if trade_id in synced_acts_ids:
return True
# Legacy comparison
for existing_act in existing_acts:
formatted_act = format_existing_act(existing_act, "figi")
formatted_new_act = format_new_act(new_act, "figi")
if formatted_act == formatted_new_act:
return True
formatted_act = format_existing_act(existing_act, "isin")
formatted_new_act = format_new_act(new_act)
if formatted_act == formatted_new_act:
return True
formatted_act = format_existing_act(existing_act)
formatted_new_act = format_new_act(new_act, "ibkrSymbol")
if formatted_act == formatted_new_act:
return True
return False
def get_diff(old_acts, new_acts):
diff = []
synced_acts_ids = set()
for old_act in old_acts:
comment = old_act["comment"]
if comment is not None:
# Extract the tradeID from the comment using regular expressions
match = re.search(r"tradeID=(\d+)", comment)
if match:
trade_id = match.group(1)
synced_acts_ids.add(trade_id)
for new_act in new_acts:
if not is_act_present(new_act, old_acts, synced_acts_ids):
del new_act["figi"]
del new_act["ibkrSymbol"]
diff.append(new_act)
return diff
class SyncIBKR:
#IBKRCATEGORY = "66b22c82-a96c-4e4f-aaf2-64b4ca41dda2"
def __init__(self, ghost_host, ibkrtoken, ibkrquery, ghost_key, ghost_token, ibkr_account_id, ghost_account_name, ghost_currency, ghost_ibkr_platform, mapping_file='mapping.yaml'):
self.account_id: Optional[str] = None
if ghost_token == "" and ghost_key != "":
self.ghost_token = self.create_ghost_token(ghost_host, ghost_key)
else:
self.ghost_token = ghost_token
if self.ghost_token is None or self.ghost_token == "":
logger.info("No bearer token provided, closing now")
raise Exception("No bearer token provided")
self.ghost_host = ghost_host
self.ibkr_account_id = ibkr_account_id
self.ghost_account_name = ghost_account_name
self.ghost_currency = ghost_currency
self.ibkrtoken = ibkrtoken
self.ibkrquery = ibkrquery
self.ibkrplatform = ghost_ibkr_platform
# Load the configuration file
with open(mapping_file, 'r') as file:
config = yaml.safe_load(file)
# Extract the symbol mapping from the configuration
self.symbol_mapping = config.get('symbol_mapping', {})
def sync_ibkr(self):
logger.info("Fetching Query")
response = client.download(self.ibkrtoken, self.ibkrquery)
#logger.info("Parsing Query:\n%s", response)
query: FlexQueryResponse = parser.parse(response)
account_statement = self.get_account_flex_statement(query)
activities = []
date_format = "%Y-%m-%d %H:%M:%S"
data_source = "YAHOO"
try:
self.ghost_currency = account_statement.AccountInformation.currency
except Exception as e:
logger.error("Error getting currency from IBKR account statement: %s", e)
account_id = self.create_or_get_IBKR_accountId()
if account_id == "":
logger.info("Failed to retrieve account ID closing now")
return
self.set_cash_to_account(account_id, get_cash_amount_from_flex(account_statement))
for trade in account_statement.Trades:
if trade.openCloseIndicator is None:
logger.info("trade is not open or close (ignoring): %s", trade)
elif trade.openCloseIndicator.CLOSE:
date = datetime.strptime(str(trade.dateTime), date_format)
iso_format = date.isoformat()
symbol = self.get_symbol_for_trade(trade, data_source)
if trade.buySell == BuySell.BUY:
buysell = "BUY"
elif trade.buySell == BuySell.SELL:
buysell = "SELL"
else:
logger.info("trade is not buy or sell (ignoring): %s", trade)
continue
activities.append({
"accountId": account_id,
"comment": f"tradeID={trade.tradeID}",
"currency": trade.currency,
"dataSource": data_source,
"date": iso_format,
"fee": abs(float(trade.ibCommission)),
"quantity": abs(float(trade.quantity)),
"symbol": symbol.replace(" ", "-"),
"type": buysell,
"unitPrice": float(trade.tradePrice),
"figi": trade.figi,
"ibkrSymbol": self.symbol_mapping[trade.symbol] if trade.symbol in self.symbol_mapping else trade.symbol
})
diff = get_diff(self.get_all_acts_for_account(), activities)
if len(diff) == 0:
logger.info("Nothing new to sync")
else:
self.import_act(diff)
def get_symbol_for_trade(self, trade: Trade, data_source: str):
symbol = trade.symbol
if data_source == "YAHOO":
if trade.isin is not None and len(trade.isin) > 0:
symbol = trade.isin # ISIN provides better mapping
if symbol in self.symbol_mapping:
logger.info("Transformed symbol %s into %s", symbol, self.symbol_mapping[symbol])
symbol = self.symbol_mapping[symbol]
else:
logger.info("Symbol %s not found in mapping.", symbol)
return symbol
def create_ghost_token(self, ghost_host, ghost_key):
logger.info("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:
logger.info(e)
return ""
if response.status_code == 201:
logger.info("Bearer token fetched")
return response.json()["authToken"]
logger.info("Failed fetching bearer token")
return ""
def set_cash_to_account(self, account_id, cash: dict):
if cash is None or len(cash) == 0:
logger.info("No cash set, no cash retrieved")
return False
for currency, amount in cash.items():
amount = {
"balance": amount,
"id": account_id,
"currency": currency,
"isExcluded": False,
"name": self.ghost_account_name,
"platformId": self.ibkrplatform
}
logger.info("Updating Cash for account " + account_id + ": " + json.dumps(amount))
url = f"{self.ghost_host}/api/v1/account/{account_id}"
payload = json.dumps(amount)
headers = {
'Authorization': f"Bearer {self.ghost_token}",
'Content-Type': 'application/json'
}
try:
response = requests.request("PUT", url, headers=headers, data=payload)
except Exception as e:
logger.info(e)
return
if response.status_code == 200:
logger.info(f"Updated Cash for account {response.json()['id']}")
else:
logger.info("Failed create: " + response.text)
def delete_act(self, act_id):
url = f"{self.ghost_host}/api/v1/order/{act_id}"
payload = {}
headers = {
'Authorization': f"Bearer {self.ghost_token}",
}
try:
response = requests.request("DELETE", url, headers=headers, data=payload)
except Exception as e:
logger.info(e)
return False
return response.status_code == 200
def import_act(self, bulk):
chunks = generate_chunks(sorted(bulk, key=lambda x: x["date"]), 10)
for acts in chunks:
logger.info("Adding activities:\n%s", json.dumps(acts, indent=4))
url = f"{self.ghost_host}/api/v1/import"
payload = json.dumps({"activities": acts})
headers = {
'Authorization': f"Bearer {self.ghost_token}",
'Content-Type': 'application/json'
}
try:
response = requests.request("POST", url, headers=headers, data=payload)
except Exception as e:
logger.info(e)
return False
if response.status_code == 201:
logger.info("Added activities. Response:\n%s", json.dumps(response.json(), indent=4))
else:
logger.info("Failed to create: " + response.text)
if response.status_code != 201:
return False
return True
def addAct(self, act):
url = f"{self.ghost_host}/api/v1/order"
payload = json.dumps(act)
headers = {
'Authorization': f"Bearer {self.ghost_token}",
'Content-Type': 'application/json'
}
logger.info("Adding activity: " + json.dumps(act))
try:
response = requests.request("POST", url, headers=headers, data=payload)
except Exception as e:
logger.info(e)
return False
if response.status_code == 201:
logger.info(f"created {response.json()['id']}")
else:
logger.info("Failed create: " + response.text)
return response.status_code == 201
def create_ibkr_account(self):
logger.info("Creating IBKR account")
account = {
"balance": 0,
"currency": self.ghost_currency,
"isExcluded": False,
"name": self.ghost_account_name,
"platformId": self.ibkrplatform
}
url = f"{self.ghost_host}/api/v1/account"
payload = json.dumps(account)
headers = {
'Authorization': f"Bearer {self.ghost_token}",
'Content-Type': 'application/json'
}
try:
response = requests.request("POST", url, headers=headers, data=payload)
except Exception as e:
logger.info(e)
return ""
if response.status_code == 201:
logger.info("IBKR account: " + response.json()["id"])
return response.json()["id"]
logger.info("Failed creating ")
return ""
def get_all_accounts(self):
logger.info("Finding all accounts")
url = f"{self.ghost_host}/api/v1/account"
payload = {}
headers = {
'Authorization': f"Bearer {self.ghost_token}",
}
try:
response = requests.request("GET", url, headers=headers, data=payload)
except Exception as e:
logger.info(e)
return []
if response.status_code == 200:
return response.json()['accounts']
else:
raise Exception(response)
def create_or_get_IBKR_accountId(self):
if self.account_id is not None:
return self.account_id
accounts = self.get_all_accounts()
logger.info("Accounts: %s", json.dumps(accounts, indent=4))
for account in accounts:
if account["name"] == self.ghost_account_name:
logger.info("IBKR account: %s", account["id"])
self.account_id = account["id"]
return account["id"]
self.account_id = self.create_ibkr_account()
return self.account_id
def delete_all_acts(self):
acts = self.get_all_acts_for_account()
if not acts:
logger.info("No activities to delete")
return True
url = f"{self.ghost_host}/api/v1/order"
payload = {}
headers = {
'Authorization': f"Bearer {self.ghost_token}",
}
try:
response = requests.request("DELETE",
url,
headers=headers,
params={"accounts": self.create_or_get_IBKR_accountId()},
data=payload)
except Exception as e:
logger.info(e)
return False
return response.status_code == 200
def get_all_acts_for_account(self, account_id: str = None, range: str = None, symbol: str = None):
if account_id is None:
account_id = self.create_or_get_IBKR_accountId()
url = f"{self.ghost_host}/api/v1/order"
payload = {}
headers = {
'Authorization': f"Bearer {self.ghost_token}",
}
try:
response = requests.request("GET",
url,
headers=headers,
params={"accounts": account_id,
"range": range, # https://github.com/ghostfolio/ghostfolio/blob/main/libs/common/src/lib/types/date-range.type.ts
"symbol": symbol},
data=payload)
except Exception as e:
logger.info(e)
return []
if response.status_code == 200:
return response.json()['activities']
else:
return []
def get_account_flex_statement(self, query: FlexQueryResponse) -> FlexStatement:
return next(
(flex_statement for flex_statement in query.FlexStatements if flex_statement.accountId == self.ibkr_account_id),
None)