Read XBRL and easily visualize changes in business performance

Overview

I wish I could easily create a graph ↓ like the one posted on MarketHack, so I wrote a script that spits out a performance graph just by inserting XBRL using the separately created securities report XBRL parser.

Industrial commodity prices are strong and Tech Resources are doing well-Market Hack MarketHack テック・リソーシズの株価

code

Use the following self-made class as a parser.

I made a class to download and parse XBRL from UFO Catcher --Qiita

As an example, let's use JustSystems' financial report for the year ended March 31, 2017.

python


import numpy as np
import pandas as pd
from UfoDataReader.util.parser import UfoXBRLParser
from bokeh.io import show
from bokeh.models import ColumnDataSource, FactorRange
from bokeh.palettes import Spectral5
from bokeh.plotting import figure
from bokeh.transform import factor_cmap

ufoparser = UfoXBRLParser()

# target XBRL file
file = 'jpcrp030000-asr-001_E04996-000_2017-03-31_01_2017-06-23.xbrl'

xbrl = ufoparser.parse(file)
dei = ufoparser.parseDEI(xbrl)

# getting each years GAAP objects
contexts = ['Prior4', 'Prior3', 'Prior2', 'Prior1', 'Current']
gaaps = [ufoparser.parseGAAP(xbrl, context=context) for context in contexts]

# getting factors
share = np.array([float(gaap.shares_outstanding) for gaap in gaaps])
sale = np.array([float(gaap.netsales) for gaap in gaaps])
cf = np.array([float(gaap.cf_from_operating) +
               float(gaap.cf_from_financing) +
               float(gaap.cf_from_financing) for gaap in gaaps])

df = pd.DataFrame(
    dict(
        year=contexts,
        dps=np.array([float(gaap.dps) for gaap in gaaps]),
        eps=np.array([float(gaap.basic_eps) for gaap in gaaps]),
        cfps=cf / share,
        sps=sale / share
    )
)


# bokeh setting
years = contexts
factors = ['dps', 'eps', 'cfps', 'sps']

x = [(year, factor) for year in years for factor in factors]
amounts = sum(zip(df['dps'], df['eps'], df['cfps'], df['sps']), ())

source = ColumnDataSource(data=dict(x=x, counts=amounts))

p = figure(x_range=FactorRange(*x), plot_height=300, title=dei.company_name)

p.vbar(x='x', top='counts', width=1.0, source=source,
       fill_color=factor_cmap('x', palette=Spectral5, factors=factors, start=1, end=2))

p.y_range.start = 0
p.x_range.range_padding = 0.1
p.xaxis.major_label_orientation = 1
p.xgrid.grid_line_color = None

# open browser
show(p)

result

It will be output like this. It may be better to add a tooltip etc. using Hover. Since bokeh has a lot of settings, there are still many things to remember.

bokeh_plot.png

Recommended Posts

Read XBRL and easily visualize changes in business performance
Assignments and changes in Python objects
Preserve and read order in PyYAML
Ignore # line and read in pandas
Create and read messagepacks in Python
Read and write JSON files in Python
Easily graph data in shell and Python