def gen_hour_macd_values(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') df_30 = get_k_data_JQ(stk_code, count=120, end_date=add_date_str(get_current_date_str(), 1), freq='30m') df_60 = get_k_data_JQ(stk_code, count=120, 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() if str(df_60.index[-1]) > get_current_datetime_str(): df_30 = df_30[:-1] df_60 = df_60[:-1] return df_30, df_60
# encoding=utf-8
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