觸價單的設定是,先決定「觸發價」,這是交易人要動作(停損)的價格,再設定「取價」,這是指當市場價格觸及或穿過觸發價,轉成正式委託下到市場時,是要用「限價」、「範圍市價」或是「市價」,三者擇其一來掛出委託。
from pydantic import BaseModel
class TouchOrderCond(BaseModel):
contract: Contract
order: Order
touch_price: float
class TouchOrder:
def __init__(self, api: sj.Shioaji, condition: TouchOrderCond
):
self.flag = False
self.api = api
self.order = condition.order
self.contract = condition.contract
self.touch_price = condition.touch_price
self.api.quote.subscribe(self.contract)
self.api.quote.set_quote_callback(self.touch)
def touch(self, topic, quote):
price = quote["Close"][0]
if price == self.touch_price and not self.flag:
self.flag = True
self.api.place_order(self.contract, self.order)
self.api.quote.unsubscribe(self.contract)