【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分起跳了。美中不足的是,目前都只有在国内,海外市场的部分目前没有明确的开发进度。希望可以很快有海期上来。

如果有机会开发程序交易的话,我觉得可以选他们的来玩,虽然功能不多,但是文件和测试环境齐全,对於开发者还满友善的。


<<:  【後转前要多久】# Day29 Angular - 各种ng指示(ngClass、ngIf、ngFor...)

>>:  30 APCS+温暖而绵长的, 余烬

Day 3 跑一下 Tensorflow 范例

安装 tensorflow-gpu (如果自己电脑没有 GPU 就装 CPU 版) pip inst...

Day25 - this&Object Prototypes Ch3 Objects - Review

Object contents 所有 property 都可以透过 Object.definePr...

30天打造品牌特色电商网站 Day.1 网站介面基础知识

处在疫情时代,电商已然成为时下的热门趋势,电商平台多元且方便,简单几步骤就能轻松开店创业,但如何在高...

Day 27 - 范例动手做 - Ansible 安装 Time-series DB

今天是安装 Time-series DB,我们要安装的 DB 是我们先前示范过的 InfluxDB ...

[Day 8] .Net Task 底层(1)

前言 昨天聊过 Task.WhenAll , 得知其底层就是等待多个 Task 完成的机制, 那 T...