Skip to content

Commit 65ccba2

Browse files
committed
支持N档行情
1 parent d3ad3f3 commit 65ccba2

11 files changed

Lines changed: 571 additions & 95 deletions

Python/base/abstract_xapi.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,8 @@ def _on_response(self, response_type, p_api1, p_api2, double1, double2, ptr1, si
183183
elif self._callbacks:
184184
for callback in self._callbacks:
185185
if response_type == OnRtnDepthMarketData.value:
186-
obj = cast(ptr1, POINTER(DepthMarketDataField)).contents
187186
if self._is_market:
188-
callback.on_market_rtn_depth_market_data(p_api2, obj)
187+
callback.on_market_rtn_depth_market_data_n(p_api2, ptr1)
189188
elif response_type == OnRspQryInstrument.value:
190189
obj = cast(ptr1, POINTER(InstrumentField)).contents
191190
callback.on_trading_rsp_qry_instrument(p_api2, obj, bool(double1))

Python/base/callbacks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ def on_market_rsp_error(self, p_api, rsp_info, b_is_last):
3434
"""
3535
pass
3636

37-
def on_market_rtn_depth_market_data(self, p_api, depth_market_data):
37+
def on_market_rtn_depth_market_data_n(self, p_api, p_depth_market_data_n):
3838
"""
3939
callback on market server when receive market data
4040
:param p_api: c_void_p
41-
:param depth_market_data: DepthMarketDataField
41+
:param p_depth_market_data_n: DepthMarketDataNField
4242
:return:
4343
"""
4444
pass

Python/base/comm.py

Lines changed: 132 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@
156156
"""
157157
BarSizeType = c_long
158158

159+
SizeType = c_int
160+
159161
"""
160162
连接状态枚举
161163
"""
@@ -579,91 +581,63 @@ class RspUserLoginField(Structure):
579581
]
580582

581583

582-
# 深度行情
583-
class DepthMarketDataField(Structure):
584+
# 深度行情N档
585+
class DepthMarketDataNField(Structure):
584586
_fields_ = [
587+
# 占用总字节大小
588+
("Size", SizeType),
585589
# 交易所时间
590+
# 交易日,用于给数据接收器划分到同一文件使用,基本没啥别的用处
586591
("TradingDay", DateIntType),
587-
# 交易日,用于给数据接收器划分到同一文件使用,基本没啥别的用处
588592
("ActionDay", DateIntType),
589593
("UpdateTime", TimeIntType),
590594
("UpdateMillisec", TimeIntType),
591-
# 最新价
595+
# 交易所代码
596+
("Exchange", ExchangeType),
597+
#唯一符号
598+
("Symbol", SymbolType),
599+
#合约代码
600+
("InstrumentID", InstrumentIDType),
601+
#最新价
592602
("LastPrice", PriceType),
593-
# 数量
603+
#数量
594604
("Volume", LargeVolumeType),
595-
# 成交金额
605+
#成交金额
596606
("Turnover", MoneyType),
597-
# 持仓量
607+
#持仓量
598608
("OpenInterest", LargeVolumeType),
599-
# 当日均价
609+
#当日均价
600610
("AveragePrice", PriceType),
601-
# 今开盘
611+
#今开盘
602612
("OpenPrice", PriceType),
603-
# 最高价
613+
#最高价
604614
("HighestPrice", PriceType),
605-
# 最低价
615+
#最低价
606616
("LowestPrice", PriceType),
607-
# 今收盘
617+
#今收盘
608618
("ClosePrice", PriceType),
609-
# 本次结算价
619+
#本次结算价
610620
("SettlementPrice", PriceType),
611-
# 涨停板价
621+
#涨停板价
612622
("UpperLimitPrice", PriceType),
613-
# 跌停板价
623+
#跌停板价
614624
("LowerLimitPrice", PriceType),
615-
# 昨收盘
625+
#昨收盘
616626
("PreClosePrice", PriceType),
617-
# 上次结算价
627+
#上次结算价
618628
("PreSettlementPrice", PriceType),
619-
# 昨持仓量
629+
#昨持仓量
620630
("PreOpenInterest", LargeVolumeType),
621-
# 申买价一
622-
("BidPrice1", PriceType),
623-
# 申买量一
624-
("BidVolume1", VolumeType),
625-
# 申卖价一
626-
("AskPrice1", PriceType),
627-
# 申卖量一
628-
("AskVolume1", VolumeType),
629-
# 申买价二
630-
("BidPrice2", PriceType),
631-
# 申买量二
632-
("BidVolume2", VolumeType),
633-
# 申卖价二
634-
("AskPrice2", PriceType),
635-
# 申卖量二
636-
("AskVolume2", VolumeType),
637-
# 申买价三
638-
("BidPrice3", PriceType),
639-
# 申买量三
640-
("BidVolume3", VolumeType),
641-
# 申卖价三
642-
("AskPrice3", PriceType),
643-
# 申卖量三
644-
("AskVolume3", VolumeType),
645-
# 申买价四
646-
("BidPrice4", PriceType),
647-
# 申买量四
648-
("BidVolume4", VolumeType),
649-
# 申卖价四
650-
("AskPrice4", PriceType),
651-
# 申卖量四
652-
("AskVolume4", VolumeType),
653-
# 申买价五
654-
("BidPrice5", PriceType),
655-
# 申买量五
656-
("BidVolume5", VolumeType),
657-
# 申卖价五
658-
("AskPrice5", PriceType),
659-
# 申卖量五
660-
("AskVolume5", VolumeType),
661-
# 交易所代码
662-
("Exchange", ExchangeType),
663-
# 唯一符号
664-
("Symbol", SymbolType),
665-
# 合约代码
666-
("InstrumentID", InstrumentIDType),
631+
#买档个数
632+
("BidCount", SizeType)
633+
]
634+
635+
636+
class DepthField(Structure):
637+
_fields_ = [
638+
("Price", PriceType),
639+
("Size", VolumeType),
640+
("Count", VolumeType)
667641
]
668642

669643

@@ -749,6 +723,8 @@ class InstrumentField(Structure):
749723
("InstrumentID", InstrumentIDType),
750724
# 交易所代码
751725
("ExchangeID", ExchangeIDType),
726+
# 产品代码
727+
("ProductID", InstrumentIDType),
752728
# 合约名称
753729
("InstrumentName", InstrumentNameType),
754730
# 基础商品代码
@@ -940,4 +916,95 @@ class HistoricalDataRequestField(Structure):
940916

941917
# function
942918
fnOnRespone = WINFUNCTYPE(None, c_char, c_void_p, c_void_p, c_double, c_double, c_void_p, c_int, c_void_p, c_int,
943-
c_void_p, c_int)
919+
c_void_p, c_int)
920+
921+
922+
def get_depth_market_data(p_market_data):
923+
"""
924+
指针转换为行情数据
925+
:param p_market_data:
926+
:return:
927+
"""
928+
market_data = cast(p_market_data, POINTER(DepthMarketDataNField)).contents
929+
return market_data
930+
931+
932+
def get_bid_count(p_market_data):
933+
"""
934+
Bid Count
935+
:param p_market_data:
936+
:return:
937+
"""
938+
market_data = get_depth_market_data(p_market_data)
939+
return market_data.BidCount
940+
941+
942+
def get_ask_count(p_market_data):
943+
"""
944+
Ask Count
945+
:param p_market_data:
946+
:return:
947+
"""
948+
market_data = get_depth_market_data(p_market_data)
949+
size = market_data.Size
950+
bid_count = market_data.BidCount
951+
count = (size - sizeof(DepthMarketDataNField)) / sizeof(DepthField)
952+
return count - bid_count
953+
954+
955+
def get_bid(p_market_data, pos):
956+
"""
957+
获取买档,pos=1表示买1
958+
:param p_market_data:
959+
:param pos:
960+
:return:
961+
"""
962+
bid_count = get_bid_count(p_market_data)
963+
if pos <= 0 or pos > bid_count:
964+
return None
965+
p = p_market_data + sizeof(DepthMarketDataNField) + sizeof(DepthField) * (pos - 1)
966+
return cast(p, POINTER(DepthField)).contents
967+
968+
969+
def get_ask(p_market_data, pos):
970+
"""
971+
获取卖档,pos=1表示卖1
972+
:param p_market_data:
973+
:param pos:
974+
:return:
975+
"""
976+
ask_count = get_ask_count(p_market_data)
977+
if pos <= 0 or pos > ask_count:
978+
return None
979+
p = p_market_data + sizeof(DepthMarketDataNField) + sizeof(DepthField) * (
980+
get_bid_count(p_market_data) + pos - 1)
981+
return cast(p, POINTER(DepthField)).contents
982+
983+
984+
def get_all_bids(p_market_data):
985+
"""
986+
获取所有买档数据
987+
:param p_market_data:
988+
:return:
989+
"""
990+
total = get_bid_count(p_market_data)
991+
bids = []
992+
for i in range(0, total):
993+
p = p_market_data + sizeof(DepthMarketDataNField) + sizeof(DepthField) * i
994+
bids.append(cast(p, POINTER(DepthField)).contents)
995+
return bids
996+
997+
998+
def get_all_asks(p_market_data):
999+
"""
1000+
获取所有卖档数据
1001+
:param p_market_data:
1002+
:return:
1003+
"""
1004+
bid_count = get_bid_count(p_market_data)
1005+
total = get_ask_count(p_market_data) + bid_count
1006+
asks = []
1007+
for i in range(bid_count, total):
1008+
p = p_market_data + sizeof(DepthMarketDataNField) + sizeof(DepthField) * i
1009+
asks.append(cast(p, POINTER(DepthField)).contents)
1010+
return asks

Python/include/LTS/SecurityFtdcTraderApi.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,24 @@ class CSecurityFtdcTraderSpi
102102
///请求查询SF合约响应
103103
virtual void OnRspQrySFInstrument(CSecurityFtdcSFInstrumentField *pSFInstrument, CSecurityFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
104104

105+
///请求查询合约单手保证金响应
106+
virtual void OnRspQryInstrumentUnitMargin(CSecurityFtdcInstrumentUnitMarginField *pInstrumentUnitMargin, CSecurityFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
107+
108+
///请求查询预交割信息响应
109+
virtual void OnRspQryPreDelivInfo(CSecurityFtdcPreDelivInfoField *pPreDelivInfo, CSecurityFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
110+
111+
///请求查询可融券分配信息响应
112+
virtual void OnRspQryCreditStockAssignInfo(CSecurityFtdcCreditStockAssignInfoField *pCreditStockAssignInfo, CSecurityFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
113+
114+
///请求查询可融资分配信息响应
115+
virtual void OnRspQryCreditCashAssignInfo(CSecurityFtdcCreditCashAssignInfoField *pCreditCashAssignInfo, CSecurityFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
116+
117+
///请求查询证券折算率响应
118+
virtual void OnRspQryConversionRate(CSecurityFtdcConversionRateField *pConversionRate, CSecurityFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
119+
120+
///请求查询历史信用负债信息响应
121+
virtual void OnRspQryHisCreditDebtInfo(CSecurityFtdcHisCreditDebtInfoField *pHisCreditDebtInfo, CSecurityFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
122+
105123
///请求查询报单响应
106124
virtual void OnRspQryOrder(CSecurityFtdcOrderField *pOrder, CSecurityFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
107125

@@ -266,6 +284,24 @@ class TRADER_API_EXPORT CSecurityFtdcTraderApi
266284
///请求查询SF合约
267285
virtual int ReqQrySFInstrument(CSecurityFtdcQrySFInstrumentField *pQrySFInstrument, int nRequestID) = 0;
268286

287+
///请求查询合约单手保证金
288+
virtual int ReqQryInstrumentUnitMargin(CSecurityFtdcQryInstrumentUnitMarginField *pQryInstrumentUnitMargin, int nRequestID) = 0;
289+
290+
///请求查询预交割信息
291+
virtual int ReqQryPreDelivInfo(CSecurityFtdcQryPreDelivInfoField *pQryPreDelivInfo, int nRequestID) = 0;
292+
293+
///请求查询可融券分配信息
294+
virtual int ReqQryCreditStockAssignInfo(CSecurityFtdcQryCreditStockAssignInfoField *pQryCreditStockAssignInfo, int nRequestID) = 0;
295+
296+
///请求查询可融资分配信息
297+
virtual int ReqQryCreditCashAssignInfo(CSecurityFtdcQryCreditCashAssignInfoField *pQryCreditCashAssignInfo, int nRequestID) = 0;
298+
299+
///请求查询证券折算率
300+
virtual int ReqQryConversionRate(CSecurityFtdcQryConversionRateField *pQryConversionRate, int nRequestID) = 0;
301+
302+
///请求查询历史信用负债信息
303+
virtual int ReqQryHisCreditDebtInfo(CSecurityFtdcQryHisCreditDebtInfoField *pQryHisCreditDebtInfo, int nRequestID) = 0;
304+
269305
///请求查询报单
270306
virtual int ReqQryOrder(CSecurityFtdcQryOrderField *pQryOrder, int nRequestID) = 0;
271307

0 commit comments

Comments
 (0)