# encoding=utf-8
def gen_Hour_MACD_Pic(stk_code, source='jq', title=''): # 生成小时macd数据 df_30, df_60 = gen_hour_macd_values(stk_code, source=source, title=title) # 根据情况设置背景色 attention = False m30 = df_30.tail(3)['MACD'].values m60 = df_60.tail(3)['MACD'].values if (m30[1] == np.min(m30)) | (m60[1] == np.min(m60)): # 设置背景红 set_background_color('b_r') elif (m30[1] == np.max(m30)) | (m60[1] == np.max(m60)): # 设置背景绿 set_background_color('b_g') else: set_background_color() fig, ax = plt.subplots(ncols=1, nrows=4) ax[0].plot(range(0, len(df_30)), df_30['close'], 'g*--', label='close_30min') ax[1].bar(range(0, len(df_30)), df_30['MACD'], label='MACD_30min') ax[2].plot(range(0, len(df_60)), df_60['close'], 'g*--', label='close_60min') ax[3].bar(range(0, len(df_60)), df_60['MACD'], label='MACD_60min') # 设置下标 ax[1] = addXticklabel_list( ax[1], list([str(x)[-11:-3] for x in df_30['datetime']]), 30, rotation=45) ax[3] = addXticklabel_list( ax[3], list([str(x)[-11:-3] for x in df_60['datetime']]), 30, rotation=45) # ax[3].set_xticks(list(range(0, len(df_60['datetime'])))) # ax[3].set_xticklabels(list([str(x)[-11:-3] for x in df_60['datetime']]), rotation=45) for ax_sig in ax: ax_sig.legend(loc='best') # 设置标题 if m30[1] == np.min(m30): ax[0].set_title(stk_code + '半小时MACD低点!') elif m60[1] == np.min(m60): ax[0].set_title(stk_code + '小时MACD低点!') elif m30[1] == np.max(m30): ax[0].set_title(stk_code + '半小时MACD高点!') elif m60[1] == np.max(m60): ax[0].set_title(stk_code + '小时MACD高点!') else: ax[0].set_title(stk_code) fig.tight_layout() plt.subplots_adjust(wspace=0, hspace=1) # 调整子图间距 # plt.close() return fig
def gen_Day_Pic(stk_df, stk_code=''): """ 函数功能:给定stk的df,已经确定stk当前处于拐点状态,需要将当前stk的信息打印成图片,便于人工判断! :param stk_df 从tushare下载下来的原生df :param root_save_dir 配置文件中定义的存储路径 :return: 返回生成图片的路径 """ """ 规划一下都画哪些图 1、该stk整体走势,包括60日均线、20日均线和收盘价 2、stk近几天的MACD走势 """ """ 在原数据的基础上增加均线和MACD """ # 按升序排序 stk_df = stk_df.sort_values(by='date', ascending=True) stk_df['M20'] = stk_df['close'].rolling(window=20).mean() stk_df['M60'] = stk_df['close'].rolling(window=60).mean() stk_df['MACD'], stk_df['MACDsignal'], stk_df['MACDhist'] = talib.MACD(stk_df.close, fastperiod=12, slowperiod=26, signalperiod=9) # 检查日级别的MACD是否有异常 attention = False MACD_list = stk_df.tail(3)['MACD'].values if MACD_list[1] == np.min(MACD_list): attention = True # 设置背景红 set_background_color('b_r') elif MACD_list[1] == np.max(MACD_list): attention = True # 设置背景绿 set_background_color('b_g') else: set_background_color() fig, ax = plt.subplots(nrows=4, ncols=1) ax[0].plot(range(0, len(stk_df['date'])), stk_df['M20'], 'b--', label='20日均线', linewidth=1) ax[0].plot(range(0, len(stk_df['date'])), stk_df['M60'], 'r--', label='60日均线', linewidth=1) ax[0].plot(range(0, len(stk_df['date'])), stk_df['close'], 'g*--', label='收盘价', linewidth=0.5, markersize=1) ax[1].bar(range(0, len(stk_df['date'])), stk_df['MACD'], label='MACD') # 准备下标 xticklabels_all_list = list(stk_df['date'].sort_values(ascending=True)) xticklabels_all_list = [x.replace('-', '')[2:] for x in xticklabels_all_list] for ax_sig in ax[0:2]: ax_sig = addXticklabel_list(ax_sig, xticklabels_all_list, 30, rotation=45) ax_sig.legend(loc='best', fontsize=5) # 画出最近几天的情况(均线以及MACD) stk_df_current = stk_df.tail(plot_current_days_amount) ax[2].plot(range(0, len(stk_df_current['date'])), stk_df_current['M20'], 'b--', label='20日均线', linewidth=2) ax[2].plot(range(0, len(stk_df_current['date'])), stk_df_current['M60'], 'r--', label='60日均线', linewidth=2) ax[2].plot(range(0, len(stk_df_current['date'])), stk_df_current['close'], 'g*-', label='收盘价', linewidth=1, markersize=5) ax[3].bar(range(0, len(stk_df_current['date'])), stk_df_current['MACD'], label='MACD') # 设置标题并返回分析结果 result_analysis = [] if MACD_list[1] == np.min(MACD_list): title_tmp = stk_code + ' ' + code2name(stk_code) + ' 日级别 MACD 低点!后续数天可能上涨!' ax[0].set_title(title_tmp) result_analysis.append(title_tmp) elif MACD_list[1] == np.max(MACD_list): title_tmp = stk_code + ' ' + code2name(stk_code) + ' 日级别 MACD 高点!后续数天可能下跌!' ax[0].set_title(title) result_analysis.append(title_tmp) # 准备下标 xticklabels_all_list = list(stk_df_current['date'].sort_values(ascending=True)) xticklabels_all_list = [x.replace('-', '')[2:] for x in xticklabels_all_list] for ax_sig in ax[2:4]: ax_sig = addXticklabel_list(ax_sig, xticklabels_all_list, 30, rotation=45) ax_sig.legend(loc='best', fontsize=5) fig.tight_layout() # 调整整体空白 plt.subplots_adjust(wspace=0, hspace=1) # 调整子图间距 # plt.close() return fig, ax, attention, result_analysis
def gen_hour_macd_pic(stk_data, debug=False, stk_code=''): if debug: print('进入GenPic.gen_hour_macd_pic函数!') debug_print_txt('macd_hour_pic', stk_code, '\n----------------------\n\n', enable=debug) # 生成小时macd数据 # df_30, df_60 = gen_hour_macd_values(stk_code, source=source, title=title) df_30, df_60 = stk_data if debug: debug_print_txt('macd_hour_pic', stk_code, 'df_30原始数据:\n' + str(df_30) + '\n\n' + 'df_60原始数据:\n' + str(df_60) + '\n\n', enable=debug) # 根据情况设置背景色 m30 = df_30.tail(3)['MACD'].values m60 = df_60.tail(3)['MACD'].values if debug: debug_print_txt( 'macd_hour_pic', stk_code, 'm30原始数据:\n' + str(m30) + '\n\n' + 'm60原始数据:\n' + str(m60) + '\n\n') if (m30[1] == np.min(m30)) | (m60[1] == np.min(m60)): # 设置背景红 GenPic.set_background_color('b_r') elif (m30[1] == np.max(m30)) | (m60[1] == np.max(m60)): # 设置背景绿 GenPic.set_background_color('b_g') else: GenPic.set_background_color() # 调整显示长度 df_30 = df_30.tail(40) df_60 = df_60.tail(40) fig, ax = plt.subplots(ncols=1, nrows=4) ax[0].plot(range(0, len(df_30)), df_30['close'], 'g*--', label='close_30min') ax[1] = GenPic.plot_macd(ax[1], df_30, '30min') ax[2].plot(range(0, len(df_60)), df_60['close'], 'g*--', label='close_60min') ax[3] = GenPic.plot_macd(ax[3], df_60, '60min') # 设置下标 ax[1] = addXticklabel_list( ax[1], list([str(x)[-9:-3] for x in df_30['datetime']]), 15, rotation=0, fontsize=6) ax[3] = addXticklabel_list( ax[3], list([str(x)[-9:-3] for x in df_60['datetime']]), 15, rotation=0, fontsize=6) for ax_sig in ax: ax_sig.legend(loc='best') # 设置默认标题 title = stk_code + ' ' + code2name(stk_code) + '/上次更新时间:%s' % str( get_current_datetime_str()) # 设置标题 if m30[1] == np.min(m30): title = stk_code + '半小时MACD低点!' elif m60[1] == np.min(m60): title = stk_code + '小时MACD低点!' elif m30[1] == np.max(m30): title = stk_code + '半小时MACD高点!' elif m60[1] == np.max(m60): title = stk_code + '小时MACD高点!' ax[0].set_title(title) if debug: debug_print_txt('macd_hour_pic', stk_code, '结论:' + title + '\n\n') fig.tight_layout() plt.subplots_adjust(wspace=0, hspace=0.15) # 调整子图间距 return fig
def gen_Hour_MACD_Pic(stk_code, source='jq', title=''): if source == 'jq': df_30 = get_k_data_JQ(stk_code, start_date=add_date_str(get_current_date_str(), -20), end_date=add_date_str(get_current_date_str(), 1), freq='30m') df_60 = get_k_data_JQ(stk_code, start_date=add_date_str(get_current_date_str(), -20), end_date=add_date_str(get_current_date_str(), 1), freq='60m') elif source == 'ts': df_30 = my_pro_bar(stk_code, start=add_date_str(get_current_date_str(), -20), freq='30min') df_60 = my_pro_bar(stk_code, start=add_date_str(get_current_date_str(), -20), freq='60min') # 去掉volume为空的行 df_30 = df_30.loc[df_30.apply(lambda x: not (x['volume'] == 0), axis=1), :] df_60 = df_60.loc[df_60.apply(lambda x: not (x['volume'] == 0), axis=1), :] df_30['MACD'], _, _ = talib.MACD(df_30.close, fastperiod=12, slowperiod=26, signalperiod=9) df_60['MACD'], _, _ = talib.MACD(df_60.close, fastperiod=12, slowperiod=26, signalperiod=9) # 生成图片 df_30 = df_30.dropna() df_60 = df_60.dropna() fig, ax = plt.subplots(ncols=1, nrows=4) ax[0].plot(range(0, len(df_30)), df_30['close'], 'g*--', label='close_30min') ax[1].bar(range(0, len(df_30)), df_30['MACD'], label='MACD_30min') ax[2].plot(range(0, len(df_60)), df_60['close'], 'g*--', label='close_60min') ax[3].bar(range(0, len(df_60)), df_60['MACD'], label='MACD_60min') # 设置下标 ax[1] = addXticklabel_list( ax[1], list([str(x)[-11:-3] for x in df_30['datetime']]), 30, rotation=45) ax[3].set_xticks(list(range(0, len(df_60['datetime'])))) ax[3].set_xticklabels(list([str(x)[-11:-3] for x in df_60['datetime']]), rotation=45) for ax_sig in ax: ax_sig.legend(loc='best') plt.title(stk_code + title) fig.tight_layout() plt.subplots_adjust(wspace=0, hspace=1) # 调整子图间距 # plt.close() return fig