iT邦幫忙

2021 iThome 鐵人賽

DAY 30
0
永豐金融APIs

永豐證API X Python系列 第 30

【D30】結尾:佳餚上桌

本日程式碼使用:d30.py

這邊結合第29天的下單功能,以及第28天的製作組合單,讓這個組合單的委託可以送到交易所。

首先我們直接取得第28天的程式碼(d28_spread.py),然後在我們trade 中新增一個新的功能函式:

def place_order(self, contract, buy_side, order_price, order_qty):
    order = t.api.Order(
        action=sj.constant.Action.Buy
        if buy_side == "b"
        else sj.constant.Action.Sell,
        price=order_price,
        quantity=order_qty,
        price_type=sj.constant.StockPriceType.LMT,
        order_type=sj.constant.FuturesOrderType.ROD,
        octype=sj.constant.FuturesOCType.Auto,
        account=t.api.futopt_account,
    )

    trade = t.api.place_order(contract, order)
    return trade

這個函式是用來下選擇權的。其中有四個參數,分別是:合約、買賣別、價格、數量。合約是在送單的時候使用,而其他的就是交易內容物件的主要項目,這邊僅讓只有買賣別、價格、數量此三種可以修改。

接著修改我們取得行情後的動作。目前就簡單一點,不組成一組組合單,直接送出一買一賣的選擇權委託單。

def quote_callback(self, topic: str, quote: dict):
    """Get the quote info and change the oder price.
    The quote's format is v0: quote is a dict and the value is a list.
    """

    if topic.find("16300") > 0:
        self.lef_leg_price = quote["AskPrice"][0]  # 取得ask的第一個值
    elif topic.find("16350") > 0:
        self.right_leg_price = quote["BidPrice"][0]  # 取得bid的第一個值

    print(
        f"價差組合:16300-sell={self.lef_leg_price}_16350-buy={self.right_leg_price}_diff={self.lef_leg_price-self.right_leg_price}"
    )

    result_sell = self.place_order(
        t.api.Contracts.Options.TXO.TXO202110016300C,
        "s",self.lef_leg_price,1
    )

    result_buy = self.place_order(
        t.api.Contracts.Options.TXO.TXO202110016350C,
        "b",self.lef_leg_price,1
    )

    print(f"下單結果:{result_sell}、{result_buy}")

並且用下單結果觀察最後的結果。最後印出來如下:

下單結果:contract=Option(code='TXO16300J1', symbol='TXO202110016300C', name='臺指選擇權10月 16300C', category='TXO', delivery_month='202110', strike_price=16300.0, option_right=<OptionRight.Call: 'C'>, underlying_kind='I', unit=1, limit_up=1850.0, limit_down=0.1, reference=216.0, update_date='2021/10/14') order=Order(action=<Action.Sell: 'Sell'>, price=344.0, quantity=1, id='cacfba18', seqno='980389', account=Account(account_type=<AccountType.Future: 'F'>, person_id='PAPIUSER07', broker_id='F002000', account_id='1107458', signed=True), price_type=<StockPriceType.LMT: 'LMT'>, order_type=<FuturesOrderType.ROD: 'ROD'>) status=OrderStatus(id='cacfba18', status=<Status.PendingSubmit: 'PendingSubmit'>, status_code='    ', order_datetime=datetime.datetime(2021, 10, 15, 0, 16, 35), deals=[])、contract=Option(code='TXO16350J1', symbol='TXO202110016350C', name='臺指選擇權10月 16350C', category='TXO', delivery_month='202110', strike_price=16350.0, option_right=<OptionRight.Call: 'C'>, underlying_kind='I', unit=1, limit_up=1820.0, limit_down=0.1, reference=186.0, update_date='2021/10/14') order=Order(action=<Action.Buy: 'Buy'>, price=344.0, quantity=1, id='c7a46d83', seqno='980390', account=Account(account_type=<AccountType.Future: 'F'>, person_id='PAPIUSER07', broker_id='F002000', account_id='1107458', signed=True), price_type=<StockPriceType.LMT: 'LMT'>, order_type=<FuturesOrderType.ROD: 'ROD'>) status=OrderStatus(id='c7a46d83', status=<Status.PendingSubmit: 'PendingSubmit'>, status_code='    ', order_datetime=datetime.datetime(2021, 10, 15, 0, 16, 36), deals=[])

最後我們這筆單的狀況為:PendingSubmit,也就是正在傳送中(這邊不處理結果與回報,僅作送單)。


後記

這樣第30天結束,也就是做出簡易的根據現在的行情,做出動態的下單系統,雖然還不能真正的上場,但是大部分的功能與雛型已經完成,剩下的就是迭代下去,最重要的是策略會賺錢呀~

簡單來總結一下這次的心得。
本次參與目的是瞭解永豐證券的API可以做到什麼事情,也順便熟悉自動化交易的做法。這30天讓我從0到可以簡易自動化下單,而API文件也幾乎跑超過8成。如果之後要我選各券商API的話,我會選擇他,畢竟比較好操作,而且憑證也比較好解決,不用綁在Windows上,光是這點就從60分起跳了。美中不足的是,目前都只有在國內,海外市場的部分目前沒有明確的開發進度。希望可以很快有海期上來。

如果有機會開發程式交易的話,我覺得可以選他們的來玩,雖然功能不多,但是文件和測試環境齊全,對於開發者還滿友善的。


上一篇
【D29】食材、廚具準備好了,準備上桌
下一篇
【Day ?(31)】測試環境無法登入
系列文
永豐證API X Python31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言