#插入订单(Python 3.6)
导入程序中需要的模块.
from QuantBaseApi import QuantCallBack,PT_QuantApi_Python36
创建交易回调类,继承父类的回调方法,用于接收回调信息。比如,登录成功后,用户会接收到对应的登录回调信息;插入订单成功后,会收到对应的插入订单回调信息。
class DataCallBack(QuantCallBack):
def __init__(self):
super(DataCallBack, self).__init__()
def OnConnect(self, type):
print("OnConnnect:", type)
#type值的含义:1为实时服务器 2为历史服务器 3为缓存服务器 推荐在1.2.3三种行情服务器全部连接成功后做行情请求能保证全时段行情稳定回调。10为交易服务器,连接成功之后才可做交易业务请求
pass
def OnDisconnect(self, type):
print("OnDisconnect:", type)
#type值的含义同上
pass
def OnRtnUserInfo(self, pInfo): #用户基本信息
print ("OnRtnUserInfo", pInfo)
pass
def OnRspOrderInsert(self, pRsp, nErr): #用户下单回调
print ("OnRspOrderInsert", pRsp)
pass
def OnRtnOrderStatusChangeNotice(self, pNotice): #订单状态改变通知
print ("OnRtnOrderStatusChangeNotice", pNotice)
pass
def OnRtnOrderMatchNotice(self, pNotice): #成交明细推送
print ("OnRtnOrderMatchNotice", pNotice)
pass
根据创建的交易回调类,创建连接对象实例。
mspi = DataCallBack(); #创建回调类
mapi = PT_QuantApi_Python36.PT_QuantApi(mspi, True, "Td_Real", "MD_Real") #此处设置,创建的是交易的实盘环境,以及行情的实时环境 交易模拟环境在账号有权限的情况下将Td_Real替换成Td_SimulateTd即可
PT_QuantApi_Python36.Init()
根据已创建的连接对象实例,调用登录函数,服务端校验用户名、用户密码的合法性。
用户登录后,触发用户登录回调,回调函数接收相应的回调信息。
retLog = mapi.Login("DevTest1", "abcd1234")
print("QuantPlus Login :", retLog)#打印登录返回码
用户登录成功后,若需插入订单,则需先对之进行参数赋值操作。
szContractCode:证券代码
nOrderPrice:申报价,申报价精确到小数 点后2位。
nOrderVol:申报量,其数值必须为100的整数倍,但是若平仓时有碎股,则不受此限制。
nTradeType:交易类型,主要有以下几种:卖出(sell)、买入(Buy)
nReqId:reqid(用户自己赋值)
nUserInt:用户保留字段(用户自己赋值)
nUserDouble:用户保留字段(用户自己赋值)
szUserStr:用户保留字段(用户自己赋值)
dictInsertReq={}
dictInsertReq["szContractCode"]="002003.SZ" #字符串型
dictInsertReq["nOrderPrice"]=91000 # 9.1*10000
dictInsertReq["nOrderVol"]=500 #整型
dictInsertReq["nTradeType"]=2 # buy:2 sell:1
dictInsertReq["nReqId"]= 1 #整型
dictInsertReq["nUserInt"]=10 #整型
dictInsertReq["nUserDouble"]=10.00 #double型
dictInsertReq["szUserStr"]="sale"#字符串型
将赋值后的参数放进插入订单函数中,API内部会依据参数值对订单进行处理。
处理完成后,订单会获得由API内部分配的订单号,要注意的是,这个订单号与订单是一一对应的关系。
之后,会API内部触发插入订单回调函数,回调函数接收对应的订单信息。
mapi.OrderInsert(dictInsertReq)
#encoding:utf-8
from QuantBaseApi import QuantCallBack,PT_QuantApi_Python36
class DataCallBack(QuantCallBack):
def __init__(self):
super(DataCallBack, self).__init__()
self.api = PT_QuantApi_Python36.PT_QuantApi(self, True, "Td_Real", "MD_Real") #此处设置,创建的是交易的测试环境,以及行情的实时环境 交易生产环境在账号有权限的情况下将Td_Test替换成Td_Real即可
def OnConnect(self, type):
print("OnConnnect:", type)
if type == 10: #1为实时服务器 2为历史服务器 3为缓存服务器 推荐在1.2.3三种行情服务器全部连接成功后做行情请求能保证全时段行情稳定回调 10为交易服务器,连接成功之后才可做交易业务请求
dictInsertReq={}
dictInsertReq["szContractCode"]="002003.SZ" #字符串型
dictInsertReq["nOrderPrice"]=91000 # 9.1*10000
dictInsertReq["nOrderVol"]=100 #整型
dictInsertReq["nTradeType"]=1 # buy:2 sell:1
dictInsertReq["nReqId"]= 1122 #整型
dictInsertReq["nUserInt"]=10 #整型
dictInsertReq["nUserDouble"]=10.00 #double型
dictInsertReq["szUserStr"]="sale"#字符串型
self.api.OrderInsert(dictInsertReq)
pass
def OnDisconnect(self, type):
print("OnDisconnect:", type)
pass
def OnRtnUserInfo(self, pInfo):
print ("OnRtnUserInfo", pInfo)
pass
def OnRspOrderInsert(self, pRsp, nErr):
print ("OnRspOrderInsert", pRsp, " nErr", nErr)
pass
def OnRspOrderDelete(self, pRsp, nErr):
print(pRsp)
pass
def OnRspQryOrder(self, pRsp, nErr, isEnd):
print (pRsp)
pass
def OnRspQryMatch(self, pRsp, nErr, isEnd):
print (pRsp)
pass
def OnRspQryPosition(self, pRsp, nErr, isEnd):
pass
def OnRspQryMaxEntrustCount(self, pRsp, nErr, isEnd):
print (pRsp)
pass
def OnRtnOrderStatusChangeNotice(self, pNotice):
print ("OnRtnOrderStatusChangeNotice", pNotice)
pass
def OnRtnOrderMatchNotice(self, pNotice):
print ("OnRtnOrderMatchNotice", pNotice)
pass
def OnRtnUserAuthen(self, pNotice):
pass
def OnRspHaltingDay(self, pData):
pass
def OnRspSubQuote(self, pData):
pass
def OnRtnTradingCode(self, pWinCode, pOptionCode):
# print(pWinCode)
pass
def OnRtnTradingDay(self, pDay):
pass
def OnRtnHaltingDay(self, pDay):
pass
def OnRtnKLine(self, pKline):
pass
def OnRtnTransaction(self, pTransaction):
pass
def OnRtnOrderQueue(self, pOrderQueue):
pass
def OnRtnOrder(self, pOrder):
pass
def OnRtnDayBegin(self, nReqId, pDate):
pass
def OnRtnDayEnd(self, nReqId, pDate):
pass
def OnRtnMarket(self, pMarket):
print (pMarket)
#采集数据进行交易逻辑处理
#下单
#req = {"nReqId":1,"nUserInt":1,"nUserDouble":1,"szUserStr":"","szContractCode":"002003.SZ","nOrderPrice":76100,"nOrderVol":200,"nTradeType":2}
#self.api.OrderInsert(req)
#撤单
#req = {"nReqId":1,"nUserInt":1,"nUserDouble":1,"szUserStr":"","nOrderId":112333367011,"szOrderStreamId":""}
#self.api.OrderDelete(req)
#查询
#req = {"nReqId":1,"nUserInt":1,"nUserDouble":1,"szUserStr":"","szContractCode":"002003.SZ"}
#self.api.QryOrder(req)
#req = {"nReqId":1,"nUserInt":1,"nUserDouble":1,"szUserStr":"","szContractCode":"002003.SZ"}
#self.api.QryMatch(req)
#req = {"nReqId":1,"nUserInt":1,"nUserDouble":1,"szUserStr":"","szContractCode":"002003.SZ"}
#self.api.QryMaxEntrustCount(req)
pass
def OnRspQryAccountMaxEntrustCount(self,pRsp, nErr, isEnd):
#print (pRsp)
pass
def OnRtnMaxEntrustCount(self,pNotice):
pass
def OnRspTradingDay(self,pData):
pass
def main():
mspi = DataCallBack()
mapi = mspi.api
PT_QuantApi_Python36.Init()
#print mapi.GetErrMsg(3)
#print mapi.getVersion()
retLog = mapi.Login("DevTest1", "abcd1234")
print("QuantPlus Login :", retLog)#打印登录返回码
mapi.Run()
if __name__ == '__main__':
main()