[bug fix] 手续费缺省万分之二

This commit is contained in:
msincenselee 2020-11-24 16:28:20 +08:00
parent 4ed16b48ac
commit 650f9f0dad
3 changed files with 30 additions and 12 deletions

View File

@ -407,7 +407,10 @@ class BackTestingEngine(object):
def get_commission_rate(self, vt_symbol: str): def get_commission_rate(self, vt_symbol: str):
""" 获取保证金比例,缺省万分之一""" """ 获取保证金比例,缺省万分之一"""
return self.commission_rate.get(vt_symbol, float(0.00001)) if vt_symbol not in self.commission_rate:
symbol, exchange = extract_vt_symbol(vt_symbol)
return self.commission_rate.get(symbol, float(0.0001))
return self.commission_rate.get(vt_symbol, float(0.0001))
def get_fix_commission(self, vt_symbol: str): def get_fix_commission(self, vt_symbol: str):
return self.fix_commission.get(vt_symbol, 0) return self.fix_commission.get(vt_symbol, 0)
@ -623,11 +626,12 @@ class BackTestingEngine(object):
self.set_margin_rate(symbol, margin_rate) self.set_margin_rate(symbol, margin_rate)
self.set_commission_rate(symbol, symbol_data.get('commission_rate', float(0.0001))) self.set_commission_rate(symbol, symbol_data.get('commission_rate', float(0.0001)))
exchange = symbol_data.get('exchange', 'LOCAL')
self.set_commission_rate(f'{symbol}.{exchange}', symbol_data.get('commission_rate', float(0.0001)))
self.set_contract( self.set_contract(
symbol=symbol, symbol=symbol,
name=symbol, name=symbol,
exchange=Exchange(symbol_data.get('exchange', 'LOCAL')), exchange=Exchange(exchange),
product=Product(symbol_data.get('product', "期货")), product=Product(symbol_data.get('product', "期货")),
size=symbol_data.get('symbol_size', 10), size=symbol_data.get('symbol_size', 10),
price_tick=symbol_data.get('price_tick', 1), price_tick=symbol_data.get('price_tick', 1),
@ -1874,6 +1878,7 @@ class BackTestingEngine(object):
# 不计算套利合约的持仓占用保证金 # 不计算套利合约的持仓占用保证金
if t.vt_symbol.endswith('SPD') or t.vt_symbol.endswith('SPD99'): if t.vt_symbol.endswith('SPD') or t.vt_symbol.endswith('SPD99'):
continue continue
cur_occupy_money = 0
# 当前空单保证金 # 当前空单保证金
if self.use_margin: if self.use_margin:
try: try:

View File

@ -1065,8 +1065,9 @@ class CtaEngine(BaseEngine):
except Exception: except Exception:
strategy.trading = False strategy.trading = False
strategy.inited = False strategy.inited = False
accountid = self.engine_config.get('accountid', 'XXX')
msg = f"触发异常已停止\n{traceback.format_exc()}" msg = f"{accountid}/{strategy.strategy_name}触发异常已停止\n{traceback.format_exc()}"
self.write_log(msg=msg, self.write_log(msg=msg,
strategy_name=strategy.strategy_name, strategy_name=strategy.strategy_name,
level=logging.CRITICAL) level=logging.CRITICAL)

View File

@ -899,6 +899,7 @@ class CtaProTemplate(CtaTemplate):
none_mi_grid = None none_mi_grid = None
none_mi_symbol = None none_mi_symbol = None
self.write_log(f'持仓换月=>启动.') self.write_log(f'持仓换月=>启动.')
# 找出非主力合约的持仓网格 # 找出非主力合约的持仓网格
for g in self.gt.get_opened_grids(direction=Direction.LONG): for g in self.gt.get_opened_grids(direction=Direction.LONG):
@ -1648,11 +1649,20 @@ class CtaProFutureTemplate(CtaProTemplate):
self.gt.save() self.gt.save()
# 普通限价单委托方式
else: else:
pre_status = old_order.get('status', Status.NOTTRADED) pre_status = old_order.get('status', Status.NOTTRADED)
old_order.update({'status': Status.CANCELLED}) old_order.update({'status': Status.CANCELLED})
self.write_log(u'委托单状态:{}=>{}'.format(pre_status, old_order.get('status'))) self.write_log(u'委托单状态:{}=>{}'.format(pre_status, old_order.get('status')))
if grid: if grid:
# 判断是否有部分交易
if order.traded > 0:
old_traded_volume = grid.traded_volume
grid.traded_volume += order.traded
self.write_log(f'{grid.direction.value}单部分{order.offset}仓,'
+ f'网格volume:{grid.volume}, traded_volume:{old_traded_volume}=>{grid.traded_volume}')
if order.vt_orderid in grid.order_ids: if order.vt_orderid in grid.order_ids:
grid.order_ids.remove(order.vt_orderid) grid.order_ids.remove(order.vt_orderid)
if len(grid.order_ids) == 0: if len(grid.order_ids) == 0:
@ -1709,14 +1719,16 @@ class CtaProFutureTemplate(CtaProTemplate):
self.active_orders.update({vt_orderid: order_info}) self.active_orders.update({vt_orderid: order_info})
ret = self.cancel_order(str(vt_orderid)) ret = self.cancel_order(str(vt_orderid))
if not ret: if not ret:
self.write_log(u'撤单逻辑 => 撤单失败,更新状态为撤单成功') self.write_error(f'{self.strategy_name}撤单逻辑 => {order_vt_symbol}撤单失败')
order_info.update({'status': Status.CANCELLED})
self.active_orders.update({vt_orderid: order_info}) #self.write_log(u'撤单逻辑 => 撤单失败,更新状态为撤单成功')
if order_grid: # order_info.update({'status': Status.CANCELLED})
if vt_orderid in order_grid.order_ids: # self.active_orders.update({vt_orderid: order_info})
order_grid.order_ids.remove(vt_orderid) # if order_grid:
if len(order_grid.order_ids) == 0: # if vt_orderid in order_grid.order_ids:
order_grid.order_status = False # order_grid.order_ids.remove(vt_orderid)
# if len(order_grid.order_ids) == 0:
# order_grid.order_status = False
continue continue
# 处理状态为‘撤销’的委托单 # 处理状态为‘撤销’的委托单