Administrator
2024-08-01 8e4bcab6c531d9f7411ae26c77ea63b8e0c5154c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import logging
import threading
import time
 
import qcvalueaddproapi
import sys
 
global g_userid, g_passwd, g_address, g_port, g_seqnum
g_seqnum = 100000
 
 
def new_seqnum():
    global g_seqnum
    g_seqnum = g_seqnum + 1
    return g_seqnum
 
 
class sampleSpi(qcvalueaddproapi.CQCValueAddProSpi):
    __result_cache = {}
    __temp_cache = {}
 
    def __init__(self, t_tapi):
        qcvalueaddproapi.CQCValueAddProSpi.__init__(self)
        self.m_api = t_tapi
 
    def __create_request_id(self):
        return new_seqnum()
 
    def queryTradeCalendar(self):
        try:
            queryField = qcvalueaddproapi.CQCVDReqQryShareCalendarField()
            queryField.BegDate = "20240701"
            queryField.EndDate = "20240801"
            queryField.PageCount = 100
            queryField.PageLocate = 1
            request_id = self.__create_request_id()
            results = self.m_api.ReqReqQryShareCalendar(queryField, request_id)
            for i in range(0, 1000):
                if request_id in self.__result_cache:
                    return self.__result_cache
                time.sleep(0.002)
 
            print("ReqReqQryShareCalendar:", results)
        except Exception as e:
            logging.exception(e)
 
    def OnFrontConnected(self):
        print("OnFrontConnected")
        # 连接上后去登录
        loginfield = qcvalueaddproapi.CQCVDReqUserLoginField()
        loginfield.LogInAccount = g_userid
        loginfield.AuthMode = qcvalueaddproapi.QCVD_AM_Password
        loginfield.Password = g_passwd
        self.m_api.ReqUserLogin(loginfield, new_seqnum())
 
    def OnFrontDisconnected(self, nReason):
        print("OnFrontDisconnected Reason[%d]"
              % (nReason))
 
    # 登录请求响应
    def OnRspUserLogin(self, pRspUserLoginField, pRspInfo, nRequestID, bIsLast):
        print("OnRspUserLogin LogInAccount[%s] RequestID[%d] ErrorID[%d] ErrorMsg[%s] "
              % (pRspUserLoginField.LogInAccount,
                 nRequestID,
                 pRspInfo.ErrorID,
                 pRspInfo.ErrorMsg))
        if (pRspInfo.ErrorID == 0):
            # 登录成功后直接查询
            # self.ReqInquiryHistoryDelivery()
            threading.Thread(target=lambda : print("交易日历:", self.queryTradeCalendar())).start()
 
    def ReqQryGGTEODPrices(self):
        QryField = qcvalueaddproapi.CQCVDQryGGTEODPricesField()
        self.m_api.ReqQryGGTEODPrices(QryField, new_seqnum())
 
    def ReqQryInvestor(self):
        QryField = qcvalueaddproapi.CQCVDQryInvestorField()
        self.m_api.ReqQryInvestor(QryField, new_seqnum())
 
    def OnRspInquiryShareCalendar(self, pShareCalendar, pRspInfo, nRequestID, bIsPageLast, bIsTotalLast):
        if nRequestID not in self.__temp_cache:
            self.__temp_cache[nRequestID] = []
 
        if pShareCalendar:
            self.__temp_cache[nRequestID].append(pShareCalendar.TradingDay)
        else:
            self.__result_cache[nRequestID] = self.__temp_cache[nRequestID]
            self.__temp_cache.pop(nRequestID)
            print("OnRspInquiryShareCalendar:", self.__result_cache[nRequestID])
 
 
def main():
    # if (len(sys.argv)< 5):
    #     ######运行命令行:
    #     ###### ip地址  端口号 用户名 密码
    #     print("usage: ipaddress port userid passwd")
    #     return
    global g_userid, g_passwd, g_address, g_port
    g_address = "101.230.90.99"
    g_port = 25556
    g_userid = "388000013942"
    g_passwd = "110808"
 
    # 回测交易是由历史行情来驱动撮合成交:
    # 因此必须同时使用traderapi和mdapi,不能单独使用traderapi,并且mdapi至少需要订阅一个以上行情。
    # 用户可使用回测traderapi的RegisterFront函数来注册此地址去连接上回测服务器
    print("GetApiVersion():", qcvalueaddproapi.CQCValueAddProApi_GetApiVersion())
    theapi = qcvalueaddproapi.CQCValueAddProApi_CreateInfoQryApi()
    thespi = sampleSpi(theapi)
    theapi.RegisterSpi(thespi)
    theapi.RegisterFront(g_address, g_port)
    theapi.Run()
    return
 
 
if __name__ == '__main__':
    main()