import tushare as ts ts.set_token('*************') pro = ts.pro_api()
tushare id:433719
小程序采用pyqt5制作,只是很短时间学习并完成的,为学习过程的制作的比较粗糙的小程序,为初学者水平,注释比较详细,也有点啰嗦,代码新手,学习过程中随手写的,多包涵,仅供参考。
Tushare大数据社区,官网所在地址。
首先,在pycharm中调用tushare函数,set_token中为自己申请的密匙
def queren1(): #显示当天买入价格 #data,shouxufei = start() data = ui.lineEdit_10.text() codelist = ui.lineEdit_3.text() number = ui.lineEdit_5.text() df = pro.daily(ts_code=codelist, trade_date=data) close = df.iloc[0,5] k = pro.stock_basic(ts_code=codelist, fields='ts_code,name,area,industry,list_date') name = k.iloc[0, 1] ui.lineEdit_4.setText(str(close)) ui.lineEdit_11.setText(str(name))
以上代码为显示当天的买入价格,pro.daily可调用指定股票在这一天的一些基本数据,此外本函数也可调用一些成交量,涨跌额等一些数据,详细可以查看Tushare大数据社区,其中包含更多的功能,本程序采用的是股票这一天的收盘价的数据。
小程序提供了买入模块和卖出模块,代码如下
def buy(): codelist = ui.lineEdit_3.text() number = ui.lineEdit_5.text() buyprice = ui.lineEdit_4.text() shouxufei = ui.lineEdit_9.text() endprice =float(buyprice)*float(number) k = pro.stock_basic(ts_code=codelist, fields='ts_code,name,area,industry,list_date') name = k.iloc[0,1] for i in range(0,30): if ui.tableWidget.item(i,1) == None: #股票名称 ui.tableWidget.setItem(i,0,QTableWidgetItem(str(name))) #股票代码 ui.tableWidget.setItem(i,1, QTableWidgetItem(codelist)) #持仓数量 ui.tableWidget.setItem(i,2,QTableWidgetItem(number)) #交易价格 ui.tableWidget.setItem(i,3, QTableWidgetItem(buyprice)) #总份额 ui.tableWidget.setItem(i,7, QTableWidgetItem(str(endprice))) #手续费 ui.tableWidget.setItem(i,8,QTableWidgetItem(str(float(buyprice)*float(number)*float(shouxufei)*0.01))) break else: if codelist == ui.tableWidget.item(i,1).text(): # 持仓数量 backnumber = ui.tableWidget.item(i,2).text() allnumber = float(number)+float(backnumber) ui.tableWidget.setItem(i,2,QTableWidgetItem(str(allnumber))) # 交易价格 backprice = ui.tableWidget.item(i,3).text() allprice = (float(backprice)*float(backnumber)+float(number)*float(buyprice))/(float(backnumber)+float(number)) ui.tableWidget.setItem(i,3,QTableWidgetItem(str(allprice))) # 总份额 backendprice = ui.tableWidget.item(i,7).text() allendprice = float(backendprice)+float(number)*float(buyprice) ui.tableWidget.setItem(i,7,QTableWidgetItem(str(allendprice))) # 手续费 backshouxufei = ui.tableWidget.item(i,8).text() allshouxufei = float(backshouxufei)+float(buyprice) * float(number) * float(shouxufei) * 0.01 ui.tableWidget.setItem(i, 8, QTableWidgetItem(str(allshouxufei))) break
卖出模块同理,不再展示代码。程序还提供了显示了个人资产的总市值,总资产,以及每次买卖股票所得到的盈亏金额与盈亏率,以及单位净值等基本功能,详细可见下图
此外程序提供了每日打开程序后的更新模块即导入之前输入的数据,可直观看出目前持有的股票与当前每个股票的盈亏金额,代码如下。
def gengxin(): data = ui.lineEdit_10.text() zongfene = 0 zongshizhi = 0 for i in range(1,30): if ui.tableWidget.item(i,1) == None: break else: codelist = ui.tableWidget.item(i,1).text() lastclose = ui.tableWidget.item(i,3).text() lastnumber = ui.tableWidget.item(i,2).text() #计算总份额 fene = ui.tableWidget.item(i,7).text() zongfene =float(zongfene) + float(fene) zongfene = round(zongfene,2) #获取股票现价 ts.set_token('184fedac133c785faa3197981f94ef25b8b34047471c081bdb6323c8') pro = ts.pro_api() df = pro.daily(ts_code=codelist, trade_date=data) close = df.iloc[0,5] #现价 ui.tableWidget.setItem(i, 5, QTableWidgetItem(str(close))) #计算股票总市值(现价) shizhi = float(close)*float(lastnumber) zongshizhi =float(zongshizhi) + float(shizhi) zongshizhi = round(zongshizhi,2) #盈亏金额 yingkui = (float(close)-float(lastclose))*float(lastnumber) yingkui =round(yingkui,3) ui.tableWidget.setItem(i,4,QTableWidgetItem(str(yingkui))) #盈亏率 yinglv = 100 * yingkui / (float(lastclose) * float(lastnumber)) yinglv = round(yinglv,2) ui.tableWidget.setItem(i,6,QTableWidgetItem(str(yinglv))) ui.lineEdit.setText(str(zongfene)) ui.lineEdit.setReadonly(True) ui.lineEdit_15.setText(str(zongshizhi)) ui.lineEdit_15.setReadonly(True) #总资产=总市值+卖出盈利 yingli = ui.lineEdit_2.text() zongzichan = float(zongshizhi)+float(yingli) ui.lineEdit_14.setText(str(zongzichan)) ui.lineEdit_14.setReadonly(True) #单位净值 jingzhi = float(zongshizhi)/float(zongfene) jingzhi = round(jingzhi,2) ui.lineEdit_13.setText(str(jingzhi)) ui.lineEdit_13.setReadonly(True)
小程序没有打包,只是用来进行学习,完整代码如下,仅包括了主程序未放置qt desigin代码,需要可自行查看
from a import * import tushare as ts import datetime from PyQt5.QtWidgets import * from b import * import xlwt import xlrd if __name__ == '__main__': app = QApplication(sys.argv) MainWindow = QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) # 实例化子窗口 child = QDialog() child_ui = Ui_Dialog() child_ui.setupUi(child) ts.set_token('184fedac133c785faa3197981f94ef25b8b34047471c081bdb6323c8') pro = ts.pro_api() def start(): data = (datetime.datetime.now() - datetime.timedelta(days=1)) data = data.strftime("%Y%m%d") ui.lineEdit_10.setText(data) #ui.lineEdit_10.setReadonly(True) #请输入手续费 shouxufei = ui.lineEdit_9.text() return data,shouxufei ui.pushButton.clicked.connect(start) def queren1(): #显示当天买入价格 #data,shouxufei = start() data = ui.lineEdit_10.text() codelist = ui.lineEdit_3.text() number = ui.lineEdit_5.text() df = pro.daily(ts_code=codelist, trade_date=data) close = df.iloc[0,5] k = pro.stock_basic(ts_code=codelist, fields='ts_code,name,area,industry,list_date') name = k.iloc[0, 1] ui.lineEdit_4.setText(str(close)) ui.lineEdit_11.setText(str(name)) ui.pushButton_6.clicked.connect(queren1) def queren2(): #显示当天的卖出价格 #data,shouxufei = start() data = ui.lineEdit_10.text() codelist1 = ui.lineEdit_6.text() number1 = ui.lineEdit_8.text() codelist1 = str(codelist1) df = pro.daily(ts_code=codelist1, trade_date=data) close = df.iloc[0, 5] k = pro.stock_basic(ts_code=codelist1, fields='ts_code,name,area,industry,list_date') name = k.iloc[0, 1] ui.lineEdit_7.setText(str(close)) ui.lineEdit_12.setText(str(name)) ui.pushButton_7.clicked.connect(queren2) def gengxin(): data = ui.lineEdit_10.text() zongfene = 0 zongshizhi = 0 for i in range(1,30): if ui.tableWidget.item(i,1) == None: break else: codelist = ui.tableWidget.item(i,1).text() lastclose = ui.tableWidget.item(i,3).text() lastnumber = ui.tableWidget.item(i,2).text() #计算总份额 fene = ui.tableWidget.item(i,7).text() zongfene =float(zongfene) + float(fene) zongfene = round(zongfene,2) #获取股票现价 ts.set_token('184fedac133c785faa3197981f94ef25b8b34047471c081bdb6323c8') pro = ts.pro_api() df = pro.daily(ts_code=codelist, trade_date=data) close = df.iloc[0,5] #现价 ui.tableWidget.setItem(i, 5, QTableWidgetItem(str(close))) #计算股票总市值(现价) shizhi = float(close)*float(lastnumber) zongshizhi =float(zongshizhi) + float(shizhi) zongshizhi = round(zongshizhi,2) #盈亏金额 yingkui = (float(close)-float(lastclose))*float(lastnumber) yingkui =round(yingkui,3) ui.tableWidget.setItem(i,4,QTableWidgetItem(str(yingkui))) #盈亏率 yinglv = 100 * yingkui / (float(lastclose) * float(lastnumber)) yinglv = round(yinglv,2) ui.tableWidget.setItem(i,6,QTableWidgetItem(str(yinglv))) ui.lineEdit.setText(str(zongfene)) ui.lineEdit.setReadonly(True) ui.lineEdit_15.setText(str(zongshizhi)) ui.lineEdit_15.setReadonly(True) #总资产=总市值+卖出盈利 yingli = ui.lineEdit_2.text() zongzichan = float(zongshizhi)+float(yingli) ui.lineEdit_14.setText(str(zongzichan)) ui.lineEdit_14.setReadonly(True) #单位净值 jingzhi = float(zongshizhi)/float(zongfene) jingzhi = round(jingzhi,2) ui.lineEdit_13.setText(str(jingzhi)) ui.lineEdit_13.setReadonly(True) ui.pushButton_5.clicked.connect(gengxin) def jisuan(): # 分红送股 n = ui.lineEdit_16.text() shuliang = ui.lineEdit_5.text() jiage = ui.lineEdit_4.text() newshuliang = float(shuliang)*(float(10)+float(n))/float(10) newjiage = float(jiage)*float(10)/(float(10)+float(n)) ui.lineEdit_18.setText(str(newshuliang)) ui.lineEdit_17.setText(str(newjiage)) ui.pushButton_8.clicked.connect(jisuan) #buy模块 def buy(): codelist = ui.lineEdit_3.text() number = ui.lineEdit_5.text() buyprice = ui.lineEdit_4.text() shouxufei = ui.lineEdit_9.text() endprice =float(buyprice)*float(number) k = pro.stock_basic(ts_code=codelist, fields='ts_code,name,area,industry,list_date') name = k.iloc[0,1] for i in range(0,30): if ui.tableWidget.item(i,1) == None: #股票名称 ui.tableWidget.setItem(i,0,QTableWidgetItem(str(name))) #股票代码 ui.tableWidget.setItem(i,1, QTableWidgetItem(codelist)) #持仓数量 ui.tableWidget.setItem(i,2,QTableWidgetItem(number)) #交易价格 ui.tableWidget.setItem(i,3, QTableWidgetItem(buyprice)) #总份额 ui.tableWidget.setItem(i,7, QTableWidgetItem(str(endprice))) #手续费 ui.tableWidget.setItem(i,8,QTableWidgetItem(str(float(buyprice)*float(number)*float(shouxufei)*0.01))) break else: if codelist == ui.tableWidget.item(i,1).text(): # 持仓数量 backnumber = ui.tableWidget.item(i,2).text() allnumber = float(number)+float(backnumber) ui.tableWidget.setItem(i,2,QTableWidgetItem(str(allnumber))) # 交易价格 backprice = ui.tableWidget.item(i,3).text() allprice = (float(backprice)*float(backnumber)+float(number)*float(buyprice))/(float(backnumber)+float(number)) ui.tableWidget.setItem(i,3,QTableWidgetItem(str(allprice))) # 总份额 backendprice = ui.tableWidget.item(i,7).text() allendprice = float(backendprice)+float(number)*float(buyprice) ui.tableWidget.setItem(i,7,QTableWidgetItem(str(allendprice))) # 手续费 backshouxufei = ui.tableWidget.item(i,8).text() allshouxufei = float(backshouxufei)+float(buyprice) * float(number) * float(shouxufei) * 0.01 ui.tableWidget.setItem(i, 8, QTableWidgetItem(str(allshouxufei))) break ui.pushButton_3.clicked.connect(buy) def sell(): shouxufei = ui.lineEdit_9.text() codelist1 = ui.lineEdit_6.text() number1 = ui.lineEdit_8.text() sellprice = ui.lineEdit_7.text() for i in range(1,30): if codelist1 == ui.tableWidget.item(i,1).text(): number = ui.tableWidget.item(i,2).text() if float(number1) > float(number): child.show() else: # 持仓数量 ui.tableWidget.setItem(i, 2, QTableWidgetItem(str(float(number)-float(number1)))) # 总份额 backendprice = ui.tableWidget.item(i,7).text() price = ui.tableWidget.item(i, 3).text() allendprice = float(backendprice) - float(number1) * float(price) ui.tableWidget.setItem(i,7, QTableWidgetItem(str(allendprice))) # 手续费 backshouxufei = ui.tableWidget.item(i,8).text() allshouxufei = float(backshouxufei) + float(sellprice) * float(number1) * float(shouxufei) * 0.01 ui.tableWidget.setItem(i,8,QTableWidgetItem(str(allshouxufei))) #总盈利 zongyingli = float(number1)*(float(sellprice)-float(price))*float(ui.tableWidget.item(i,3).text())-float(allshouxufei) if len(ui.lineEdit_2.text()) == 0: ui.lineEdit_2.setText(str(zongyingli)) else: backzongyingli = ui.lineEdit_2.text() ui.lineEdit_2.setText(str(float(zongyingli)+float(backzongyingli))) ui.lineEdit_2.setReadonly(True) break ui.pushButton_4.clicked.connect(sell) def baocun(): qian = ui.lineEdit_2.text() workbook = xlwt.Workbook(encoding='utf-8') sheet = workbook.add_sheet('数据') sheet.write(0,0,qian) for i in range(1,30): for j in range(0,9): if ui.tableWidget.item(i,j) == None: continue else: shuju = ui.tableWidget.item(i,j).text() sheet.write(i,j,shuju) workbook.save('shuju.xls') ui.pushButton_2.clicked.connect(baocun) def duqu(): excel_path = "shuju.xls" excel = xlrd.open_workbook(excel_path, encoding_override="utf-8") all_sheet = excel.sheets() for sheet in all_sheet: qian = sheet.cell_value(0,0) sheet_row_mount = sheet.nrows#行 sheet_col_mount = sheet.ncols#列 ui.lineEdit_2.setText(str(qian)) for i in range(1,sheet_row_mount): for j in range(0,sheet_col_mount): if sheet.cell_value(i,j) == None: continue else: shuju = sheet.cell_value(i,j) ui.tableWidget.setItem(i,j, QTableWidgetItem(str(shuju))) ui.pushButton_9.clicked.connect(duqu) MainWindow.show() sys.exit(app.exec_())
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)