Beispiel #1
0
    def Test15(self):
        # 创建ITrade char * path指xxx.exe同级子目录中的xxx.dll文件
        self.trade = XFinApi_TradeApi.XFinApi_CreateTradeApi(
            "XTA_W32/Api/CTP_v6.3.15_20190220/XFinApi.CTPTradeApiSE.dll")
        if isinstance(self.trade, int):
            print("* Trade XFinApiCreateError={};".format(
                StrCreateErrors[self.trade]))
            return
        else:
            self.trade = self.trade[0]

        self.tradeEvent = TradeEvent(self.trade, self.cfgSE)
        self.trade.SetListener(self.tradeEvent)
        openParams = XFinApi_TradeApi.OpenParams()
        openParams.HostAddress = self.cfgSE.TradeAddress
        openParams.BrokerID = self.cfgSE.BrokerID
        openParams.UserID = self.cfgSE.UserName
        openParams.Password = self.cfgSE.Password
        openParams.Configs["AppID"] = self.cfgSE.AppID
        openParams.Configs["AuthCode"] = self.cfgSE.AuthCode
        openParams.IsUTF8 = True

        self.trade.Open(openParams)

        self.QryAndOrder(self.cfgSE)
Beispiel #2
0
    def QryAndOrder(self, cfg):
        # 连接成功后才能执行查询、委托等操作,检测方法有两种:
        # 1、self.trade.IsOpened() == 1
        # 2、TradeEvent的OnNotify中
        #  XFinApi_TradeApi.ActionKind_Open == notifyParams.ActionType and XFinApi_TradeApi.ResultKind_Success == notifyParams.ResultType
        while self.trade.IsOpened() != 1:
            time.sleep(1)

        qryParam = XFinApi_TradeApi.QueryParams()
        qryParam.InstrumentID = cfg.InstrumentID

        # 查询委托单 有些接口查询有间隔限制,如:CTP查询间隔为1秒
        time.sleep(1)
        print("Press any key to QueryOrder.")
        input()
        self.trade.QueryOrder(qryParam)

        # 查询成交单
        time.sleep(3)
        print("Press any key to QueryTradeOrder.")
        input()
        self.trade.QueryTradeOrder(qryParam)

        # 查询合约
        time.sleep(3)
        print("Press any key to QueryInstrument.")
        input()
        self.trade.QueryInstrument(qryParam)
        # 查询持仓
        time.sleep(3)
        print("Press any key to QueryPosition.")
        input()
        self.trade.QueryPosition(qryParam)

        # 查询账户
        time.sleep(3)
        print("Press any key to QueryAccount.")
        input()
        self.trade.QueryAccount(qryParam)

        # 委托下单
        time.sleep(1)
        print("Press any key to OrderAction.")
        input()
        order = XFinApi_TradeApi.Order()
        order.InstrumentID = cfg.InstrumentID
        order.Price = cfg.AskPrice1
        order.Volume = 1
        order.Direction = XFinApi_TradeApi.DirectionKind_Buy
        order.OpenCloseType = XFinApi_TradeApi.OpenCloseKind_Open
        # 下单高级选项,可选择性设置
        order.ActionType = XFinApi_TradeApi.OrderActionKind_Insert  # 下单
        order.OrderType = XFinApi_TradeApi.OrderKind_Order  # 标准单
        order.PriceCond = XFinApi_TradeApi.PriceConditionKind_LimitPrice  # 限价
        order.VolumeCond = XFinApi_TradeApi.VolumeConditionKind_AnyVolume  # 任意数量
        order.TimeCond = XFinApi_TradeApi.TimeConditionKind_GFD  # 当日有效
        order.ContingentCond = XFinApi_TradeApi.ContingentCondKind_Immediately  # 立即
        order.HedgeType = XFinApi_TradeApi.HedgeKind_Speculation  # 投机
        self.trade.OrderAction(order)
Beispiel #3
0
 def OnQueryPosition(self,poss):
     print("- OnQueryPosition")
     for pos in poss:
         postoday = 0
         posyesterday = 0
         if XFinApi_TradeApi.IsDefaultValue(pos.PositionToday) == False:
             postoday = pos.PositionToday
         if XFinApi_TradeApi.IsDefaultValue(pos.PositionYesterday) == False:
             posyesterday = pos.PositionYesterday
         print(" InstID={}, Direction={}, PosToday={}, PosYesterday={}".format(pos.InstrumentID, pos.Direction,postoday, posyesterday))
Beispiel #4
0
 def OnQueryPosition(self,poss):
     print("- OnQueryPosition")
     for pos in poss:
         buyposition = 0
         sellposition = 0
         netposition = 0
         if XFinApi_TradeApi.IsDefaultValue(pos.BuyPosition) == False:
             buyposition = pos.BuyPosition
         if XFinApi_TradeApi.IsDefaultValue(pos.SellPosition) == False:
             sellposition = pos.SellPosition
         if XFinApi_TradeApi.IsDefaultValue(pos.NetPosition) == False:
             netposition = pos.NetPosition
         print(" InstID={} {} {}, BuyPosition={},SellPosition={}, NetPosition={}".format(pos.ExchangeID, pos.ProductID, pos.InstrumentID, buyposition, sellposition, netposition))
Beispiel #5
0
 def test(self):
     self.market = XFinApi_TradeApi.XFinApi_CreateMarketApi("XTA_W32/Api/ESITap_v9.3.0.1_20160406/XFinApi.ESITapTradeApi.dll")
     if isinstance(self.market,int):
         print("* Market XFinApiCreateError={};".format(StrCreateErrors[self.market]))
         return
     else:
         self.market = self.market[0]
     self.marketEvent = MarketEvent(self.market,self.cfg)
     self.market.SetListener(self.marketEvent)
     openParams = XFinApi_TradeApi.OpenParams()
     openParams.HostAddress = self.cfg.MarketAddress
     openParams.UserID = self.cfg.MarketUserName
     openParams.Password = self.cfg.MarketPassword
     openParams.IsUTF8 = True
     self.market.Open(openParams)
Beispiel #6
0
 def test15(self):
     self.market = XFinApi_TradeApi.XFinApi_CreateMarketApi(
         "XTA_W32/Api/CTP_v6.3.15_20190220/XFinApi.CTPTradeApiSE.dll")
     if isinstance(self.market, int):
         print("* Market XFinApiCreateError={};".format(
             StrCreateErrors[self.market]))
         return
     else:
         self.market = self.market[0]
     self.marketEvent = MarketEvent(self.market, self.cfgSE)
     self.market.SetListener(self.marketEvent)
     openParams = XFinApi_TradeApi.OpenParams()
     openParams.HostAddress = self.cfgSE.MarketAddress
     openParams.BrokerID = self.cfgSE.BrokerID
     openParams.UserID = self.cfgSE.UserName
     openParams.Password = self.cfgSE.Password
     openParams.IsUTF8 = True
     self.market.Open(openParams)
Beispiel #7
0
 def test(self):
     self.market = XFinApi_TradeApi.XFinApi_CreateMarketApi(
         "XTA_W32/Api/ZDCTP_v20180404/XFinApi.ZDCTPTradeApi.dll")
     if isinstance(self.market, int):
         print("* Market XFinApiCreateError={};".format(
             StrCreateErrors[self.market]))
         return
     else:
         self.market = self.market[0]
     self.marketEvent = MarketEvent(self.market, self.cfg)
     self.market.SetListener(self.marketEvent)
     openParams = XFinApi_TradeApi.OpenParams()
     openParams.HostAddress = self.cfg.MarketAddress
     openParams.Configs["LoginAddress"] = self.cfg.TradeAddress
     openParams.UserID = self.cfg.UserName
     openParams.Password = self.cfg.Password
     openParams.IsUTF8 = True
     self.market.Open(openParams)
Beispiel #8
0
 def OnQueryPosition(self, poss):
     print("- OnQueryPosition:")
     for pos in poss:
         positionTotal = 0
         posSellable = 0
         posYestoday = 0
         avgPrice = 0
         if XFinApi_TradeApi.IsDefaultValue(pos.PositionTotal) == False:
             positionTotal = pos.PositionTotal
         if XFinApi_TradeApi.IsDefaultValue(pos.PositionSellable) == False:
             posSellable = pos.PositionSellable
         if XFinApi_TradeApi.IsDefaultValue(pos.PositionYesterday) == False:
             posYestoday = pos.PositionYesterday
         if XFinApi_TradeApi.IsDefaultValue(pos.AvgPrice) == False:
             avgPrice = pos.AvgPrice
         print(
             " InstID={} {}, Direction={}, PositionTotal={}, PositionSellable={}, PositionYesterday={}, AvgPrice={}"
             .format(pos.ExchangeID, pos.InstrumentID, pos.Direction,
                     positionTotal, posSellable, posYestoday, avgPrice))
Beispiel #9
0
 def test(self):
     self.market = XFinApi_TradeApi.XFinApi_CreateMarketApi(
         "XTA_W32/Api/XTP_v1.1.18.13_20180516/XFinApi.XTPTradeApi.dll")
     if isinstance(self.market, int):
         print("* Market XFinApiCreateError={};".format(
             StrCreateErrors[self.market]))
         return
     else:
         self.market = self.market[0]
     self.marketEvent = MarketEvent(self.market, self.cfg)
     self.market.SetListener(self.marketEvent)
     openParams = XFinApi_TradeApi.OpenParams()
     openParams.HostAddress = self.cfg.MarketAddress
     openParams.UserID = self.cfg.UserName
     openParams.Password = self.cfg.Password
     openParams.Configs["AuthCode"] = self.cfg.AuthCode
     openParams.Configs["ClientID"] = self.cfg.ClientID  #可选
     openParams.IsUTF8 = True
     self.market.Open(openParams)
Beispiel #10
0
 def OnQueryPosition(self, poss):
     print("- OnQueryPosition")
     for pos in poss:
         buyposition = 0
         sellposition = 0
         positioNet = 0
         posyesterday = 0
         if XFinApi_TradeApi.IsDefaultValue(pos.BuyPosition) == False:
             buyposition = pos.BuyPosition
         if XFinApi_TradeApi.IsDefaultValue(pos.SellPosition) == False:
             sellposition = pos.SellPosition
         if XFinApi_TradeApi.IsDefaultValue(pos.NetPosition) == False:
             positioNet = pos.NetPosition
         if XFinApi_TradeApi.IsDefaultValue(pos.PositionYesterday) == False:
             posyesterday = pos.PositionYesterday
             print(
                 " InstID={}, PositionYesterdayDirection={}, PositionYesterday={}, BuyPosition={}, SellPosition={}, NetPosition={}"
                 .format(pos.InstrumentID, pos.PositionYesDirection,
                         posyesterday, buyposition, sellposition,
                         positioNet))
Beispiel #11
0
 def test(self):
     self.market = XFinApi_TradeApi.XFinApi_CreateMarketApi(
         "XTA_W32/Api/UFX_V1.0.0.100/XFinApi.UFXTradeApi.dll")
     if isinstance(self.market, int):
         print("* Market XFinApiCreateError={};".format(
             StrCreateErrors[self.market]))
         return
     else:
         self.market = self.market[0]
     self.marketEvent = MarketEvent(self.market, self.cfg)
     self.market.SetListener(self.marketEvent)
     openParams = XFinApi_TradeApi.OpenParams()
     openParams.HostAddress = self.cfg.HostAddress
     openParams.UserID = self.cfg.UserName
     openParams.Password = self.cfg.Password
     openParams.Configs["LicenseFile"] = self.cfg.LicenseFile
     openParams.Configs["LicensePwd"] = self.cfg.LicensePwd
     openParams.Configs["SendQueueSize"] = self.cfg.SendQueueSize
     openParams.IsUTF8 = True
     self.market.Open(openParams)
Beispiel #12
0
 def OnQueryPosition(self, poss):
     print("- OnQueryPosition")
     for pos in poss:
         buyposition = 0
         sellposition = 0
         postoday = 0
         posyesterday = 0
         if XFinApi_TradeApi.IsDefaultValue(pos.BuyPosition) == False:
             buyposition = pos.BuyPosition
         if XFinApi_TradeApi.IsDefaultValue(pos.SellPosition) == False:
             sellposition = pos.SellPosition
         if XFinApi_TradeApi.IsDefaultValue(pos.PositionToday) == False:
             postoday = pos.PositionToday
         if XFinApi_TradeApi.IsDefaultValue(pos.PositionYesterday) == False:
             posyesterday = pos.PositionYesterday
         print(
             " InstID={}, Direction={}, Price={}, PosToday={}, PosYesterday={}, BuyPosition={},SellPosition={}"
             .format(pos.InstrumentID, pos.Direction,
                     pos.PreSettlementPrice, postoday, posyesterday,
                     buyposition, sellposition))
Beispiel #13
0
 def OnNotify(self, notifyParams: 'NotifyParams'):
     print("* Market")
     str = ""
     for codeinfo in notifyParams.CodeInfos:
         str += "(Code={};LowerCode={};LowerMessage={})".format(
             codeinfo.Code, codeinfo.LowerCode, codeinfo.LowerMessage)
     print(" OnNotify: Action={}, Result={}{}".format(
         notifyParams.ActionType, notifyParams.ResultType, str))
     if XFinApi_TradeApi.ActionKind_Open == notifyParams.ActionType and XFinApi_TradeApi.ResultKind_Success == notifyParams.ResultType:
         # 订阅
         param = XFinApi_TradeApi.QueryParams()
         param.InstrumentID = self.cfg.InstrumentID
         self.market.Subscribe(param)
Beispiel #14
0
 def __del__(self):
     if isinstance(self.marketEvent, MarketEvent):
         del self.marketEvent
     if isinstance(self.market, XFinApi_TradeApi.IMarket):
         XFinApi_TradeApi.XFinApi_ReleaseMarketApi(self.market)
Beispiel #15
0
    def Test(self):
        # 创建ITrade char * path指xxx.exe同级子目录中的xxx.dll文件
        self.trade = XFinApi_TradeApi.XFinApi_CreateTradeApi(
            "XTA_W32/Api/CTP_v6.3.6_20160606/XFinApi.CTPTradeApi.dll")
        if isinstance(self.trade, int):
            print("* Trade XFinApiCreateError={};".format(
                StrCreateErrors[self.trade]))
            return
        else:
            self.trade = self.trade[0]

        self.tradeEvent = TradeEvent(self.trade, self.cfg)
        self.trade.SetListener(self.tradeEvent)
        openParams = XFinApi_TradeApi.OpenParams()
        openParams.HostAddress = self.cfg.TradeAddress
        openParams.BrokerID = self.cfg.BrokerID
        openParams.UserID = self.cfg.UserName
        openParams.Password = self.cfg.Password
        openParams.IsUTF8 = True

        self.trade.Open(openParams)
        # 连接服务器 Cfg.SetNonTradeTime();  非交易时段

        # 连接成功后才能执行查询、委托等操作,检测方法有两种:
        # 1、ITrade.IsOpened() = true
        # 2、TradeListener.OnNotify中
        #  XFinApi_TradeApi.ActionKind_Open == notifyParams.ActionType and XFinApi_TradeApi.ResultKind_Success == notifyParams.ResultType
        while self.trade.IsOpened() != 1:
            time.sleep(1)

        qryParam = XFinApi_TradeApi.QueryParams()
        qryParam.InstrumentID = self.cfg.InstrumentID

        # 查询委托单 有些接口查询有间隔限制,如:CTP查询间隔为1秒
        time.sleep(1)
        print("Press any key to QueryOrder.")
        input()
        self.trade.QueryOrder(qryParam)

        # 查询成交单
        time.sleep(3)
        print("Press any key to QueryTradeOrder.")
        input()
        self.trade.QueryTradeOrder(qryParam)

        # 查询合约
        time.sleep(3)
        print("Press any key to QueryInstrument.")
        input()
        self.trade.QueryInstrument(qryParam)
        # 查询持仓
        time.sleep(3)
        print("Press any key to QueryPosition.")
        input()
        self.trade.QueryPosition(qryParam)

        # 查询账户
        time.sleep(3)
        print("Press any key to QueryAccount.")
        input()
        self.trade.QueryAccount(qryParam)

        # 委托下单
        time.sleep(1)
        print("Press any key to OrderAction.")
        input()
        order = XFinApi_TradeApi.Order()
        order.InstrumentID = self.cfg.InstrumentID
        order.Price = self.cfg.BidPrice1
        order.Volume = 1
        order.Direction = XFinApi_TradeApi.DirectionKind_Buy
        order.OpenCloseType = XFinApi_TradeApi.OpenCloseKind_Open
        # 下单高级选项,可选择性设置
        order.ActionType = XFinApi_TradeApi.OrderActionKind_Insert  # 下单
        order.OrderType = XFinApi_TradeApi.OrderKind_Order  # 标准单
        order.PriceCond = XFinApi_TradeApi.PriceConditionKind_LimitPrice  # 限价
        order.VolumeCond = XFinApi_TradeApi.VolumeConditionKind_AnyVolume  # 任意数量
        order.TimeCond = XFinApi_TradeApi.TimeConditionKind_GFD  # 当日有效
        order.ContingentCond = XFinApi_TradeApi.ContingentCondKind_Immediately  # 立即
        order.HedgeType = XFinApi_TradeApi.HedgeKind_Speculation  # 投机
        self.trade.OrderAction(order)
Beispiel #16
0
 def __del__(self):
     if isinstance(self.tradeEvent, TradeEvent):
         del self.tradeEvent
     if isinstance(self.trade, XFinApi_TradeApi.ITrade):
         XFinApi_TradeApi.XFinApi_ReleaseTradeApi(self.trade)
Beispiel #17
0
    def Test(self):
        # 创建ITrade char * path指xxx.exe同级子目录中的xxx.dll文件
        self.trade = XFinApi_TradeApi.XFinApi_CreateTradeApi(
            "XTA_W32/Api/XTP_v1.1.18.13_20180516/XFinApi.XTPTradeApi.dll")
        if isinstance(self.trade, int):
            print("* Trade XFinApiCreateError={};".format(
                StrCreateErrors[self.trade]))
            return
        else:
            self.trade = self.trade[0]

        self.tradeEvent = TradeEvent(self.trade, self.cfg)
        self.trade.SetListener(self.tradeEvent)
        openParams = XFinApi_TradeApi.OpenParams()
        openParams.HostAddress = self.cfg.TradeAddress
        openParams.UserID = self.cfg.UserName
        openParams.Password = self.cfg.Password
        openParams.Configs["AuthCode"] = self.cfg.AuthCode
        openParams.Configs["ClientID"] = self.cfg.ClientID  #可选
        openParams.IsUTF8 = True

        self.trade.Open(openParams)

        # 连接成功后才能执行查询、委托等操作,检测方法有两种:
        # 1、self.trade.IsOpened() == 1
        # 2、TradeEvent的OnNotify中
        #  XFinApi_TradeApi.ActionKind_Open == notifyParams.ActionType and XFinApi_TradeApi.ResultKind_Success == notifyParams.ResultType
        while self.trade.IsOpened() != 1:
            time.sleep(1)

        qryParam = XFinApi_TradeApi.QueryParams()

        # 查询委托单 有些接口查询有间隔限制,如:CTP查询间隔为1秒
        time.sleep(1)
        print("Press any key to QueryOrder.")
        input()
        self.trade.QueryOrder(qryParam)

        # 查询成交单
        time.sleep(3)
        print("Press any key to QueryTradeOrder.")
        input()
        self.trade.QueryTradeOrder(qryParam)

        # 查询合约
        time.sleep(3)
        print("Press any key to QueryInstrument.")
        input()
        self.trade.QueryInstrument(qryParam)

        # 查询持仓
        time.sleep(3)
        print("Press any key to QueryPosition.")
        input()
        self.trade.QueryPosition(qryParam)

        # 查询账户
        time.sleep(3)
        print("Press any key to QueryAccount.")
        input()
        self.trade.QueryAccount(qryParam)

        # 委托下单
        time.sleep(1)
        print("Press any key to OrderAction.")
        input()
        order = XFinApi_TradeApi.Order()
        order.ExchangeID = self.cfg.MarketID
        order.InstrumentID = self.cfg.InstrumentID
        order.Price = self.cfg.AskPrice1
        order.Volume = 100
        order.Direction = XFinApi_TradeApi.DirectionKind_Buy

        # 下单高级选项,可选择性设置
        order.ActionType = XFinApi_TradeApi.OrderActionKind_Insert  # 下单
        order.OrderType = XFinApi_TradeApi.OrderKind_Order  # 标准单
        order.PriceCond = XFinApi_TradeApi.PriceConditionKind_LimitPrice  # 限价

        self.trade.OrderAction(order)
Beispiel #18
0
    def Test(self):
        # 创建ITrade char * path指xxx.exe同级子目录中的xxx.dll文件
        self.trade = XFinApi_TradeApi.XFinApi_CreateTradeApi("XTA_W32/Api/ESITap_v9.3.0.1_20160406/XFinApi.ESITapTradeApi.dll")
        if isinstance(self.trade,int):
            print("* Trade XFinApiCreateError={};".format(StrCreateErrors[self.trade]))
            return
        else:
            self.trade = self.trade[0]

        self.tradeEvent = TradeEvent(self.trade,self.cfg)
        self.trade.SetListener(self.tradeEvent)
        openParams = XFinApi_TradeApi.OpenParams()
        openParams.HostAddress = self.cfg.TradeAddress
        openParams.UserID = self.cfg.TradeUserName
        openParams.Password = self.cfg.TradePassword
        openParams.IsUTF8 = True

        self.trade.Open(openParams)

        # 连接成功后才能执行查询、委托等操作,检测方法有两种:
        # 1、self.trade.IsOpened() == 1
        # 2、TradeEvent的OnNotify中
        #  XFinApi_TradeApi.ActionKind_Open == notifyParams.ActionType and XFinApi_TradeApi.ResultKind_Success == notifyParams.ResultType
        while self.trade.IsOpened() != 1:
            time.sleep(1)

        qryParam = XFinApi_TradeApi.QueryParams()

        # 查询委托单 有些接口查询有间隔限制,如:CTP查询间隔为1秒
        time.sleep(1)
        print("Press any key to QueryOrder.")
        input()
        self.trade.QueryOrder(qryParam)

        # 查询成交单
        time.sleep(3)
        print("Press any key to QueryTradeOrder.")
        input()
        self.trade.QueryTradeOrder(qryParam)

        # 查询合约
        qryParam.ExchangeID = self.cfg.ExchangeID
        qryParam.ProductID = self.cfg.ProductID
        time.sleep(3)
        print("Press any key to QueryInstrument.")
        input()
        self.trade.QueryInstrument(qryParam)
        # 查询持仓
        time.sleep(3)
        print("Press any key to QueryPosition.")
        input()
        self.trade.QueryPosition(qryParam)

        # 查询账户
        time.sleep(3)
        print("Press any key to QueryAccount.")
        input()
        self.trade.QueryAccount(qryParam)

        # 委托下单
        time.sleep(1)
        print("Press any key to OrderAction.");
        input()
        order = XFinApi_TradeApi.Order()
		order.InvestorID = self.cfg.TradeUserName