[增强功能] 回放K线,支持套利合约

This commit is contained in:
msincenselee 2020-09-15 14:24:06 +08:00
parent e7c7c209c1
commit fb8bd779b2
4 changed files with 173 additions and 68 deletions

View File

@ -429,30 +429,54 @@ class CtaSpreadTemplate(CtaTemplate):
dist_record['price'] = price dist_record['price'] = price
dist_record['symbol'] = self.vt_symbol dist_record['symbol'] = self.vt_symbol
dist_record['operation'] = operation dist_record['operation'] = operation
symbol, exchange = extract_vt_symbol(self.vt_symbol)
trade = TradeData(
symbol=symbol,
exchange=exchange,
volume=volume,
price=price,
datetime=self.cur_datetime,
orderid=self.cur_datetime.strftime('o_%Y%m%d%H%M%S%f'),
tradeid=self.cur_datetime.strftime('t_%Y%m%d%H%M%S%f'),
sys_orderid=self.cur_datetime.strftime('so_%Y%m%d%H%M%S%f'),
time=self.cur_datetime.strftime('%Y-%m-%d %H:%M:%S'),
strategy_name=self.strategy_name,
gateway_name='-'
)
if operation == 'buy': if operation == 'buy':
self.position.open_pos(Direction.LONG, volume=volume) self.position.open_pos(Direction.LONG, volume=volume)
dist_record['long_pos'] = self.position.long_pos dist_record['long_pos'] = self.position.long_pos
dist_record['short_pos'] = self.position.short_pos dist_record['short_pos'] = self.position.short_pos
trade.offset=Offset.OPEN
trade.direction=Direction.LONG
if operation == 'short': if operation == 'short':
self.position.open_pos(Direction.SHORT, volume=volume) self.position.open_pos(Direction.SHORT, volume=volume)
dist_record['long_pos'] = self.position.long_pos dist_record['long_pos'] = self.position.long_pos
dist_record['short_pos'] = self.position.short_pos dist_record['short_pos'] = self.position.short_pos
trade.offset = Offset.OPEN
trade.direction = Direction.SHORT
if operation == 'cover': if operation == 'cover':
self.position.close_pos(Direction.LONG, volume=volume) self.position.close_pos(Direction.LONG, volume=volume)
dist_record['long_pos'] = self.position.long_pos dist_record['long_pos'] = self.position.long_pos
dist_record['short_pos'] = self.position.short_pos dist_record['short_pos'] = self.position.short_pos
trade.offset = Offset.CLOSE
trade.direction = Direction.LONG
if operation == 'sell': if operation == 'sell':
self.position.close_pos(Direction.SHORT, volume=volume) self.position.close_pos(Direction.SHORT, volume=volume)
dist_record['long_pos'] = self.position.long_pos dist_record['long_pos'] = self.position.long_pos
dist_record['short_pos'] = self.position.short_pos dist_record['short_pos'] = self.position.short_pos
trade.offset = Offset.CLOSE
trade.direction = Direction.SHORT
self.save_dist(dist_record) self.save_dist(dist_record)
self.pos = self.position.pos self.pos = self.position.pos
# 回测时补充self.vt_symbol的交易记录
if self.backtesting:
self.cta_engine.append_trade(trade)
def save_dist(self, dist_data): def save_dist(self, dist_data):
""" """
保存策略逻辑过程记录= csv文件按 保存策略逻辑过程记录= csv文件按
@ -1126,11 +1150,12 @@ class CtaSpreadTemplate(CtaTemplate):
if len(self.active_orders) == 0: if len(self.active_orders) == 0:
self.entrust = 0 self.entrust = 0
def check_liquidity(self): def check_liquidity(self, direction=None, ask_volume=1, bid_volume=1):
"""实盘检查流动性缺失""" """实盘检查流动性缺失"""
if self.backtesting: if self.backtesting:
return True return True
# 检查流动性缺失
if not self.cur_act_tick.bid_price_1 <= self.cur_act_tick.last_price <= self.cur_act_tick.ask_price_1 \ if not self.cur_act_tick.bid_price_1 <= self.cur_act_tick.last_price <= self.cur_act_tick.ask_price_1 \
and self.cur_act_tick.volume > 0: and self.cur_act_tick.volume > 0:
self.write_log(u'流动性缺失导致leg1最新价{0} /V:{1}超出买1 {2}卖1 {3}范围,' self.write_log(u'流动性缺失导致leg1最新价{0} /V:{1}超出买1 {2}卖1 {3}范围,'
@ -1145,6 +1170,22 @@ class CtaSpreadTemplate(CtaTemplate):
self.cur_pas_tick.bid_price_1, self.cur_pas_tick.ask_price_1)) self.cur_pas_tick.bid_price_1, self.cur_pas_tick.ask_price_1))
return False return False
# 如果设置了方向和volume检查是否满足
if direction==Direction.LONG:
if self.cur_act_tick.ask_volume_1 < ask_volume:
self.write_log(f'主动腿的卖1委量:{self.cur_act_tick.ask_volume_1}不满足:{ask_volume}')
return False
if self.cur_pas_tick.bid_volume_1 < bid_volume:
self.write_log(f'被动腿的买1委量:{self.cur_pas_tick.bid_volume_1}不满足:{bid_volume}')
return False
elif direction == Direction.SHORT:
if self.cur_act_tick.bid_volume_1 < bid_volume:
self.write_log(f'主动腿的买1委量:{self.cur_act_tick.bid_volume_1}不满足:{bid_volume}')
return False
if self.cur_pas_tick.ask_volume_1 < ask_volume :
self.write_log(f'被动腿的卖1委量:{self.cur_pas_tick.ask_volume_1}不满足:{ask_volume}')
return False
return True return True
def check_near_up_nor_down(self): def check_near_up_nor_down(self):
@ -1199,7 +1240,10 @@ class CtaSpreadTemplate(CtaTemplate):
self.write_log(u'强制平仓日,不开仓') self.write_log(u'强制平仓日,不开仓')
return [] return []
# 检查流动性缺失 # 检查流动性缺失
if not self.check_liquidity() and not force: if not self.check_liquidity( direction=Direction.SHORT,
ask_volume=grid.volume * self.pas_vol_ratio,
bid_volume=grid.volume * self.act_vol_ratio
) and not force:
return [] return []
# 检查涨跌停距离 # 检查涨跌停距离
if self.check_near_up_nor_down(): if self.check_near_up_nor_down():
@ -1265,7 +1309,12 @@ class CtaSpreadTemplate(CtaTemplate):
self.write_log(u'强制平仓日,不开仓') self.write_log(u'强制平仓日,不开仓')
return [] return []
# 检查流动性缺失 # 检查流动性缺失
if not self.check_liquidity() and not force: if not self.check_liquidity(
direction=Direction.LONG,
ask_volume=grid.volume * self.act_vol_ratio,
bid_volume=grid.volume * self.pas_vol_ratio
) \
and not force:
return [] return []
# 检查涨跌停距离 # 检查涨跌停距离
if self.check_near_up_nor_down(): if self.check_near_up_nor_down():
@ -1319,7 +1368,11 @@ class CtaSpreadTemplate(CtaTemplate):
self.write_log(u'停止状态,不平仓') self.write_log(u'停止状态,不平仓')
return [] return []
# 检查流动性缺失 # 检查流动性缺失
if not self.check_liquidity() and not force: if not self.check_liquidity(
direction=Direction.SHORT,
ask_volume=grid.volume * self.pas_vol_ratio,
bid_volume=grid.volume * self.act_vol_ratio
) and not force:
return [] return []
# 检查涨跌停距离 # 检查涨跌停距离
if self.check_near_up_nor_down(): if self.check_near_up_nor_down():
@ -1393,7 +1446,11 @@ class CtaSpreadTemplate(CtaTemplate):
self.write_log(u'停止状态,不平仓') self.write_log(u'停止状态,不平仓')
return [] return []
# 检查流动性缺失 # 检查流动性缺失
if not self.check_liquidity() and not force: if not self.check_liquidity(
direction=Direction.LONG,
ask_volume=grid.volume * self.act_vol_ratio,
bid_volume=grid.volume * self.pas_vol_ratio
) and not force:
return [] return []
# 检查涨跌停距离 # 检查涨跌停距离
if self.check_near_up_nor_down(): if self.check_near_up_nor_down():

View File

@ -4,7 +4,7 @@
"mi_symbol": "a2101", "mi_symbol": "a2101",
"full_symbol": "A2101", "full_symbol": "A2101",
"exchange": "DCE", "exchange": "DCE",
"margin_rate": 0.1, "margin_rate": 0.09,
"symbol_size": 10, "symbol_size": 10,
"price_tick": 1.0, "price_tick": 1.0,
"open_interesting": 123, "open_interesting": 123,
@ -22,7 +22,7 @@
"mi_symbol": "ag2012", "mi_symbol": "ag2012",
"full_symbol": "AG2012", "full_symbol": "AG2012",
"exchange": "SHFE", "exchange": "SHFE",
"margin_rate": 0.1, "margin_rate": 0.12,
"symbol_size": 15, "symbol_size": 15,
"price_tick": 1.0, "price_tick": 1.0,
"open_interesting": 652, "open_interesting": 652,
@ -70,7 +70,7 @@
"mi_symbol": "AP010", "mi_symbol": "AP010",
"full_symbol": "AP2010", "full_symbol": "AP2010",
"exchange": "CZCE", "exchange": "CZCE",
"margin_rate": 0.1, "margin_rate": 0.08,
"symbol_size": 10, "symbol_size": 10,
"price_tick": 1.0, "price_tick": 1.0,
"open_interesting": 188275, "open_interesting": 188275,
@ -88,7 +88,7 @@
"mi_symbol": "au2012", "mi_symbol": "au2012",
"full_symbol": "AU2012", "full_symbol": "AU2012",
"exchange": "SHFE", "exchange": "SHFE",
"margin_rate": 0.1, "margin_rate": 0.08,
"symbol_size": 1000, "symbol_size": 1000,
"price_tick": 0.02, "price_tick": 0.02,
"open_interesting": 35, "open_interesting": 35,
@ -108,7 +108,7 @@
"mi_symbol": "b2010", "mi_symbol": "b2010",
"full_symbol": "B2010", "full_symbol": "B2010",
"exchange": "DCE", "exchange": "DCE",
"margin_rate": 0.1, "margin_rate": 0.08,
"symbol_size": 10, "symbol_size": 10,
"price_tick": 1.0, "price_tick": 1.0,
"open_interesting": 111, "open_interesting": 111,
@ -132,7 +132,7 @@
"mi_symbol": "bb2105", "mi_symbol": "bb2105",
"full_symbol": "BB2105", "full_symbol": "BB2105",
"exchange": "DCE", "exchange": "DCE",
"margin_rate": 0.1, "margin_rate": 0.2,
"symbol_size": 500, "symbol_size": 500,
"price_tick": 0.05, "price_tick": 0.05,
"open_interesting": 2, "open_interesting": 2,
@ -180,7 +180,7 @@
"mi_symbol": "c2101", "mi_symbol": "c2101",
"full_symbol": "C2101", "full_symbol": "C2101",
"exchange": "DCE", "exchange": "DCE",
"margin_rate": 0.1, "margin_rate": 0.07,
"symbol_size": 10, "symbol_size": 10,
"price_tick": 1.0, "price_tick": 1.0,
"open_interesting": 1337, "open_interesting": 1337,
@ -198,7 +198,7 @@
"mi_symbol": "CF101", "mi_symbol": "CF101",
"full_symbol": "CF2101", "full_symbol": "CF2101",
"exchange": "CZCE", "exchange": "CZCE",
"margin_rate": 0.1, "margin_rate": 0.07,
"symbol_size": 5, "symbol_size": 5,
"price_tick": 5.0, "price_tick": 5.0,
"open_interesting": 1431, "open_interesting": 1431,
@ -216,7 +216,7 @@
"mi_symbol": "CJ101", "mi_symbol": "CJ101",
"full_symbol": "CJ2101", "full_symbol": "CJ2101",
"exchange": "CZCE", "exchange": "CZCE",
"margin_rate": 0.1, "margin_rate": 0.07,
"symbol_size": 5, "symbol_size": 5,
"price_tick": 5.0, "price_tick": 5.0,
"open_interesting": 34982, "open_interesting": 34982,
@ -234,7 +234,7 @@
"mi_symbol": "cs2101", "mi_symbol": "cs2101",
"full_symbol": "CS2101", "full_symbol": "CS2101",
"exchange": "DCE", "exchange": "DCE",
"margin_rate": 0.1, "margin_rate": 0.07,
"symbol_size": 10, "symbol_size": 10,
"price_tick": 1.0, "price_tick": 1.0,
"open_interesting": 252, "open_interesting": 252,
@ -276,7 +276,7 @@
"mi_symbol": "CY101", "mi_symbol": "CY101",
"full_symbol": "CY2101", "full_symbol": "CY2101",
"exchange": "CZCE", "exchange": "CZCE",
"margin_rate": 0.1, "margin_rate": 0.07,
"symbol_size": 5, "symbol_size": 5,
"price_tick": 5.0, "price_tick": 5.0,
"open_interesting": 1, "open_interesting": 1,
@ -300,7 +300,7 @@
"mi_symbol": "eb2101", "mi_symbol": "eb2101",
"full_symbol": "EB2101", "full_symbol": "EB2101",
"exchange": "DCE", "exchange": "DCE",
"margin_rate": 0.1, "margin_rate": 0.12,
"symbol_size": 5, "symbol_size": 5,
"price_tick": 1.0, "price_tick": 1.0,
"open_interesting": 115, "open_interesting": 115,
@ -324,7 +324,7 @@
"mi_symbol": "eg2101", "mi_symbol": "eg2101",
"full_symbol": "EG2101", "full_symbol": "EG2101",
"exchange": "DCE", "exchange": "DCE",
"margin_rate": 0.1, "margin_rate": 0.11,
"symbol_size": 10, "symbol_size": 10,
"price_tick": 1.0, "price_tick": 1.0,
"open_interesting": 722, "open_interesting": 722,
@ -372,7 +372,7 @@
"mi_symbol": "FG104", "mi_symbol": "FG104",
"full_symbol": "FG2104", "full_symbol": "FG2104",
"exchange": "CZCE", "exchange": "CZCE",
"margin_rate": 0.1, "margin_rate": 0.06,
"symbol_size": 20, "symbol_size": 20,
"price_tick": 1.0, "price_tick": 1.0,
"open_interesting": 999, "open_interesting": 999,
@ -420,7 +420,7 @@
"mi_symbol": "hc2101", "mi_symbol": "hc2101",
"full_symbol": "HC2101", "full_symbol": "HC2101",
"exchange": "SHFE", "exchange": "SHFE",
"margin_rate": 0.1, "margin_rate": 0.08,
"symbol_size": 10, "symbol_size": 10,
"price_tick": 1.0, "price_tick": 1.0,
"open_interesting": 586, "open_interesting": 586,
@ -444,7 +444,7 @@
"mi_symbol": "i2101", "mi_symbol": "i2101",
"full_symbol": "I2101", "full_symbol": "I2101",
"exchange": "DCE", "exchange": "DCE",
"margin_rate": 0.1, "margin_rate": 0.11,
"symbol_size": 100, "symbol_size": 100,
"price_tick": 0.5, "price_tick": 0.5,
"open_interesting": 1361, "open_interesting": 1361,
@ -468,7 +468,7 @@
"mi_symbol": "IC2009", "mi_symbol": "IC2009",
"full_symbol": "IC2009", "full_symbol": "IC2009",
"exchange": "CFFEX", "exchange": "CFFEX",
"margin_rate": 0.1, "margin_rate": 0.12,
"symbol_size": 200, "symbol_size": 200,
"price_tick": 0.2, "price_tick": 0.2,
"open_interesting": 113728, "open_interesting": 113728,
@ -516,7 +516,7 @@
"mi_symbol": "j2101", "mi_symbol": "j2101",
"full_symbol": "J2101", "full_symbol": "J2101",
"exchange": "DCE", "exchange": "DCE",
"margin_rate": 0.1, "margin_rate": 0.09,
"symbol_size": 100, "symbol_size": 100,
"price_tick": 0.5, "price_tick": 0.5,
"open_interesting": 171367, "open_interesting": 171367,
@ -540,7 +540,7 @@
"mi_symbol": "jd2010", "mi_symbol": "jd2010",
"full_symbol": "JD2010", "full_symbol": "JD2010",
"exchange": "DCE", "exchange": "DCE",
"margin_rate": 0.1, "margin_rate": 0.07,
"symbol_size": 10, "symbol_size": 10,
"price_tick": 1.0, "price_tick": 1.0,
"open_interesting": 368055, "open_interesting": 368055,
@ -564,7 +564,7 @@
"mi_symbol": "jm2101", "mi_symbol": "jm2101",
"full_symbol": "JM2101", "full_symbol": "JM2101",
"exchange": "DCE", "exchange": "DCE",
"margin_rate": 0.1, "margin_rate": 0.09,
"symbol_size": 60, "symbol_size": 60,
"price_tick": 0.5, "price_tick": 0.5,
"open_interesting": 203, "open_interesting": 203,
@ -588,7 +588,7 @@
"mi_symbol": "JR101", "mi_symbol": "JR101",
"full_symbol": "JR2101", "full_symbol": "JR2101",
"exchange": "CZCE", "exchange": "CZCE",
"margin_rate": 0.1, "margin_rate": 0.06,
"symbol_size": 20, "symbol_size": 20,
"price_tick": 1.0, "price_tick": 1.0,
"open_interesting": 2, "open_interesting": 2,
@ -606,7 +606,7 @@
"mi_symbol": "l2101", "mi_symbol": "l2101",
"full_symbol": "L2101", "full_symbol": "L2101",
"exchange": "DCE", "exchange": "DCE",
"margin_rate": 0.1, "margin_rate": 0.11,
"symbol_size": 5, "symbol_size": 5,
"price_tick": 5.0, "price_tick": 5.0,
"open_interesting": 383, "open_interesting": 383,
@ -630,7 +630,7 @@
"mi_symbol": "LR011", "mi_symbol": "LR011",
"full_symbol": "LR2011", "full_symbol": "LR2011",
"exchange": "CZCE", "exchange": "CZCE",
"margin_rate": 0.1, "margin_rate": 0.06,
"symbol_size": 20, "symbol_size": 20,
"price_tick": 1.0, "price_tick": 1.0,
"open_interesting": 45, "open_interesting": 45,
@ -647,7 +647,7 @@
"mi_symbol": "m2101", "mi_symbol": "m2101",
"full_symbol": "M2101", "full_symbol": "M2101",
"exchange": "DCE", "exchange": "DCE",
"margin_rate": 0.1, "margin_rate": 0.08,
"symbol_size": 10, "symbol_size": 10,
"price_tick": 1.0, "price_tick": 1.0,
"open_interesting": 1145871, "open_interesting": 1145871,
@ -667,7 +667,7 @@
"mi_symbol": "MA105", "mi_symbol": "MA105",
"full_symbol": "MA2105", "full_symbol": "MA2105",
"exchange": "CZCE", "exchange": "CZCE",
"margin_rate": 0.1, "margin_rate": 0.07,
"symbol_size": 10, "symbol_size": 10,
"price_tick": 1.0, "price_tick": 1.0,
"open_interesting": 4000, "open_interesting": 4000,
@ -715,7 +715,7 @@
"mi_symbol": "nr2011", "mi_symbol": "nr2011",
"full_symbol": "NR2011", "full_symbol": "NR2011",
"exchange": "INE", "exchange": "INE",
"margin_rate": 0.1, "margin_rate": 0.08,
"symbol_size": 10, "symbol_size": 10,
"price_tick": 5.0, "price_tick": 5.0,
"open_interesting": 4, "open_interesting": 4,
@ -739,7 +739,7 @@
"mi_symbol": "OI101", "mi_symbol": "OI101",
"full_symbol": "OI2101", "full_symbol": "OI2101",
"exchange": "CZCE", "exchange": "CZCE",
"margin_rate": 0.1, "margin_rate": 0.06,
"symbol_size": 10, "symbol_size": 10,
"price_tick": 1.0, "price_tick": 1.0,
"open_interesting": 1417, "open_interesting": 1417,
@ -757,7 +757,7 @@
"mi_symbol": "p2101", "mi_symbol": "p2101",
"full_symbol": "P2101", "full_symbol": "P2101",
"exchange": "DCE", "exchange": "DCE",
"margin_rate": 0.1, "margin_rate": 0.09,
"symbol_size": 10, "symbol_size": 10,
"price_tick": 2.0, "price_tick": 2.0,
"open_interesting": 674, "open_interesting": 674,
@ -805,7 +805,7 @@
"mi_symbol": "PM103", "mi_symbol": "PM103",
"full_symbol": "PM2103", "full_symbol": "PM2103",
"exchange": "CZCE", "exchange": "CZCE",
"margin_rate": 0.1, "margin_rate": 0.06,
"symbol_size": 50, "symbol_size": 50,
"price_tick": 1.0, "price_tick": 1.0,
"open_interesting": 3, "open_interesting": 3,
@ -823,7 +823,7 @@
"mi_symbol": "pp2101", "mi_symbol": "pp2101",
"full_symbol": "PP2101", "full_symbol": "PP2101",
"exchange": "DCE", "exchange": "DCE",
"margin_rate": 0.1, "margin_rate": 0.11,
"symbol_size": 5, "symbol_size": 5,
"price_tick": 1.0, "price_tick": 1.0,
"open_interesting": 697, "open_interesting": 697,
@ -847,7 +847,7 @@
"mi_symbol": "rb2101", "mi_symbol": "rb2101",
"full_symbol": "RB2101", "full_symbol": "RB2101",
"exchange": "SHFE", "exchange": "SHFE",
"margin_rate": 0.1, "margin_rate": 0.08,
"symbol_size": 10, "symbol_size": 10,
"price_tick": 1.0, "price_tick": 1.0,
"open_interesting": 1190, "open_interesting": 1190,
@ -889,7 +889,7 @@
"mi_symbol": "RM101", "mi_symbol": "RM101",
"full_symbol": "RM2101", "full_symbol": "RM2101",
"exchange": "CZCE", "exchange": "CZCE",
"margin_rate": 0.1, "margin_rate": 0.06,
"symbol_size": 10, "symbol_size": 10,
"price_tick": 1.0, "price_tick": 1.0,
"open_interesting": 1318, "open_interesting": 1318,
@ -908,7 +908,7 @@
"mi_symbol": "rr2012", "mi_symbol": "rr2012",
"full_symbol": "RR2012", "full_symbol": "RR2012",
"exchange": "DCE", "exchange": "DCE",
"margin_rate": 0.1, "margin_rate": 0.06,
"symbol_size": 10, "symbol_size": 10,
"price_tick": 1.0, "price_tick": 1.0,
"open_interesting": 10, "open_interesting": 10,
@ -932,7 +932,7 @@
"mi_symbol": "RS011", "mi_symbol": "RS011",
"full_symbol": "RS2011", "full_symbol": "RS2011",
"exchange": "CZCE", "exchange": "CZCE",
"margin_rate": 0.1, "margin_rate": 0.2,
"symbol_size": 10, "symbol_size": 10,
"price_tick": 1.0, "price_tick": 1.0,
"open_interesting": 6, "open_interesting": 6,
@ -948,7 +948,7 @@
"mi_symbol": "ru2101", "mi_symbol": "ru2101",
"full_symbol": "RU2101", "full_symbol": "RU2101",
"exchange": "SHFE", "exchange": "SHFE",
"margin_rate": 0.1, "margin_rate": 0.08,
"symbol_size": 10, "symbol_size": 10,
"price_tick": 5.0, "price_tick": 5.0,
"open_interesting": 600, "open_interesting": 600,
@ -970,7 +970,7 @@
"mi_symbol": "SA101", "mi_symbol": "SA101",
"full_symbol": "SA2101", "full_symbol": "SA2101",
"exchange": "CZCE", "exchange": "CZCE",
"margin_rate": 0.1, "margin_rate": 0.06,
"symbol_size": 20, "symbol_size": 20,
"price_tick": 1.0, "price_tick": 1.0,
"open_interesting": 290637, "open_interesting": 290637,
@ -1026,7 +1026,7 @@
"mi_symbol": "SF010", "mi_symbol": "SF010",
"full_symbol": "SF2010", "full_symbol": "SF2010",
"exchange": "CZCE", "exchange": "CZCE",
"margin_rate": 0.1, "margin_rate": 0.07,
"symbol_size": 5, "symbol_size": 5,
"price_tick": 2.0, "price_tick": 2.0,
"open_interesting": 46583, "open_interesting": 46583,
@ -1050,7 +1050,7 @@
"mi_symbol": "SM101", "mi_symbol": "SM101",
"full_symbol": "SM2101", "full_symbol": "SM2101",
"exchange": "CZCE", "exchange": "CZCE",
"margin_rate": 0.1, "margin_rate": 0.07,
"symbol_size": 5, "symbol_size": 5,
"price_tick": 2.0, "price_tick": 2.0,
"open_interesting": 116292, "open_interesting": 116292,
@ -1098,7 +1098,7 @@
"mi_symbol": "sp2012", "mi_symbol": "sp2012",
"full_symbol": "SP2012", "full_symbol": "SP2012",
"exchange": "SHFE", "exchange": "SHFE",
"margin_rate": 0.1, "margin_rate": 0.07,
"symbol_size": 10, "symbol_size": 10,
"price_tick": 2.0, "price_tick": 2.0,
"open_interesting": 249, "open_interesting": 249,
@ -1122,7 +1122,7 @@
"mi_symbol": "SR107", "mi_symbol": "SR107",
"full_symbol": "SR2107", "full_symbol": "SR2107",
"exchange": "CZCE", "exchange": "CZCE",
"margin_rate": 0.1, "margin_rate": 0.07,
"symbol_size": 10, "symbol_size": 10,
"price_tick": 1.0, "price_tick": 1.0,
"open_interesting": 3059, "open_interesting": 3059,
@ -1140,7 +1140,7 @@
"mi_symbol": "ss2011", "mi_symbol": "ss2011",
"full_symbol": "SS2011", "full_symbol": "SS2011",
"exchange": "SHFE", "exchange": "SHFE",
"margin_rate": 0.1, "margin_rate": 0.08,
"symbol_size": 5, "symbol_size": 5,
"price_tick": 5.0, "price_tick": 5.0,
"open_interesting": 12, "open_interesting": 12,
@ -1164,7 +1164,7 @@
"mi_symbol": "T2012", "mi_symbol": "T2012",
"full_symbol": "T2012", "full_symbol": "T2012",
"exchange": "CFFEX", "exchange": "CFFEX",
"margin_rate": 0.1, "margin_rate": 0.02,
"symbol_size": 10000, "symbol_size": 10000,
"price_tick": 0.005, "price_tick": 0.005,
"open_interesting": 61523, "open_interesting": 61523,
@ -1179,7 +1179,7 @@
"mi_symbol": "TA102", "mi_symbol": "TA102",
"full_symbol": "TA2102", "full_symbol": "TA2102",
"exchange": "CZCE", "exchange": "CZCE",
"margin_rate": 0.1, "margin_rate": 0.06,
"symbol_size": 5, "symbol_size": 5,
"price_tick": 2.0, "price_tick": 2.0,
"open_interesting": 3082, "open_interesting": 3082,
@ -1203,7 +1203,7 @@
"mi_symbol": "TF2012", "mi_symbol": "TF2012",
"full_symbol": "TF2012", "full_symbol": "TF2012",
"exchange": "CFFEX", "exchange": "CFFEX",
"margin_rate": 0.1, "margin_rate": 0.012,
"symbol_size": 10000, "symbol_size": 10000,
"price_tick": 0.005, "price_tick": 0.005,
"open_interesting": 27699, "open_interesting": 27699,
@ -1218,7 +1218,7 @@
"mi_symbol": "TS2012", "mi_symbol": "TS2012",
"full_symbol": "TS2012", "full_symbol": "TS2012",
"exchange": "CFFEX", "exchange": "CFFEX",
"margin_rate": 0.1, "margin_rate": 0.005,
"symbol_size": 20000, "symbol_size": 20000,
"price_tick": 0.005, "price_tick": 0.005,
"open_interesting": 5922, "open_interesting": 5922,
@ -1233,7 +1233,7 @@
"mi_symbol": "UR101", "mi_symbol": "UR101",
"full_symbol": "UR2101", "full_symbol": "UR2101",
"exchange": "CZCE", "exchange": "CZCE",
"margin_rate": 0.1, "margin_rate": 0.05,
"symbol_size": 20, "symbol_size": 20,
"price_tick": 1.0, "price_tick": 1.0,
"open_interesting": 102002, "open_interesting": 102002,
@ -1257,7 +1257,7 @@
"mi_symbol": "v2101", "mi_symbol": "v2101",
"full_symbol": "V2101", "full_symbol": "V2101",
"exchange": "DCE", "exchange": "DCE",
"margin_rate": 0.1, "margin_rate": 0.09,
"symbol_size": 5, "symbol_size": 5,
"price_tick": 5.0, "price_tick": 5.0,
"open_interesting": 396, "open_interesting": 396,
@ -1281,7 +1281,7 @@
"mi_symbol": "WH101", "mi_symbol": "WH101",
"full_symbol": "WH2101", "full_symbol": "WH2101",
"exchange": "CZCE", "exchange": "CZCE",
"margin_rate": 0.1, "margin_rate": 0.07,
"symbol_size": 20, "symbol_size": 20,
"price_tick": 1.0, "price_tick": 1.0,
"open_interesting": 35, "open_interesting": 35,
@ -1299,7 +1299,7 @@
"mi_symbol": "wr2107", "mi_symbol": "wr2107",
"full_symbol": "WR2107", "full_symbol": "WR2107",
"exchange": "SHFE", "exchange": "SHFE",
"margin_rate": 0.1, "margin_rate": 0.08,
"symbol_size": 10, "symbol_size": 10,
"price_tick": 1.0, "price_tick": 1.0,
"open_interesting": 5, "open_interesting": 5,
@ -1323,7 +1323,7 @@
"mi_symbol": "y2101", "mi_symbol": "y2101",
"full_symbol": "Y2101", "full_symbol": "Y2101",
"exchange": "DCE", "exchange": "DCE",
"margin_rate": 0.1, "margin_rate": 0.08,
"symbol_size": 10, "symbol_size": 10,
"price_tick": 2.0, "price_tick": 2.0,
"open_interesting": 664, "open_interesting": 664,
@ -1343,7 +1343,7 @@
"mi_symbol": "ZC102", "mi_symbol": "ZC102",
"full_symbol": "ZC2102", "full_symbol": "ZC2102",
"exchange": "CZCE", "exchange": "CZCE",
"margin_rate": 0.1, "margin_rate": 0.05,
"symbol_size": 100, "symbol_size": 100,
"price_tick": 0.2, "price_tick": 0.2,
"open_interesting": 394, "open_interesting": 394,
@ -1392,7 +1392,7 @@
"mi_symbol": "pg2011", "mi_symbol": "pg2011",
"full_symbol": "PG2011", "full_symbol": "PG2011",
"exchange": "DCE", "exchange": "DCE",
"margin_rate": 0.1, "margin_rate": 0.11,
"symbol_size": 20, "symbol_size": 20,
"price_tick": 1.0, "price_tick": 1.0,
"open_interesting": 59, "open_interesting": 59,

View File

@ -1146,7 +1146,7 @@ class KLineWidget(KeyWraper):
self.pi_main.addItem(arrow) self.pi_main.addItem(arrow)
self.list_trade_arrow.append(arrow) self.list_trade_arrow.append(arrow)
def add_trades(self, df_trades): def add_trades(self, df_trades,include_symbols=[], exclude_symbols=[]):
""" """
批量导入交易记录vnpy回测中导出的trade.csv) 批量导入交易记录vnpy回测中导出的trade.csv)
:param df_trades: :param df_trades:
@ -1161,6 +1161,15 @@ class KLineWidget(KeyWraper):
else: else:
col_datetime = 'time' col_datetime = 'time'
for idx in df_trades.index: for idx in df_trades.index:
# 要显示的合约
symbol = df_trades['symbol'].loc[idx]
if len(include_symbols) > 0 and symbol not in include_symbols:
continue
if len(exclude_symbols) > 0 and symbol in exclude_symbols:
continue
# 时间 # 时间
trade_time = df_trades[col_datetime].loc[idx] trade_time = df_trades[col_datetime].loc[idx]
if not isinstance(trade_time, datetime) and isinstance(trade_time, str): if not isinstance(trade_time, datetime) and isinstance(trade_time, str):
@ -1499,6 +1508,43 @@ class KLineWidget(KeyWraper):
class GridKline(QtWidgets.QWidget): class GridKline(QtWidgets.QWidget):
"""多kline同时展示时间联动""" """多kline同时展示时间联动"""
# kline_setting说明
# dict 结构,{图:配置}。 配置也是dict结构配置项:配置值
# 配置项目1data_filek线的csv文件
# 包含时间高低开平volume等
# 包含主图/副图指标等数据
# 一次生成或者在策略中通过K线export生成
# 配置项目2main_indicators主图指标
# 指标变量必须在data_file文件中存在字段
# 配置项3sub_indicators 副图指标
# 指标变量必须在data_file文件中存在字段
# 配置项4trade_list_file开平仓交易记录
# 每条记录包含开仓,平仓,收益信息
# 回测时每个策略实例都产生trade_list.csv文件
# 配置项5:trade_file交易记录
# 每条记录,为单一开仓或平仓信息
# 实盘时引擎为每个策略实例产生trade.csv
# 回测时引擎生成一个trade.csv包含多个策略实例的交易
# 配置项6trade_include_symbols[symbol, symbol]
# 必须包含的显示交易信息的合约,例如套利合约时,只显示套利合约对,不显示主动腿、被动腿
# 配置项7trade_exclude_symbols[symbol, symbol]
# 排除显示交易信息的合约,例如套利合约时,只显示套利合约对,不显示主动腿、被动腿
# 配置项8: tns_file多空事务的csv文件
# 包含时间,多、空,价格
# 由策略生成,一般用于显示一段时间的趋势
# 配置项9: dist_file, 复杂事务的csv文件
# 记录自定义的逻辑记录
# 包含时间价格volume信号识别操作止损价止盈价当前多单仓位、空单仓位等
# 在策略模板的dist_fieldnames定义自行扩展
# 回放时支持include、exclude操作[operation]’这个字段的值进行过滤。
# 配置选项10dist_include_list 显示dist数据时对operation字段需要包含的内容
# 配置选项11dist_exclude_list 显示dist数据时对operation字段需要排除的内容
# 配置项12: bi_file / duan_file / bi_zs_file / duan_zs_file支持缠论的化线
def __init__(self, parent=None, kline_settings={}, title='', relocate=True): def __init__(self, parent=None, kline_settings={}, title='', relocate=True):
self.parent = parent self.parent = parent
@ -1622,7 +1668,12 @@ class GridKline(QtWidgets.QWidget):
if trade_file and os.path.exists(trade_file): if trade_file and os.path.exists(trade_file):
print(f'loading {trade_file}') print(f'loading {trade_file}')
df_trade = pd.read_csv(trade_file) df_trade = pd.read_csv(trade_file)
self.kline_dict[kline_name].add_trades(df_trade)
self.kline_dict[kline_name].add_trades(
df_trades=df_trade,
include_symbols=kline_setting.get('trade_include_symbols' ,[]),
exclude_symbols=kline_setting.get('trade_excclude_symbols', []))
# 加载tns( 回测、实盘产生的) # 加载tns( 回测、实盘产生的)
tns_file = kline_setting.get('tns_file', None) tns_file = kline_setting.get('tns_file', None)
@ -1640,7 +1691,7 @@ class GridKline(QtWidgets.QWidget):
df_markup.rename(columns={'operation': 'markup'}, inplace=True) df_markup.rename(columns={'operation': 'markup'}, inplace=True)
self.kline_dict[kline_name].add_markups(df_markup=df_markup, self.kline_dict[kline_name].add_markups(df_markup=df_markup,
include_list=kline_setting.get('dist_include_list', []), include_list=kline_setting.get('dist_include_list', []),
exclude_list=['buy', 'short', 'sell', 'cover']) exclude_list=kline_setting.get('dist_exclude_list',['buy', 'short', 'sell', 'cover']))
# 笔 # 笔
bi_file = kline_setting.get('bi_file', None) bi_file = kline_setting.get('bi_file', None)

View File

@ -118,11 +118,8 @@ def get_full_symbol(symbol: str):
symbol_month = symbol.replace(underlying_symbol, '') symbol_month = symbol.replace(underlying_symbol, '')
if len(symbol_month) == 3: if len(symbol_month) == 3:
if symbol_month[0] == '0':
# 支持2020年合约 # 支持2020年合约
return '{0}2{1}'.format(underlying_symbol, symbol_month) return '{0}2{1}'.format(underlying_symbol, symbol_month)
else:
return '{0}1{1}'.format(underlying_symbol, symbol_month)
else: else:
return symbol return symbol