[PYTHON] Try using kabu station API of kabu.com Securities

Introduction

The trading API of financial products has been published by various companies in the virtual currency field such as Bitcoin for a long time, but I feel that there are not so many in stocks and ETFs. Meanwhile, kabu.com Securities released a trading API that supports REST format, so I tried using it.

environment

Advance preparation

To use the kabu station API, you need to have an account with kabu.com Securities and have a Fintech plan or higher. Therefore, those who do not have an account need to perform the following steps to start using it. Please refer to here for the detailed procedure.

  1. Open an account with kabu.com Securities
  2. Join the "Fintech Plan" or "Premium Plan"
  3. Set the usage of "kabu station API"
  4. Install "kabu station" on your own PC
  5. Check "Use API" in "API System Settings" of "kabu Station" and set the API password and soft limit.
  6. Restart "kabu station" to reflect the setting of 5.

Get token

When making various API requests, it is necessary to specify the token acquired in advance in the request header, so first acquire that token. When acquiring the token, the API password set in Preparation 5 in the previous section is required, but this time, put it in the environment variable.

import os
import json
import requests

API_URL = "http://localhost:18080/kabusapi"
API_PASSWORD = os.environ["API_PASSWORD"]

def get_token():  

    URL = API_URL + "/token"
    
    headers = {"content-type": "application/json"}
    payload = {"APIPassword": API_PASSWORD}

    try:
        response = requests.post(URL, data=json.dumps(payload).encode("utf8"), headers=headers)  
    except Exception as e:
        print (e)
    
    return json.loads(response.text).get("Token")

Acquisition of market price information

Get the market value information of any brand. The response also includes detailed board information. By the way, if you specify symbol as 101, you can get the Nikkei Stock Average.

variable Description
token Token acquired in the previous section
symbol Brand code you want to get
exchange Market code you want to get(1 is TSE)

def get_price(token):

    symbol = "9433" 
    exchange = "1"	
    URL = API_URL + "/board/" + symbol + "@" + exchange

    headers = {
        "content-type": "application/json",
        "X-API-KEY": token
    }

    try:
        response = requests.get(URL, headers=headers) 
    except Exception as e:
        print (e)
    
    return json.loads(response.text).get("CurrentPrice") 

Other

Reference describes various other request methods, so if you have an operation you want to perform, please refer to this. There are some preparations before the API becomes available, but once that's done, the API request itself doesn't seem to be particularly difficult. I feel that there are still few such resources in Japan, so it would be nice if the openness progressed as in the virtual currency field.

Recommended Posts

Try using kabu station API of kabu.com Securities
Receive websocket of kabu station ® API in Python
Try using the Twitter API
Try using the Twitter API
Try using the PeeringDB 2.0 API
Try using Janus gateway's Admin API
Try using Pleasant's API (python / FastAPI)
Use kabu Station® API from Python
Try using Python argparse's action API
Try using the Wunderlist API in Python
Try using the Kraken API in Python
Try using Dropbox API v2 with Go
Try speed comparison of BigQuery Storage API
Awareness of using Aurora Severless Data API
Try using the BitFlyer Ligntning API in Python
Try using the collections module (ChainMap) of python3
Anonymous upload of images using Imgur API (using Python)
Try using Jupyter Notebook of Azure Machine Learning
Try using ChatWork API and Qiita API in Python
Try using the DropBox Core API in Python
kabu Station® API --Updated Python wrapper for PUSH API
Try using Tkinter
Try using docker-py
Try using cookiecutter
Try using PDFMiner
Try using geopandas
Try using Selenium
Try using scipy
Try using pandas.DataFrame
Try using django-swiftbrowser
Try using matplotlib
Try using tf.metrics
Try using PyODE
Try drawing a social graph using Twitter API v2
Try projective transformation of images using OpenCV with Python
Try to make RESTful API with MVC using Flask 1.0.2
[Python] Using Line API [1st Creation of Beauty Bot]
Try to delete tweets in bulk using Twitter API
Easy usage memo of OpenStack Swift API using swiftclient