This repository has been archived by the owner on Aug 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtd_qry_instrument_commission_rate.py
117 lines (94 loc) · 3.86 KB
/
td_qry_instrument_commission_rate.py
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
"""
交易demo - 请求查询合约手续费率
"""
import inspect
from openctp_tts import tdapi
from config import (
td_fronts,
td_front,
user,
password,
broker_id,
authcode,
appid,
)
class CTdSpiImpl(tdapi.CThostFtdcTraderSpi):
""" 交易回调实现类 """
def __init__(self, _api: tdapi.CThostFtdcTraderApi):
super().__init__()
self._api = _api
def OnFrontConnected(self):
""" 前置连接成功 """
print("交易前置连接成功")
# 认证请求
req = tdapi.CThostFtdcReqAuthenticateField()
req.BrokerID = broker_id
req.UserID = user
req.AppID = appid
req.AuthCode = authcode
self._api.ReqAuthenticate(req, 0)
def OnFrontDisconnected(self, nReason: int):
""" 前置断开 """
print("交易前置连接断开: nReason=", nReason)
def OnRspAuthenticate(self, pRspAuthenticateField: tdapi.CThostFtdcRspAuthenticateField,
pRspInfo: tdapi.CThostFtdcRspInfoField, nRequestID: int, bIsLast: bool):
""" 客户端认证应答 """
if pRspInfo and pRspInfo.ErrorID:
print("认证失败: ErrorID=", pRspInfo.ErrorID, "ErrorMsg=", pRspInfo.ErrorMsg)
return
print("认证成功")
if pRspInfo is None or pRspInfo.ErrorID == 0:
# 登录请求
req = tdapi.CThostFtdcReqUserLoginField()
req.BrokerID = broker_id
req.UserID = user
req.Password = password
req.UserProductInfo = "openctp"
self._api.ReqUserLogin(req, 0)
def OnRspUserLogin(self, pRspUserLogin: tdapi.CThostFtdcRspUserLoginField,
pRspInfo: tdapi.CThostFtdcRspInfoField, nRequestID: int, bIsLast: bool):
""" 登录应答 """
if pRspInfo and pRspInfo.ErrorID:
print("登录失败: ErrorID=", pRspInfo.ErrorID, "ErrorMsg=", pRspInfo.ErrorMsg)
return
print("登录成功:", pRspUserLogin.UserID, "TradingDay=", pRspUserLogin.TradingDay)
# 请求查询合约手续费率, 若 合约不存在则无返回
req = tdapi.CThostFtdcQryInstrumentCommissionRateField()
req.BrokerID = broker_id
req.InvestorID = user
req.InstrumentID = 'IF2310' # 更新需要查询的合约ID
# req.ExchangeID = 'CFFEX'
self._api.ReqQryInstrumentCommissionRate(req, 0)
def OnRspQryInstrumentCommissionRate(self, pInstrumentCommissionRate: tdapi.CThostFtdcInstrumentCommissionRateField,
pRspInfo: tdapi.CThostFtdcRspInfoField, nRequestID: int, bIsLast: bool):
""" 请求查询合约手续费率响应 """
if pRspInfo and pRspInfo.ErrorID:
print("请求查询合约手续费率失败: ErrorID=", pRspInfo.ErrorID, "ErrorMsg=", pRspInfo.ErrorMsg)
return
detail = ''
for name, value in inspect.getmembers(pInstrumentCommissionRate):
if name[0].isupper():
detail = f'{detail} {name}={value}, '
print("请求查询合约手续费率成功:", detail)
if __name__ == '__main__':
# 实例化交易请求类
api = tdapi.CThostFtdcTraderApi.CreateFtdcTraderApi(user) # type: tdapi.CThostFtdcTraderApi
print("TTS交易API版本号:", api.GetApiVersion())
print("交易前置:", td_fronts[td_front], td_front)
# 实例化交易回调实现类
spi = CTdSpiImpl(api)
# 注册交易前置地址
api.RegisterFront(td_front)
# 交易请求实例 注册 交易回调实例
api.RegisterSpi(spi)
# 订阅私有流
api.SubscribePrivateTopic(tdapi.THOST_TERT_QUICK)
# 订阅公有流
api.SubscribePublicTopic(tdapi.THOST_TERT_QUICK)
# 初始化交易实例
api.Init()
# 阻塞 等待
print("Press Enter key to exit ...")
input()
# 释放实例
api.Release()