[增强功能] 增加get_margin方法,支持单品种/自定义合约

This commit is contained in:
msincenselee 2020-12-11 15:56:57 +08:00
parent 349b7f813f
commit 01282333a7
3 changed files with 24 additions and 1 deletions

View File

@ -325,6 +325,13 @@ class BackTestingEngine(object):
def get_price(self, vt_symbol: str):
return self.price_dict.get(vt_symbol, None)
def get_margin(self, vt_symbol:str):
"""获取某合约的单位为1的保证金"""
cur_price = self.get_price(vt_symbol)
if cur_price is None:
return None
return cur_price * self.get_margin_rate(vt_symbol)
def set_commission_rate(self, vt_symbol: str, rate: float):
"""设置佣金比例"""
self.commission_rate.update({vt_symbol: rate})

View File

@ -811,6 +811,22 @@ class CtaEngine(BaseEngine):
return None
def get_margin(self, vt_symbol: str):
"""
按照当前价格计算1手合约需要得保证金
:param vt_symbol:
:return: 普通合约 => 当前价格 * margin_rate
"""
cur_price = self.get_price(vt_symbol)
cur_margin_rate = self.get_margin_rate(vt_symbol)
if cur_price and cur_margin_rate:
return abs(cur_price * cur_margin_rate)
else:
# 取不到价格取不到size或者取不到保证金比例
self.write_error(f'无法计算{vt_symbol}的保证金,价格:{cur_price}或margin_rate:{cur_margin_rate}')
return None
def get_contract(self, vt_symbol):
return self.main_engine.get_contract(vt_symbol)

View File

@ -1338,7 +1338,7 @@ class CtaFutureTemplate(CtaTemplate):
continue
if grid.open_status and not grid.order_status:
dn_grids_info += f'持多中: [数量:{grid.volume}\n, 开仓时间:{grid.open_time}]\n'
dn_grids_info += f'持多中: [数量:{grid.volume}, 开仓价:{grid.open_price},开仓时间:{grid.open_time}]\n'
continue
if not grid.open_status and grid.order_status: