[PYTHON] Created a class to download and parse XBRL from UFO Catcher

About this article

I wondered if it would be possible to collect fundamental investment data with Python, and when I looked it up, I found some articles that downloaded XBRL from the UFO catcher and did various things, but I thought that all of them would be scratchy and troublesome. I made a module that can get and analyze XBRL quickly. I just made it, so I'd be happy if you could contact me if there is something wrong with it.

1. Installation

Install from the Git repository.

sawadyrr5/UfoDataReader

python


pip install git+https://github.com/sawadyrr5/UfoDataReader

2. Get XBRL

Use the DataReader class to get the XBRL. Specify symbol, start, end to display all matching documents. Those with "Securities Report" or "Quarterly Report" in the title are is_yuho = True. If fetch_xbrl = True, XBRL data will be downloaded. (Note that there are about several MB).

python


In [1]:
# 1. getting xbrl
from UfoDataReader.io.data import DataReader
from datetime import datetime

start = datetime(2017, 6, 23)
end = datetime(2017, 6, 23)
ufos = DataReader('7203', 'ufo', start, end, fetch_xbrl=False)  # if fetch_xbrl=True, download xbrl files.
ufos
Out[1]:
[{'docid': 'S100AKEX',
  'id': 'ED2017062301177',
  'is_yuho': True,
  'title': '[E02144] Toyota Motor Corporation Securities Report-113th Term(April 1, 2016-March 31, 2017)',
  'updated': datetime.datetime(2017, 6, 23, 0, 0),
  'url': 'http://resource.ufocatch.com/data/edinet/ED2017062301177'},
 {'docid': 'S100AI3I',
  'id': 'ED2017062301247',
  'is_yuho': False,
  'title': '[E02144] Toyota Motor Corporation Internal Control Report-113th Term(April 1, 2016-March 31, 2017)',
  'updated': datetime.datetime(2017, 6, 23, 0, 0),
  'url': 'http://resource.ufocatch.com/data/edinet/ED2017062301247'}]

3. XBRL perspective

XBRL parsing uses the UfoXBRLParser class. Passing an XBRL file to the parse () method will return a parsed XBRL document. You can get the basic information of the document by passing the XBRL document to parseDEI ().

python


In [2]:
# 2.1 parse xbrl (US GAAP)
from UfoDataReader.util.parser import UfoXBRLParser
ufoparser = UfoXBRLParser()

toyota = 'jpcrp030000-asr-001_E02144-000_2015-03-31_01_2015-06-24.xbrl'     # US GAAP

xbrl = ufoparser.parse(toyota)
dei = ufoparser.parseDEI(xbrl)
dei.__dict__

python


Out[2]:
{'accounting_standards': 'US GAAP',
 'company_name': 'Toyota Motor Corporation',
 'current_fy_end': '2015-03-31',
 'current_fy_start': '2014-04-01',
 'edinet_code': 'E02144',
 'trading_symbol': '72030'}

You can get basic financial indicators etc. by passing the XBRL document to parseGAAP (). There are three types of accounting standards for preparing securities reports: US GAAP, IFRS, and Japan GAAP. The element names for parsing each are slightly different, but for the time being, if you go into parseGAAP (), I try to distinguish between US GAAP and Japan GAAP and parse them. IFRS is not supported. (Refer to the following file published by the Financial Services Agency for details such as element names)

(e) Taxonomy element list (EXCEL: 6,281KB)

In the case of Toyota Motor, it is created based on US GAAP.

python


In [3]:
gaap = ufoparser.parseGAAP(xbrl)
gaap.__dict__

python


Out[3]:
{'assets': '15128623000000',
 'basic_eps': '688.02',
 'cashflow_from_financing': '306045000000',
 'cashflow_from_investing': '-3813490000000',
 'cashflow_from_operation': '3685753000000',
 'comprehensive_income': '3294275000000',
 'current_assets': '6000524000000',
 'current_liabilities': '3571917000000',
 'diluted_eps': '687.66',
 'liabilities': '4944351000000',
 'net_assets': '10184271000000',
 'net_income_loss': '2173338000000',
 'non_current_assets': '9128099000000',
 'non_current_liabilities': '1372433000000',
 'operating_income_loss': 0,
 'per': '12.2',
 'profit_loss_before_tax': '2892828000000',
 'revenues': '27234521000000',
 'shares_outstanding': '3417997000'}

This is the case for companies created with the domestic standard Japan GAAP.

python


In [4]:
# 2.2 parse xbrl (Japan GAAP)
amiyaki = 'jpcrp030000-asr-001_E03398-000_2017-03-31_01_2017-06-23.xbrl'    # Japan GAAP

xbrl = ufoparser.parse(amiyaki)
dei = ufoparser.parseDEI(xbrl)
dei.__dict__

python


Out[4]:
{'accounting_standards': 'Japan GAAP',
 'company_name': 'Amiyaki Tei Co., Ltd.',
 'current_fy_end': '2017-03-31',
 'current_fy_start': '2016-04-01',
 'edinet_code': 'E03398',
 'trading_symbol': '27530'}

python


In [5]:
gaap = ufoparser.parseGAAP(xbrl)
gaap.__dict__

python


Out[5]:
{'assets': '23304000000',
 'basic_eps': '315.96',
 'bps': '2769.14',
 'cashflow_from_financing': '-744000000',
 'cashflow_from_investing': '-1339000000',
 'cashflow_from_operation': '3100000000',
 'comprehensive_income': '2163000000',
 'current_assets': '11004000000',
 'current_liabilities': '3538000000',
 'diluted_eps': '',
 'equity_to_asset_ratio': '0.814',
 'liabilities': '4339000000',
 'net_assets': '18965000000',
 'netsales': '30564000000',
 'non_current_assets': '12299000000',
 'non_current_liabilities': '800000000',
 'ordinary_income_loss': '3053000000',
 'per': '13.1',
 'profit_loss': '2163000000',
 'roe': '0.119',
 'shares_outstanding': '6848800',
 'total_assets': '23304000000'}

reference

Financial analysis for investing step1 "Getting financial information XBRL" Financial analysis for investment step2 "Import XBRL file into DB" Reported XBRL item memo

Recommended Posts

Created a class to download and parse XBRL from UFO Catcher
[pyqtgraph] Created a class to display the crosshairs that follow the cursor and their coordinates
I created a class in Python and tried duck typing
Convert pixiv to mp4 and download from pixiv using python's pixivpy
[EC2] How to install and download chromedriver from the command line
Django: Import a class from a string
OpenMPI installation from download to pass-through
Migration from Python2 to Python3 (Python2 is rebuilt as a virtual environment and coexists)
Connect to postgreSQL from Python and use stored procedures in a loop.