def close_position(self): """平仓""" row = self.position_table.currentRow() symbol = self.position_table.item( row, position_table_column.index('symbol')).text() direction = self.position_table.item( row, position_table_column.index('direction')).text() exchange = self.position_table.item( row, position_table_column.index('exchange')).text() volume = self.position_table.item( row, position_table_column.index('volume')).text() local_symbol = symbol + '.' + exchange tick = TickData(symbol=symbol, exchange=exchange_map[exchange]) try: price = self.bee_ext.app.recorder.get_tick(local_symbol).last_price except AttributeError: TipDialog("未订阅此合约行情") else: try: if direction == "long": self.bee_ext.app.action.cover(price=float(price), volume=float(volume), origin=tick) if direction == "short": self.bee_ext.app.action.sell(price=float(price), volume=float(volume), origin=tick) TipDialog("平仓请求发送成功") except Exception as e: print(e) QMessageBox().warning(self, "提示", "平仓请求发送失败" + str(e), QMessageBox.Ok, QMessageBox.Ok)
def radio_slot_callback(self, res): if not res: G.config.update(G.temp_var) create_db_conn(**G.temp_var['info']) # 创建数据库连接 G.config.to_file() TipDialog("修改成功") else: TipDialog(f"修改失败{res}")
def open_strategy_slot(self): row = self.strategy_table.currentRow() name = self.strategy_table.item( row, strategy_table_column.index('name')).text() res = bee_current_app.enable_extension(name) if res: TipDialog("开启成功") self.fill_table() else: TipDialog("开启失败")
def save_btn_slot(self): name = self.name.text() path = self.path.text() if os.path.exists(path) and os.path.isfile(path): G.config.python_path.pop(self.raw_name) G.config.python_path.update({name: path}) G.config.to_file() TipDialog("修改成功") self.close() else: TipDialog("未知路径")
def subscribe_slot(self): text = self.symbol_list.currentText().split(contract_space) local_symbol = text[0] name = text[1] if local_symbol not in G.all_contracts: TipDialog("未知合约") return if local_symbol: res = current_app.subscribe(local_symbol) if res == 0: G.subscribes.update({local_symbol: name}) self.reload() TipDialog("订阅成功") else: TipDialog("订阅失败")
def set_app_py_slot(self): """设置为当前app解释器""" py_ = self.path.text() G.config.installed_apps[self.app_name.text()].update({"py_": py_}) G.config.to_file() self.cur_py.setText(self.py_box.currentText()) TipDialog("设置成功")
def open_order(self, direction): """下单""" local_symbol = G.choice_local_symbol exchange = local_symbol.split(".")[1] type = "LIMIT" if self.price_type.currentText() == "限价" else "MARKET" price = self.price.text() volume = self.volume.text() offset = "open" req = helper.generate_order_req_by_str(symbol=local_symbol, exchange=exchange, direction=direction, offset=offset, volume=int(volume), price=float(price), type=type) try: req_id = self.bee_ext.app.send_order(req) sleep(0.2) order = self.bee_ext.app.recorder.get_order(req_id) if order.status.value == "拒单": msg = self.bee_ext.app.recorder.get_new_error( )['data']['ErrorMsg'] QMessageBox().warning(self, "提示", str(msg), QMessageBox.Ok, QMessageBox.Ok) else: TipDialog("下单请求发送成功") except Exception as e: QMessageBox().warning(self, "提示", "下单请求发送失败" + str(e), QMessageBox.Ok, QMessageBox.Ok)
def keyReleaseEvent(self, event: QtGui.QKeyEvent): sp = self.text().split("+")[-1] if len(sp) != 1: self.setText("--") G.config.SHORTCUT.update({self.name: self.text()}) G.config.to_file() self.parent_widget.mainwindow.update_shortcut() TipDialog("修改成功")
def pre_page_slot(self): try: i = self.page_history.pop() while i == self.stackedWidget.currentIndex(): i = self.page_history.pop() self.stackedWidget.setCurrentIndex(i) except IndexError: TipDialog("到底啦~")
def pypi_use_slot(self): if self.pypi_checkBox.isChecked(): G.config.pypi_use = True else: G.config.pypi_use = False G.config.to_file() if self.sm: TipDialog("修改成功")
def install_path_slot(self): path = QFileDialog.getExistingDirectory(self, "安装路径", '/') if not path: return if os.path.exists(path) and os.path.isdir(path): G.config.install_path = path G.config.to_file() self.install_path.setText(path) TipDialog("修改成功")
def gen_strategy_slot(self): filename, _ = QFileDialog.getSaveFileName(self, "策略模板", "/", "Python files(*.py)") if filename: filename = filename if filename.endswith( '.py') else filename + '.py' with open(filename, 'w') as f: f.write(strategy_template) TipDialog(f"策略模板已生成") QDesktopServices.openUrl(QUrl("file:" + os.path.dirname(filename)))
def pypi_change_slot(self): i_ = self.pypi_source.text().strip() if i_ and re.match( r"^(https?://)?([\da-z\.-]+)\.([a-z\.]{2,6})([/\w .-]*)*/?$", i_): G.config.pypi_source = i_ G.config.to_file() if self.sm: TipDialog("修改成功") else: self.pypi_source.setText(G.config.pypi_source)
def update_config(self): G.config.REFRESH_INTERVAL = bee_current_app.config['REFRESH_INTERVAL'] = float(self.REFRESH_INTERVAL.text()) G.config.SLIPPAGE_SHORT = bee_current_app.config['SLIPPAGE_SHORT'] = float(self.SLIPPAGE_SHORT.text()) G.config.SLIPPAGE_BUY = bee_current_app.config['SLIPPAGE_BUY'] = float(self.SLIPPAGE_BUY.text()) G.config.SLIPPAGE_COVER = bee_current_app.config['SLIPPAGE_COVER'] = float(self.SLIPPAGE_COVER.text()) G.config.SLIPPAGE_SELL = bee_current_app.config['SLIPPAGE_SELL'] = float(self.SLIPPAGE_SELL.text()) G.config.INSTRUMENT_INDEPEND = bee_current_app.config[ 'INSTRUMENT_INDEPEND'] = True if self.INSTRUMENT_INDEPEND.isChecked() else False G.config.CLOSE_PATTERN = bee_current_app.config['CLOSE_PATTERN'] = str(self.CLOSE_PATTERN.currentText()) G.config.to_file() TipDialog('修改成功')
def run_slot(self): if self.data_list.count() <= 0: TipDialog("还未传入数据") return if self.backtrack_list.count() <= 0: TipDialog("还未传入回测API") return if self.size_map_table.rowCount() <= 0: TipDialog("还未选择size_map") return self.name = f"回测{self.counter} {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}" try: worker = BacktrackrWorker(name=self.name, sig=self.sig.report_sig, data=self.get_data(), strategy=self.get_ext(), params=self.get_params()) except Exception as e: QMessageBox.information(self, '提示', str(e)) return self.thread_pool.start(worker) self.counter += 1
def del_btn_slot(self): row = self.py_table.currentRow() name = self.py_table.item(row, 0).text() path = self.py_table.item(row, 1).text() replay = QMessageBox.question(self, '提示', f'确定删除[ {name} ]吗?\n(不删除本地环境)', QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if replay == QMessageBox.Yes: G.config.python_path.pop(name, None) for p in G.config.installed_apps.values(): if path == p.get('py_', ""): p.pop('py_') G.config.to_file() self.load_py() TipDialog("已删除")
def delete_strategy_slot(self): reply = QMessageBox.question(self, '策略提示', "确定删除吗?", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) if reply == QMessageBox.No: return row = self.strategy_table.currentRow() name = self.strategy_table.item( row, strategy_table_column.index('name')).text() bee_current_app.del_extension(name) G.config.STRATEGYS.pop(name) G.config.to_file() TipDialog("删除成功") self.fill_table()
def ok_btn_slot(self): if self.check_res is None: self.test_btn_slot() if self.check_res is True: G.config.DB_INFO[self.name.text()] = dict( host=self.host.text(), user=self.user.text(), password=self.password.text() if self.savebox.currentText() == '总是' else "", database=self.database.text(), port=self.port.text(), url=self.url.text()) G.config.to_file() TipDialog("成功") self.tabWidget.setCurrentIndex(0)
def add_strategy_slot(self): pattern = r"ext\s*=\s*\w*[(][\"\'](.*)[\"\'][)]" filename, _ = QFileDialog.getOpenFileName(self, '选择文件', '/', 'Python files(*.py)') if not filename: return with open(filename, 'r') as f: try: ext = dynamic_loading_api(f) bee_current_app.add_extension(ext) G.config.STRATEGYS.update({ext.extension_name: filename}) G.config.to_file() TipDialog("策略添加成功") self.fill_table() except Exception as e: QMessageBox.warning(self, 'ctpbee策略', str(e), QMessageBox.Ok, QMessageBox.Ok)
def subscribe_type_slot(self): text = self.symbol_list.currentText() local_symbol_ = text.split(contract_space)[0] symbol = ''.join( [x for x in local_symbol_.split('.')[0] if x.isalpha()]) # AP if local_symbol_ not in G.all_contracts: TipDialog("未知合约") return for local_symbol in G.all_contracts: if local_symbol.startswith(symbol): res = current_app.subscribe(local_symbol) if res == 0: G.subscribes.update( {local_symbol: G.all_contracts[local_symbol]}) else: self.load_status.setText(f"{local_symbol}订阅失败") break else: self.reload()
def cancel_order(self): """撤单""" row = self.activate_order_table.currentRow() symbol = self.activate_order_table.item( row, activate_order_table_column.index('symbol')).text() exchange = self.activate_order_table.item( row, activate_order_table_column.index('exchange')).text() local_symbol = symbol + '.' + exchange order_id = self.activate_order_table.item( row, activate_order_table_column.index('order_id')).text() req = helper.generate_cancel_req_by_str(symbol=local_symbol, exchange=exchange, order_id=order_id) try: bee_current_app.cancel_order(req) TipDialog("撤单请求发送成功") except Exception as e: QMessageBox().warning(self, "提示", "撤单请求发送失败" + str(e), QMessageBox.Ok, QMessageBox.Ok)
def run_handler(self): self.py_ = G.config.installed_apps[self.pack_name].get('py_') if not self.py_: QMessageBox.warning(self.parent, "提示", "未选择Python解释器") self.act_setting_slot() return if not os.path.exists(self.py_) or not os.path.isfile(self.py_): QMessageBox.warning(self.parent, "提示", f"{self.py_} 不存在") return try: self.entry_, requirement_ = self.get_build() except Exception as e: QMessageBox.warning(self.parent, "提示", str(e)) return ##检测依赖 p = QProcess() p.start(' '.join(([self.py_, "-m", 'pip', "freeze"]))) p.waitForFinished() out = p.readAllStandardOutput().data().decode() output = out.splitlines() with open(requirement_, 'r') as f: requirement = f.read().splitlines() dissatisfy, version_less = diff_pip(output, requirement) if dissatisfy: msgbox = QMessageBox(self.parent) msgbox.setWindowTitle("缺少依赖") msgbox.setText("\n".join(dissatisfy[:15]) + "\n...") yes = msgbox.addButton('立即安装', QMessageBox.AcceptRole) no = msgbox.addButton('稍后', QMessageBox.RejectRole) msgbox.setDefaultButton(yes) reply = msgbox.exec_() if reply == QMessageBox.AcceptRole: self.requirement_ = dissatisfy self.install_handler() return # run TipDialog("正在启动...") cmd = ' '.join([self.py_, self.entry_]) QProcess().startDetached(cmd)
def db_callback(self, res): if res: TipDialog("数据库连接出错,请在数据源中查看") else: from app.lib.helper import create_db_conn create_db_conn(**G.temp_var)
def default_sc_slot(self): G.config.back_default() self.mainwindow.update_shortcut() self.close() TipDialog('已恢复')
def local_btn_slot(self): self.db_widget.hide() G.config.LOCAL_SOURCE = True G.config.to_file() TipDialog("修改成功")