Ejemplo n.º 1
0
def test_SettlementInfoConfirm():
    '''
    测试结算确认
    '''
    traderChannel = TraderChannel(frontAddress, brokerID, userID, password)
    requestData = CThostFtdcSettlementInfoConfirmField()
    requestData.BrokerID = brokerID
    requestData.InvestorID = userID
    requestData.ConfirmDate = ''
    requestData.ConfirmTime = ''
    result = traderChannel.SettlementInfoConfirm(requestData)
    assert result[0] == 0
    responseData = result[2][0]
    assert responseData.BrokerID == brokerID
    assert responseData.InvestorID == userID
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