-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathibkr_flex_query_client.py
66 lines (64 loc) · 2.28 KB
/
ibkr_flex_query_client.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
from ibflex import client
import pandas as pd
import xml.etree.ElementTree as ET
def get_ib_flex_report(ib_flex_token, ib_flex_query_id, report_type):
# Get the IB FLEX report
response = client.download(ib_flex_token, ib_flex_query_id)
xml_string = response.decode('utf-8')
root = ET.fromstring(xml_string)
# <CashReport>要素を探索
report_element = root.find(f'FlexStatements/FlexStatement/{report_type}')
# データを格納するためのリストを作成
data_list = []
def extract_data(element):
# currencyが"BASE_SUMMARY"の場合は除外する
if report_type == 'CashReport':
if element.get('currency') == "BASE_SUMMARY":
return
# 株以外のアセットは除外する
elif report_type == 'OpenPositions':
if element.get('assetCategory') != "STK":
return
else:
return False
# データを格納するための辞書
row = {}
# 必要な属性のみ辞書に追加
attributes_to_keep = [
'accountId',
'currency',
'fromDate',
'toDate',
'reportDate',
'endingCash',
'assetCategory',
'subCategory',
'symbol',
'description',
'listingExchange',
'openPrice',
'costBasisPrice',
'costBasisMoney',
'side',
'positionValue',
'fifoPnlUnrealized',
'position'
]
for attr in attributes_to_keep:
if attr in element.attrib:
row[attr] = element.attrib[attr]
# データをリストに追加
data_list.append(row)
# CashReportCurrency要素を抽出する関数を呼び出す
if report_type == 'CashReport':
for report_element in report_element.findall('CashReportCurrency'):
extract_data(report_element)
elif report_type == 'OpenPositions':
for report_element in report_element.findall('OpenPosition'):
extract_data(report_element)
else:
return False
# リストからDataFrameを作成
df = pd.DataFrame(data_list)
#print(df)
return df