目录
运行情况:
编辑
结果json文件格式:
代码:
承接 【python_PyQt5开发验证K线视觉想法工具V1.1 _增加标记类型_线段】 博文
地址:python_PyQt5开发验证K线视觉想法工具V1.1 _增加标记类型_线段_程序猿与金融与科技的博客-CSDN博客
运行情况:
1 选择“批量验证”
2 选择时间区间,与验证的数据相同的时间区间
3 选择股票日数据所在文件夹
4 选择标记结果json文件
5 可以选择某一个股票显示结果,也可以通过“上一个”,“下一个”按钮切换股票查看对应股票标记
结果json文件格式:
代码:
界面布局
1 在 EyeCheckMainWidget 类中实现 fill_stack_widget_two 方法
def fill_stack_widget_two(self):
batch_choice_daily_dir_btn = QtWidgets.QPushButton('选择股票日数据文件夹')
batch_choice_daily_dir_btn.clicked.connect(self.batch_choice_daily_dir_btn_clicked)
self.batch_choice_daily_dir_lineedit = QtWidgets.QLineEdit()
batch_results_file_btn = QtWidgets.QPushButton('批量结果json文件')
batch_results_file_btn.clicked.connect(self.batch_results_file_btn_clicked)
self.batch_results_file_lineedit = QtWidgets.QLineEdit()
batch_layout_one = QtWidgets.QFormLayout()
batch_layout_one.addRow(batch_choice_daily_dir_btn,self.batch_choice_daily_dir_lineedit)
batch_layout_one.addRow(batch_results_file_btn,self.batch_results_file_lineedit)
self.batch_targets_combox = QtWidgets.QComboBox()
self.batch_targets_combox.currentTextChanged.connect(self.batch_targets_combox_currentTextChanged)
batch_pre_btn = QtWidgets.QPushButton('上一个')
batch_pre_btn.clicked.connect(self.batch_pre_btn_clicked)
batch_next_btn = QtWidgets.QPushButton('下一个')
batch_next_btn.clicked.connect(self.batch_next_btn_clicked)
batch_layout_two = QtWidgets.QHBoxLayout()
batch_layout_two.addWidget(self.batch_targets_combox)
batch_layout_two.addWidget(batch_pre_btn)
batch_layout_two.addWidget(batch_next_btn)
batch_layout = QtWidgets.QVBoxLayout()
batch_layout.addLayout(batch_layout_one)
batch_layout.addLayout(batch_layout_two)
self.stack_two.setLayout(batch_layout)
pass
2 在 EyeCheckMainWidget 类 init_data 方法
def init_data(self):
self.please_select_str: str = '-- 请选择 --'
self.single_settingMark_widget: QtWidgets.QWidget = None
self.target_column_list: List[str] = ['xTick', 'open', 'close', 'highest', 'lowest']
self.target_names_list: List[str] = []
self.target_marks_map: Dict = {}
self.batch_current_target_name: str = None
pass
3 在 EyeCheckMainWidget 类 增加批量验证用到的方法
def caculate_k_data_and_show(self,daily_file:str,title_name:str):
left_point = self.left_point.date().toString('yyyy-MM-dd')
right_point = self.right_point.date().toString('yyyy-MM-dd')
left_datetime = datetime.datetime.strptime(left_point, '%Y-%m-%d')
right_datetime = datetime.datetime.strptime(right_point, '%Y-%m-%d')
if left_datetime >= right_datetime:
QtWidgets.QMessageBox.information(
self,
'提示',
'请选择时间区间',
QtWidgets.QMessageBox.Yes
)
return
df = pd.read_csv(daily_file, encoding='utf-8')
df = df.loc[df['openPrice'] > 0].copy()
df['o_date'] = pd.to_datetime(df['tradeDate'])
df = df.loc[(df['o_date'] >= left_point) & (df['o_date'] <= right_point)].copy()
df['open'] = df['openPrice'] * df['accumAdjFactor']
df['close'] = df['closePrice'] * df['accumAdjFactor']
df['highest'] = df['highestPrice'] * df['accumAdjFactor']
df['lowest'] = df['lowestPrice'] * df['accumAdjFactor']
df['xTick'] = df['tradeDate']
k_data = {
'whole_df': df,
'whole_header': ['日期', '开盘', '收盘', '最高', '最低'],
'whole_pd_header': self.target_column_list
}
self.graph_widget.first_setData(k_data)
self.graph_title_label.setText(title_name)
pass
def batch_choice_daily_dir_btn_clicked(self):
path = QtWidgets.QFileDialog.getExistingDirectory(
self,
'打开股票日数据所在目录',
'.'
)
if not path:
return
self.batch_choice_daily_dir_lineedit.setText(path)
def batch_results_file_btn_clicked(self):
path,_ = QtWidgets.QFileDialog.getOpenFileName(
self,
'打开标记计算结果json文件',
'.',
'JSON(*.json)'
)
if not path:
return
try:
with open(path,'r',encoding='utf-8') as fr:
json_obj = json.load(fr)
self.target_marks_map = json_obj
self.target_names_list.clear()
self.target_names_list = list(json_obj.keys())
self.batch_targets_combox.clear()
self.batch_current_target_name = None
self.batch_targets_combox.addItem(self.please_select_str)
self.batch_targets_combox.addItems(self.target_names_list)
self.batch_results_file_lineedit.setText(path)
pass
except Exception as e:
print(e)
QtWidgets.QMessageBox.information(
self,
'提示',
e.__str__(),
QtWidgets.QMessageBox.Yes
)
return
pass
def batch_targets_combox_currentTextChanged(self,txt:str):
cur_txt = self.batch_targets_combox.currentText()
if not cur_txt or cur_txt == self.please_select_str:
return
self.batch_current_target_name = cur_txt
self.batch_show_current_target()
pass
def batch_pre_btn_clicked(self):
if not self.batch_current_target_name:
self.batch_current_target_name = self.target_names_list[-1]
else:
node_index = self.target_names_list.index(self.batch_current_target_name)
if node_index <=0:
self.batch_current_target_name = self.target_names_list[-1]
else:
self.batch_current_target_name = self.target_names_list[node_index-1]
pass
self.batch_show_current_target()
pass
def batch_next_btn_clicked(self):
if not self.batch_current_target_name:
self.batch_current_target_name = self.target_names_list[0]
else:
node_index = self.target_names_list.index(self.batch_current_target_name)
if node_index >= len(self.target_names_list)-1:
self.batch_current_target_name = self.target_names_list[0]
else:
self.batch_current_target_name = self.target_names_list[node_index+1]
self.batch_show_current_target()
pass
def batch_show_current_target(self):
self.mark_table.clear_table_contents()
one_node = self.target_marks_map[self.batch_current_target_name]
self.mark_table.set_data(one_node)
# daily_file,title_name
daily_dir = self.batch_choice_daily_dir_lineedit.text()
daily_file = daily_dir + os.path.sep + self.batch_current_target_name + '.csv'
if not daily_dir or not os.path.exists(daily_file):
QtWidgets.QMessageBox.information(
self,
'提示',
'股票日数据目录没有选择,或对应日数据不存在',
QtWidgets.QMessageBox.Yes
)
return
self.caculate_k_data_and_show(daily_file,self.batch_current_target_name)
self.graph_widget.add_marks(one_node)
pass