Use kabu Station® API from Python

Overview

Use the kabu station API provided by au kabu.com Securities to individuals from Python. The kabu station is assumed to be installed.

environment

Additional packages

code

In the following, the trading capacity is acquired, the balance is displayed, and the order is displayed.

import json
import requests
import yaml

# ---

def get_token():  #Get a token. It changes every time you get it or every time you restart the kabu station.
    with open('auth.yaml', 'r') as yml:
        auth = yaml.safe_load(yml)

    url = 'http://localhost:18080/kabusapi/token'
    headers = {'content-type': 'application/json'}
    payload = json.dumps(
        {'APIPassword': auth['PASS'],}
        ).encode('utf8')

    response = requests.post(url, data=payload, headers=headers)

    return json.loads(response.text)['Token']

# ---

token = get_token()

url = 'http://localhost:18080/kabusapi/wallet/cash'
response = requests.get(url, headers={'X-API-KEY': token,})
cash = json.loads(response.text)

print("Trading capacity\t{}".format(cash['StockAccountWallet']))

url = 'http://localhost:18080/kabusapi/positions'
response = requests.get(url, headers={'X-API-KEY': token,})
positions = json.loads(response.text)

print('Code Brand, average acquisition price, number of holdings, current price, profit and loss')
for position in positions:
    print("{}\t{}\t{}\t{}\t{}\t{}".format(
        position['Symbol'],
        position['SymbolName'],
        position['Price'],
        position['LeavesQty'],
        position['CurrentPrice'],
        position['ProfitLoss']))

url = 'http://localhost:18080/kabusapi/orders'
response = requests.get(url, headers={'X-API-KEY': token,})
orders = json.loads(response.text)

print('Code Brand Order Price Order Number Deadline')
for order in orders:
    if order['State'] == 1:
        print("{}\t{}\t{}\t{}\t{}".format(
            order['Symbol'],
            order['SymbolName'],
            order['Price'],
            order['OrderQty'],
            order['ExpireDay'],))

Other

The production environment port is 18080 and the verification environment port is 18081. Also, connections other than localhost are not possible.

[Request] About API access from other than localhost   Issue#34 https://github.com/kabucom/kabusapi/issues/34

Try it with PowerShell

netsh http add urlacl url=http://[ IP ADDRESS ]:18080/ user=Everyone

When I bound it with, it was rejected with a 503 error.

Recommended Posts

Use kabu Station® API from Python
Use e-Stat API from Python
Use the Flickr API from Python
Use Google Analytics API from Python
Use Google Cloud Vision API from Python
Use thingsspeak from python
Use fluentd from python
Use MySQL from Python
Use MySQL from Python
Use BigQuery from python.
Use mecab-ipadic-neologd from python
Receive websocket of kabu station ® API in Python
kabu Station® API --Updated Python wrapper for PUSH API
Use Trello API with python
Use Twitter API with Python
Use MySQL from Anaconda (python)
Use subsonic API with python3
kabu Station® API-I made a Python wrapper for REST API
Read and use Python files from Python
Forcibly use Google Translate from python
How to use OpenPose's Python API
Use Azure Blob Storage from Python
Get upcoming weather from python weather api
Run Ansible from Python using API
Use fastText trained model from Python
[Python] How to use Typetalk API
Handle SOAP API from Python (Zeep)
[Python] Web application from 0! Hands-on (3) -API implementation-
Use PostgreSQL data type (jsonb) from Python
Python: Reading JSON data from web API
Use machine learning APIs A3RT from Python
I want to use jar from python
I tried using UnityCloudBuild API from Python
Use Django from a local Python script
Use C ++ functions from python with pybind11
API explanation to touch mastodon from python
Connect to coincheck's Websocket API from Python
sql from python
Use JIRA API
MeCab from Python
Firebase: Use Cloud Firestore and Cloud Storage from Python
Procedure to use TeamGant's WEB API (using python)
Study from Python Hour7: How to use classes
[Bash] Use here-documents to get python power from bash
Wrap C with Cython for use from Python
Use Python in your environment from Win Automation
I want to use ceres solver from python
Try using kabu station API of kabu.com Securities
Let's use different versions of SQLite3 from Python3!
Wrap C ++ with Cython for use from Python
Use the nghttp2 Python module from Homebrew from pyenv's Python
Try accessing the YQL API directly from Python 3
Use Tor to connect from urllib2 [Python] [Mac]
Python: Use zipfile to unzip from standard input
Use R density ratio estimation package densratio from Python
[BigQuery] How to use BigQuery API for Python -Table creation-
Use config.ini in Python
Operate Filemaker from Python
Translator in Python from Visual Studio 2017 (Microsoft Translator Text API)
Use dates in Python
Access bitcoind from python