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
kabusapi
.Context
.Context
, specify token
obtained from the other program that was started first.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)
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))
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