[增强功能] 增加get_margin方法,支持单品种/自定义合约
This commit is contained in:
parent
349b7f813f
commit
01282333a7
@ -325,6 +325,13 @@ class BackTestingEngine(object):
|
|||||||
def get_price(self, vt_symbol: str):
|
def get_price(self, vt_symbol: str):
|
||||||
return self.price_dict.get(vt_symbol, None)
|
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):
|
def set_commission_rate(self, vt_symbol: str, rate: float):
|
||||||
"""设置佣金比例"""
|
"""设置佣金比例"""
|
||||||
self.commission_rate.update({vt_symbol: rate})
|
self.commission_rate.update({vt_symbol: rate})
|
||||||
|
@ -811,6 +811,22 @@ class CtaEngine(BaseEngine):
|
|||||||
|
|
||||||
return None
|
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):
|
def get_contract(self, vt_symbol):
|
||||||
return self.main_engine.get_contract(vt_symbol)
|
return self.main_engine.get_contract(vt_symbol)
|
||||||
|
|
||||||
|
@ -1338,7 +1338,7 @@ class CtaFutureTemplate(CtaTemplate):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
if grid.open_status and not grid.order_status:
|
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
|
continue
|
||||||
|
|
||||||
if not grid.open_status and grid.order_status:
|
if not grid.open_status and grid.order_status:
|
||||||
|
Loading…
Reference in New Issue
Block a user