From 01fed04246479cbe94e92f7bac45f780f5573cfc Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Fri, 18 Oct 2019 23:05:09 +0800 Subject: [PATCH] [Mod] add support for reverse contract in CtaBacktester --- vnpy/app/cta_backtester/engine.py | 12 ++++++++++-- vnpy/app/cta_backtester/ui/widget.py | 16 ++++++++++++++++ vnpy/app/cta_strategy/backtesting.py | 6 ++++-- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/vnpy/app/cta_backtester/engine.py b/vnpy/app/cta_backtester/engine.py index 5b677c9a..20663cad 100644 --- a/vnpy/app/cta_backtester/engine.py +++ b/vnpy/app/cta_backtester/engine.py @@ -133,6 +133,7 @@ class BacktesterEngine(BaseEngine): size: int, pricetick: float, capital: int, + inverse: bool, setting: dict ): """""" @@ -151,7 +152,8 @@ class BacktesterEngine(BaseEngine): slippage=slippage, size=size, pricetick=pricetick, - capital=capital + capital=capital, + inverse=inverse ) strategy_class = self.classes[class_name] @@ -184,6 +186,7 @@ class BacktesterEngine(BaseEngine): size: int, pricetick: float, capital: int, + inverse: bool, setting: dict ): if self.thread: @@ -204,6 +207,7 @@ class BacktesterEngine(BaseEngine): size, pricetick, capital, + inverse, setting ) ) @@ -240,6 +244,7 @@ class BacktesterEngine(BaseEngine): size: int, pricetick: float, capital: int, + inverse: bool, optimization_setting: OptimizationSetting, use_ga: bool ): @@ -263,7 +268,8 @@ class BacktesterEngine(BaseEngine): slippage=slippage, size=size, pricetick=pricetick, - capital=capital + capital=capital, + inverse=inverse ) strategy_class = self.classes[class_name] @@ -303,6 +309,7 @@ class BacktesterEngine(BaseEngine): size: int, pricetick: float, capital: int, + inverse: bool, optimization_setting: OptimizationSetting, use_ga: bool ): @@ -324,6 +331,7 @@ class BacktesterEngine(BaseEngine): size, pricetick, capital, + inverse, optimization_setting, use_ga ) diff --git a/vnpy/app/cta_backtester/ui/widget.py b/vnpy/app/cta_backtester/ui/widget.py index 6412a99a..352fde47 100644 --- a/vnpy/app/cta_backtester/ui/widget.py +++ b/vnpy/app/cta_backtester/ui/widget.py @@ -84,6 +84,9 @@ class BacktesterManager(QtWidgets.QWidget): self.pricetick_line = QtWidgets.QLineEdit("0.2") self.capital_line = QtWidgets.QLineEdit("1000000") + self.inverse_combo = QtWidgets.QComboBox() + self.inverse_combo.addItems(["正向", "反向"]) + backtesting_button = QtWidgets.QPushButton("开始回测") backtesting_button.clicked.connect(self.start_backtesting) @@ -136,6 +139,7 @@ class BacktesterManager(QtWidgets.QWidget): form.addRow("合约乘数", self.size_line) form.addRow("价格跳动", self.pricetick_line) form.addRow("回测资金", self.capital_line) + form.addRow("合约模式", self.inverse_combo) left_vbox = QtWidgets.QVBoxLayout() left_vbox.addLayout(form) @@ -248,6 +252,11 @@ class BacktesterManager(QtWidgets.QWidget): pricetick = float(self.pricetick_line.text()) capital = float(self.capital_line.text()) + if self.inverse_combo.currentText() == "正向": + inverse = False + else: + inverse = True + old_setting = self.settings[class_name] dialog = BacktestingSettingEditor(class_name, old_setting) i = dialog.exec() @@ -268,6 +277,7 @@ class BacktesterManager(QtWidgets.QWidget): size, pricetick, capital, + inverse, new_setting ) @@ -298,6 +308,11 @@ class BacktesterManager(QtWidgets.QWidget): pricetick = float(self.pricetick_line.text()) capital = float(self.capital_line.text()) + if self.inverse_combo.currentText() == "正向": + inverse = False + else: + inverse = True + parameters = self.settings[class_name] dialog = OptimizationSettingEditor(class_name, parameters) i = dialog.exec() @@ -318,6 +333,7 @@ class BacktesterManager(QtWidgets.QWidget): size, pricetick, capital, + inverse, optimization_setting, use_ga ) diff --git a/vnpy/app/cta_strategy/backtesting.py b/vnpy/app/cta_strategy/backtesting.py index 1116e3c9..52560fb2 100644 --- a/vnpy/app/cta_strategy/backtesting.py +++ b/vnpy/app/cta_strategy/backtesting.py @@ -1157,7 +1157,8 @@ def optimize( pricetick: float, capital: int, end: datetime, - mode: BacktestingMode + mode: BacktestingMode, + inverse: bool ): """ Function for running in multiprocessing.pool @@ -1174,7 +1175,8 @@ def optimize( pricetick=pricetick, capital=capital, end=end, - mode=mode + mode=mode, + inverse=inverse ) engine.add_strategy(strategy_class, setting)