承接 【python_PyQt5运行股票研究python方法工具V1.0】
地址 python_PyQt5运行股票研究python方法工具V1.0_程序猿与金融与科技的博客-CSDN博客
目录
ExcuteShowWidget 中 process_excute_event 方法修改
结果展示:
1 选择py文件所在目录
2 选择py文件目录后,会提起该目录下所有的py文件,放入下拉列表
3 选择要执行的方法,点击“执行”,子线程会执行对应py文件中的代码,执行完毕后会返回结果
4 返回结果为map形式,map中的key用于识别不用结果分别对应什么内容,key会在下拉列表中展示,可以通过下拉列表切换,或上一个,下一个切换表格内容
5 显示执行结果内容
代码:
示例py文件代码(计算股票日横截面数据)
def excute_code(json_obj:dict):
'''
横截面股票日数据查看
:param json_obj:
:return:
'''
import pandas as pd
import os
code_list = json_obj['code_list']
daily_dir = json_obj['daily_dir']
start_date = json_obj['start_date']
end_date = json_obj['end_date']
# second_level {行业名:[股票代码]}
second_level = json_obj['second_level']
res_target_name_map = {}
df = None
column_list = ['ticker','tradeDate','o','c','h','l','turnoverVol','dealAmount','turnoverRate','negMarketValue','marketValue','chgPct','PE']
for code in code_list:
daily_file_path = daily_dir + os.path.sep + code + '.csv'
one_df = pd.read_csv(daily_file_path,encoding='utf-8')
one_df['o_date'] = pd.to_datetime(one_df['tradeDate'])
one_df = one_df.loc[(one_df['o_date'] >= start_date) & (one_df['o_date'] <= end_date)].copy()
one_df = one_df.loc[one_df['openPrice'] > 0].copy()
one_df['o'] = one_df['openPrice'] * one_df['accumAdjFactor']
one_df['c'] = one_df['closePrice'] * one_df['accumAdjFactor']
one_df['h'] = one_df['highestPrice'] * one_df['accumAdjFactor']
one_df['l'] = one_df['lowestPrice'] * one_df['accumAdjFactor']
one_df['chgPct'] = one_df['chgPct']*100
one_df['ticker'] = code
one_df.sort_values(by='o_date',ascending=False,inplace=True)
one_df = one_df.loc[:,column_list]
if df is None:
df = one_df
else:
df = pd.concat([df,one_df])
pass
df_group = df.groupby(by='tradeDate')
for name,group in df_group:
key_name = f'{name},all'
val_data = group.loc[:,column_list].values.tolist()
res_target_name_map[key_name] = val_data
pass
for key,val in second_level.items():
df_two = df.loc[df['ticker'].isin(val)].copy()
df_two_group = df_two.groupby(by='tradeDate')
for name, group in df_two_group:
key_name = f'{name},{key}'
val_data = group.loc[:, column_list].values.tolist()
res_target_name_map[key_name] = val_data
pass
pass
target_style = 'table'
target_data = res_target_name_map
header_list = ['ticker','日期','开盘价','收盘价','最高价','最低价','成交量','成交笔数','日换手率','流通市值','总市值','涨跌幅','滚动市盈率']
final_map = {
'target_style':target_style,
'target_header':header_list,
'target_data':target_data
}
return final_map
参数json文件
{
"code_list":["000025","000031","000058","000061","000068","000096","000410","000428","000503","000509","000514","000523","000536","000548","000557","000558","000565","000566","000599","000605","000632","000638","000652","000655","000701","000709","000727","000755","000758","000767","000797","000825","000850","000856","000878","000897","000898","000901","000912","000927","000952","000958","000960","000972","001896","002022","002037","002047","002061","002097","002106","002114","002160","002168","002183","002217","002278","002285","002305","002339","002343","002344","002354","002366","002400","002427","002431","002479","002480","002500","002522","002542","002573","002642","002659","002672","002673","002676","002682","002712","002722","002783","002807","600022","600037","600058","600076","600100","600108","600115","600119","600149","600150","600156","600169","600198","600202","600239","600249","600287","600292","600299","600300","600307","600320","600322","600343","600358","600376","600397","600403","600423","600433","600448","600526","600550","600601","600604","600617","600623","600626","600629","600630","600637","600648","600649","600650","600651","600653","600675","600676","600679","600684","600686","600692","600705","600708","600712","600716","600726","600743","600748","600758","600783","600798","600800","600805","600815","600825","600844","600855","600864","600871","600874","600880","600881","600889","600939","600959","600960","600981","600986","601016","601106","601168","601198","601368","601588","601600","601718","601727","601766","601857","601866","601918","601989","601991","603021","603169","603817","603999"],
"daily_dir":"E:/daily/",
"start_date":"2023-07-01",
"end_date":"2023-08-11",
"second_level":{"\u4ea4\u901a\u8fd0\u8f93": ["000548", "000557", "000755", "002183", "002682", "600115", "600119", "600650", "600798", "601866"], "\u4f20\u5a92": ["002343", "002354", "002400", "002712", "600037", "600358", "600637", "600825", "600959", "600986", "603999"], "\u516c\u7528\u4e8b\u4e1a": ["000767", "001896", "002479", "002480", "600149", "600617", "600726", "601016", "601991"], "\u519c\u6797\u7267\u6e14": ["000972", "600108"], "\u533b\u836f\u751f\u7269": ["000566", "000952", "002022"], "\u5546\u8d38\u96f6\u552e": ["000058", "000061", "002344", "600058", "600287", "600712", "600981"], "\u56fd\u9632\u519b\u5de5": ["600150", "601989"], "\u57fa\u7840\u5316\u5de5": ["000565", "000912", "002037", "002427", "002522", "002783", "600299", "600423", "600623", "600844", "600889"], "\u5bb6\u7528\u7535\u5668": ["002676"], "\u5efa\u7b51\u88c5\u9970": ["002047", "002061", "002431", "002542", "600629", "600939"], "\u623f\u5730\u4ea7": ["000031", "000514", "000558", "000797", "000897", "002285", "002305", "600239", "600322", "600376", "600604", "600648", "600649", "600675", "600684", "600692", "600708", "600716", "600743", "600748", "601588"], "\u6709\u8272\u91d1\u5c5e": ["000758", "000878", "000960", "002114", "002160", "601168", "601600"], "\u673a\u68b0\u8bbe\u5907": ["000410", "000856", "000927", "002097", "002278", "600169", "600202", "600320", "600343", "600783", "600815", "601106", "601766", "603169"], "\u6c7d\u8f66": ["000599", "000901", "600626", "600653", "600679", "600686", "600960"], "\u7164\u70ad": ["600397", "600403", "600758", "601918"], "\u73af\u4fdd": ["000068", "000605", "002573", "002672", "600292", "600526", "600874", "601368", "603817"], "\u7535\u529b\u8bbe\u5907": ["002168", "002339", "002366", "600550", "601727"], "\u7535\u5b50": ["000509", "000536", "000701", "000727", "002106", "002217", "600601", "600651"], "\u77f3\u6cb9\u77f3\u5316": ["000096", "600800", "600871", "601857"], "\u793e\u4f1a\u670d\u52a1": ["000428", "002659", "600880"], "\u7eba\u7ec7\u670d\u9970": ["000850", "002722", "600156", "600448", "600630", "601718"], "\u7efc\u5408": ["000025", "000632", "000652", "600676", "600805", "600881"], "\u7f8e\u5bb9\u62a4\u7406": ["000523", "600249"], "\u8ba1\u7b97\u673a": ["000503", "000638", "002642", "600100", "600855"], "\u8f7b\u5de5\u5236\u9020": ["600076", "600433", "603021"], "\u901a\u4fe1": ["600198"], "\u94a2\u94c1": ["000655", "000709", "000825", "000898", "600022", "600307"], "\u94f6\u884c": ["002807"], "\u975e\u94f6\u91d1\u878d": ["000958", "002500", "002673", "600705", "600864", "601198"], "\u98df\u54c1\u996e\u6599": ["600300"]}
}
表格控件
class TableNodeWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.init_data()
self.init_ui()
pass
def init_data(self):
self.whole_data:Dict = None
self.target_name_list: List = None
self.current_target_name: str = None
self.please_select_str: str = '-- 请选择 --'
pass
def init_ui(self):
pre_btn = QtWidgets.QPushButton('上一个')
pre_btn.clicked.connect(self.pre_btn_clicked)
self.target_combox = QtWidgets.QComboBox()
self.target_combox.currentTextChanged.connect(self.target_combox_currentTextChanged)
next_btn = QtWidgets.QPushButton('下一个')
next_btn.clicked.connect(self.next_btn_clicked)
layout_one = QtWidgets.QHBoxLayout()
layout_one.addStretch(1)
layout_one.addWidget(pre_btn)
layout_one.addWidget(self.target_combox)
layout_one.addWidget(next_btn)
layout_one.addStretch(1)
self.title_label = QtWidgets.QLabel()
self.title_label.setAlignment(Qt.AlignCenter)
self.title_label.setStyleSheet('QLabel{font-size:32px;font-weight:bold;}')
tip_label = QtWidgets.QLabel('提示:最多显示500条数据')
self.table = QtWidgets.QTableWidget()
layout = QtWidgets.QVBoxLayout()
layout.addLayout(layout_one)
layout.addWidget(self.title_label)
layout.addWidget(tip_label)
layout.addWidget(self.table)
self.setLayout(layout)
pass
def set_data(self,data:Dict):
self.table.clear()
self.target_combox.clear()
header = data['header']
self.whole_data = data['data']
self.target_name_list = list(self.whole_data.keys())
self.current_target_name = self.target_name_list[0]
self.target_combox.addItem(self.please_select_str)
self.target_combox.addItems(self.target_name_list)
self.table.setColumnCount(len(header))
self.table.setHorizontalHeaderLabels(header)
self.fill_table_content()
pass
def fill_table_content(self):
data = self.whole_data[self.current_target_name]
self.title_label.setText(self.current_target_name)
self.table.setRowCount(len(data))
for i_r, row in enumerate(data):
for i_c, col in enumerate(row):
self.table.setItem(i_r, i_c, QtWidgets.QTableWidgetItem(str(col)))
pass
def pre_btn_clicked(self):
cur_i = self.target_name_list.index(self.current_target_name)
if cur_i<=0:
cur_i = len(self.target_name_list)-1
else:
cur_i = cur_i -1
self.current_target_name = self.target_name_list[cur_i]
self.fill_table_content()
pass
def next_btn_clicked(self):
cur_i = self.target_name_list.index(self.current_target_name)
if cur_i >= len(self.target_name_list) - 1:
cur_i = 0
else:
cur_i = cur_i + 1
self.current_target_name = self.target_name_list[cur_i]
self.fill_table_content()
pass
def target_combox_currentTextChanged(self,txt:str):
cur_txt = self.target_combox.currentText()
if not cur_txt or cur_txt == self.please_select_str:
return
self.current_target_name = cur_txt
self.fill_table_content()
pass
ExcuteShowWidget 中 process_excute_event 方法修改
def process_excute_event(self,data:Dict):
mark_str = data['mark_str']
status = data['status']
if status == 'success':
self.excute_info_label.setText(f'{mark_str} 执行成功')
ret = data['ret']
target_style = ret['target_style']
if target_style == 'table':
# 表格类型结果
pre_map = {
'header':ret['target_header'],
'data':ret['target_data']
}
node_widget = TableNodeWidget()
node_widget.set_data(pre_map)
self.fill_scroll_area([node_widget])
pass
else:
self.excute_info_label.setText(f'{mark_str} 失败!失败!')
now_str = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
log_str = f'{now_str}::{mark_str}::{data["msg"]}'
self.log_list.append(log_str)
self.thread_caculate = None
self.progress_finished()
pass
pass