Skip to content

Commit 07cf3ba

Browse files
committed
api,backtest,objs: 更改从合约文件中获取的数据字段。
增加backtest模块的trading_time、expire_datetime、delivery_month、delivery_year、ins_class字段;增加api中的ins_class字段;删除chang、change_percent字段。 close: #208
1 parent 4662dde commit 07cf3ba

File tree

3 files changed

+15
-16
lines changed

3 files changed

+15
-16
lines changed

tqsdk/api.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def __init__(self, account=None, url=None, backtest=None, debug=None, loop=None,
124124
if not self._logger.handlers:
125125
sh = logging.StreamHandler()
126126
sh.setLevel(logging.INFO)
127-
if backtest: # 如果回测, 则去除将第一个本地时间
127+
if backtest: # 如果回测, 则去除第一个本地时间
128128
log_format = logging.Formatter('%(levelname)s - %(message)s')
129129
else:
130130
log_format = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
@@ -1133,8 +1133,9 @@ def _fetch_symbol_info(self, url):
11331133
rsp.raise_for_status()
11341134
return {
11351135
k: {
1136-
"margin": v.get("margin"),
1137-
"commission": v.get("commission"),
1136+
"ins_class": v.get("class", ""),
1137+
"margin": v.get("margin"), # 用于内部实现模拟交易, 不作为 api 对外可用数据(即 Quote 类中无此字段)
1138+
"commission": v.get("commission"), # 用于内部实现模拟交易, 不作为 api 对外可用数据(即 Quote 类中无此字段)
11381139
"price_tick": v["price_tick"],
11391140
"price_decs": v["price_decs"],
11401141
"volume_multiple": v["volume_multiple"],
@@ -1144,8 +1145,6 @@ def _fetch_symbol_info(self, url):
11441145
"min_market_order_volume": v.get("min_market_order_volume", 0),
11451146
"underlying_symbol": v.get("underlying_symbol", ""),
11461147
"strike_price": v.get("strike_price", float("nan")),
1147-
"change": None,
1148-
"change_percent": None,
11491148
"expired": v["expired"],
11501149
"trading_time": v.get("trading_time"),
11511150
"expire_datetime": v.get("expire_datetime"),

tqsdk/backtest.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,9 @@ async def _send_snapshot(self):
149149
"pre_open_interest": None,
150150
"pre_settlement": None,
151151
"pre_close": None,
152-
"margin": quote.get("margin"),
153-
"commission": quote.get("commission"),
152+
"ins_class": quote.get("ins_class", ""),
153+
"margin": quote.get("margin"), # 用于内部实现模拟交易, 不作为api对外可用数据(即 Quote 类中无此字段)
154+
"commission": quote.get("commission"), # 用于内部实现模拟交易, 不作为api对外可用数据(即 Quote 类中无此字段)
154155
"price_tick": quote["price_tick"],
155156
"price_decs": quote["price_decs"],
156157
"volume_multiple": quote["volume_multiple"],
@@ -160,10 +161,11 @@ async def _send_snapshot(self):
160161
"min_market_order_volume": quote["min_market_order_volume"],
161162
"underlying_symbol": quote["underlying_symbol"],
162163
"strike_price": quote["strike_price"],
163-
"change": None,
164-
# todo: 回测添加合约文件字段,trading_time等
165-
"change_percent": None,
166164
"expired": None,
165+
"trading_time": quote.get("trading_time"),
166+
"expire_datetime": quote.get("expire_datetime"),
167+
"delivery_month": quote.get("delivery_month"),
168+
"delivery_year": quote.get("delivery_year"),
167169
}
168170
self.diffs.append({
169171
"quotes": quotes,
@@ -192,7 +194,7 @@ async def _send_diff(self):
192194
if quotes_diff and (quote_info["min_duration"] != 0 or min_serial[1] == 0):
193195
quotes[min_serial[0]] = quotes_diff
194196
await self._fetch_serial(min_serial)
195-
if not self.serials:
197+
if not self.serials: # 当无可发送数据时则抛出BacktestFinished例外,包括未订阅任何行情 或 所有已订阅行情的最后一笔行情获取完成
196198
self.logger.warning("回测结束")
197199
raise BacktestFinished() from None
198200
for ins, diff in quotes.items():
@@ -243,7 +245,7 @@ async def _gen_serial(self, ins, dur):
243245
"chart_id": TqApi._generate_chart_id("backtest"),
244246
"ins_list": ins,
245247
"duration": dur,
246-
"view_width": 8964,
248+
"view_width": 8964, # 设为8964原因:可满足用户所有的订阅长度,并在backtest中将所有的 相同合约及周期 的K线用同一个serial存储
247249
"focus_datetime": int(self.current_dt),
248250
"focus_position": 8964,
249251
}

tqsdk/objs.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,8 @@ def __init__(self, api):
101101
self.underlying_symbol = ""
102102
#: 行权价
103103
self.strike_price = float("nan")
104-
#: 涨跌
105-
self.change = float("nan")
106-
#: 涨跌幅
107-
self.change_percent = float("nan")
104+
#: 合约类型
105+
self.ins_class = ""
108106
#: 合约是否已下市
109107
self.expired = False
110108
#: 交易时间段

0 commit comments

Comments
 (0)