Shioaji

shioaji-logosinopac-logo

PyPI - Status PyPI - Python Version PyPI - Downloads Binder doc Gitter

Shioaji is sinopac provide the most pythonic api for trading the taiwan and global financial market.

You can use your favorite Python packages such as numpy, scipy, pandas, pytorch or tensorflow to build your own trading model with intergrated the shioaji api on cross platform.

We are in early-release alpha. Expect some adventures and rough edges.

Installation

Binaries

simple using pip to install

pip install shioaji

Docker Image

simple run with interactive mode in docker

docker run -it sinotrade/shioaji:latest

run with jupyter lab or notebook

docker run -p 8888:8888 sinotrade/shioaji:jupyter
In [ ]:
! pip install shioaji

Getting Started

In [1]:
from datetime import date, timedelta
import pandas as pd
In [2]:
import shioaji as sj

Use Shioaji object to setup setting and login

In [4]:
sj.Shioaji?
Init signature: sj.Shioaji(backend='http', simulation=False, 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:
    Quote
    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 
File:           shioaji/shioaji.py
Type:           type
Subclasses:     
In [3]:
api = sj.Shioaji(backend='http', simulation=False)
In [5]:
api.login?
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)
File:      shioaji/shioaji.py
Type:      method
In [4]:
person_id = 'SCCEIEFAJA'
In [5]:
api.login(person_id=person_id, passwd='2222')
In [8]:
api.fut_account
Out[8]:
FutureAccount(person_id='SCCEIEFAJA', broker_id='F002000', account_id='9104000', username='莊*芬')

List all your account

In [9]:
api.list_accounts()
Out[9]:
[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='莊*芬')]

Set your default trading account

In [10]:
api.set_default_account(api.list_accounts()[0])

Activate your cetifacation to start ordering

In [11]:
api.activate_ca?
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
File:      shioaji/shioaji.py
Type:      method
In [12]:
api.activate_ca(ca_path='../ca/Sinopac.pfx', ca_passwd='SCCEIEFAJA', person_id=person_id)
Ca Initial Done.
Out[12]:
0

Contract object for subscribe quote and place order

using tab to direct get avaliable trading product with Contracts

In [6]:
api.Contracts
Out[6]:
Contracts(Stocks=(OTC, TSE), Futures=(BRF, CAF, CBF, CCF, CDF, CEF, CFF, CGF, CHF, CJF, CKF, CLF, CMF, CNF, CQF, CRF, CSF, CUF, CWF, CXF, CYF, CZ1, CZ2, CZF, DCF, DD1, DDF, DEF, DFF, DGF, DH1, DHF, DIF, DJF, DKF, DLF, DNF, DOF, DPF, DQF, DSF, DUF, DVF, DWF, DXF, DYF, DZF, EEF, EGF, EHF, EMF, EPF, ERF, EXF, EYF, FEF, FFF, FGF, FKF, FQF, FRF, FTF, FVF, FWF, FXF, FYF, FZF, GAF, GBF, GCF, GDF, GHF, GIF, GJF, GLF, GMF, GNF, GOF, GRF, GTF, GUF, GWF, GXF, GZF, HAF, HBF, HCF, HHF, HIF, HLF, HOF, HS1, HSF, HYF, I5F, IA1, IAF, IHF, IIF, IJF, IMF, INF, IOF, IPF, IQF, IRF, ITF, IVF, IXF, IYF, IZF, JBF, JFF, JIF, JNF, JPF, JSF, JWF, JZF, KAF, KCF, KDF, KFF, KGF, KIF, KKF, KLF, KOF, KPF, KSF, KWF, LBF, LCF, LEF, LIF, LMF, LOF, LQF, LRF, LTF, LUF, LVF, LWF, LXF, MAF, MBF, MCF, MEF, MIF, MJF, MKF, MPF, MQF, MVF, MXF, MYF, NAF, NBF, NCF, NDF, NEF, NGF, NHF, NIF, NJF, NLF, NMF, NNF, NOF, NQF, NSF, NTF, NUF, NVF, NWF, NXF, NYF, NZF, OAF, OBF, OCF, ODF, OEF, OGF, OHF, OJF, OKF, OLF, OMF, OOF, OPF, OQF, ORF, OSF, OTF, OUF, OVF, OWF, OXF, OYF, OZF, PAF, PBF, PCF, PDF, PEF, PFF, PGF, PHF, PIF, PJF, PKF, PLF, PMF, PNF, RHF, RTF, SPF, T5F, TGF, TJF, TXF, UDF, XAF, XBF, XEF, XIF, XJF), Options=(CAO, CBO, CCO, CDO, CEO, CFO, CGO, CHO, CJO, CKO, CLO, CMO, CNO, CQO, CRO, CSO, CXO, CZO, DCO, DEO, DFO, DGO, DHO, DJO, DKO, DLO, DNO, DOO, DPO, DQO, DSO, DUO, DVO, DWO, DXO, GIO, GTO, GXO, HCO, IJO, LOO, NYO, NZO, OAO, OBO, OCO, OJO, OKO, OOO, OZO, RHO, RTO, TEO, TFO, TGO, TXO, XIO))

Futures

In [7]:
api.Contracts.Futures
Out[7]:
(BRF, CAF, CBF, CCF, CDF, CEF, CFF, CGF, CHF, CJF, CKF, CLF, CMF, CNF, CQF, CRF, CSF, CUF, CWF, CXF, CYF, CZ1, CZ2, CZF, DCF, DD1, DDF, DEF, DFF, DGF, DH1, DHF, DIF, DJF, DKF, DLF, DNF, DOF, DPF, DQF, DSF, DUF, DVF, DWF, DXF, DYF, DZF, EEF, EGF, EHF, EMF, EPF, ERF, EXF, EYF, FEF, FFF, FGF, FKF, FQF, FRF, FTF, FVF, FWF, FXF, FYF, FZF, GAF, GBF, GCF, GDF, GHF, GIF, GJF, GLF, GMF, GNF, GOF, GRF, GTF, GUF, GWF, GXF, GZF, HAF, HBF, HCF, HHF, HIF, HLF, HOF, HS1, HSF, HYF, I5F, IA1, IAF, IHF, IIF, IJF, IMF, INF, IOF, IPF, IQF, IRF, ITF, IVF, IXF, IYF, IZF, JBF, JFF, JIF, JNF, JPF, JSF, JWF, JZF, KAF, KCF, KDF, KFF, KGF, KIF, KKF, KLF, KOF, KPF, KSF, KWF, LBF, LCF, LEF, LIF, LMF, LOF, LQF, LRF, LTF, LUF, LVF, LWF, LXF, MAF, MBF, MCF, MEF, MIF, MJF, MKF, MPF, MQF, MVF, MXF, MYF, NAF, NBF, NCF, NDF, NEF, NGF, NHF, NIF, NJF, NLF, NMF, NNF, NOF, NQF, NSF, NTF, NUF, NVF, NWF, NXF, NYF, NZF, OAF, OBF, OCF, ODF, OEF, OGF, OHF, OJF, OKF, OLF, OMF, OOF, OPF, OQF, ORF, OSF, OTF, OUF, OVF, OWF, OXF, OYF, OZF, PAF, PBF, PCF, PDF, PEF, PFF, PGF, PHF, PIF, PJF, PKF, PLF, PMF, PNF, RHF, RTF, SPF, T5F, TGF, TJF, TXF, UDF, XAF, XBF, XEF, XIF, XJF)
In [8]:
api.Contracts.Futures.TXF
Out[8]:
TXF(TXF202003, TXF201906, TXF201907, TXF201908, TXF201909, TXF201912)
In [9]:
api.Contracts.Futures.TXF.TXF201906
Out[9]:
Future(symbol='TXF201906', code='TXFF9', name='台指期貨', category='TXF', delivery_month='201906', underlying_kind='I', underlying_code='#001', unit=1.0)
In [10]:
api.Contracts.Futures.TXF['TXFF9']
Out[10]:
Future(symbol='TXF201906', code='TXFF9', name='台指期貨', category='TXF', delivery_month='201906', underlying_kind='I', underlying_code='#001', unit=1.0)
In [12]:
api.Contracts.Futures['TXFF9']
Out[12]:
Future(symbol='TXF201906', code='TXFF9', name='台指期貨', category='TXF', delivery_month='201906', underlying_kind='I', underlying_code='#001', unit=1.0)
In [11]:
TXFR1 = api.Contracts.Futures.TXF.TXF201906

Stocks

In [15]:
api.Contracts.Stocks
Out[15]:
(OTC, TSE)
In [17]:
api.Contracts.Stocks.TSE.TSE2330
Out[17]:
Stock(symbol='TSE2330', exchange='TSE', code='2330', name='台積電', category='24')
In [13]:
api.Contracts.Stocks['2330']
Out[13]:
Stock(symbol='TSE2330', exchange='TSE', code='2330', name='台積電', category='24')
In [18]:
contract_2330 = api.Contracts.Stocks['2330']

Quote Api

In [6]:
api.quote?
Type:        Quote
String form: <shioaji.backend.solace.quote.Quote object at 0x7fe4bba6d828>
File:        shioaji/backend/solace/quote.py
Docstring:  
Quote Api

Functions:
    set_callback
    set_event_callback
    subscribe 

Set event and quote callback

In [7]:
api.quote.set_callback?
Signature: api.quote.set_callback(func)
Docstring:
set quote callback

Args: 
    func (:function): func(topic: str, quote_msg: dict) -> int
        when receive quote will call this function to processed the msg
File:      shioaji/backend/solace/quote.py
Type:      method
In [8]:
api.quote.set_event_callback?
Signature: api.quote.set_event_callback(func)
Docstring:
set event callback

Args:
    func (:function): func(resp_code: int, event_code: int, event: str) -> None
        when event happend will call this function to deal with event
File:      shioaji/backend/solace/quote.py
Type:      method

Prepare your callback, now just keep it simple

In [7]:
@sj.on_quote
def quote_callback(topic, quote_msg):
    print(topic, quote_msg)

@sj.on_event
def event_callback(resp_code, event_code, event):
    print("Respone Code: {} | Event Code: {} | Event: {}".format(resp_code, event_code, event))
In [8]:
api.quote.set_callback(quote_callback)
api.quote.set_event_callback(event_callback)

Subscribe quote with Contract

In [9]:
api.quote.subscribe?
Signature: api.quote.subscribe(contract, quote_type='tick')
Docstring:
subscribe the quote of contract 

Args:
    contract (:obj:Shioaji.Contract): the contract you want to subscribe
    quote_type (str): {tick, bidask}
File:      shioaji/backend/solace/quote.py
Type:      method
In [15]:
TSE2330 = api.Contracts.Stocks['2330']
TXFR1 = api.Contracts.Futures['TXFF9']
In [10]:
api.quote.subscribe(TXFR1)
api.quote.subscribe(TXFR1, quote_type='bidask')
api.quote.subscribe(TSE2330)
api.quote.subscribe(TSE2330, quote_type='bidask')
Respone Code: 200 | Event Code: 16 | Event: Subscribe or Unsubscribe ok
Respone Code: 200 | Event Code: 16 | Event: Subscribe or Unsubscribe ok
Respone Code: 200 | Event Code: 16 | Event: Subscribe or Unsubscribe ok
MKT/redisrd/TSE/2330 {'Close': [239.5], 'Time': '11:11:42.624718', 'VolSum': [12206], 'Volume': [5]}
QUT/redisrd/TSE/2330 {'AskPrice': [240.0, 240.5, 241.0, 241.5, 242.0], 'AskVolume': [1808, 1789, 1645, 582, 1170], 'BidPrice': [239.5, 239.0, 238.5, 238.0, 237.5], 'BidVolume': [204, 765, 389, 475, 359], 'Date': '2019/03/18', 'Time': '11:11:42.624718'}
L/TFE/TXFC9 {'Amount': [10466.0], 'AmountSum': [631295113.0], 'AvgPrice': [10465.247302024103], 'Close': [10466.0], 'Code': 'TXFC9', 'Date': '2019/03/18', 'DiffPrice': [53.0], 'DiffRate': [0.508979160664554], 'DiffType': [2], 'High': [10474.0], 'Low': [10420.0], 'Open': 10437.0, 'TargetKindPrice': 10476.09, 'TickType': [2], 'Time': '11:11:44.457000', 'TradeAskVolSum': 29697, 'TradeBidVolSum': 27732, 'VolSum': [60323], 'Volume': [1]}
MKT/redisrd/TSE/2330 {'Close': [240.0], 'Time': '11:11:47.643968', 'VolSum': [12208], 'Volume': [2]}
QUT/redisrd/TSE/2330 {'AskPrice': [240.0, 240.5, 241.0, 241.5, 242.0], 'AskVolume': [1807, 1791, 1645, 582, 1170], 'BidPrice': [239.5, 239.0, 238.5, 238.0, 237.5], 'BidVolume': [207, 763, 389, 475, 359], 'Date': '2019/03/18', 'Time': '11:11:47.643968'}

Making Order object to place order

In [19]:
api.Order?
Init signature:
api.Order(
    price,
    quantity,
    action,
    price_type,
    order_type,
    order_cond='Cash',
    octype='Auto',
    *args,
    **kwargs,
)
Docstring:     
The basic order object to place order
All attributes can be found in shioaji.constant and shioaji.orderprops for nest struct

Attributes:
    price (float or int): the price of order
    quantity (int): the quantity of order
    action (str): order action to buy or sell
    price_type (str): pricing type of order
    order_type (str): the type of order
    order_cond (str): order condition stock only
    octype (str): the type or order to open new position or close position future only
    account (:obj:Account): which account to place this order
    ca (binary): the ca of this order
Init docstring:
the __init__ method of order      

Args:
    price (float or int): 
        the price of order
    quantity (int): 
        the quantity of order
    action (str): {Buy, Sell} 
        order action to buy or sell
    price_type (str): 
        order price type
        Stock {LMT, Close, LimitUp, LimitDown}
        Futures {LMT, MKT, MKP}
    order_type (str): 
        the type of order
        Stock {Common, BlockTrade, Fixing, Odd}
        Futures {ROD, IOC, FOK}
    order_cond (str): {Cash, Netting, MarginTrading, ShortSelling}
        (Stock Only) 
        order condition
    octype (str): {Auto, NewPosition, Cover, DayTrade}
        (Futures Only)
        the type of order to open new position or close position 
    account (:obj:Account): which account to place this order
File:           shioaji/order.py
Type:           type
Subclasses:     

Constant that use in Order

In [6]:
sj.constant??
Type:        module
String form: <module 'shioaji.constant' from 'shioaji/constant.py'>
File:        shioaji/constant.py
Source:     
ACTION_BUY = 'Buy'
ACTION_SELL = 'Sell'

STOCK_PRICE_TYPE_LIMITPRICE = 'LMT'
STOCK_PRICE_TYPE_CLOSE = 'Close'
STOCK_PRICE_TYPE_LIMITUP = 'LimitUp'
STOCK_PRICE_TYPE_LIMITDOWN = 'LimitDown'

STOCK_ORDER_TYPE_COMMON = 'Common'  # 整股
STOCK_ORDER_TYPE_BLOCKTRADE = 'BlockTrade'  # 鉅額
STOCK_ORDER_TYPE_FIXING = 'Fixing'  # 定盤
STOCK_ORDER_TYPE_ODD = 'Odd'  # 零股

STOCK_ORDER_COND_CASH = 'Cash'  # 現股
STOCK_ORDER_COND_NETTING = 'Netting'  # 餘額交割
STOCK_ORDER_COND_MARGINTRADING = 'MarginTrading'  # 融資
STOCK_ORDER_COND_SHORTSELLING = 'ShortSelling'  # 融券

STOCK_FIRST_SELL_YES = 'true'
STOCK_FIRST_SELL_NO = 'false'

FUTURES_PRICE_TYPE_LMT = 'LMT'
FUTURES_PRICE_TYPE_MKT = 'MKT'
FUTURES_PRICE_TYPE_MKP = 'MKP'

FUTURES_ORDER_TYPE_ROD = 'ROD'
FUTURES_ORDER_TYPE_IOC = 'IOC'
FUTURES_ORDER_TYPE_FOK = 'FOK'

FUTURES_OCTYPE_AUTO = 'Auto'
FUTURES_OCTYPE_NEWPOSITION = 'New'
FUTURES_OCTYPE_COVER = 'Cover'
FUTURES_OCTYPE_DAYTRADE = 'DayTrade'

FUTURES_CALLPUT_FUT = 'F'
FUTURES_CALLPUT_CALL = 'C'
FUTURES_CALLPUT_PUT = 'P'

For Developing use

using tab to direct get all the Order properties with autocomplete

In [13]:
api.OrderProps.
Out[13]:
shioaji.backend.http.order.Order_props
In [7]:
api.OrderProps.Future.order_type.IOC
Out[7]:
'IOC'
In [9]:
sample_order = api.Order(price=9600,
                         quantity=5,
                         action=api.OrderProps.Future.action.Buy,
                         price_type=api.OrderProps.Future.price_type.LMT,
                         order_type=api.OrderProps.Future.order_type.ROD,
                         octype=api.OrderProps.Future.octype.Auto,
                         account=api.fut_account,
                        )
sample_order
Out[9]:
Order(action='Buy', price_type='LMT', order_type='ROD', price=9600, quantity=5, account=FutureAccount())

Directly use string to build Order according to shioaji.constant

In [25]:
sample_order = api.Order(price=9600,
                         quantity=5,
                         action='Buy',
                         price_type='LMT',
                         order_type='ROD',
                        )
sample_order
Out[25]:
Order(action='Buy', price_type='LMT', order_type='ROD', price=9600, quantity=5)
In [26]:
from shioaji.constant import *
sample_order = api.Order(price=9600,
                         quantity=5,
                         action=ACTION_SELL,
                         price_type=FUTURES_PRICE_TYPE_LMT,
                         order_type=FUTURES_ORDER_TYPE_IOC,
                        )
sample_order
Out[26]:
Order(action='Sell', price_type='LMT', order_type='IOC', price=9600, quantity=5)

just pass Order object to place_order fuction to place order then will get the Trade object return

In [27]:
trade = api.place_order(TXFR3, sample_order)
In [28]:
trade
Out[28]:
Trade(contract=Future(symbol='TXF201903', code='TXFC9', name='台指期貨', category='TXF', delivery_month='201903', underlying_kind='I', underlying_code='#001', unit=1.0), order=Order(product_id='TXFC9', action='B', price_type='LMT', order_type='ROD', price=9600, quantity=5, account=FutureAccount(person_id='SCCEIEFAJA', broker_id='F002000', account_id='9104000', username='莊*芬')), status=OrderStatus(seqno='701124', order_id='7521840eb43914f94f98f025b1762e0b250ded21', status='PendingSubmit', order_datetime=datetime.datetime(2019, 1, 16, 12, 39, 28)))

Update the trade object status to get the trade information

In [29]:
api.update_status()
In [30]:
trade
Out[30]:
Trade(contract=Future(symbol='TXF201903', code='TXFC9', name='台指期貨', category='TXF', delivery_month='201903', underlying_kind='I', underlying_code='#001', unit=1.0), order=Order(product_id='TXFC9', action='B', price_type='LMT', order_type='ROD', price=9600, quantity=5, account=FutureAccount(person_id='SCCEIEFAJA', broker_id='F002000', account_id='9104000', username='莊*芬')), status=OrderStatus(seqno='701124', ordno='ky00P', order_id='7521840eb43914f94f98f025b1762e0b250ded21', status='Submitted', status_code='0000', msg='ky00P', modified_price=9600.0, remaining=5, order_datetime=datetime.datetime(2019, 1, 16, 12, 39, 28)))

Modify price or qty of trade

In [31]:
trade = api.update_order(trade, price=9800, qty=1)
In [32]:
trade
Out[32]:
Trade(contract=Future(symbol='TXF201903', code='TXFC9', name='台指期貨', category='TXF', delivery_month='201903', underlying_kind='I', underlying_code='#001', unit=1.0), order=Order(product_id='TXFC9', action='B', price_type='LMT', order_type='ROD', price=9600, quantity=5, account=FutureAccount(person_id='SCCEIEFAJA', broker_id='F002000', account_id='9104000', username='莊*芬')), status=OrderStatus(seqno='701124', ordno='ky00P', order_id='7521840eb43914f94f98f025b1762e0b250ded21', status='Submitted', status_code='0000', msg='ky00P', modified_price=9800.0, remaining=5, order_datetime=datetime.datetime(2019, 1, 16, 12, 39, 28)))

Cancel order of the trade

In [ ]:
trade = api.cancel_order(trade)
trade

Account & Portfolio

Account Margin

In [27]:
api.get_account_margin?
Signature: api.get_account_margin(currency='NTD', margin_type='1', account={})
Docstring:
query margin    
currency:{NTX, USX, NTD, USD, HKD, EUR, JPY, GBP}
    the margin calculate in which currency
    - NTX: 約當台幣
    - USX: 約當美金
    - NTD: 新台幣
    - USD: 美元
    - HKD: 港幣
    - EUR: 歐元
    - JPY: 日幣
    - GBP: 英鎊
margin_type: {'1', '2'}
    query margin type
    - 1 : 即時
    - 2 : 風險
File:       
Type:      method
In [21]:
account_margin = api.get_account_margin()
account_margin
Out[21]:
AccountMargin
    Currency: NTD
    Account: F0020009104000
    Detail:
        OrderPSecurity: 207000.0
        ProfitAccCount: 207000.0
        FProfit: 0.0
        FMissConProfit: 0.0
        OMissConProfit: 0.0
        OColse: 0.0
        OMarketPrice: 0.0
        OTodayDiff: 0.0
        HandCharge: 0.0
        TradeTax: 0.0
        Security: 0.0
        StartSecurity: 0.0
        UpKeepSecurity: 0.0
        Statistics: 99999.0
        Flow: 999.0
        orderBid: 0.0
        orderAsk: 0.0
        Conclusionbid: 0.0
        Conclusionask: 0.0
        YesterdayBalance: 207000.0
        PayMoney: 0.0
        Equity: 207000.0
        Ogain: 0.0
        exrate: 1.0
        xgdamt: 0.0
        agtamt: 0.0
        YesterdayEquity: 207000.0
        Munet: 0.0
        Cashamt: 207000.0
        Bapamt: 0.0
        Sapamt: 0.0
        Adps: 0.0
        Adamt: 0.0
        Ybaln: 207000.0

directly pass our AccountMargin object to pandas to using your model

In [24]:
df_margin = pd.DataFrame([{**account_margin}])
df_margin
Out[24]:
Adamt Adps Bapamt Cashamt Conclusionask Conclusionbid Equity FMissConProfit FProfit Flow ... TradeTax UpKeepSecurity Ybaln YesterdayBalance YesterdayEquity agtamt exrate orderAsk orderBid xgdamt
0 0.0 0.0 0.0 207000.0 0.0 0.0 207000.0 0.0 0.0 999.0 ... 0.0 0.0 207000.0 207000.0 207000.0 0.0 1.0 0.0 0.0 0.0

1 rows × 34 columns

Get Open Position

In [28]:
api.get_account_openposition?
Signature: api.get_account_openposition(product_type='0', query_type='0', account={})
Docstring:
query open position
product_type: {0, 1, 2, 3}
    filter product type of open position
    - 0: all
    - 1: future
    - 2: option
    - 3: usd base
query_type: {0, 1}
    query return with detail or summary
    - 0: detail
    - 1: summary
File:      
Type:      method
In [29]:
positions = api.get_account_openposition(query_type='1', account=api.fut_account)
positions
Out[29]:
AccountOpenPosition
In [30]:
df_positions = pd.DataFrame(positions.data())
df_positions
Out[30]:
Account Code CodeName ContractAverPrice Currency Date FlowProfitLoss MTAMT OTAMT OrderBS OrderNum OrderType RealPrice SettlePrice SettleProfitLoss StartSecurity UpKeepSecurity Volume paddingByte
0 FF0020009104000 TXFA9 台指期貨 01 9508.4137 NTD 00000000 4795201.620000 6438000.000000 8352000.000000 B 9784.0 9784.00 4795201.620000 8352000.000000 6438000.000000 87.000000
1 FF0020009104000 XJFF9 日圓期貨 06 80.0000 JPY 00000000 31400.000000 47000.000000 61000.000000 B 0.0 81.57 31400.000000 61000.000000 47000.000000 1.000000
2 FF0020009104000 TXO08000L8 台指選擇權 8000 C 12 1870.0000 NTD 00000000 -14000.000000 0.000000 0.000000 B 1730.0 1810.00 -6000.000000 0.000000 0.000000 2.000000
3 FF0020009104000 TXO09200L8 台指選擇權 9200 C 12 720.0000 NTD 00000000 11250.000000 147000.000000 162000.000000 S 645.0 660.00 9000.000000 162000.000000 147000.000000 3.000000
4 FF0020009104000 TXO09400X8 台指選擇權 9400 P 12 199.0000 NTD 00000000 21200.000000 57600.000000 65600.000000 S 93.0 93.00 21200.000000 65600.000000 57600.000000 4.000000
5 FF0020009104000 TXO10200L8 台指選擇權 10200 C 12 111.0000 NTD 00000000 33550.000000 125950.000000 147950.000000 S 50.0 50.00 33550.000000 147950.000000 125950.000000 11.000000

Get Settle ProfitLoss

In [31]:
api.get_account_settle_profitloss?
Signature: api.get_account_settle_profitloss(product_type='0', summary='Y', start_date='', end_date='', currency='', account={})
Docstring:
query settlement profit loss
product_type: {0, 1, 2}
    filter product type of open position
    - 0: all
    - 1: future
    - 2: option
summary: {Y, N}
    query return with detail or summary
    - Y: summary
    - N: detail
start_date: str
    the start date of query range format with %Y%m%d
    ex: 20180101
end_date: str
    the end date of query range format with %Y%m%d
    ex: 20180201
currency: {NTD, USD, HKD, EUR, CAD, BAS}
    the profit loss calculate in which currency
    - NTD: 新台幣
    - USD: 美元
    - HKD: 港幣
    - EUR: 歐元
    - CAD: 加幣 
    - BAS: 基幣
File:      
Type:      method
In [40]:
st_date = (date.today() - timedelta(days=60)).strftime('%Y%m%d')
settle_profitloss = api.get_account_settle_profitloss(summary='Y', start_date=st_date)
settle_profitloss
Out[40]:
AccountSettleProfitLoss
In [41]:
df_profitloss = pd.DataFrame(settle_profitloss.data())
df_profitloss
Out[41]:
account averagePrice code codeName currency floatProfitLoss handCharge ord_bs ord_type ordno ordno_b settleAvgPrc settleDate settleVolume tFlag tdate tradeProfitLoss tradeTax unVolume volume
0 F0020009104000 9900.0 TXFK8 台指期貨 11 NTD 460.000000 60.000000 S 00 kY002 kY003 9897.0 20181022 1.000000 1 20181022 600.000000 80.000000 0.000000 1.000000

Communication

Gitter

  • Gitter: general chat, online discussions, collaboration etc.
  • GitHub issues: bug reports, feature requests, install issues, RFCs, thoughts, etc.

Releases and Contributing

Shioaji current state is Pre-Alpha, we expect no obvious bugs. Please let us know if you encounter a bug by filing an issue.

We appreciate all suggestions. If you have any idea want us to implement, please discuss with us in gitter.

The Team

Shioaji is currently maintained by Yvictor, TK Huang, Sky Wu and Po Chien Yang with major contributions.