parsing interactive broker fundamental data

2024/7/7 14:24:12

I've successfully pulled data from IB using the api. It comes in XML format and it looks like this...

 <TotalRevenues currency="USD"><TotalRevenue asofDate="2017-12-31" reportType="TTM" period="12M">239176000000.000000</TotalRevenue><TotalRevenue asofDate="2017-09-30" reportType="TTM" period="12M">229234000000.000000</TotalRevenue><TotalRevenue asofDate="2017-06-30" reportType="TTM" period="12M">223507000000.000000</TotalRevenue>
</TotalRevenues><DividendPerShares currency="USD"><DividendPerShare asofDate="2017-12-31" reportType="A" period="3M">0.630000</DividendPerShare><DividendPerShare asofDate="2017-09-30" reportType="A" period="3M">0.630000</DividendPerShare><DividendPerShare asofDate="2017-09-30" reportType="A" period="12M">2.400000</DividendPerShare></DividendPerShares><Dividends currency="USD"><Dividend type="CD" exDate="2018-02-09" recordDate="2018-02-12" payDate="2018-02-15" declarationDate="2018-02-01">0.630000</Dividend><Dividend type="CD" exDate="2017-11-10" recordDate="2017-11-13" payDate="2017-11-16" declarationDate="2017-11-03">0.630000</Dividend><Dividend type="CD" exDate="2017-08-10" recordDate="2017-08-14" payDate="2017-08-17" declarationDate="2017-07-02">0.630000</Dividend></Dividends><EPSs currency="USD"><EPS asofDate="2017-12-31" reportType="A" period="3M">3.920000</EPS><EPS asofDate="2017-09-30" reportType="A" period="3M">2.090000</EPS><EPS asofDate="2017-09-30" reportType="A" period="12M">9.270000</EPS></EPSs>
</FinancialSummary>

I want to convert this information to CSV format in this format:

            total revenue report type     period rev dividendpershare period div
2017-12-31  239176000000     ttm           12m        0.630000          3m
2017-09-30   229234000000    ttm           12m        0.630000          3m

is there an easy way to do this?

Answer

You can try this:

import csv
import xmltodictXML = """
<FinancialSummary><TotalRevenues currency="USD"><TotalRevenue asofDate="2017-12-31" reportType="TTM" period="12M">239176000000.000000</TotalRevenue><TotalRevenue asofDate="2017-09-30" reportType="TTM" period="12M">229234000000.000000</TotalRevenue><TotalRevenue asofDate="2017-06-30" reportType="TTM" period="12M">223507000000.000000</TotalRevenue></TotalRevenues><DividendPerShares currency="USD"><DividendPerShare asofDate="2017-12-31" reportType="A" period="3M">0.630000</DividendPerShare><DividendPerShare asofDate="2017-09-30" reportType="A" period="3M">0.630000</DividendPerShare><DividendPerShare asofDate="2017-09-30" reportType="A" period="12M">2.400000</DividendPerShare></DividendPerShares><Dividends currency="USD"><Dividend type="CD" exDate="2018-02-09" recordDate="2018-02-12" payDate="2018-02-15" declarationDate="2018-02-01">0.630000</Dividend><Dividend type="CD" exDate="2017-11-10" recordDate="2017-11-13" payDate="2017-11-16" declarationDate="2017-11-03">0.630000</Dividend><Dividend type="CD" exDate="2017-08-10" recordDate="2017-08-14" payDate="2017-08-17" declarationDate="2017-07-02">0.630000</Dividend></Dividends><EPSs currency="USD"><EPS asofDate="2017-12-31" reportType="A" period="3M">3.920000</EPS><EPS asofDate="2017-09-30" reportType="A" period="3M">2.090000</EPS><EPS asofDate="2017-09-30" reportType="A" period="12M">9.270000</EPS></EPSs>
</FinancialSummary>
"""def write_to_csv(rows):header = 'date, total revenue, report type, period rev, dividendpershare, period div'.split(', ')with open('sample.csv', 'w', newline='') as fo:writer = csv.writer(fo)writer.writerow(header)writer.writerows(rows)def main():d = xmltodict.parse(XML)root = d['FinancialSummary']total_revenue_list = root['TotalRevenues']['TotalRevenue']dividend_per_share_list = root['DividendPerShares']['DividendPerShare']rows = []for total_rev, dps in zip(total_revenue_list, dividend_per_share_list):row = [total_rev['@asofDate'],total_rev['#text'],total_rev['@reportType'],total_rev['@period'],dps['#text'],dps['@period']]rows.append(row)write_to_csv(rows)if __name__ == '__main__':main()

Which will produce a sample.csv like this:

date,total revenue,report type,period rev,dividendpershare,period div
2017-12-31,239176000000.000000,TTM,12M,0.630000,3M
2017-09-30,229234000000.000000,TTM,12M,0.630000,3M
2017-06-30,223507000000.000000,TTM,12M,2.400000,12M

This sample program is written in Python3, and used a third-party library named xmltodict, you can install it by pip install xmltodict.

https://en.xdnf.cn/q/120326.html

Related Q&A

How to format HTTP request to discord API?

While this code works, it sends "Bad Request". import socket, ssl token = NzMyMzQ1MTcwNjK2MTR5OEU3.XrzQug.BQzbrckR-THB9eRwZi3Dn08BWrM HOST = "discord.com" PORT = 443 t = POST / HTTP…

Python 3.30 TypeError: object of type int has no len() [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable…

Is it possible to disable negative indexing? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 7 years ago.Improve…

Google Colab Notebook completely freezes when training a YOLO model

I am following a tutorial to train custom object detection model using YOLO. This is the tutorial and also where I got the Notebook Everything works fine until the training bit which is the last cell. …

Generate 4 columns of data such that each row sum to 100

How do I write a python program that can randomly generate 4 columns of data such that the sum of the numbers of each row is 100?

Nan to Num Python

I have multiple array that for those I calculate a linear regression, but sometimes it gives me 0/0 values which gives me a NaN. I know that to convert an array where there are numbers that are NaN you…

Class constructor able to init with an instance of the same class object

Can python create a class that can be initialised with an instance of the same class object?Ive tried this:class Class(): def __init__(self,**kwargs):print selfself = kwargs.get(obj,self)print selfif …

Python Logical Operators

I am currently learning Python3, but I dont understand Logical Operators. Here is the link: http://pymbook.readthedocs.org/en/py3/operatorsexpressions.html#logical-operators First of all, it seems that…

Partial Pivoting In Pandas SQL Or Spark

Partial Pivoting In Pandas SQL Or Spark Make the Year remain as Rows, and have the States Transpose to columns Take Pecentage value of a Gender Male Race White, InputOutput

Python to create a find-replace dictionary from a word vba macro

I have a very big macro Selection.Find.ClearFormattingSelection.Find.Replacement.ClearFormattingWith Selection.Find.Text = "asa".Replacement.Text = "fsa".Forward = True.Wrap = wdFin…