[PYTHON] Created a class to check whether it is a constituent of TOPIX Core30, Large70, Mid400, Small

Introduction

Is this brand large? Medium size? Small? When you are worried about it, it would be useful to have information on whether or not it is a constituent of the index. So, if you give an arbitrary stock code, we created a class that returns the free-float ratio and composition weight as well as which of the TOPIX size indexes are included. Since it was difficult to get the original data online, updating is manual. Tsumami.

What to prepare

Save the following PDF in txt format in the same location as the class. (You can do it with Acrobat Reader)

TOPIX (Tokyo Stock Price Index) | Japan Exchange Group List of free-float stock ratios and constituent weights by constituent stock

code

Nothing, just reading and parsing the text.

TOPIX


#! /usr/bin/env python
# -*- coding: utf-8 -*-
import re


class TOPIX:
    """
How to use

    1.Preparation
    http://www.jpx.co.jp/markets/indices/topix/tvdivq00000030ne-att/list-j.pdf
DL and list in the same location as the script-j.Save as txt.(Save txt of Acrobat Reader)

that's all
    """
    def __init__(self):
        re_date = re.compile(r"\d{4}/\d{2}/\d{2}")

        self.result = dict()
        for line in open('list-j.txt', 'r'):
            matchOB = re_date.match(line)
            if matchOB:
                line = line.replace('Small ', 'Small').replace('○ ', '').replace('-', '- ').replace('\n', '')
                line = line.replace(matchOB.group(), matchOB.group()+' ')
                l = line.split(' ')
                self.result[l[2]] = dict(
                    newindex=l[3],
                    float_ratio=l[4],
                    weight=l[5]
                )

    def newindex(self, code):
        return self.result[code]['newindex']

    def float_ratio(self, code):
        return self.result[code]['float_ratio']

    def weight(self, code):
        return self.result[code]['weight']

    def includes(self, newindex):
        result = [code for code in self.result if self.result[code]['newindex'] == newindex]
        return result

Method

Returns the index name included in newindex (), float_ratio () the floating stock ratio, and weight () the composition ratio. If you specify the index name in includes (), the constituent stocks of the index are returned.

Test code

python


myTOPIX = TOPIX()

s = '9984'
print(myTOPIX.newindex(s))
print(myTOPIX.float_ratio(s))
print(myTOPIX.weight(s))
print(myTOPIX.includes('Core30'))

result

Core30
0.75000
1.6244%
['9432', '6752', '6981', '4502', '8031', '9437', '8058', '9433', '8802', '6501', '8766', '6902', '7267', '8306', '4503', '4063', '7203', '2914', '7751', '9022', '8604', '9020', '8801', '8411', '8316', '9984', '6954', '3382', '6758', '7201']

Recommended Posts

Created a class to check whether it is a constituent of TOPIX Core30, Large70, Mid400, Small
I created a script to check if English is entered in the specified position of the JSON file in Python.
Upload a large number of images to Wordpress
Judge whether it is a prime number [Python]
How to use any or all to check if it is in a dictionary (Hash)
Usage to call a method of an instance before it is returned by __new__