Ordering

order

開始下單的部分吧!首先api的place_order下單的函式中,需要傳入兩個物件,分別是Contract及Order,我們可以從Contracts物件中找到我們要交易的商品Contract,以及用Order製作出一筆order物件,然後丟進函式中,這個函式會回傳一個Trade物件包含Contract, Order及這筆單的狀態物件(OrderStatus)。

Order物件

Order物件需要帶入的參數部分一樣可以用?直接看到文件

input

api.Order?

output

Init signature: api.Order(action, price_type, order_type, price, quantity, *args, **kwargs)
Docstring:     
The basic order object to place order

Attributes:
    product_id (str): the code of product that order to placing
    action (srt): {B, S}, order action to buy or sell
        - B: buy
        - S: sell
    price_type (str): {LMT, MKT, MKP}, pricing type of order
        - LMT: limit
        - MKT: market
        - MKP: market range
    order_type (str): {ROD, IOC, FOK}, the type of order
        - ROD: Rest of Day
        - IOC: Immediate-or-Cancel
        - FOK: Fill-or-Kill
    octype (str): {' ', '0', '1', '6'}, the type or order to open new position or close position 
        - ' ': auto
        - '0': new position
        - '1': close position
        - '6': day trade
    price (float or int): the price of order
    quantity (int): the quantity of order
    account (:obj:Account): which account to place this order
    ca (binary): the ca of this order

接著我們有做一件方便大家開發的事,有兩個物件api.OrderPropsapi.Contracts這兩個物件可以快速輔助開發的時候直接用tab自動補齊的方式找到Order物件中需要填入的欄位。

input

api.OrderProps.</tab>
api.Contracts.</tab>

這邊我們以期貨為範例按tab鍵自動補齊可交易的合約,回傳的Future物件就是一個期貨的Contract物件,可以以isinstance(api.Contracts.Futures.TXF.TXF201903, shioaji.contracts.Contract)來確認他確實是個Contract物件 input

api.Contracts.Futures.TXF

output

TXF(TXF201903, TXF201906, TXF201809, TXF201810, TXF201811, TXF201812)

input

api.Contracts.Futures.TXF.TXF201903

output

Future(symbol='TXF201903', code='TXFC9', name='台指期貨', category='TXF', delivery_month='201903', underlying_kind='I', underlying_code='#001', unit=1.0)

知道使用方式後我們直接用自動補齊的方式幫我快速創建一筆Order,參數在OrderProps都有相對應的欄位可以幫我們免去用文件的選項輸入,當然也可以直接使用文件的選項直接填入。

sample_order = api.Order(price=9600,
                         action=api.OrderProps.action.Buy,
                         price_type=api.OrderProps.price_type.LMT,
                         order_type=api.OrderProps.order_type.ROD,
                         octype=api.OrderProps.octype.auto,
                         quantity=5,
                         account=api.fut_account,
                        )

另外為了不用煩鎖的Order我們可以直接使用MarketOrder, LimitOrder物件,需要填的選項就會很少。

MarketOrder

MarketOrder

    Args:
        product_id (str, optional): the code of product that order to placing 
                                    if not provide will gen from contract when placing order 
        action (srt): {B, S}, order action to buy or sell
            - B: buy
            - S: sell
        quantity (int): the quantity of order
        order_type (str, optional): {IOC, FOK}, the type of order
            - IOC: Immediate-or-Cancel
            - FOK: Fill-or-Kill
        octype (str, optional): {' ', '0', '1', '6'}, the type or order 
                                to open new position or close position 
                                if not provide will become auto mode 
            - ' ': auto
            - '0': new position
            - '1': close position
            - '6': day trade

製作一個MarketOrder物件就非常簡單,用下面填入買或賣,及口數就完成了。 > input

sample_mkt_order = api.MarketOrder('B', 5)
sample_mkt_order

output

MarketOrder(action='B', order_type='IOC', quantity=5)

LimitOrder

LimitOrder

    Args:
        product_id (str, optional): the code of product that order to placing 
                                    if not provide will gen from contract when placing order 
        action (srt): {B, S}, order action to buy or sell
            - B: buy
            - S: sell
        price (float or int): the price of order
        quantity (int): the quantity of order
        order_type (str, optional): {ROD, IOC, FOK}, the type of order
            - ROD: Rest of Day
            - IOC: Immediate-or-Cancel
            - FOK: Fill-or-Kill
        octype (str, optional): {' ', '0', '1', '6'}, the type or order 
                                to open new position or close position 
                                if not provide will become auto mode 
            - ' ': auto
            - '0': new position
            - '1': close position
            - '6': day trade

限價單的部分就多一個參數,價格就完成了。

input

sample_limit_order = api.LimitOrder('B', 9700, 5)
sample_limit_order

output

LimitOrder(action='B', order_type='ROD', price=9700, quantity=5)

下單函式

接著我們把製作好的Contract及Order物件傳進place_order函式中就會送出委託單了,這時候會回傳trade物件,Trade.status底下會是這筆交易的狀態,目前可以看到剛下出去是PendingSubmit。

input

txf = api.Contracts.Futures.TXF.TXF201903
trade = api.place_order(txf, sample_order)

output

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)))

Trade

拿到的Trade物件我可以透過update_status函式來更新所有Trade的狀態,這時候就會看到狀態變為Submitted,成交後會變為Filled。

input

api.update_status()
trade

output

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)))

改價或改量的部分使用api.update_order函式傳入Trade物件,可以看到modified_price就變成9800並

input

trade = api.update_order(trade, price=9800, qty=1)
trade

output

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)))

取消委託使用api.cancel_order函式並傳入欲取消的trade物件,就不寫出範例code了。