-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathib2csv.py
More file actions
50 lines (41 loc) · 1.8 KB
/
ib2csv.py
File metadata and controls
50 lines (41 loc) · 1.8 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
import logging
import polars as pl
import locale
import os
import sys
from utils import SetupLogger, ibConnect, getAccounts, getOpenOrders, getCurrencies, getAssetDetails, ibDisconnect, computeThings
logger = SetupLogger()
logging.getLogger().setLevel(logging.DEBUG)
ipaddr = os.getenv("IPADDR", "127.0.0.1")
BaseCur = ['USD', 'CHF', 'EUR']
app = ibConnect(ipaddr)
if app is None:
print("Cannot reach instance. Please check the IP address to use and your Trader Workstation configuration.\n"
"See https://www.interactivebrokers.com/campus/ibkr-api-page/twsapi-doc/#remote-connection\n"
" https://www.interactivebrokers.com/campus/ibkr-api-page/twsapi-doc/#tws-config-api"
" https://www.interactivebrokers.com/campus/trading-lessons/installing-configuring-tws-for-the-api/\n\n"
"Check also the python module ibapi version, should be 10.14.1 or better.", file=sys.stderr)
sys.exit(-2)
else:
try:
getAccounts(app)
getOpenOrders(app)
getCurrencies(app, BaseCur)
getAssetDetails(app)
ibDisconnect(app)
computeThings(app, BaseCur)
for account in app.accounts:
df = pl.DataFrame(app.portfolios[account])
with pl.Config(
tbl_cell_numeric_alignment="RIGHT",
thousands_separator="",
decimal_separator=locale.localeconv()["decimal_point"],
float_precision=4,
set_tbl_column_data_type_inline=False,
):
filename = f"{account}.csv"
logger.warning(f"Writing CSV file '{filename}'...")
df.write_csv(file=filename, separator='\t', float_precision=4, null_value="")
except Exception as exe:
logger.fatal(type(exe))
sys.exit(-1)