iT邦幫忙

2021 iThome 鐵人賽

DAY 13
0
永豐金融APIs

在賭場尋求財富是否搞錯了什麼系列 第 13

開個六給他,讓他贏莊家一百塊 - 根據五檔報價之期貨買賣

  • 分享至 

  • xImage
  •  

原本想要實作根據五檔掛買賣下單,發現 snapshots 沒有五檔買賣價格

假如我今天就是想掛某一檔價格的話怎麼辦呢?

這樣的下單沒有價格根據,只好用 streaming marketing data 的 BidAsk


api.quote.subscribe(
    api.Contracts.Futures.TXF['TXF202107'],
    quote_type = sj.constant.QuoteType.BidAsk, # or 'bidask'
    version = sj.constant.QuoteVersion.v1 # or 'v1'
)

回傳結果為


Response Code: 200 | Event Code: 16 | Info: QUO/v1/FOP/*/TFE/TXFG1 | Event: Subscribe or Unsubscribe ok

Exchange.TAIFEX 
BidAsk(
    code = 'TXFG1', 
    datetime = datetime.datetime(2021, 7, 1, 10, 51, 31, 999000), 
    bid_total_vol = 66,  # total bid volume (lot, 買量總計)
    ask_total_vol = 101,  # total ask volume (lot, 賣量總計)
    bid_price = [Decimal('17746'), Decimal('17745'), Decimal('17744'), Decimal('17743'), Decimal('17742')], 
    bid_volume = [1, 14, 19, 17, 15], 
    diff_bid_vol = [0, 1, 0, 0, 0],  # (lot, 買價增減量)
    ask_price = [Decimal('17747'), Decimal('17748'), Decimal('17749'), Decimal('17750'), Decimal('17751')], 
    ask_volume = [6, 22, 25, 32, 16], 
    diff_ask_vol = [0, 0, 0, 0, 0],  # (lot, 賣價增減量)
    first_derived_bid_price = Decimal('17743'),  # first derived bid price (衍生一檔買價)
    first_derived_ask_price = Decimal('17751'),  # first derived ask price (衍生一檔賣價)
    first_derived_bid_vol = 1,  # first derived bid volume (衍生一檔買量)
    first_derived_ask_vol = 1,  # first derived bid volume (衍生一檔賣量)
    underlying_price = Decimal('17827.94'),  # underlying price (標的物價格)
    simtrade = 0
)

想到簡單的做法為先訂閱五檔報價之後挑選一檔下單,這裡的作法是掛買盤的第二檔,也就是口數14口的17745 quote['BidPrice'][1]

買盤
bid_price = [Decimal('17746'), Decimal('17745'), Decimal('17744'), Decimal('17743'), Decimal('17742')]

賣盤
ask_price = [Decimal('17747'), Decimal('17748'), Decimal('17749'), Decimal('17750'), Decimal('17751')]

口數 委買(bid) 委賣(ask) 口數
----- ----- 17751 16
----- ----- 17750 32
----- ----- 17749 25
----- ----- 17748 22
----- ----- 17747 6
1 17746 ----- -----
14 17745 ----- -----
19 17744 ----- -----
17 17743 ----- -----
15 17742 ----- -----
import time

for i in range(0,1):

    api.quote.subscribe(api.Contracts.Futures.TXF.TXF202110, quote_type=shioaji.constant.QuoteType.BidAsk)

    @api.quote.on_quote
    def quote_callback(topic: str, quote: dict):
        print(f"Topic: {topic}, Quote: {quote}")
        order = api.Order(action=shioaji.constant.Action.Buy,
                  price=quote['BidPrice'][1],
                  quantity=1,
                  price_type=shioaji.constant.StockPriceType.LMT,
                  order_type=shioaji.constant.FuturesOrderType.ROD, 
                  octype=shioaji.constant.FuturesOCType.Auto,
                  account=api.futopt_account)
        print(order)

    time.sleep(1)

    api.quote.unsubscribe(api.Contracts.Futures.TXF.TXF202110, quote_type=shioaji.constant.QuoteType.BidAsk)

回傳結果


Topic: Q/TFE/TXFJ1, Quote: {'AskPrice': [16929.0, 16930.0, 16931.0, 16932.0, 16933.0], 'AskVolSum': 47, 'AskVolume': [1, 6, 13, 16, 11], 'BidPrice': [16928.0, 16927.0, 16926.0, 16925.0, 16924.0], 'BidVolSum': 50, 'BidVolume': [5, 9, 14, 10, 12], 'Code': 'TXFJ1', 'Date': '', 'DiffAskVol': [0, 0, 0, 0, 0], 'DiffAskVolSum': 0, 'DiffBidVol': [1, 0, 5, -5, 0], 'DiffBidVolSum': 0, 'FirstDerivedAskPrice': 0.0, 'FirstDerivedAskVolume': 0, 'FirstDerivedBidPrice': 0.0, 'FirstDerivedBidVolume': 0, 'TargetKindPrice': 17181.44, 'Time': '23:55:13.064000'}
action=<Action.Buy: 'Buy'> price=16930.0 quantity=1 account=FutureAccount(person_id='', broker_id='', account_id='', signed=True, username='') price_type=<StockPriceType.LMT: 'LMT'> order_type=<FuturesOrderType.ROD: 'ROD'>

接下來送出訂單就可以等看看是否交易成功了


上一篇
買菸賠菸 - 零股買賣
下一篇
我跟你一百萬,再大你一百萬,再show hand - 跟莊家吃香喝辣 ?
系列文
在賭場尋求財富是否搞錯了什麼22
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言