小白一枚如有问题欢迎交流可以加我微信:y1906891715 备注:ctp使用交流
github: https://github.com/nicai0609/
来自大神的封装:是Windows 64位平台下利用Swig工具将CTP C++接口trader API转换为python可调用的接口
可以下载simnow快期新一代 这个期货交易软件是基于ctp
下载地址: http://www.simnow.com.cn/static/softwareOthersDownload.action
然后注册一个账号就可以模拟期货交易一开始有十万的虚拟金额(别亏完了哈哈哈一个账号只能用一次)
说明文档百度云分享
链接:https://pan.baidu.com/s/1dg2Yo4HhUtx2tAwZXJC4Lg
提取码:rloc
说明文档是基于c++的原生接口说明,主要的使用方法可以参考它
# -*- coding: utf-8 -*-
import thosttraderapi as api
import time
class CTradeSpi(api.CThostFtdcTraderSpi):
tapi=''
def __init__(self,tapi):
api.CThostFtdcTraderSpi.__init__(self)
self.tapi=tapi
def OnFrontConnected(self):
print ("OnFrontConnected")
#time.sleep( 60 )
loginfield = api.CThostFtdcReqUserLoginField()
loginfield.BrokerID="9999"
loginfield.UserID="*******" 填写自己账号
loginfield.Password="********"填写自己账号的密码
loginfield.UserProductInfo="python dll"
self.tapi.ReqUserLogin(loginfield,0)
print ("send login ok")
def OnRspUserLogin(self, *args):
print ("OnRspUserLogin")
rsploginfield=args[0]
rspinfofield=args[1]
msg=args[2]
print ("SessionID=",rsploginfield.SessionID)
print ("ErrorID=",rspinfofield.ErrorID)
print ("ErrorMsg=",rspinfofield.ErrorMsg.encode("utf-8",errors='surrogateescape').decode('gbk'))
qryinfofield = api.CThostFtdcQrySettlementInfoField()
qryinfofield.BrokerID="9999"
qryinfofield.InvestorID="*****"填写自己的账号
self.tapi.ReqQrySettlementInfo(qryinfofield,0)
print ("send ReqQrySettlementInfo ok")
#ReqorderfieldInsert(self.tapi)
def OnRspQrySettlementInfo(self, *args):
print ("OnRspQrySettlementInfo")
pSettlementInfo=args[0]
print ("content:",pSettlementInfo.Content.encode("utf-8",errors='surrogateescape').decode('gbk'))
def OnRtnOrder(self, *args):
print ("OnRtnOrder")
rtnfield=args[0]
print ("OrderStatus=",rtnfield.OrderStatus)
print ("StatusMsg=",rtnfield.StatusMsg.encode("utf-8",errors='surrogateescape').decode('gbk'))
print ("LimitPrice=",rtnfield.LimitPrice)
def OnRspOrderInsert(self, *args):
print ("OnRspOrderInsert")
rspinfofield=args[1]
print ("ErrorID=",rspinfofield.ErrorID)
print ("ErrorMsg=",rspinfofield.ErrorMsg.encode("utf-8",errors='surrogateescape').decode('gbk'))
def main():
tradeapi=api.CThostFtdcTraderApi_CreateFtdcTraderApi()
tradespi=CTradeSpi(tradeapi)
#去simnon快期新一代的官网网址可以获取地址
tradeapi.RegisterFront("tcp://180.168.146.187:10001")
tradeapi.RegisterSpi(tradespi)
tradeapi.SubscribePrivateTopic(api.THOST_TERT_RESUME)
tradeapi.SubscribePublicTopic(api.THOST_TERT_RESUME)
tradeapi.Init()
tradeapi.Join()
if __name__ == '__main__':
main()
下多单空单平单都一样具体就是参数不同
平单的话ctp分平今天单和平昨天单
# -*- coding: utf-8 -*-
import thosttraderapi as api
import time
a=""
#下单函数
def ReqorderfieldInsert(tradeapi):
print("ReqOrderInsert Start")
orderfield = api.CThostFtdcInputOrderField()
orderfield.BrokerID = "9999"
orderfield.InstrumentID = "rb1905"
orderfield.UserID = "********"账号
orderfield.InvestorID = "*******"账号
#开买单
orderfield.Direction = api.THOST_FTDC_D_Buy
#下单限价单价格
orderfield.LimitPrice = 4130
orderfield.VolumeTotalOriginal = 1
orderfield.OrderPriceType = api.THOST_FTDC_OPT_LimitPrice
orderfield.ContingentCondition = api.THOST_FTDC_CC_Immediately
orderfield.TimeCondition = api.THOST_FTDC_TC_GFD
orderfield.VolumeCondition = api.THOST_FTDC_VC_AV
orderfield.CombHedgeFlag = "1"
#开平标志 '0'开单 '1'平昨单 '3' 平今单
orderfield.CombOffsetFlag = "0"
orderfield.GTDDate = ""
orderfield.orderfieldRef = "1"
orderfield.MinVolume = 0
orderfield.ForceCloseReason = api.THOST_FTDC_FCC_NotForceClose
orderfield.IsAutoSuspend = 0
tradeapi.ReqOrderInsert(orderfield, 0)
print("ReqOrderInsert End")
#撤单
def ReqOrderfieldAction(tradeapi,z,f):
orderfield = api.CThostFtdcInputOrderActionField()
orderfield.BrokerID = "9999"
orderfield.InstrumentID = "rb1905"
orderfield.UserID = "******"
orderfield.InvestorID='*******'
orderfield.OrderSysID=z
orderfield.ExchangeID=f
orderfield.ActionFlag=api.THOST_FTDC_AF_Delete
tradeapi.ReqOrderAction(orderfield,1)
print("撤单")
class CTradeSpi(api.CThostFtdcTraderSpi):
tapi = ''
def __init__(self, tapi):
api.CThostFtdcTraderSpi.__init__(self)
self.tapi = tapi
def OnFrontConnected(self):
print("OnFrontConnected")
# time.sleep( 60 )
loginfield = api.CThostFtdcReqUserLoginField()
loginfield.BrokerID = "9999"
loginfield.UserID = "*******"自己账号
loginfield.Password = "********"自己账号的密码
loginfield.UserProductInfo = "python dll"
self.tapi.ReqUserLogin(loginfield, 0)
print("send login ok")
def OnRspUserLogin(self, *args):
print("OnRspUserLogin")
rsploginfield = args[0]
rspinfofield = args[1]
msg = args[2]
print("SessionID=", rsploginfield.SessionID)
print("ErrorID=", rspinfofield.ErrorID)
print("ErrorMsg=", rspinfofield.ErrorMsg.encode("utf-8", errors='surrogateescape').decode('gbk'))
#下单
ReqorderfieldInsert(self.tapi)
#qry=api.CThostFtdcQryOrderField()
#qry.ExchangeID='SHFE'
#qry.BrokerID = "9999"
#qry.InvestorID = "******"自己账号
#self.tapi.ReqQryOrder(qry,1)
# qryinfofield = api.CThostFtdcQryTradingAccountField()
# qryinfofield.BrokerID = "9999"
# qryinfofield.InvestorID = "******"自己账号
# self.tapi.ReqQryTradingAccount(qryinfofield, 1)
#查询报单响应
def OnRspQryOrder(self, pOrder, pRspInfo, nRequestID, bIsLast):
what=pOrder
print(what.OrderStatus,what.ExchangeID,what.OrderSysID)
#撤掉为成交的订单
if what.OrderStatus=='3':
ReqOrderfieldAction(self.tapi,what.OrderSysID,what.ExchangeID)
def OnRtnOrder(self, *args):
print("OnRtnOrder")
rtnfield = args[0]
print("OrderStatus=", rtnfield.OrderStatus)
print("StatusMsg=", rtnfield.StatusMsg.encode("utf-8", errors='surrogateescape').decode('gbk'))
print("LimitPrice=", rtnfield.LimitPrice)
print("报单编号",rtnfield.OrderSysID)
def OnRspOrderInsert(self, *args):
print("OnRspOrderInsert")
rspinfofield = args[1]
print("ErrorID=", rspinfofield.ErrorID)
print("ErrorMsg=", rspinfofield.ErrorMsg.encode("utf-8", errors='surrogateescape').decode('gbk'))
def main():
tradeapi = api.CThostFtdcTraderApi_CreateFtdcTraderApi()
tradespi = CTradeSpi(tradeapi)
tradeapi.RegisterFront("tcp://180.168.146.187:10001")
tradeapi.RegisterSpi(tradespi)
tradeapi.SubscribePrivateTopic(api.THOST_TERT_RESUME)
tradeapi.SubscribePublicTopic(api.THOST_TERT_RESUME)
tradeapi.Init()
tradeapi.Join()
if __name__ == '__main__':
main()