Ejemplo n.º 1
0
def test_OrderInsert_WithInvalidValue():
    '''
    测试报单
    '''
    traderChannel = TraderChannel(frontAddress,
                                  brokerID,
                                  userID,
                                  password,
                                  timeout=1)
    print u'日志文件输出到:%s/trader.log' % traderChannel.workdir

    # 尝试提交订单
    requestData = getDefaultInputOrderField_buyOpen()
    requestData.VolumeTotalOriginal = 0  # 手数=0 使表单无效
    result = traderChannel.OrderInsert(requestData)
    print result
    assert result[0] == 15
    assert result[1] == u'CTP:报单字段有误'
Ejemplo n.º 2
0
def test_OrderInsert_OpenAndClose():
    '''
    测试报单
    '''
    # 创建交易通道
    traderChannel = TraderChannel(frontAddress,
                                  brokerID,
                                  userID,
                                  password,
                                  timeout=1)
    print u'日志文件输出到:%s/trader.log' % traderChannel.workdir

    # 交易确认
    requestData = CThostFtdcSettlementInfoConfirmField()
    requestData.BrokerID = brokerID
    requestData.InvestorID = userID
    result = traderChannel.SettlementInfoConfirm(requestData)
    assert result[0] == 0

    # 尝试提交订单
    requestData = getDefaultInputOrderField_buyOpen()
    result = traderChannel.OrderInsert(requestData)
    print result[0], result[1]
    assert result[0] in (0, -2003)

    if result[0] == -2003:  # 非交易时段提交订单报此错误
        return

    # 程序能到这里说明开仓成功
    responseDataList = result[2]
    assert len(responseDataList) == 1
    price0 = responseDataList[0].Price

    # 查询头寸
    requestData = CThostFtdcQryInvestorPositionField()
    requestData.InstrumentID = getDefaultInstrumentID()
    requestData.Direction = '0'  # 做多
    result = traderChannel.QryInvestorPosition(requestData)
    print result[0], result[1]
    responseDataList = result[2]
    assert len(responseDataList) == 1  # 指定了品种和头寸方,应该只有一条记录
    openVolume0 = responseDataList[0].OpenVolume
    closeVolume0 = responseDataList[0].CloseVolume
    print 'openVolume0=', openVolume0, ',closeVolume0=', closeVolume0
    assert openVolume0 > 0
    assert openVolume0 > closeVolume0

    # 尝试进行平仓
    requestData = getDefaultInputOrderField_buyClose()
    result = traderChannel.OrderInsert(requestData)
    assert result[0] == 0
    responseDataList = result[2]
    assert len(responseDataList) == 1
    price1 = responseDataList[0].Price

    # 查询头寸
    requestData = CThostFtdcQryInvestorPositionField()
    requestData.InstrumentID = getDefaultInstrumentID()
    requestData.Direction = '0'  # 做多
    result = traderChannel.QryInvestorPosition(requestData)
    print result[0], result[1]
    responseData = result[2]
    assert len(responseData) == 1  # 指定了品种和头寸方,应该只有一条记录
    openVolume1 = responseData[0].OpenVolume
    closeVolume1 = responseData[0].CloseVolume
    print 'openVolume1=', openVolume1, ',closeVolume1=', closeVolume1
    assert openVolume0 == openVolume1
    assert closeVolume1 - closeVolume0 == 1

    print 'price0=', price0, 'price1=', price1
    print 'diff=', price1 - price0