Skip to main content

创建API实例

创建接口实例

事件消息需要继承api中QuoteEvent和TradeEvent类,并重写对应方法,即可在对应的回调方法收到实时更新事件 TCoreAPI参数也可以不带入事件类名,不带入事件类就无法收到实时消息,只能使用同步接口方法, 之后所有方法操作都需要基于此段代码

from icetcore import (TCoreAPI,
                    QuoteEvent,
                    TradeEvent,
                    BarType,
                    GreeksType,
                    OrderStruct,
                    SymbolType,
                    OrderSide,
                    TimeInForce,
                    OrderType,
                    PositionEffect)
#api=TCoreAPI(APIEvent)#icetcore 6.6之前版本
api=TCoreAPI(apppath="C:/AlgoMaster2/APPs64",eventclass=APIEvent)#icetcore 6.6及之之后版本
re=api.connect()#(appid="AlgoMaster")
time.sleep(1)

实时消息

实时消息需要创建继承自TradeEvent和QuoteEvent的类,并重写父类方法,可以全部重写,或者按照 需要重写部分父类方法,并在接口实例化时,传入定义类TCoreAPI(APIEvent),并通过api.join()来阻塞进程,确保进程进入消息接收状态

class APIEvent(TradeEvent,QuoteEvent):
    #连线成功通知apitype="quoteapi"是行情api的通知,"tradeapi"是交易通知
    def onconnected(self,apitype:str):
        print(apitype)
    #断线成功通知
    def ondisconnected(self,apitype:str):
        print(apitype)
    #subATM订阅的ATM合约信息,动态推送期权的ATM平值,OTM-1C虚值一档认购,OTM-1P虚值一档认沽,和认购期权合约列表
    def onATM(self,datatype,symbol,data:dict):
        pass
    #实时greeks
    def ongreeksreal(self,datatype,symbol,data:dict):
        print("实时\n")
        print(data)
        pass
    #subgreeksline订阅的动态线图数据
    def ongreeksline(self,datatype,interval,symbol,data,isreal):
        print(data)
        pass
    #subquote订阅的合约实时行情
    def onquote(self,data):
        #print(data)
        pass
    #subbar订阅的动态K线数据
    def onbar(self,bartype,interval,symbol,data:list,isreal:bool):
        # print("onbar",self._tcoreapi.getsymbol_session("TC.F.SHFE.rb.HOT"))
        # print("############\n",bartype,"  ",interval,"  ",data[1],"\n",data[2],"\n",data[3])
        print("$$$$$$$$$$$$$$$$$$$$$$\n",bartype,"  ",interval,"  ",data[-3],"\n",data[-2],"\n",data[-1])
    #server时间
    def onservertime(self, serverdt):
        print("!!!!!!!!!!!!!!!",serverdt)
        pass
    #资金账户列表,当有资金账户登出或者登入时
    def onaccountlist(self,data,count):
        print(data,count)
    # #实时委托信息
    def onordereportreal(self,data):
        print(data)
    # #实时成交信息
    def onfilledreportreal(self,data):
        print(data)
    #账户登出登入时委托资料需要清理通知
    def onorderreportreset(self):
        print("onorderreportreset")
    # #期权持仓监控信息
    # def onpositionmoniter(self,data):
    #     print(data)
#api=TCoreAPI()#icetcore 6.6之前版本
api=TCoreAPI(apppath="C:/AlgoMaster2/APPs64",eventclass=APIEvent)#icetcore 6.6及之之后版本
re=api.connect()#(appid="AlgoMaster")
api.join()

请求查询

from icetcore import (TCoreAPI,
                    QuoteEvent,
                    TradeEvent,
                    BarType,
                    GreeksType,
                    OrderStruct,
                    SymbolType,
                    OrderSide,
                    TimeInForce,
                    OrderType,
                    PositionEffect)
#api=TCoreAPI(APIEvent)#此处没有用到实时消息时,无需传入事件类(#icetcore 6.6之前版本)
api=TCoreAPI(apppath="C:/AlgoMaster2/APPs64")#icetcore 6.6及之之后版本
re=api.connect()#(appid="AlgoMaster")
time.sleep(1)

##获取热门月列表,填入时间时返回对应时间的热门对应指定月合约,Key是换月时间,value是指定月
#获取全部时段换月信息,热门月合约对应的指定月
print(api.gethotmonth("TC.F.CFFEX.T.HOT"))
#获取指定日期热门月合约对应的指定月
print(api.gethotmonth("TC.F.CFFEX.T.HOT","20221223","0900"))
# 获取历史合约信息
print(api.getsymbolhistory(SymbolType.Options,"20221213"))
#获取当日合约列表
print(api.getallsymbol())
#获取合成期货合约列表
print(api.get_u_futuresymbol())
#获取合约信息
print(api.getsymbol_allinfo("TC.O.SSE.510050.202301.C.2.5"))
#获取合约到期日
print(api.getexpirationdate("TC.O.SSE.510050.202301.C.2.5"))
#获取合约最小跳动
print(api.getsymbol_ticksize("TC.O.SSE.510050.202301.C.2.5"))
#获取合约乘数,合约规格大小
print(api.getsymbolvolume_multiple("TC.O.SSE.510050.202301.C.2.5"))
#获取合约编码
print(api.getsymbol_id("TC.O.SSE.510050.202301.C.2.5"))