def ReqorderfieldInsert(tradeapi):
	print ("ReqOrderInsert Start")
	orderfield=api.CThostFtdcInputOrderField()
	orderfield.BrokerID=BROKERID
	orderfield.InstrumentID=INSTRUMENTID
	orderfield.UserID=USERID
	orderfield.InvestorID=USERID
	orderfield.Direction=DIRECTION
	orderfield.LimitPrice=PRICE
	orderfield.VolumeTotalOriginal=VOLUME
	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"
	orderfield.CombOffsetFlag=OFFSET
	orderfield.GTDDate=""
	orderfield.OrderRef="1"
	orderfield.MinVolume = 0
	orderfield.ForceCloseReason = api.THOST_FTDC_FCC_NotForceClose
	orderfield.IsAutoSuspend = 0
	tradeapi.ReqOrderInsert(orderfield,0)
	print ("ReqOrderInsert End")
Exemple #2
0
def ReqorderfieldInsert(tradeapi):
    print "ReqOrderInsert Start"
    orderfield = api.CThostFtdcInputOrderField()
    orderfield.BrokerID = "8000"
    orderfield.InstrumentID = "rb2005"
    orderfield.UserID = "000005"
    orderfield.InvestorID = "000005"
    orderfield.Direction = api.THOST_FTDC_D_Sell
    orderfield.LimitPrice = 52050
    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"
    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"
Exemple #3
0
    def order(self, code, side, amount, price):
        # 下单,简单化处理,1秒内CTP没有成交,视为废单
        assert side in ['long', 'short'], 'side should be long or short !'
        while self.lock == True or self.position is None:
            time.sleep(0.1)
        order = tdapi.CThostFtdcInputOrderField()
        order.BrokerID = BROKERID
        order.ExchangeID = self.contract.loc[code, 'exchange']  # 交易所代码
        order.InstrumentID = code  # 合约代码
        order.UserID = USERID
        order.InvestorID = USERID
        order.Direction = tdapi.THOST_FTDC_D_Buy if side == 'long' else tdapi.THOST_FTDC_D_Sell  # 多空
        order.LimitPrice = price  # 下单价格
        order.VolumeTotalOriginal = abs(amount)  # 下单数量
        order.OrderPriceType = tdapi.THOST_FTDC_OPT_LimitPrice  # 市价单、限价单
        order.ContingentCondition = tdapi.THOST_FTDC_CC_Immediately  # 立即成交,否则作废
        order.TimeCondition = tdapi.THOST_FTDC_TC_GFD
        order.VolumeCondition = tdapi.THOST_FTDC_VC_AV
        order.CombHedgeFlag = "1"  # 投机套保标志
        order.CombOffsetFlag = '0' if amount > 0 else '1'  # '0'开仓,'1'平仓,'3'平今,'4'平昨
        order.GTDDate = ""
        order.orderfieldRef = "1"
        order.MinVolume = 0
        order.ForceCloseReason = tdapi.THOST_FTDC_FCC_NotForceClose
        order.IsAutoSuspend = 0

        # 上期所需要区分平今仓和平昨仓
        if self.contract.loc[code,
                             'exchange'] == 'SHFE' and amount < 0 and abs(
                                 amount) > self.position.loc[code,
                                                             'yd_amount']:
            new_amount = int(self.position.loc[code, 'yd_amount'])
            order.VolumeTotalOriginal = new_amount
            order.CombOffsetFlag = '4'
            self.api.ReqOrderInsert(order, 0)
            _order = GatewayOrder(code=code,
                                  amount=new_amount,
                                  side='short',
                                  action='close',
                                  price=price)
            self.orderList.append(_order)

            new_amount = int(
                abs(amount) - self.position.loc[code, 'yd_amount'])
            order.VolumeTotalOriginal = new_amount
            order.CombOffsetFlag = '3'
            self.api.ReqOrderInsert(order, 0)
            _order = GatewayOrder(code=code,
                                  amount=new_amount,
                                  side='short',
                                  action='close',
                                  price=price)
            self.orderList.append(_order)
        else:
            self.api.ReqOrderInsert(order, 0)
            _order = GatewayOrder(code=code,
                                  amount=amount,
                                  side=side,
                                  action='open' if amount > 0 else 'close',
                                  price=price)
            self.orderList.append(_order)