def count_cur_yield(self): """ 计算实时股票收益曲线 计算方式: 1、按照每日收盘价进行买卖操作 2、平均持股,单个股票的上涨对应总市值比例为 单只股票上涨幅度/当前总股票数 3、每日收益累加中复利按天计算,没有考虑单支股票的复利 """ rate_lst = list() if self.date_begin == self.date_end: LOG.info('收益曲线开始日期等于结束日期') return np.array(0), np.array(0) date_lst = self.ds_tushare.get_trade_cal(self.date_begin, self.date_end) code_lst_pre = self.ds_tushare.get_cur_stock_pool_code_lst( self.date_begin) for item_date in date_lst: pct_chg_lst = list() for item_code_pre in code_lst_pre: dic_price = self.ds_tushare.get_stock_price_info( item_code_pre, item_date) pct_chg = 0.0 if dic_price is None else dic_price[ 'pct_chg'] # 股票停牌则涨幅为0 pct_chg_lst.append(pct_chg) rate_lst.append(np.mean(pct_chg_lst)) code_lst_pre = self.ds_tushare.get_cur_stock_pool_code_lst( item_date) if len(rate_lst) != 0: rate_lst[0] = 0.0 #第一天的收盘价建仓,初始设置为0 rate_lst_ret = self.cnt_compound_interest(rate_lst) return np.array(date_lst), np.array(rate_lst_ret)
def count_daily_yield(self, date): """ 计算每日股票收益曲线 计算方式: 1、按照每日收盘价进行买卖操作 2、卖出股票对应份额后,持有现金 3、平均持股,单个股票的上涨对应总市值比例为 单只股票上涨幅度/建仓日的总股票数 4、每日收益累加中复利按天计算,没有考虑单支股票的复利 """ lst_daily_code = self.ds_tushare.get_daily_stock_pool(date) lst_trade_cal = self.ds_tushare.get_trade_cal(date) lst_yield = list() hold_stock = len(lst_daily_code) # 该日建仓的总持股数量 for item_date in lst_trade_cal: pct_daily = 0.0 for item_daily in lst_daily_code: if item_daily['date_sell'] is None or item_daily[ 'date_sell'] >= item_date: dic_price = self.ds_tushare.get_stock_price_info( item_daily['ts_code'], item_date) pct_chg = 0.0 if dic_price is None else dic_price[ 'pct_chg'] / float(hold_stock) pct_daily += pct_chg lst_yield.append(pct_daily) LOG.info(lst_yield) if len(lst_yield) != 0: lst_yield[0] = 0.0 #第一天的收盘价建仓,初始涨幅设置为0 rate_lst_ret = self.cnt_compound_interest(lst_yield) return np.array(lst_trade_cal), np.array(rate_lst_ret)
def plot_yield_curve_union(self, x, y, line_name): # 图像叠加绘制 LOG.info(x) LOG.info(y) tick_spacing = math.ceil(x.size / 5) # 横坐标显示5个日期数字 tick_spacing = tick_spacing if tick_spacing != 0 else 1 self.axes.plot(x, y, label=line_name) self.axes.legend() self.axes.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
def plot_yield_curve_cover(self, x, y): # 图像重绘 LOG.info(x) LOG.info(y) self.axes.cla() tick_spacing = math.ceil(x.size / 5) # 横坐标显示5个日期数字 tick_spacing = tick_spacing if tick_spacing != 0 else 1 self.axes.plot(x, y) self.axes.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing)) self.fig.canvas.draw() # 这里注意是画布重绘,self.figs.canvas self.fig.canvas.flush_events() # 画布刷新self.figs.canvas
def on_pushButton_oper_clicked(self): content = self.__ui.textEdit_buy.toPlainText() content_sell = self.__ui.textEdit_sell.toPlainText() date = self.__ui.dateEdit_oper.date() lst_code = content.split(',') lst_code_sell = content_sell.split(',') lst_code_transfer = list() lst_code_transfer_sell = list() code_error = False if content == '': lst_code = [] if content_sell == '': lst_code_sell = [] for item in lst_code: if item[0] in ['0', '3']: item += '_SZ' else: item += '_SH' if item not in self.ds_tushare.get_stock_list(): LOG.info('stock code error! %s' % item) code_error = True self.show_warning_message() break lst_code_transfer.append(item) for item in lst_code_sell: if item[0] in ['0', '3']: item += '_SZ' else: item += '_SH' if item not in self.ds_tushare.get_stock_list(): LOG.info('stock code error! %s' % item) code_error = True self.show_warning_message() break lst_code_transfer_sell.append(item) if code_error is False: self.ds_tushare.daily_stock_pool_in_db(lst_code_transfer, date.toString("yyyyMMdd")) self.ds_tushare.cur_stock_pool_in_db(lst_code_transfer, date.toString("yyyyMMdd")) self.ds_tushare.set_daily_stock_pool(lst_code_transfer_sell, date.toString("yyyyMMdd")) self.ds_tushare.del_cur_stock_pool(lst_code_transfer_sell, date.toString("yyyyMMdd")) self.__ui.textEdit_buy.clear() self.__ui.textEdit_sell.clear() self.set_table_widget_stock_hold()
def set_table_widget_stock_hold(self): date_lst = self.ds_tushare.get_cur_stock_pool_date_lst() if self.date_begin == self.date_end or len(date_lst) == 0: LOG.info('收益曲线开始日期等于结束日期') return lst_hold_stock = self.ds_tushare.get_cur_stock_pool_code_lst( date_lst[-1]) date_lst.reverse() for trade_date in date_lst: stock_daily_lst = self.ds_tushare.get_daily_stock_pool(trade_date) for item_daily in stock_daily_lst: if item_daily['ts_code'] not in lst_hold_stock: lst_hold_stock.append(item_daily['ts_code']) self.__ui.tableWidget_cur.clearContents() self.__ui.tableWidget_cur.setRowCount(len(lst_hold_stock)) self.__ui.tableWidget_cur.setColumnCount(4) line_idx = 0 for code in lst_hold_stock: self.__ui.tableWidget_cur.setItem(line_idx, 0, QTableWidgetItem(code)) stock_daily = self.ds_tushare.get_daily_stock(code) if stock_daily is not None: self.__ui.tableWidget_cur.setItem( line_idx, 1, QTableWidgetItem(stock_daily['date_buy'])) date_sell = stock_daily['date_sell'] if stock_daily[ 'date_sell'] is not None else None self.__ui.tableWidget_cur.setItem(line_idx, 2, QTableWidgetItem(date_sell)) k_data_buy = self.ds_tushare.get_stock_price_info( code, stock_daily['date_buy']) if stock_daily['date_sell'] is None: k_data_sell = self.ds_tushare.get_stock_price_info_last( code) else: k_data_sell = self.ds_tushare.get_stock_price_info( code, stock_daily['date_sell']) if k_data_sell is not None and k_data_buy is not None: profit = (k_data_sell['close'] - k_data_buy['close']) / k_data_buy['close'] else: profit = 0 self.__ui.tableWidget_cur.setItem( line_idx, 3, QTableWidgetItem("%.4f" % (100 * (profit)))) line_idx += 1
def count_index_yield(self, code_index): """ 计算指数收益曲线 code_index: 指数代码 ['000001_SH', '399001_SZ', '399005_SZ', '399006_SZ'] """ if self.date_begin == self.date_end: LOG.info('收益曲线开始日期等于结束日期') return np.array(0), np.array(0) value_index_lst = list() index_lst = self.ds_tushare.get_stock_price_lst( code_index, self.date_begin, self.date_end) for item_index in index_lst: value_index_lst.append(item_index['close']) rate_lst_ret = list() for item_index_value in value_index_lst: rate_lst_ret.append(item_index_value / value_index_lst[0]) date_lst = self.ds_tushare.get_trade_cal(self.date_begin, self.date_end) return np.array(date_lst), np.array(rate_lst_ret)
lst_close.append(item_price['close']) lst_pct_chg_code.append(item_price['pct_chg']) arr_pct_chg_code = np.array(lst_pct_chg_code) arr_pct_chg_code = np.roll(arr_pct_chg_code, -1) arr_pct_chg_code = (arr_pct_chg_code > 0.0).astype(int) lst_ma5.pop() lst_ma10.pop() lst_ma20.pop() lst_ma30.pop() lst_ma60.pop() lst_ma120.pop() lst_close.pop() try: arr_pct_chg_code = np.delete(arr_pct_chg_code, -1) except: LOG.info(arr_pct_chg_code) lst_pct_chg += list(arr_pct_chg_code) arr_ma5 = np.array(lst_ma5) arr_ma10 = np.array(lst_ma10) arr_ma20 = np.array(lst_ma20) arr_ma30 = np.array(lst_ma30) arr_ma60 = np.array(lst_ma60) arr_ma120 = np.array(lst_ma120) arr_close = np.array(lst_close) arr_ma5_sub_close = (arr_close - arr_ma5) * 100 / arr_close arr_ma10_sub_close = (arr_close - arr_ma10) * 100 / arr_close arr_ma20_sub_close = (arr_close - arr_ma20) * 100 / arr_close arr_ma30_sub_close = (arr_close - arr_ma30) * 100 / arr_close arr_ma60_sub_close = (arr_close - arr_ma60) * 100 / arr_close arr_ma120_sub_close = (arr_close - arr_ma120) * 100 / arr_close arr_pct_chg = np.array(lst_pct_chg)
def mousePressEvent(self, event): # if(event->button() == Qt::RightButton) LOG.info('BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB')
def mousePressEvent(self, event): LOG.info('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA')