From 01282333a7b815f2ecd17a7575d521ef609085b6 Mon Sep 17 00:00:00 2001 From: msincenselee Date: Fri, 11 Dec 2020 15:56:57 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=A2=9E=E5=BC=BA=E5=8A=9F=E8=83=BD]=20?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0get=5Fmargin=E6=96=B9=E6=B3=95=EF=BC=8C?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=8D=95=E5=93=81=E7=A7=8D/=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E5=90=88=E7=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/app/cta_crypto/back_testing.py | 7 +++++++ vnpy/app/cta_crypto/engine.py | 16 ++++++++++++++++ vnpy/app/cta_crypto/template.py | 2 +- 3 files changed, 24 insertions(+), 1 deletion(-) 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: