How to use python-kabusapi

Overview

Last time, kabu Station® API --I created a Python wrapper for REST API, but I will explain how to use it easily. Also, enter the code that you are actually using to check the stocks you are holding or ordering.

See Github: https://github.com/shirasublue/python-kabusapi

environment

Rough flow

Normal case

  1. Import kabusapi.
  2. Make initial settings with Context.
  3. Boil or bake.

When executing multiple programs at the same time

  1. Similarly
  2. In the initial setting of Context, specify token obtained from the other program that was started first.
  3. Boil or bake.

Sample code

Get import token

import kabusapi
api = kabusapi.Context('localhost', '18080', 'hogehoge')

By the way, the default value of the argument of Context is

hostname='localhost',
port=18080,
password=None,
token=None,

Therefore, in normal usage, you can connect to the production environment by specifying only the password as shown below.

api = kabusapi.Context(password='hogehoge')

Also, in the kabu station API, the token is changed every time the token is acquired or activated. If token is acquired by another program, it can be set as follows.

api = kabusapi.Context(token='fugafuga')

To check the token, refer to ʻapi.token`.


print(api.token)

Display of stocks in possession


positions = api.positions()

positions = sorted(positions, key=lambda x: x['Symbol'])  #Sort by stock code

print('Code brand, average acquisition price, number of possessions, current value, profit and loss')
for position in positions:
    profit_loss = position['ProfitLoss']
    if profit_loss == None:
        profit_loss = '---'

    print("{}\t{:11.7}\t{:>10}\t{:>10}\t{:>10}\t{:>10}".format(
        position['Symbol'],
        position['SymbolName'],
        position['Price'],
        position['LeavesQty'],
        position['CurrentPrice'],
        profit_loss))

Brand display during ordering

orders = api.orders()

print('Code brand, order price, number of orders, current price, deadline')
for order in orders:
    state = order['State']
    if state >= 4:  # 1,2,3:Wait,processing,Processed
        continue

    price = order['Price']
    if price == 0.0:
        price = 'Market'

    side = order['Side']
    if side == '2':
        side = 'Buy'
    elif side == '1':
        side = 'Sell'

    board = api.board(symbol=order['Symbol'], exchange=1)
    current_price = board["CurrentPrice"]
    if current_price == None:
        current_price = "---"

    print("{}\t{:11.7}\t{:>10}/{:<}\t{:>10}\t{:>10}\t{:>10}".format(
        order['Symbol'],
        order['SymbolName'],
        price,
        side,
        order['OrderQty'],
        current_price,
        order['ExpireDay'],
        ))

Recommended Posts

How to use python-kabusapi
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
How to use virtualenv
How to use Seaboan
How to use image-match
How to use shogun
How to use Pandas 2
How to use Virtualenv
How to use numpy.vectorize
How to use partial
How to use Bio.Phylo
How to use SymPy
How to use WikiExtractor.py
How to use IPython
How to use virtualenv
How to use Matplotlib
How to use iptables
How to use numpy
How to use TokyoTechFes2015
How to use venv
How to use dictionary {}
How to use list []
How to use OptParse
How to use return
How to use dotenv
How to use pyenv-virtualenv
How to use Go.mod
How to use imutils
How to use import
How to use Qt Designer
[gensim] How to use Doc2Vec
python3: How to use bottle (2)
Understand how to use django-filter
How to use the generator
[Python] How to use list 1
How to use FastAPI ③ OpenAPI
How to use Python argparse
How to use IPython Notebook
[Note] How to use virtualenv
How to use redis-py Dictionaries
Python: How to use pydub
[Python] How to use checkio
[Go] How to use "... (3 periods)"
How to use Django's GeoIp2
[Python] How to use input ()
How to use the decorator
[Introduction] How to use open3d
How to use Python lambda
How to use Jupyter Notebook
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Google Colaboratory
How to use Python bytes
How to use cron (personal memo)
Python: How to use async with
How to use the zip function
How to use the optparse module
How to use SWIG from waf