def onAddOptionGroup(self): text, ok = QInputDialog.getText(self.backtest, "请输入期权组名称", "名称", QLineEdit.Normal) current_item = self.backtest_tree.currentItem() # parent_item = current_item.parent() current_item_text = current_item.text(0) # parent_item_text = parent_item.text(0) if ok and text: node = QTreeWidgetItem(current_item) node.setText(0, text) node.setCheckState(0, Qt.Unchecked) node.setWhatsThis(0, "option_group") node.setIcon(0, QtGui.QIcon("../icon/group.png")) self.backtest_tree.expandItem(self.backtest_tree.currentItem()) group_dict = { "name": text, "enable": 1, "contracts": [], "signal": { "type": "list", "value": 0, "list": [] }, "ratio": { "type": "float", "value": 0, }, } for underlying in self.config["options"]["underlyings"]: if underlying.get("name") == current_item_text: underlying["groups"].append(group_dict)
def populate(self, backup_name=None): self.backups_tree_widget.clear() self.original_item_texts = {} plans = Backups.load_all() if len(plans) is 0: self.hide_regular_widgets() self.welcome_widget.show() return self.show_regular_widgets() self.welcome_widget.hide() selected_item = None for plan in plans.values(): schedule = "Manual" daily = plan.backup_daily_time if daily is not None and plan.backup_days is not None: days_order = { "mon": 0, "tue": 1, "wed": 2, "thu": 3, "fri": 4, "sat": 5, "sun": 6 } days_map = { "mon": "M", "tue": "T", "wed": "W", "thu": "Th", "fri": "F", "sat": "S", "sun": "Su" } time_str = datetime.datetime.strptime( f"{daily.hour()}:{daily.minute()}", "%H:%M").strftime("%I:%M %p") schedule = f"{time_str} | {','.join(days_map[d] for d in sorted(plan.backup_days.split(','), key=lambda x: days_order[x]))}" if plan.every_hour is not None and plan.every_min is not None: schedule = f"Every {plan.every_hour} hour(s) ({plan.every_min} mins)" item = QTreeWidgetItem( [plan.name, plan.location, "Idle", schedule]) item.setWhatsThis(0, plan.name) if backup_name is None: if selected_item is None: selected_item = item else: if plan.name == backup_name: selected_item = item self.backups_tree_widget.addTopLevelItem(item) self.log_file_thread.reset = True self.backups_tree_widget.setCurrentItem(selected_item) self.update_logs(force=True)
def onAddOptionUnderlying(self): text, ok = QInputDialog.getText(self.backtest, "请输入期权标的名称", "名称", QLineEdit.Normal) if ok and text: node = QTreeWidgetItem(self.backtest_tree.currentItem()) node.setText(0, text) node.setCheckState(0, Qt.Unchecked) node.setWhatsThis(0, "option_underlying") self.backtest_tree.expandItem(self.backtest_tree.currentItem()) ids = [ i.btId for i in self.mdi_area.subWindowList() if hasattr(i, "btType") and i.btType in ["option_underlying", "excel", "csv"] ] group_dict = { "name": text, "enable": 0, "ratio": { "type": "int", "value": 0, }, "id": { "type": "list", "value": 0, "list": ids, "data": getattr( self.parent.getSubWindowByAttribute("btId", ids[0]), "btData") }, "signal": { "type": "list", "value": 0, "list": [] }, "option_side": { "type": "list", "value": 0, "list": [u"买入"] }, "volume": { "type": "int", "value": 0, }, "groups": [], "contracts": [], } self.config["options"]["underlyings"].append(group_dict)
def onAddOptionContract(self): text, ok = QInputDialog.getText(self.backtest, "请输入期权合约名称", "名称", QLineEdit.Normal) current_item = self.backtest_tree.currentItem() current_item_whats_this = current_item.whatsThis(0) current_item_text = current_item.text(0) parent_item = current_item.parent() parent_whats_this = parent_item.whatsThis(0) parent_item_text = parent_item.text(0) if ok and text: node = QTreeWidgetItem(current_item) node.setText(0, text) node.setCheckState(0, Qt.Unchecked) node.setWhatsThis(0, "option_contract") node.setIcon(0, QtGui.QIcon("../icon/contract.png")) self.backtest_tree.expandItem(self.backtest_tree.currentItem()) filter_dict = { "name": text, "enable": 1, "open_status": False, "ids": [], "option_type": { "type": "list", "value": 0, "list": setting.OPTION_TYPE, }, "option_side": { "type": "list", "value": 0, "list": setting.OPTION_SIDE, }, "close_method": { "type": "list", "value": 0, "list": setting.OPTION_CLOSE_METHOD, }, "change_feq": { "type": "list", "value": 0, "list": setting.OPTION_CHANGE_FEQ, }, "change_condition": { "type": "list", "value": 0, "list": setting.OPTION_CHANGE_CONDITION, }, "month_interval": { "type": "list", "value": 0, "list": [ setting.OPTION_INTERVAL[i] for i in range(len(setting.OPTION_INTERVAL)) if i != 2 ], }, "strike_interval": { "type": "list", "value": 0, "list": setting.OPTION_STRIKE_INTERVAL, }, "smart_selection": { "type": "list", "value": 1, "list": setting.OPTION_SMART_SELECTION, }, "type": "option", "volume": { "type": "int", "value": 0 }, "deposit_coefficient": { "type": "int", "value": 1, }, "delta": { "type": "int", "value": 0, }, "gamma": { "type": "int", "value": 0, }, "theta": { "type": "int", "value": 0, }, "vega": { "type": "int", "value": 0, }, "rho": { "type": "int", "value": 0, }, "ivix": { "type": "float", "value": 0 } } for underlying in self.config["options"]["underlyings"]: underlying_name = underlying.get("name") if current_item_whats_this == "option_group": if underlying_name == parent_item_text: groups = underlying.get("groups") for group in groups: if group.get("name") == current_item_text: group["contracts"].append(filter_dict) elif current_item_whats_this == "option_underlying": if underlying_name == current_item_text: underlying.get("contracts").append(filter_dict)
def loadBacktestTree(self): options = self.config.get("options", {}) underlyings = options.get("underlyings", []) for i in range(1): item = self.backtest_tree.topLevelItem(i) item.setExpanded(True) for j in range(item.childCount()): child_item = item.child(j) child_item.setExpanded(True) whatsthis = child_item.whatsThis(0) if whatsthis == "option": for underlying in underlyings: current_item = child_item node = QTreeWidgetItem(current_item) node.setText(0, underlying["name"]) node.setCheckState(0, Qt.Unchecked) node.setWhatsThis(0, "option_underlying") node.setExpanded(True) data = underlying.get("id", {}).get("data", pd.DataFrame()) if not data.empty: id_dict = underlying.get("id", {}) name = id_dict["list"][id_dict["value"]] childSubWindow = { "title": "%s的当日合约", "type": "option_contract_table", "table_name": "%date%", "where": "", "select": id, "hidden_columns": [], "index_column": [], "childSubWindow": {}, } hidden_columns = [ 'total_turnover', 'limit_up', 'limit_down', 'settlement', 'prev_settlement', 'discount_rate', 'acc_net_value', 'unit_net_value', 'date', 'open_interest', 'iopv', 'num_trades' ] GridView(self.parent, name, data, id=name, hidden_columns=hidden_columns, index_column='date', childSubWindow=childSubWindow, type="option_underlying") current_item = node groups = underlying.get("groups", []) for group in groups: node = QTreeWidgetItem(current_item) node.setText(0, group["name"]) node.setCheckState(0, Qt.Unchecked) node.setIcon(0, QtGui.QIcon("../icon/group.png")) node.setWhatsThis(0, "option_group") node.setExpanded(True) current_item = node contracts = group.get("contracts") for contract in contracts: node = QTreeWidgetItem(current_item) node.setText(0, contract["name"]) node.setCheckState(0, Qt.Unchecked) node.setWhatsThis(0, "option_contract") node.setExpanded(True) current_item = node