def init_ui(self): """""" form = QtWidgets.QFormLayout() # Add vt_symbols and name edit if add new strategy if self.class_name: self.setWindowTitle(f"添加策略:{self.class_name}") button_text = "添加" parameters = {"strategy_name": "", "vt_symbols": ""} parameters.update(self.parameters) else: self.setWindowTitle(f"参数编辑:{self.strategy_name}") button_text = "确定" parameters = self.parameters for name, value in parameters.items(): type_ = type(value) edit = QtWidgets.QLineEdit(str(value)) if type_ is int: validator = QtGui.QIntValidator() edit.setValidator(validator) elif type_ is float: validator = QtGui.QDoubleValidator() edit.setValidator(validator) form.addRow(f"{name} {type_}", edit) self.edits[name] = (edit, type_) button = QtWidgets.QPushButton(button_text) button.clicked.connect(self.accept) form.addRow(button) self.setLayout(form)
def init_ui(self): """ Initialize line edits and form layout based on setting. """ self.setMaximumWidth(400) form = QtWidgets.QFormLayout() for field_name, field_value in self.default_setting.items(): field_type = type(field_value) if field_type == list: widget = QtWidgets.QComboBox() widget.addItems(field_value) else: widget = QtWidgets.QLineEdit() display_name = NAME_DISPLAY_MAP.get(field_name, field_name) form.addRow(display_name, widget) self.widgets[field_name] = (widget, field_type) start_algo_button = QtWidgets.QPushButton("启动算法") start_algo_button.clicked.connect(self.start_algo) form.addRow(start_algo_button) load_csv_button = QtWidgets.QPushButton("CSV启动") load_csv_button.clicked.connect(self.load_csv) form.addRow(load_csv_button) form.addRow(QtWidgets.QLabel("")) form.addRow(QtWidgets.QLabel("")) form.addRow(QtWidgets.QLabel("")) self.setting_name_line = QtWidgets.QLineEdit() form.addRow("配置名称", self.setting_name_line) save_setting_button = QtWidgets.QPushButton("保存配置") save_setting_button.clicked.connect(self.save_setting) form.addRow(save_setting_button) for button in [ start_algo_button, load_csv_button, save_setting_button ]: button.setFixedHeight(button.sizeHint().height() * 2) self.setLayout(form)
def __init__(self, start: datetime, end: datetime, parent=None): """""" super().__init__(parent) self.setWindowTitle("选择数据区间") self.start_edit = QtWidgets.QDateEdit( QtCore.QDate(start.year, start.month, start.day)) self.end_edit = QtWidgets.QDateEdit( QtCore.QDate(end.year, end.month, end.day)) button = QtWidgets.QPushButton("确定") button.clicked.connect(self.accept) form = QtWidgets.QFormLayout() form.addRow("开始时间", self.start_edit) form.addRow("结束时间", self.end_edit) form.addRow(button) self.setLayout(form)
def __init__(self, engine: ManagerEngine, parent=None): """""" super().__init__() self.engine = engine self.setWindowTitle("下载历史数据") self.setFixedWidth(300) self.setWindowFlags((self.windowFlags() | QtCore.Qt.CustomizeWindowHint) & ~QtCore.Qt.WindowMaximizeButtonHint) self.symbol_edit = QtWidgets.QLineEdit() self.exchange_combo = QtWidgets.QComboBox() for i in Exchange: self.exchange_combo.addItem(str(i.name), i) self.interval_combo = QtWidgets.QComboBox() for i in Interval: self.interval_combo.addItem(str(i.name), i) 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)) button = QtWidgets.QPushButton("下载") button.clicked.connect(self.download) form = QtWidgets.QFormLayout() form.addRow("代码", self.symbol_edit) form.addRow("交易所", self.exchange_combo) form.addRow("周期", self.interval_combo) form.addRow("开始日期", self.start_date_edit) form.addRow(button) self.setLayout(form)
def init_ui(self): """""" form = QtWidgets.QFormLayout() # Add vt_symbol and name edit if add new strategy self.setWindowTitle(f"策略参数配置:{self.class_name}") button_text = "确定" parameters = self.parameters for name, value in parameters.items(): type_ = type(value) edit = QtWidgets.QLineEdit(str(value)) if type_ is int: validator = QtGui.QIntValidator() edit.setValidator(validator) elif type_ is float: validator = QtGui.QDoubleValidator() edit.setValidator(validator) form.addRow(f"{name} {type_}", edit) self.edits[name] = (edit, type_) button = QtWidgets.QPushButton(button_text) button.clicked.connect(self.accept) form.addRow(button) widget = QtWidgets.QWidget() widget.setLayout(form) scroll = QtWidgets.QScrollArea() scroll.setWidgetResizable(True) scroll.setWidget(widget) vbox = QtWidgets.QVBoxLayout() vbox.addWidget(scroll) self.setLayout(vbox)
def init_ui(self): """""" self.setWindowTitle("模拟交易") self.setFixedHeight(200) self.setFixedWidth(500) interval_spin = QtWidgets.QSpinBox() interval_spin.setMinimum(1) interval_spin.setValue(self.paper_engine.timer_interval) interval_spin.setSuffix(" 秒") interval_spin.valueChanged.connect(self.paper_engine.set_timer_interval) slippage_spin = QtWidgets.QSpinBox() slippage_spin.setMinimum(0) slippage_spin.setValue(self.paper_engine.trade_slippage) slippage_spin.setSuffix(" 跳") slippage_spin.valueChanged.connect(self.paper_engine.set_trade_slippage) instant_check = QtWidgets.QCheckBox() instant_check.setChecked(self.paper_engine.instant_trade) instant_check.stateChanged.connect(self.paper_engine.set_instant_trade) clear_button = QtWidgets.QPushButton("清空所有持仓") clear_button.clicked.connect(self.paper_engine.clear_position) clear_button.setFixedHeight(clear_button.sizeHint().height() * 2) form = QtWidgets.QFormLayout() form.addRow("市价委托和停止委托的成交滑点", slippage_spin) form.addRow("模拟交易持仓盈亏的计算频率", interval_spin) form.addRow("下单后立即使用当前盘口撮合", instant_check) form.addRow(clear_button) vbox = QtWidgets.QVBoxLayout() vbox.addStretch() vbox.addLayout(form) vbox.addStretch() self.setLayout(vbox)
def init_ui(self): """""" self.setWindowTitle("CTA回测") # Setting Part self.class_combo = QtWidgets.QComboBox() self.symbol_line = QtWidgets.QLineEdit("BTCUSDT.BINANCE") self.interval_combo = QtWidgets.QComboBox() for interval in Interval: if interval != Interval.TICK: self.interval_combo.addItem(interval.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.0004") self.slippage_line = QtWidgets.QLineEdit("0.5") self.size_line = QtWidgets.QLineEdit("1") self.pricetick_line = QtWidgets.QLineEdit("0.01") 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) 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) self.order_button = QtWidgets.QPushButton("委托记录") self.order_button.clicked.connect(self.show_backtesting_orders) self.order_button.setEnabled(False) self.trade_button = QtWidgets.QPushButton("成交记录") self.trade_button.clicked.connect(self.show_backtesting_trades) self.trade_button.setEnabled(False) self.daily_button = QtWidgets.QPushButton("每日盈亏") self.daily_button.clicked.connect(self.show_daily_results) self.daily_button.setEnabled(False) self.candle_button = QtWidgets.QPushButton("K线图表") self.candle_button.clicked.connect(self.show_candle_chart) self.candle_button.setEnabled(False) reload_button = QtWidgets.QPushButton("策略重载") reload_button.clicked.connect(self.reload_strategy_class) for button in [ backtesting_button, optimization_button, downloading_button, self.result_button, self.order_button, self.trade_button, self.daily_button, self.candle_button, reload_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("合约模式", self.inverse_combo) result_grid = QtWidgets.QGridLayout() result_grid.addWidget(self.trade_button, 0, 0) result_grid.addWidget(self.order_button, 0, 1) result_grid.addWidget(self.daily_button, 1, 0) result_grid.addWidget(self.candle_button, 1, 1) left_vbox = QtWidgets.QVBoxLayout() left_vbox.addLayout(form) left_vbox.addWidget(backtesting_button) left_vbox.addWidget(downloading_button) left_vbox.addStretch() left_vbox.addLayout(result_grid) left_vbox.addStretch() left_vbox.addWidget(optimization_button) left_vbox.addWidget(self.result_button) left_vbox.addStretch() left_vbox.addWidget(reload_button) # Result part self.statistics_monitor = StatisticsMonitor() self.log_monitor = QtWidgets.QTextEdit() self.log_monitor.setMaximumHeight(400) self.chart = BacktesterChart() self.chart.setMinimumWidth(1000) self.trade_dialog = BacktestingResultDialog( self.main_engine, self.event_engine, "回测成交记录", BacktestingTradeMonitor ) self.order_dialog = BacktestingResultDialog( self.main_engine, self.event_engine, "回测委托记录", BacktestingOrderMonitor ) self.daily_dialog = BacktestingResultDialog( self.main_engine, self.event_engine, "回测每日盈亏", DailyResultMonitor ) # Candle Chart self.candle_dialog = CandleChartDialog() # 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 __init__(self, parent=None): """""" super().__init__() self.setWindowTitle("从CSV文件导入数据") self.setFixedWidth(300) self.setWindowFlags((self.windowFlags() | QtCore.Qt.CustomizeWindowHint) & ~QtCore.Qt.WindowMaximizeButtonHint) file_button = QtWidgets.QPushButton("选择文件") file_button.clicked.connect(self.select_file) load_button = QtWidgets.QPushButton("确定") load_button.clicked.connect(self.accept) self.file_edit = QtWidgets.QLineEdit() self.symbol_edit = QtWidgets.QLineEdit() self.exchange_combo = QtWidgets.QComboBox() for i in Exchange: self.exchange_combo.addItem(str(i.name), i) self.interval_combo = QtWidgets.QComboBox() for i in Interval: if i != Interval.TICK: self.interval_combo.addItem(str(i.name), i) self.datetime_edit = QtWidgets.QLineEdit("datetime") self.open_edit = QtWidgets.QLineEdit("open") self.high_edit = QtWidgets.QLineEdit("high") self.low_edit = QtWidgets.QLineEdit("low") self.close_edit = QtWidgets.QLineEdit("close") self.volume_edit = QtWidgets.QLineEdit("volume") self.open_interest_edit = QtWidgets.QLineEdit("open_interest") self.format_edit = QtWidgets.QLineEdit("%Y-%m-%d %H:%M:%S") info_label = QtWidgets.QLabel("合约信息") info_label.setAlignment(QtCore.Qt.AlignCenter) head_label = QtWidgets.QLabel("表头信息") head_label.setAlignment(QtCore.Qt.AlignCenter) format_label = QtWidgets.QLabel("格式信息") format_label.setAlignment(QtCore.Qt.AlignCenter) form = QtWidgets.QFormLayout() form.addRow(file_button, self.file_edit) form.addRow(QtWidgets.QLabel()) form.addRow(info_label) form.addRow("代码", self.symbol_edit) form.addRow("交易所", self.exchange_combo) form.addRow("周期", self.interval_combo) form.addRow(QtWidgets.QLabel()) form.addRow(head_label) form.addRow("时间戳", self.datetime_edit) form.addRow("开盘价", self.open_edit) form.addRow("最高价", self.high_edit) form.addRow("最低价", self.low_edit) form.addRow("收盘价", self.close_edit) form.addRow("成交量", self.volume_edit) form.addRow("持仓量", self.open_interest_edit) form.addRow(QtWidgets.QLabel()) form.addRow(format_label) form.addRow("时间格式", self.format_edit) form.addRow(QtWidgets.QLabel()) form.addRow(load_button) self.setLayout(form)
def init_ui(self): """""" self.setWindowTitle("算法交易") # Left side control widgets self.template_combo = QtWidgets.QComboBox() self.template_combo.currentIndexChanged.connect(self.show_algo_widget) form = QtWidgets.QFormLayout() form.addRow("算法", self.template_combo) widget = QtWidgets.QWidget() widget.setLayout(form) vbox = QtWidgets.QVBoxLayout() vbox.addWidget(widget) for algo_template in self.algo_engine.algo_templates.values(): widget = AlgoWidget(self.algo_engine, algo_template) vbox.addWidget(widget) template_name = algo_template.__name__ display_name = algo_template.display_name self.algo_widgets[template_name] = widget self.template_combo.addItem(display_name, template_name) vbox.addStretch() stop_all_button = QtWidgets.QPushButton("全部停止") stop_all_button.setFixedHeight(stop_all_button.sizeHint().height() * 2) stop_all_button.clicked.connect(self.algo_engine.stop_all) vbox.addWidget(stop_all_button) # Right side monitor widgets active_algo_monitor = ActiveAlgoMonitor( self.algo_engine, self.event_engine ) inactive_algo_monitor = InactiveAlgoMonitor( self.algo_engine, self.event_engine ) tab1 = QtWidgets.QTabWidget() tab1.addTab(active_algo_monitor, "执行中") tab1.addTab(inactive_algo_monitor, "已结束") log_monitor = LogMonitor(self.event_engine) tab2 = QtWidgets.QTabWidget() tab2.addTab(log_monitor, "日志") setting_monitor = SettingMonitor(self.algo_engine, self.event_engine) setting_monitor.use_signal.connect(self.use_setting) tab3 = QtWidgets.QTabWidget() tab3.addTab(setting_monitor, "配置") grid = QtWidgets.QGridLayout() grid.addWidget(tab1, 0, 0, 1, 2) grid.addWidget(tab2, 1, 0) grid.addWidget(tab3, 1, 1) hbox2 = QtWidgets.QHBoxLayout() hbox2.addLayout(vbox) hbox2.addLayout(grid) self.setLayout(hbox2) self.show_algo_widget()
def init_ui(self): """""" self.setWindowTitle("启动算法") self.setFrameShape(self.Box) self.setLineWidth(1) self.name_line = QtWidgets.QLineEdit() self.direction_combo = QtWidgets.QComboBox() self.direction_combo.addItems( [Direction.LONG.value, Direction.SHORT.value]) self.offset_combo = QtWidgets.QComboBox() self.offset_combo.addItems( [Offset.NONE.value, Offset.OPEN.value, Offset.CLOSE.value]) float_validator = QtGui.QDoubleValidator() self.price_line = QtWidgets.QLineEdit() self.price_line.setValidator(float_validator) self.volume_line = QtWidgets.QLineEdit() self.volume_line.setValidator(float_validator) int_validator = QtGui.QIntValidator() self.payup_line = QtWidgets.QLineEdit() self.payup_line.setValidator(int_validator) self.interval_line = QtWidgets.QLineEdit() self.interval_line.setValidator(int_validator) button_start = QtWidgets.QPushButton("启动") button_start.clicked.connect(self.start_algo) self.lock_combo = QtWidgets.QComboBox() self.lock_combo.addItems(["否", "是"]) 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.strategy_engine.init_all_strategies) start_button = QtWidgets.QPushButton("全部启动") start_button.clicked.connect(self.strategy_engine.start_all_strategies) stop_button = QtWidgets.QPushButton("全部停止") stop_button.clicked.connect(self.strategy_engine.stop_all_strategies) add_spread_button = QtWidgets.QPushButton("创建价差") add_spread_button.clicked.connect(self.add_spread) remove_spread_button = QtWidgets.QPushButton("移除价差") remove_spread_button.clicked.connect(self.remove_spread) form = QtWidgets.QFormLayout() form.addRow("价差", self.name_line) form.addRow("方向", self.direction_combo) form.addRow("开平", self.offset_combo) form.addRow("价格", self.price_line) form.addRow("数量", self.volume_line) form.addRow("超价", self.payup_line) form.addRow("间隔", self.interval_line) form.addRow("锁仓", self.lock_combo) form.addRow(button_start) vbox = QtWidgets.QVBoxLayout() vbox.addLayout(form) vbox.addStretch() vbox.addWidget(self.class_combo) vbox.addWidget(add_button) vbox.addWidget(init_button) vbox.addWidget(start_button) vbox.addWidget(stop_button) vbox.addStretch() vbox.addWidget(add_spread_button) vbox.addWidget(remove_spread_button) self.setLayout(vbox)