iT邦幫忙

2021 iThome 鐵人賽

DAY 6
0
永豐金融APIs

永豐金融APIs - 從零開始到放棄!?系列 第 6

基本操作 - 下單

  1. 建立訂單
from shioaji.constant import *

# 股票
order = api.Order(
    price=600, # 價錢 600
    quantity=1, # 1 張
    action=ACTION_BUY, # 買單
    price_type=STOCK_PRICE_TYPE_LIMITPRICE, # 限價
    order_type=ORDER_TYPE_ROD, # ROD 
)

# action, price_type, order_type 也可以直接用字串,相關對應,請參考最後面的 "參數列表"
order = api.Order(
    price=600, # 價錢 600
    quantity=1, # 1 張
    action="Buy", # 買單
    price_type="LMT", # 限價
    order_type="ROD", # ROD 
)

  1. 下單
# 這裡的 "order" 是上一步建立的訂單
trade = api.place_order(api.Contracts.Stocks['2330'], order)
  1. 查看訂單內容
trade
# 回應
Trade(
    contract=Stock(exchange=<Exchange.TSE: 'TSE'>, 
    code='2330', 
    symbol='TSE2330', 
    name='台積電', 
    category='24', 
    unit=1000, 
    limit_up=660.0, 
    limit_down=540.0, 
    reference=600.0, 
    update_date='2021/09/17', 
    margin_trading_balance=1249, 
    short_selling_balance=208, 
    day_trade=<DayTrade.OnlyBuy: 'OnlyBuy'>), 
    order=Order(action=<Action.Buy: 'Buy'>, 
    price=600, 
    quantity=1, 
    id='e92767cc', 
    seqno='702370', 
    ordno='00000', 
    account=Account(account_type=<AccountType.Stock: 'S'>, 
    person_id='J*********', 
    broker_id='9A9U', 
    account_id='******8', 
    signed=True), 
    price_type=<StockPriceType.LMT: 'LMT'>, 
    order_type=<FuturesOrderType.ROD: 'ROD'>), 
    status=OrderStatus(id='e92767cc', 
    status=<Status.PendingSubmit: 'PendingSubmit'>, 
    status_code='0', 
    order_datetime=datetime.datetime(2021, 9, 21, 20, 7, 1), deals=[])
)

# 更新訂單內容
trade = api.update_status()
  1. 修改訂單
trade = api.update_order(trade, price=610, qty=2)
  1. 取消訂單
trade = api.cancel_order(trade)

參數列表

ACTION_BUY = "Buy" # 買
ACTION_SELL = "Sell" # 賣

STOCK_PRICE_TYPE_LIMITPRICE = "LMT" # 限價
STOCK_PRICE_TYPE_MKT = "MKT" # 市價
STOCK_PRICE_TYPE_CLOSE = "Close" # 平盤價
STOCK_PRICE_TYPE_LIMITUP = "LimitUp" # 跌停價
STOCK_PRICE_TYPE_LIMITDOWN = "LimitDown" # 漲停價

STOCK_ORDER_LOT_COMMON = "Common"  # 整股
STOCK_ORDER_LOT_BLOCKTRADE = "BlockTrade"  # 鉅額
STOCK_ORDER_LOT_FIXING = "Fixing"  # 定盤
STOCK_ORDER_LOT_ODD = "Odd"  # 零股
STOCK_ORDER_LOT_INTRADAY_ODD = "IntradayOdd"  # 零股

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_ORDER_COND_EMERGING = "Emerging"  # 興櫃

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" # 範圍市價

# 當日有效 (Rest of Day)
ORDER_TYPE_ROD = "ROD"
# 立即成交否則取消 (Immediate or Cancel): 允許部份成交,沒有成交的部份就取消
ORDER_TYPE_IOC = "IOC"
# 全部成交否則取消 (Fill or Kill): 一定要全部成交,否則全部取消
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"

QUOTE_TYPE_TICK = "tick" # 成交價
QUOTE_TYPE_BIDASK = "bidask" # 五筆

上一篇
基本操作 - 取得報價
下一篇
基本操作 - 帳務
系列文
永豐金融APIs - 從零開始到放棄!?30

尚未有邦友留言

立即登入留言