-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
260 lines (211 loc) · 10.5 KB
/
utils.py
File metadata and controls
260 lines (211 loc) · 10.5 KB
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
import sys
import logging
from wrapper import TradeApp
import threading
import time
from ibapi.contract import Contract
from typing import Dict, List, Optional, Any
import os
from cachedfaz import CachedFrankfurter
basedir = os.getenv("GT_DG_DIRECTORY") or "."
forex_api = CachedFrankfurter(os.path.join(basedir, "cacheFrankfurter.bin"))
logger = logging.getLogger()
def SetupLogger() -> logging.Logger:
logging.basicConfig(handlers=[logging.StreamHandler(sys.stdout)],
level=logging.DEBUG,
format="%(asctime)s - %(name)s:%(filename)s:%(funcName)s:%(lineno)d - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",)
logger = logging.getLogger()
return logger
def ibConnect(ipaddr: str) -> Optional[TradeApp]:
'''
see https://www.interactivebrokers.com/campus/ibkr-api-page/twsapi-doc/#remote-connection
'''
app = TradeApp()
app.connect(ipaddr, 7496, clientId=app.reqId)
time.sleep(1)
api_thread = threading.Thread(target=app.run)
time.sleep(.1)
api_thread.start()
time.sleep(1)
if app.isConnected():
return app
logger.warning("Could not connect to TWS!")
return None
def getAccounts(app: TradeApp) -> Dict[str, Dict[str, Any]]:
app.reqMarketDataType(4)
time.sleep(.1)
app.reqAccountSummary(0, "All", "AccountType")
time.sleep(1)
logger.warning(f"Found the following accounts: {app.accounts}")
for account in app.accounts.keys():
app.reqAccountUpdates(True, account)
time.sleep(1)
numLines = 0
for account in app.accounts.keys():
# print(app.accounts[account])
app.reqAccountUpdates(False, account)
time.sleep(.1)
numLines += len(app.portfolios[account])
logger.warning(f"Found {numLines} lines in all portfolios")
return app.accounts
def getOpenOrders(app: TradeApp) -> Dict[int, Dict[str, Any]]:
time.sleep(1)
app.reqAllOpenOrders()
time.sleep(1)
numLines = 0
for account in app.accounts.keys():
numLines += len(app.portfolios[account])
logger.warning(f"Found {numLines} lines including live orders")
return app.orders
def getCurrencies(app: TradeApp, BaseCur: List[str], cooked: Optional[Dict[str, float]] = None) -> Dict[str, float]:
app.reqMarketDataType(4)
time.sleep(.1)
# preparing data for currency conversion
BaseCur = ['USD', 'CHF', 'EUR']
otherCur = set()
otherCur.update(BaseCur)
for account in app.portfolios.keys():
for line in app.portfolios[account]:
otherCur.add(line['currency'])
app.currency = dict()
for cur in list(otherCur):
if len(cur) == 3: # forbid "BASE" fake currency
app.currency[cur] = -1 if cur != "USD" else 1
query2Cancel = list()
# trying to convert 1 USD in local currency
for cur in app.currency.keys():
if cur != 'USD':
c = Contract()
c.symbol = 'USD'
c.secType = 'CASH'
c.exchange = "IDEALPRO"
c.primaryExchange = "IDEALPRO"
c.currency = cur
id = list(app.currency.keys()).index(cur)
query2Cancel.append(id)
logger.debug(f"Trying to convert USD in {cur} (reqID:{id})")
app.reqMktData(id, c, "", True, False, [])
time.sleep(.1)
time.sleep(.5)
# in case of failure, trying to convert local currency to USD
for cur in app.currency.keys():
if cur != 'USD' and app.currency[cur] < 0:
c = Contract()
c.currency = 'USD'
c.secType = 'CASH'
c.exchange = "IDEALPRO"
c.primaryExchange = "IDEALPRO"
c.symbol = cur
id = 1000 + list(app.currency.keys()).index(cur)
query2Cancel.append(id)
logger.debug(f"Trying to convert {cur} in USD (reqID:{id})")
app.reqMktData(id, c, "", True, False, [])
time.sleep(.1)
time.sleep(.5)
for q in query2Cancel:
app.cancelAccountSummary(q)
time.sleep(.1)
if cooked:
app.currency = {**app.currency, **cooked}
for cur in app.currency.keys():
rate = app.currency[cur]
if rate <= 0:
app.currency[cur] = forex_api.convert(cur, "USD")
for cur in app.currency.keys():
if app.currency[cur] < 0:
logger.error(f"Could not manage to get currency {cur}.USD -- assessement may be wrong")
logger.warning(f"Managed to get following currencies (base 1=USD): {app.currency}")
return app.currency
def getAssetDetails(app: TradeApp) -> None:
for account in app.portfolios.keys():
logger.info("GETTING STOCK DETAILS AND PRICE MAGNIFIER")
app.contract = dict()
for idx, line in enumerate(app.portfolios[account]):
c = Contract()
c.conId = line['conId']
c.secType = line['secType']
c.exchange = line['primaryExchange']
c.primaryExchange = line['primaryExchange']
c.symbol = line['symbol']
c.currency = line['currency']
c.localSymbol = line['localSymbol']
app.reqContractDetails(2000 + idx, c)
time.sleep(.1)
time.sleep(2)
for idx, line in enumerate(app.portfolios[account]):
if idx in app.contract.keys():
contractDetails = app.contract[idx]
pfcontract = {}
pfcontract['stockType'] = getattr(contractDetails, 'stockType', "")
# if pfcontract['stockType'] != "RIGHT": # issuance of new shares - not a portofio position
pfcontract['longName'] = getattr(contractDetails, 'longName', "")
pfcontract['industry'] = getattr(contractDetails, 'industry', "")
pfcontract['category'] = getattr(contractDetails, 'category', "")
pfcontract['subcategory'] = getattr(contractDetails, 'subcategory', "")
pfcontract['priceMagnifier'] = int(getattr(contractDetails, 'priceMagnifier', 1.0))
pfcontract['minSize'] = float(getattr(contractDetails, 'minSize', 0.0001))
pfcontract['sizeIncrement'] = float(getattr(contractDetails, 'sizeIncrement', 1.0))
pfcontract['minTick'] = float(getattr(contractDetails, 'minTick', 0.0001))
app.portfolios[account][idx] = { **app.portfolios[account][idx], **pfcontract }
else:
logger.error(f"Missing contract {app.portfolios[account][idx]['localSymbol']}")
def ibDisconnect(app: TradeApp) -> None:
app.disconnect()
def computeThings(app: TradeApp, BaseCur: List[str]) -> Dict[str, List[Dict[str, Any]]]:
for account in app.portfolios.keys():
for k in app.accounts[account].keys():
if k.startswith('NetLiquidation.'):
logger.info(f"NetLiquidation k:'{k}' val:'{app.accounts[account][k]}' ")
grandtotalCur = k[len(k) - 3:]
grandtotalVal = app.accounts[account][k]
app.portfolios[account].append({
'orderAct': '',
'orderVal': 0,
'orderPos': 0,
'secType': "TOTAL",
'currency': grandtotalCur,
'marketValue': grandtotalVal,
})
break
totalAsset = dict(zip(BaseCur, [0] * len(BaseCur)))
usd = BaseCur[0]
marketValueUSD = f"marketValue.{usd}"
for idx, line in enumerate(app.portfolios[account]):
# action = line['orderAct']
# if action != "": # line from the order book
# try:
# # applying magnifier for ongoing orders only (for example prices expressed in penny /100p -> 1£)
# line['orderVal'] /= app.portfolios[account][idx]['priceMagnifier']
# except:
# logger.info(f"cannot apply magnifier for {app.portfolios[account][idx]['symbol']} current order")
for column in ['marketPrice', 'marketValue', 'averageCost', 'unrealizedPNL', 'realizedPNL', 'orderVal']:
for currency in BaseCur:
if column in line:
cur = line['currency']
if app.currency[cur] > 0: # value is neg if conversion is unknown
val = line[column]
valCur = val / app.currency[cur] * app.currency[currency]
# valCur /= magnifier if column in ['marketPrice', 'averageCost'] else 1.0
# app.portfolios[account][idx][f"{column}.{currency}"] = valCur
line[f"{column}.{currency}"] = valCur
logger.debug(f"convert currency {val} {cur} -> {valCur} {currency}, conversion rate {app.currency[cur] * app.currency[currency]}")
if column == 'marketValue' and line['secType'] not in ['TOTAL', 'CASH']:
totalAsset[currency] += valCur
if len(app.portfolios[account]) > 0 and marketValueUSD in app.portfolios[account][-1]:
for idx, line in enumerate(app.portfolios[account]):
action = line['orderAct']
if line['secType'] != "TOTAL" and action == "" and marketValueUSD in line: # line from the portfolio
line['pct'] = line[marketValueUSD] / app.portfolios[account][-1][marketValueUSD] * 100.0
# else:
# logger.warning(f"missing computation for asset id {idx} - won't compute percentages")
# else:
# logger.warning(f"missing grand total in usd - won't compute percentages")
app.portfolios[account].append({'orderAct': '', 'secType': "CASH"})
logger.debug(f"-1 {app.portfolios[account][-1]}") # CASH line
logger.debug(f"-2 {app.portfolios[account][-2]}") # TOTAL line
logger.debug(f"totalAsset {totalAsset}") # value of all assets
for currency in BaseCur:
app.portfolios[account][-1][f"marketValue.{currency}"] = app.portfolios[account][-2][f"marketValue.{currency}"] - totalAsset[currency]
app.portfolios[account][-1]['pct'] = 100.0 * (app.portfolios[account][-2][marketValueUSD] - totalAsset[usd]) / app.portfolios[account][-2][marketValueUSD]
return app.portfolios