class LogMonitor(QtWidgets.QTableWidget): """""" signal = QtCore.pyqtSignal(Event) def __init__(self, event_engine: EventEngine): """""" super().__init__() self.event_engine = event_engine self.init_ui() self.register_event() def init_ui(self): """""" labels = ["时间", "信息"] self.setColumnCount(len(labels)) self.setHorizontalHeaderLabels(labels) self.verticalHeader().setVisible(False) self.setEditTriggers(self.NoEditTriggers) self.verticalHeader().setSectionResizeMode( QtWidgets.QHeaderView.ResizeToContents) self.horizontalHeader().setSectionResizeMode( 1, QtWidgets.QHeaderView.Stretch) self.setWordWrap(True) def register_event(self): """""" self.signal.connect(self.process_log_event) self.event_engine.register(EVENT_ALGO_LOG, self.signal.emit) def process_log_event(self, event): """""" msg = event.data timestamp = datetime.now().strftime("%H:%M:%S") timestamp_cell = QtWidgets.QTableWidgetItem(timestamp) msg_cell = QtWidgets.QTableWidgetItem(msg) self.insertRow(0) self.setItem(0, 0, timestamp_cell) self.setItem(0, 1, msg_cell)
def init_ui(self): """""" self.setWindowTitle("CTA回测") # Setting Part self.class_combo = QtWidgets.QComboBox() self.class_combo.addItems(self.class_names) self.symbol_line = QtWidgets.QLineEdit("IF88.CFFEX") self.interval_combo = QtWidgets.QComboBox() for inteval in Interval: self.interval_combo.addItem(inteval.value) end_dt = datetime.now() start_dt = end_dt - timedelta(days=3 * 365) self.start_date_edit = QtWidgets.QDateEdit( QtCore.QDate(start_dt.year, start_dt.month, start_dt.day)) self.end_date_edit = QtWidgets.QDateEdit(QtCore.QDate.currentDate()) self.rate_line = QtWidgets.QLineEdit("0.000025") self.slippage_line = QtWidgets.QLineEdit("0.2") self.size_line = QtWidgets.QLineEdit("300") self.pricetick_line = QtWidgets.QLineEdit("0.2") self.capital_line = QtWidgets.QLineEdit("1000000") backtesting_button = QtWidgets.QPushButton("开始回测") backtesting_button.clicked.connect(self.start_backtesting) optimization_button = QtWidgets.QPushButton("参数优化") optimization_button.clicked.connect(self.start_optimization) self.result_button = QtWidgets.QPushButton("优化结果") self.result_button.clicked.connect(self.show_optimization_result) self.result_button.setEnabled(False) downloading_button = QtWidgets.QPushButton("下载数据") downloading_button.clicked.connect(self.start_downloading) for button in [ backtesting_button, optimization_button, downloading_button, self.result_button ]: button.setFixedHeight(button.sizeHint().height() * 2) form = QtWidgets.QFormLayout() form.addRow("交易策略", self.class_combo) form.addRow("本地代码", self.symbol_line) form.addRow("K线周期", self.interval_combo) form.addRow("开始日期", self.start_date_edit) form.addRow("结束日期", self.end_date_edit) form.addRow("手续费率", self.rate_line) form.addRow("交易滑点", self.slippage_line) form.addRow("合约乘数", self.size_line) form.addRow("价格跳动", self.pricetick_line) form.addRow("回测资金", self.capital_line) form.addRow(backtesting_button) left_vbox = QtWidgets.QVBoxLayout() left_vbox.addLayout(form) left_vbox.addWidget(downloading_button) left_vbox.addStretch() left_vbox.addWidget(optimization_button) left_vbox.addWidget(self.result_button) # Result part self.statistics_monitor = StatisticsMonitor() self.log_monitor = QtWidgets.QTextEdit() self.log_monitor.setMaximumHeight(400) self.chart = BacktesterChart() self.chart.setMinimumWidth(1000) # Layout vbox = QtWidgets.QVBoxLayout() vbox.addWidget(self.statistics_monitor) vbox.addWidget(self.log_monitor) hbox = QtWidgets.QHBoxLayout() hbox.addLayout(left_vbox) hbox.addLayout(vbox) hbox.addWidget(self.chart) self.setLayout(hbox)
class BacktesterManager(QtWidgets.QWidget): """""" signal_log = QtCore.pyqtSignal(Event) signal_backtesting_finished = QtCore.pyqtSignal(Event) signal_optimization_finished = QtCore.pyqtSignal(Event) def __init__(self, main_engine: MainEngine, event_engine: EventEngine): """""" super().__init__() self.main_engine = main_engine self.event_engine = event_engine self.backtester_engine = main_engine.get_engine(APP_NAME) self.class_names = [] self.settings = {} self.target_display = "" self.init_strategy_settings() self.init_ui() self.register_event() self.backtester_engine.init_engine() def init_strategy_settings(self): """""" self.class_names = self.backtester_engine.get_strategy_class_names() for class_name in self.class_names: setting = self.backtester_engine.get_default_setting(class_name) self.settings[class_name] = setting def init_ui(self): """""" self.setWindowTitle("CTA回测") # Setting Part self.class_combo = QtWidgets.QComboBox() self.class_combo.addItems(self.class_names) self.symbol_line = QtWidgets.QLineEdit("IF88.CFFEX") self.interval_combo = QtWidgets.QComboBox() for inteval in Interval: self.interval_combo.addItem(inteval.value) end_dt = datetime.now() start_dt = end_dt - timedelta(days=3 * 365) self.start_date_edit = QtWidgets.QDateEdit( QtCore.QDate(start_dt.year, start_dt.month, start_dt.day)) self.end_date_edit = QtWidgets.QDateEdit(QtCore.QDate.currentDate()) self.rate_line = QtWidgets.QLineEdit("0.000025") self.slippage_line = QtWidgets.QLineEdit("0.2") self.size_line = QtWidgets.QLineEdit("300") self.pricetick_line = QtWidgets.QLineEdit("0.2") self.capital_line = QtWidgets.QLineEdit("1000000") backtesting_button = QtWidgets.QPushButton("开始回测") backtesting_button.clicked.connect(self.start_backtesting) optimization_button = QtWidgets.QPushButton("参数优化") optimization_button.clicked.connect(self.start_optimization) self.result_button = QtWidgets.QPushButton("优化结果") self.result_button.clicked.connect(self.show_optimization_result) self.result_button.setEnabled(False) downloading_button = QtWidgets.QPushButton("下载数据") downloading_button.clicked.connect(self.start_downloading) for button in [ backtesting_button, optimization_button, downloading_button, self.result_button ]: button.setFixedHeight(button.sizeHint().height() * 2) form = QtWidgets.QFormLayout() form.addRow("交易策略", self.class_combo) form.addRow("本地代码", self.symbol_line) form.addRow("K线周期", self.interval_combo) form.addRow("开始日期", self.start_date_edit) form.addRow("结束日期", self.end_date_edit) form.addRow("手续费率", self.rate_line) form.addRow("交易滑点", self.slippage_line) form.addRow("合约乘数", self.size_line) form.addRow("价格跳动", self.pricetick_line) form.addRow("回测资金", self.capital_line) form.addRow(backtesting_button) left_vbox = QtWidgets.QVBoxLayout() left_vbox.addLayout(form) left_vbox.addWidget(downloading_button) left_vbox.addStretch() left_vbox.addWidget(optimization_button) left_vbox.addWidget(self.result_button) # Result part self.statistics_monitor = StatisticsMonitor() self.log_monitor = QtWidgets.QTextEdit() self.log_monitor.setMaximumHeight(400) self.chart = BacktesterChart() self.chart.setMinimumWidth(1000) # Layout vbox = QtWidgets.QVBoxLayout() vbox.addWidget(self.statistics_monitor) vbox.addWidget(self.log_monitor) hbox = QtWidgets.QHBoxLayout() hbox.addLayout(left_vbox) hbox.addLayout(vbox) hbox.addWidget(self.chart) self.setLayout(hbox) def register_event(self): """""" self.signal_log.connect(self.process_log_event) self.signal_backtesting_finished.connect( self.process_backtesting_finished_event) self.signal_optimization_finished.connect( self.process_optimization_finished_event) self.event_engine.register(EVENT_BACKTESTER_LOG, self.signal_log.emit) self.event_engine.register(EVENT_BACKTESTER_BACKTESTING_FINISHED, self.signal_backtesting_finished.emit) self.event_engine.register(EVENT_BACKTESTER_OPTIMIZATION_FINISHED, self.signal_optimization_finished.emit) def process_log_event(self, event: Event): """""" msg = event.data self.write_log(msg) def write_log(self, msg): """""" timestamp = datetime.now().strftime("%H:%M:%S") msg = f"{timestamp}\t{msg}" self.log_monitor.append(msg) def process_backtesting_finished_event(self, event: Event): """""" statistics = self.backtester_engine.get_result_statistics() self.statistics_monitor.set_data(statistics) df = self.backtester_engine.get_result_df() self.chart.set_data(df) def process_optimization_finished_event(self, event: Event): """""" self.write_log("请点击[优化结果]按钮查看") self.result_button.setEnabled(True) def start_backtesting(self): """""" class_name = self.class_combo.currentText() vt_symbol = self.symbol_line.text() interval = self.interval_combo.currentText() start = self.start_date_edit.date().toPyDate() end = self.end_date_edit.date().toPyDate() rate = float(self.rate_line.text()) slippage = float(self.slippage_line.text()) size = float(self.size_line.text()) pricetick = float(self.pricetick_line.text()) capital = float(self.capital_line.text()) old_setting = self.settings[class_name] dialog = BacktestingSettingEditor(class_name, old_setting) i = dialog.exec() if i != dialog.Accepted: return new_setting = dialog.get_setting() self.settings[class_name] = new_setting result = self.backtester_engine.start_backtesting( class_name, vt_symbol, interval, start, end, rate, slippage, size, pricetick, capital, new_setting) if result: self.statistics_monitor.clear_data() self.chart.clear_data() def start_optimization(self): """""" class_name = self.class_combo.currentText() vt_symbol = self.symbol_line.text() interval = self.interval_combo.currentText() start = self.start_date_edit.date().toPyDate() end = self.end_date_edit.date().toPyDate() rate = float(self.rate_line.text()) slippage = float(self.slippage_line.text()) size = float(self.size_line.text()) pricetick = float(self.pricetick_line.text()) capital = float(self.capital_line.text()) parameters = self.settings[class_name] dialog = OptimizationSettingEditor(class_name, parameters) i = dialog.exec() if i != dialog.Accepted: return optimization_setting, use_ga = dialog.get_setting() self.target_display = dialog.target_display self.backtester_engine.start_optimization(class_name, vt_symbol, interval, start, end, rate, slippage, size, pricetick, capital, optimization_setting, use_ga) self.result_button.setEnabled(False) def start_downloading(self): """""" vt_symbol = self.symbol_line.text() interval = self.interval_combo.currentText() start = self.start_date_edit.date().toPyDate() end = self.end_date_edit.date().toPyDate() self.backtester_engine.start_downloading(vt_symbol, interval, start, end) def show_optimization_result(self): """""" result_values = self.backtester_engine.get_result_values() dialog = OptimizationResultMonitor(result_values, self.target_display) dialog.exec_() def show(self): """""" self.showMaximized()
class SettingMonitor(QtWidgets.QTableWidget): """""" setting_signal = QtCore.pyqtSignal(Event) use_signal = QtCore.pyqtSignal(dict) def __init__(self, algo_engine: AlgoEngine, event_engine: EventEngine): """""" super().__init__() self.algo_engine = algo_engine self.event_engine = event_engine self.settings = {} self.setting_cells = {} self.init_ui() self.register_event() def init_ui(self): """""" labels = ["", "", "名称", "配置"] self.setColumnCount(len(labels)) self.setHorizontalHeaderLabels(labels) self.verticalHeader().setVisible(False) self.setEditTriggers(self.NoEditTriggers) self.verticalHeader().setSectionResizeMode( QtWidgets.QHeaderView.ResizeToContents) self.horizontalHeader().setSectionResizeMode( 3, QtWidgets.QHeaderView.Stretch) self.setWordWrap(True) def register_event(self): """""" self.setting_signal.connect(self.process_setting_event) self.event_engine.register(EVENT_ALGO_SETTING, self.setting_signal.emit) def process_setting_event(self, event): """""" data = event.data setting_name = data["setting_name"] setting = data["setting"] cells = self.get_setting_cells(setting_name) if setting: self.settings[setting_name] = setting cells["setting"].setText(to_text(setting)) else: if setting_name in self.settings: self.settings.pop(setting_name) row = self.row(cells["setting"]) self.removeRow(row) self.setting_cells.pop(setting_name) def get_setting_cells(self, setting_name: str): """""" cells = self.setting_cells.get(setting_name, None) if not cells: use_func = partial(self.use_setting, setting_name=setting_name) use_button = QtWidgets.QPushButton("使用") use_button.clicked.connect(use_func) remove_func = partial(self.remove_setting, setting_name=setting_name) remove_button = QtWidgets.QPushButton("移除") remove_button.clicked.connect(remove_func) name_cell = QtWidgets.QTableWidgetItem(setting_name) setting_cell = QtWidgets.QTableWidgetItem() self.insertRow(0) self.setCellWidget(0, 0, use_button) self.setCellWidget(0, 1, remove_button) self.setItem(0, 2, name_cell) self.setItem(0, 3, setting_cell) cells = {"name": name_cell, "setting": setting_cell} self.setting_cells[setting_name] = cells return cells def use_setting(self, setting_name: str): """""" setting = self.settings[setting_name] setting["setting_name"] = setting_name self.use_signal.emit(setting) def remove_setting(self, setting_name: str): """""" self.algo_engine.remove_algo_setting(setting_name)
class AlgoMonitor(QtWidgets.QTableWidget): """""" parameters_signal = QtCore.pyqtSignal(Event) variables_signal = QtCore.pyqtSignal(Event) def __init__(self, algo_engine: AlgoEngine, event_engine: EventEngine, mode_active: bool): """""" super().__init__() self.algo_engine = algo_engine self.event_engine = event_engine self.mode_active = mode_active self.algo_cells = {} self.init_ui() self.register_event() def init_ui(self): """""" labels = ["", "算法", "参数", "状态"] self.setColumnCount(len(labels)) self.setHorizontalHeaderLabels(labels) self.verticalHeader().setVisible(False) self.setEditTriggers(self.NoEditTriggers) self.verticalHeader().setSectionResizeMode( QtWidgets.QHeaderView.ResizeToContents) for column in range(2, 4): self.horizontalHeader().setSectionResizeMode( column, QtWidgets.QHeaderView.Stretch) self.setWordWrap(True) if not self.mode_active: self.hideColumn(0) def register_event(self): """""" self.parameters_signal.connect(self.process_parameters_event) self.variables_signal.connect(self.process_variables_event) self.event_engine.register(EVENT_ALGO_PARAMETERS, self.parameters_signal.emit) self.event_engine.register(EVENT_ALGO_VARIABLES, self.variables_signal.emit) def process_parameters_event(self, event): """""" data = event.data algo_name = data["algo_name"] parameters = data["parameters"] cells = self.get_algo_cells(algo_name) text = to_text(parameters) cells["parameters"].setText(text) def process_variables_event(self, event): """""" data = event.data algo_name = data["algo_name"] variables = data["variables"] cells = self.get_algo_cells(algo_name) variables_cell = cells["variables"] text = to_text(variables) variables_cell.setText(text) row = self.row(variables_cell) active = variables["active"] if self.mode_active: if active: self.showRow(row) else: self.hideRow(row) else: if active: self.hideRow(row) else: self.showRow(row) def stop_algo(self, algo_name: str): """""" self.algo_engine.stop_algo(algo_name) def get_algo_cells(self, algo_name: str): """""" cells = self.algo_cells.get(algo_name, None) if not cells: stop_func = partial(self.stop_algo, algo_name=algo_name) stop_button = QtWidgets.QPushButton("停止") stop_button.clicked.connect(stop_func) name_cell = QtWidgets.QTableWidgetItem(algo_name) parameters_cell = QtWidgets.QTableWidgetItem() variables_cell = QtWidgets.QTableWidgetItem() self.insertRow(0) self.setCellWidget(0, 0, stop_button) self.setItem(0, 1, name_cell) self.setItem(0, 2, parameters_cell) self.setItem(0, 3, variables_cell) cells = { "name": name_cell, "parameters": parameters_cell, "variables": variables_cell } self.algo_cells[algo_name] = cells return cells
class CtaManager(QtWidgets.QWidget): """""" signal_log = QtCore.pyqtSignal(Event) signal_strategy = QtCore.pyqtSignal(Event) def __init__(self, main_engine: MainEngine, event_engine: EventEngine): super(CtaManager, self).__init__() self.main_engine = main_engine self.event_engine = event_engine self.cta_engine = main_engine.get_engine(APP_NAME) self.managers = {} self.init_ui() self.register_event() self.cta_engine.init_engine() self.update_class_combo() def init_ui(self): """""" self.setWindowTitle("CTA策略") # Create widgets self.class_combo = QtWidgets.QComboBox() add_button = QtWidgets.QPushButton("添加策略") add_button.clicked.connect(self.add_strategy) init_button = QtWidgets.QPushButton("全部初始化") init_button.clicked.connect(self.cta_engine.init_all_strategies) start_button = QtWidgets.QPushButton("全部启动") start_button.clicked.connect(self.cta_engine.start_all_strategies) stop_button = QtWidgets.QPushButton("全部停止") stop_button.clicked.connect(self.cta_engine.stop_all_strategies) self.scroll_layout = QtWidgets.QVBoxLayout() self.scroll_layout.addStretch() scroll_widget = QtWidgets.QWidget() scroll_widget.setLayout(self.scroll_layout) scroll_area = QtWidgets.QScrollArea() scroll_area.setWidgetResizable(True) scroll_area.setWidget(scroll_widget) self.log_monitor = LogMonitor(self.main_engine, self.event_engine) self.stop_order_monitor = StopOrderMonitor(self.main_engine, self.event_engine) # Set layout hbox1 = QtWidgets.QHBoxLayout() hbox1.addWidget(self.class_combo) hbox1.addWidget(add_button) hbox1.addStretch() hbox1.addWidget(init_button) hbox1.addWidget(start_button) hbox1.addWidget(stop_button) grid = QtWidgets.QGridLayout() grid.addWidget(scroll_area, 0, 0, 2, 1) grid.addWidget(self.stop_order_monitor, 0, 1) grid.addWidget(self.log_monitor, 1, 1) vbox = QtWidgets.QVBoxLayout() vbox.addLayout(hbox1) vbox.addLayout(grid) self.setLayout(vbox) def update_class_combo(self): """""" self.class_combo.addItems( self.cta_engine.get_all_strategy_class_names()) def register_event(self): """""" self.signal_strategy.connect(self.process_strategy_event) self.event_engine.register(EVENT_CTA_STRATEGY, self.signal_strategy.emit) def process_strategy_event(self, event): """ Update strategy status onto its monitor. """ data = event.data strategy_name = data["strategy_name"] if strategy_name in self.managers: manager = self.managers[strategy_name] manager.update_data(data) else: manager = StrategyManager(self, self.cta_engine, data) self.scroll_layout.insertWidget(0, manager) self.managers[strategy_name] = manager def remove_strategy(self, strategy_name): """""" manager = self.managers.pop(strategy_name) manager.deleteLater() def add_strategy(self): """""" class_name = str(self.class_combo.currentText()) if not class_name: return parameters = self.cta_engine.get_strategy_class_parameters(class_name) editor = SettingEditor(parameters, class_name=class_name) n = editor.exec_() if n == editor.Accepted: setting = editor.get_setting() vt_symbol = setting.pop("vt_symbol") strategy_name = setting.pop("strategy_name") self.cta_engine.add_strategy(class_name, strategy_name, vt_symbol, setting) def show(self): """""" self.showMaximized()
class RecorderManager(QtWidgets.QWidget): """""" signal_log = QtCore.pyqtSignal(Event) signal_update = QtCore.pyqtSignal(Event) signal_contract = QtCore.pyqtSignal(Event) def __init__(self, main_engine: MainEngine, event_engine: EventEngine): super().__init__() self.main_engine = main_engine self.event_engine = event_engine self.recorder_engine = main_engine.get_engine(APP_NAME) self.init_ui() self.register_event() self.recorder_engine.put_event() def init_ui(self): """""" self.setWindowTitle("行情记录") self.resize(1000, 600) # Create widgets self.symbol_line = QtWidgets.QLineEdit() self.symbol_line.setFixedHeight( self.symbol_line.sizeHint().height() * 2) contracts = self.main_engine.get_all_contracts() self.vt_symbols = [contract.vt_symbol for contract in contracts] self.symbol_completer = QtWidgets.QCompleter(self.vt_symbols) self.symbol_completer.setFilterMode(QtCore.Qt.MatchContains) self.symbol_completer.setCompletionMode( self.symbol_completer.PopupCompletion) self.symbol_line.setCompleter(self.symbol_completer) add_bar_button = QtWidgets.QPushButton("添加") add_bar_button.clicked.connect(self.add_bar_recording) remove_bar_button = QtWidgets.QPushButton("移除") remove_bar_button.clicked.connect(self.remove_bar_recording) add_tick_button = QtWidgets.QPushButton("添加") add_tick_button.clicked.connect(self.add_tick_recording) remove_tick_button = QtWidgets.QPushButton("移除") remove_tick_button.clicked.connect(self.remove_tick_recording) self.bar_recording_edit = QtWidgets.QTextEdit() self.bar_recording_edit.setReadOnly(True) self.tick_recording_edit = QtWidgets.QTextEdit() self.tick_recording_edit.setReadOnly(True) self.log_edit = QtWidgets.QTextEdit() self.log_edit.setReadOnly(True) # Set layout grid = QtWidgets.QGridLayout() grid.addWidget(QtWidgets.QLabel("K线记录"), 0, 0) grid.addWidget(add_bar_button, 0, 1) grid.addWidget(remove_bar_button, 0, 2) grid.addWidget(QtWidgets.QLabel("Tick记录"), 1, 0) grid.addWidget(add_tick_button, 1, 1) grid.addWidget(remove_tick_button, 1, 2) hbox = QtWidgets.QHBoxLayout() hbox.addWidget(QtWidgets.QLabel("本地代码")) hbox.addWidget(self.symbol_line) hbox.addWidget(QtWidgets.QLabel(" ")) hbox.addLayout(grid) hbox.addStretch() grid2 = QtWidgets.QGridLayout() grid2.addWidget(QtWidgets.QLabel("K线记录列表"), 0, 0) grid2.addWidget(QtWidgets.QLabel("Tick记录列表"), 0, 1) grid2.addWidget(self.bar_recording_edit, 1, 0) grid2.addWidget(self.tick_recording_edit, 1, 1) grid2.addWidget(self.log_edit, 2, 0, 1, 2) vbox = QtWidgets.QVBoxLayout() vbox.addLayout(hbox) vbox.addLayout(grid2) self.setLayout(vbox) def register_event(self): """""" self.signal_log.connect(self.process_log_event) self.signal_contract.connect(self.process_contract_event) self.signal_update.connect(self.process_update_event) self.event_engine.register(EVENT_CONTRACT, self.signal_contract.emit) self.event_engine.register( EVENT_RECORDER_LOG, self.signal_log.emit) self.event_engine.register( EVENT_RECORDER_UPDATE, self.signal_update.emit) def process_log_event(self, event: Event): """""" timestamp = datetime.now().strftime("%H:%M:%S") msg = f"{timestamp}\t{event.data}" self.log_edit.append(msg) def process_update_event(self, event: Event): """""" data = event.data self.bar_recording_edit.clear() bar_text = "\n".join(data["bar"]) self.bar_recording_edit.setText(bar_text) self.tick_recording_edit.clear() tick_text = "\n".join(data["tick"]) self.tick_recording_edit.setText(tick_text) def process_contract_event(self, event: Event): """""" contract = event.data self.vt_symbols.append(contract.vt_symbol) model = self.symbol_completer.model() model.setStringList(self.vt_symbols) def add_bar_recording(self): """""" vt_symbol = self.symbol_line.text() self.recorder_engine.add_bar_recording(vt_symbol) def add_tick_recording(self): """""" vt_symbol = self.symbol_line.text() self.recorder_engine.add_tick_recording(vt_symbol) def remove_bar_recording(self): """""" vt_symbol = self.symbol_line.text() self.recorder_engine.remove_bar_recording(vt_symbol) def remove_tick_recording(self): """""" vt_symbol = self.symbol_line.text() self.recorder_engine.remove_tick_recording(vt_symbol)