diff --git a/vnpy/app/cta_crypto/back_testing.py b/vnpy/app/cta_crypto/back_testing.py index 8c0b4af1..bdb91af2 100644 --- a/vnpy/app/cta_crypto/back_testing.py +++ b/vnpy/app/cta_crypto/back_testing.py @@ -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}) diff --git a/vnpy/app/cta_crypto/engine.py b/vnpy/app/cta_crypto/engine.py index 24408e8d..11021057 100644 --- a/vnpy/app/cta_crypto/engine.py +++ b/vnpy/app/cta_crypto/engine.py @@ -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) diff --git a/vnpy/app/cta_crypto/template.py b/vnpy/app/cta_crypto/template.py index b2ac7a2f..b2ace400 100644 --- a/vnpy/app/cta_crypto/template.py +++ b/vnpy/app/cta_crypto/template.py @@ -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: