Skip to content

Login

Login Shioaji

In

import shioaji as sj
api = sj.Shioaji()
api.login(
    person_id="YOUR_ID", 
    passwd="YOUR_PASSWORD", 
    contracts_cb=lambda security_type: print(f"{repr(security_type)} fetch done.")
)

Out

[FutureAccount(person_id='', broker_id='', account_id='', signed=True, username=''),
    StockAccount(person_id='', broker_id='', account_id='', signed=True, username='')]
<SecurityType.Index: 'IND'> fetch done.
<SecurityType.Future: 'FUT'> fetch done.
<SecurityType.Option: 'OPT'> fetch done.
<SecurityType.Stock: 'STK'> fetch done.
  • If you cannot find signed in your accounts, please sign the document first.

Default Account

List Accounts

There are 2 ways you can get your accounts:

  1. api.login
  2. api.list_accounts

In: login

accounts = api.login(
    person_id="YOUR_ID", 
    passwd="YOUR_PASSWORD", 
)

In: list_accounts

accounts = api.list_accounts()

Out

# print(accounts)
[
    FutureAccount(person_id='PERSON_ID_1', broker_id='BROKER_ID_1', account_id='ACCOUNT_ID_1', signed=True, username='USERNAME_1'), 
    FutureAccount(person_id='PERSON_ID_2', broker_id='BROKER_ID_2', account_id='ACCOUNT_ID_2', username='USERNAME_2'), 
    StockAccount(person_id='PERSON_ID_3', broker_id='BROKER_ID_3', account_id='ACCOUNT_ID_3', username='USERNAME_3'), 
    StockAccount(person_id='PERSON_ID_4', broker_id='BROKER_ID_4', account_id='ACCOUNT_ID_4', signed=True, username='USERNAME_4')
]
  • signed is invisible, such as ACCOUNT_ID_2, ACCOUNT_ID_3, if you haven't signed the api documents or haven't passed the api test in simulation mode. Please refer to CA & Signature.
  • signed=True means your signature has been activated, ex: ACCOUNT_ID_1, ACCOUNT_ID_4.

Default Account

In: login

# Stock default account
print(api.stock_account)

# Futures default account
print(api.futopt_account)

Out

StockAccount(person_id='PERSON_ID_3', broker_id='BROKER_ID_3', account_id='ACCOUNT_ID_3', username='USERNAME_3')

FutureAccount(person_id='PERSON_ID_1', broker_id='BROKER_ID_1', account_id='ACCOUNT_ID_1', signed=True, username='USERNAME_1')


Default Account in Order

In Order object, you need to specify which account you want to place order. For more information about Order, please refer to Stock Order and Futures Order.

In

# stock order
order = api.Order(
    price=12, 
    quantity=1, 
    action=sj.constant.Action.Buy, 
    price_type=sj.constant.StockPriceType.LMT, 
    order_type=sj.constant.TFTOrderType.ROD, 
    order_lot=sj.constant.TFTStockOrderLot.Common, 
    account=api.stock_account
)

# futures order
order = api.Order(
    action=sj.constant.Action.Buy,
    price=10200,
    quantity=2,
    price_type=sj.constant.StockPriceType.LMT,
    order_type=sj.constant.FuturesOrderType.ROD, 
    octype=sj.constant.FuturesOCType.Auto,
    account=api.futopt_account
)


Set Default Account

In

# stock default account
api.set_default_account(accounts[-1])
print(api.stock_account)

# futures default account
api.set_default_account(accounts[1])
print(api.futopt_account)

Out

StockAccount(person_id='PERSON_ID_4', broker_id='BROKER_ID_4', account_id='ACCOUNT_ID_4', signed=True, username='USERNAME_4')

FutureAccount(person_id='PERSON_ID_2', broker_id='BROKER_ID_2', account_id='ACCOUNT_ID_2', username='USERNAME_2')

Note that:

  • Default stock account switch to ACCOUNT_ID_4 from ACCOUNT_ID_3.
  • Default futures account switch to ACCOUNT_ID_2 from ACCOUNT_ID_1.

set subscribe trade callback

There are 2 ways you can set subscribe trade callback:

  1. api.login
  2. api.subscribe_trade and api.unsubscribe_trade

In: login

    api.login(
        person_id="YOUR_ID", 
        passwd="YOUR_PASSWORD", 
        subscribe_trade=False,
        )

In: subscribe_trade

    api.subscribe_trade(account)

In: unsubscribe_trade

    api.unsubscribe_trade(account)

Note that: * Default subscribe trade callback is True.

Logout Shioaji

Logout funciton will close the connection between the client and the server.
In order to provide high quality services, starting from 2021/08/06, we've limit the number of connections used. It's a good practice to logout or to terminate the program when it is not in use.

api.logout()
# True