def OnRtnTrade(self, Trade): """成交回报""" # print('OnRtnTrade:', Trade) print('OnRtnTrade:\n', Utils.code_transform(Trade)) PyCTP_Trader_API.dfOnRtnTrade = DataFrame.append( PyCTP_Trader_API.dfOnRtnTrade, other=Utils.code_transform(Trade), ignore_index=True)
def OnRtnOrder(self, Order): """报单回报""" # print('OnRtnOrder:', Order) print('OnRtnOrder:\n', Utils.code_transform(Order)) # 未调用API OrderInsert之前还未生成属性_PyCTP_Trader_API__rsp_OrderInsert if hasattr(self, '_PyCTP_Trader_API__rsp_OrderInsert'): if self.__rsp_OrderInsert['InputOrder']['OrderRef'] == Order[ 'OrderRef']: self.__rsp_OrderInsert['event'].set() PyCTP_Trader_API.dfOnRtnOrder = DataFrame.append( PyCTP_Trader_API.dfOnRtnOrder, other=Utils.code_transform(Order), ignore_index=True)
def OnRtnInstrumentStatus(self, InstrumentStatus): # 将查询结果用df保存 series_InstrumentStatus = Series( Utils.code_transform(InstrumentStatus)) PyCTP_Trader.dfQryInstrumentStatus = pd.DataFrame.append( PyCTP_Trader.dfQryInstrumentStatus, other=series_InstrumentStatus, ignore_index=True)
def __init__(self, InstrumentID): self.BrokerID = b'9999' self.UserID = b'123141' # 063802 self.Password = b'062929AAA' # 123456 self.ExchangeID = b'SHFE' self.listInstrumentID = [b'j1901', b'cu1612'] self.InstrumentID = InstrumentID self.trader = Trade1.PyCTP_Trader.CreateFtdcTraderApi( b'tmp/_tmp_t_') # Trade实例 self.market = Market1.PyCTP_Market.CreateFtdcMdApi( b'tmp/_tmp_m_') # Market实例 #连接交易前置 self.trader.Connect(b'tcp://180.168.146.187:10000') self.market.Connect(b'tcp://180.168.146.187:10010') #交易账号登陆 self.trader.Login(self.BrokerID, self.UserID, self.Password) self.market.Login(self.BrokerID, self.UserID, self.Password) print('交易日', Utils.code_transform(self.trader.GetTradingDay())) print( '查询行情', Utils.code_transform(self.trader.QryDepthMarketData(InstrumentID)))
def queryMarketDate(self): # 查询的行情数据 k_data = Utils.code_transform( self.trader.QryDepthMarketData(self.InstrumentID)) # 将结果添加到数据库 print(datetime.datetime.now()) for dict_one in k_data: try: res = db01.add_kdata(dict_one) print("添加数据成功") except Exception as e: print("错误为", e) print("添加数据失败")
def OnRspQryInstrument(self, Instrument, RspInfo, RequestID, IsLast): """ 请求查询合约响应 """ # print('OnRspQryInstrument:', Instrument, IsLast) series_Instrument = Series(Utils.code_transform(Instrument)) PyCTP_Trader.dfQryInstrument = pd.DataFrame.append( PyCTP_Trader.dfQryInstrument, other=series_Instrument, ignore_index=True) if RequestID == self.__rsp_QryInstrument['RequestID']: if RspInfo is not None: self.__rsp_QryInstrument.update(RspInfo) if Instrument is not None: self.__rsp_QryInstrument['results'].append(Instrument) if IsLast: self.__rsp_QryInstrument['event'].set()
def OnRspQryInvestorPosition(self, InvestorPosition, RspInfo, RequestID, IsLast): """ 请求查询投资者持仓响应 """ if RequestID == self.__rsp_QryInvestorPosition['RequestID']: if RspInfo is not None: self.__rsp_QryInvestorPosition.update(RspInfo) if InvestorPosition is not None: self.__rsp_QryInvestorPosition['results'].append( InvestorPosition) if IsLast: # 将查询结果转为df格式 for i in self.__rsp_QryInvestorPosition['results']: PyCTP_Trader_API.dfQryInvestorPosition = DataFrame.append( PyCTP_Trader_API.dfQryInvestorPosition, other=Utils.code_transform(i), ignore_index=True) self.__rsp_QryInvestorPosition['event'].set()
def OnRspQryTrade(self, Trade, RspInfo, RequestID, IsLast): """请求查询成交单响应""" if RequestID == self.__rsp_QryTrade['RequestID']: if RspInfo is not None: self.__rsp_QryTrade.update(RspInfo) if Trade is not None: self.__rsp_QryTrade['results'].append(Trade) PyCTP_Trader_API.dfQryTrade = DataFrame.append( PyCTP_Trader_API.dfQryTrade, other=Utils.code_transform(Trade), ignore_index=True) if IsLast: print("OnRspQryTrade() Trade", Trade) print("OnRspQryTrade() RspInfo", RspInfo) print("OnRspQryTrade() RequestID", RequestID) print("OnRspQryTrade() IsLast", IsLast) self.__rsp_QryTrade['event'].set()
def OnRspQryOrder(self, Order, RspInfo, RequestID, IsLast): """请求查询投资者持仓响应""" if RequestID == self.__rsp_QryOrder['RequestID']: if RspInfo is not None: self.__rsp_QryOrder.update(RspInfo) if Order is not None: self.__rsp_QryOrder['results'].append(Order) PyCTP_Trader_API.dfQryOrder = DataFrame.append( PyCTP_Trader_API.dfQryOrder, other=Utils.code_transform(Order), ignore_index=True) if IsLast: print("OnRspQryOrder() Order", Order) print("OnRspQryOrder() RspInfo", RspInfo) print("OnRspQryOrder() RequestID", RequestID) print("OnRspQryOrder() IsLast", IsLast) self.__rsp_QryOrder['event'].set()
def OnErrRtnOrderInsert(self, InputOrder, RspInfo): """报单录入错误回报""" print('OnErrRtnOrderInsert:', Utils.code_transform(InputOrder), Utils.code_transform(RspInfo))
def OnErrRtnOrderAction(self, OrderAction, RspInfo): """ 报单操作错误回报 """ print('OnErrRtnOrderAction:', Utils.code_transform(OrderAction), Utils.code_transform(RspInfo))
def queryMarketDate(self): time.sleep(1.0) #查询的行情数据 return Utils.code_transform( self.trader.QryDepthMarketData(self.InstrumentID))