Skip to content

Instantly share code, notes, and snippets.

@sawadyrr5
Created March 26, 2020 13:56
Show Gist options
  • Save sawadyrr5/744b9d2dd550ce224033a3ff4de2d735 to your computer and use it in GitHub Desktop.
Save sawadyrr5/744b9d2dd550ce224033a3ff4de2d735 to your computer and use it in GitHub Desktop.
TOPIX Core30, Large70, Mid400, Smallの構成銘柄かどうか調べたりするクラスを作った

はじめに

この銘柄って大型?中型?小型?と気になるときに、指数の構成銘柄かどうかいう情報があると便利ですね。というわけで、任意の銘柄コードを与えるとTOPIXのサイズ別指数のどれに含まれるかのほか、浮動株比率や構成ウェイトを返すクラスを作りました。 なお元データをオンラインで取ってくるのが難しかったので更新は手動です。つらみ。

用意するもの

クラスと同じ場所に以下のPDFをtxt形式で保存しといてください。(Acrobat Readerでできます)

TOPIX(東証株価指数) | 日本取引所グループ 構成銘柄別浮動株比率及び構成ウエイト一覧

コード

なんのことはない、単にテキストを読んでパースしてるだけです。

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


class TOPIX:
    """
    使い方

    1. 準備
    http://www.jpx.co.jp/markets/indices/topix/tvdivq00000030ne-att/list-j.pdf
    をDLして、スクリプトと同じ場所にlist-j.txtとして保存。(Acrobat Readerのtxt保存)

    以上
    """
    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

メソッド

newindex()で含まれるインデックス名、float_ratio()で浮動株比率、weight()で構成比率を返します。includes()にインデックス名を指定すると、インデックスの構成銘柄を返します。

テストコード

myTOPIX = TOPIX()

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

結果

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']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment