Initialization

init

pip install shioaji 了之後,接著我們就可以用 python, ipython, jupyter notebook 或 JupyterLab 等編輯器開始接下來旅程。

在這邊我們還需要一個條件,就是請先開好永豐金證券的帳戶,實體開戶或是直接線上開戶

準備好永豐帳戶就讓我們開始吧!

載入

起手式 import我們的api就像import tensorflow pandas等等一樣

import shioaji as sj

初始api

接著我們需要初始化Shioaji這個物件來開始接下來要做的事

ipython有個很方便的方法可以看這個物件或函式的文件,就是直接在function或object後面加上?

下面文件部分可以看到我們api底下所有可以用的function跟object以及初始化Shioaji的參數說明

input

sj.Shioaji?

output

Init signature: sj.Shioaji(backend='http', simulation=True, proxies={}, currency='NTD')
Docstring:    
shioaji api 

Functions:
    login 
    activate_ca
    list_accounts
    set_default_account
    get_account_margin 
    get_account_openposition
    get_account_settle_profitloss
    place_order
    update_order
    update_status
    list_trades

Objects:
    Contracts
    Order
Init docstring:
initialize Shioaji to start trading

Args:
    backend (str): {http, socket} 
        use http or socket as backend currently only support http, async socket backend coming soon.
    simulation (bool): 
        - False: to trading on real market (just use your Sinopac account to start trading)
        - True: become simulation account(need to contract as to open simulation account)
    proxies (dict): specific the proxies of your https
        ex: {'https': 'your-proxy-url'}
    currency (str): {NTX, USX, NTD, USD, HKD, EUR, JPY, GBP}
        set the default currency for display

這邊我們選擇http為backend,並且關閉模擬模式,如果有需要使用模擬的客戶麻煩在gitter中聯絡我們,http比較完整socket會在近日釋出敬請期待。

input

api = sj.Shioaji(backend='http', simulation=False)

登入

接著我們要登入才有辦法開始使用api,登入帳號就是永豐金證券電子平台(如:eLeader)的登入帳號密碼。

input

api.login?

output

Signature: api.login(person_id, passwd)
Docstring:
login to trading server

Args:
    person_id (str): Same as your eleader, ileader login id(usually your person ID)
    passwd  (str): the password of your eleader login password(not ca password)

登入後會預設抓出第一個期貨帳戶與證券戶作為預設帳戶

input

api.login(person_id=person_id, passwd='2222')
api.fut_account

output

FutureAccount(person_id='SCCEIEFAJA', broker_id='F002000', account_id='9104000', username='莊*芬')

也可以列出我們所有帳戶,並使用api.set_default_account將常用設為預設的帳戶

input

api.list_accounts()

output

[Account(account_type='H', person_id='QCCAHIFFDH', broker_id='1300', account_id='09800762', username='n*m'),
FutureAccount(person_id='SCCEIEFAJA', broker_id='F002000', account_id='9104000', username='莊*芬'),
StockAccount(person_id='SCCEIEFAJA', broker_id='9A92', account_id='9802195', username='莊*芬')]

激活憑證

以上動作已經完成與下單無關的動作的初始化了,需要下單的話必須啟動憑證,若只是帳務相關等功能無需憑證。

input

api.activate_ca?

output

Signature: api.activate_ca(ca_path, ca_passwd, person_id)
Docstring:
activate your ca for trading

Args: 
    ca_path (str):
        the path of your ca, support both absloutely and relatively path, use same ca with eleader
    ca_passwd (str): password of your ca
    person_id (str): the ca belong which person ID

input

api.activate_ca(ca_path='../ca/Sinopac.pfx', ca_passwd='SCCEIEFAJA', person_id=person_id)

output

Ca Initial Done.
0