forked from agusalex/ghostfolio-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SyncIBKR.py
302 lines (263 loc) · 9.63 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
from time import sleep
import requests
from ibflex import client, parser, FlexQueryResponse, BuySell
from datetime import datetime
import json
def get_cash_amount_from_flex(query):
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)
return cash
def generate_chunks(lst, n):
for i in range(0, len(lst), n):
yield lst[i:i + n]
def format_act(act):
symbol_nested = act.get("SymbolProfile", {"symbol": ""}).get("symbol")
return {
"accountId": act["accountId"],
"date": act["date"][0:18],
"fee": float(act["fee"]),
"quantity": act["quantity"],
"symbol": act.get("symbol", symbol_nested),
"type": act["type"],
"unitPrice": act["unitPrice"]
}
def is_act_present(act_search, acts):
for act in acts:
act1 = format_act(act)
act2 = format_act(act_search)
if act1 == act2:
return True
return False
def get_diff(old_acts, new_acts):
diff = []
for new_act in new_acts:
if not is_act_present(new_act, old_acts):
diff.append(new_act)
return diff
class SyncIBKR:
IBKRCATEGORY = "9da3a8a7-4795-43e3-a6db-ccb914189737"
def __init__(self, ghost_host, ibkrtoken, ibkrquery, ghost_token, ghost_currency):
self.ghost_token = ghost_token
self.ghost_host = ghost_host
self.ghost_currency = ghost_currency
self.ibkrtoken = ibkrtoken
self.ibkrquery = ibkrquery
def sync_ibkr(self):
print("Fetching Query")
response = client.download(self.ibkrtoken, self.ibkrquery)
print("Parsing Query")
query: FlexQueryResponse = parser.parse(response)
activities = []
date_format = "%Y-%m-%d"
account_id = self.create_or_get_IBKR_accountId()
if account_id == "":
print("Failed to retrieve account ID closing now")
return
self.set_cash_to_account(account_id, get_cash_amount_from_flex(query))
for trade in query.FlexStatements[0].Trades:
if trade.openCloseIndicator is None:
print("trade is not open or close (ignoring): %s", trade)
elif trade.openCloseIndicator.CLOSE:
date = datetime.strptime(str(trade.tradeDate), date_format)
iso_format = date.isoformat()
symbol = trade.symbol
if ".USD-PAXOS" in trade.symbol:
symbol = trade.symbol.replace(".USD-PAXOS", "") + "USD"
elif "VUAA" in trade.symbol:
symbol = trade.symbol + ".L"
if trade.buySell == BuySell.BUY:
buysell = "BUY"
elif trade.buySell == BuySell.SELL:
buysell = "SELL"
else:
print("trade is not buy or sell (ignoring): %s", trade)
continue
activities.append({
"accountId": account_id,
"comment": None,
"currency": trade.currency,
"dataSource": "YAHOO",
"date": iso_format,
"fee": float(0),
"quantity": abs(float(trade.quantity)),
"symbol": symbol.replace(" ", "-"),
"type": buysell,
"unitPrice": float(trade.tradePrice)
})
diff = get_diff(self.get_all_acts_for_account(account_id), activities)
if len(diff) == 0:
print("Nothing new to sync")
else:
self.import_act(diff)
def set_cash_to_account(self, account_id, cash):
if cash == 0:
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",
"platformId": self.IBKRCATEGORY
}
url = f"{self.ghost_host}/api/v1/account/{account_id}"
payload = json.dumps(account)
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:
print(e)
return False
if response.status_code == 200:
print(f"Updated Cash for account {response.json()['id']}")
else:
print("Failed create: " + response.text)
return response.status_code == 200
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:
print(e)
return False
return response.status_code == 200
def import_act(self, bulk):
chunks = generate_chunks(bulk, 10)
for acts in chunks:
url = f"{self.ghost_host}/api/v1/import"
formatted_acts = json.dumps({"activities": sorted(acts, key=lambda x: x["date"])})
payload = formatted_acts
headers = {
'Authorization': f"Bearer {self.ghost_token}",
'Content-Type': 'application/json'
}
print("Adding activities: " + formatted_acts)
try:
response = requests.request("POST", url, headers=headers, data=payload)
except Exception as e:
print(e)
return False
if response.status_code == 201:
print(f"created {formatted_acts}")
else:
print("Failed 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'
}
print("Adding activity: " + json.dumps(act))
try:
response = requests.request("POST", url, headers=headers, data=payload)
except Exception as e:
print(e)
return False
if response.status_code == 201:
print(f"created {response.json()['id']}")
else:
print("Failed create: " + response.text)
return response.status_code == 201
def create_ibkr_account(self):
account = {
"accountType": "SECURITIES",
"balance": 0,
"currency": self.ghost_currency,
"isExcluded": False,
"name": "IBKR",
"platformId": "9da3a8a7-4795-43e3-a6db-ccb914189737"
}
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:
print(e)
return ""
if response.status_code == 201:
return response.json()["id"]
print("Failed creating ")
return ""
def get_account(self):
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:
print(e)
return []
if response.status_code == 200:
return response.json()['accounts']
else:
raise Exception(response)
def create_or_get_IBKR_accountId(self):
accounts = self.get_account()
for account in accounts:
if account["name"] == "IBKR":
return account["id"]
return self.create_ibkr_account()
def delete_all_acts(self):
account_id = self.create_or_get_IBKR_accountId()
acts = self.get_all_acts_for_account(account_id)
if not acts:
print("No activities to delete")
return True
complete = True
for act in acts:
if act['accountId'] == account_id:
act_complete = self.delete_act(act['id'])
complete = complete and act_complete
if act_complete:
print("Deleted: " + act['id'])
else:
print("Failed Delete: " + act['id'])
return complete
def get_all_acts_for_account(self, account_id):
acts = self.get_all_acts()
filtered_acts = []
for act in acts:
if act['accountId'] == account_id:
filtered_acts.append(act)
return filtered_acts
def get_all_acts(self):
url = f"{self.ghost_host}/api/v1/order"
payload = {}
headers = {
'Authorization': f"Bearer {self.ghost_token}",
}
try:
response = requests.request("GET", url, headers=headers, data=payload)
except Exception as e:
print(e)
return []
if response.status_code == 200:
return response.json()['activities']
else:
return []