Use the kabu station API provided by au kabu.com Securities to individuals from Python. The kabu station is assumed to be installed.
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'],))
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