def plot(self, m): """ 图示m线概况 :param m: :return: """ self.add_average_line(m) self.add_average_pn(m) self.add_idx() df = self.data df = df.reset_index().reset_index() df_p = df[df.apply(lambda x: x['m_pn_' + str(m)], axis=1)] df_n = df[df.apply(lambda x: not x['m_pn_' + str(m)], axis=1)] fig, ax = subplots(ncols=1, nrows=2) ax[0].plot(df['level_0'], df['close'], 'b*--', label='close') ax[0].plot(df_p['level_0'], df_p['close'], 'r*', label='close_m' + str(m) + '_p') ax[0].plot(df_n['level_0'], df_n['close'], 'g*', label='close_m' + str(m) + '_n') ax[0].legend(loc='best') ax[0] = add_axis(ax[0], df['datetime'], 60, fontsize=5, rotation=90) ax[1].bar(df['level_0'], df['MACD']) plt.show()
def plot_op_result(df): """ 打印BS操作效果 列名字: origin_money :不使用策略时的效益 strategy_money :使用策略时的效益 BS : 记录操作行为的列 money_remain : 剩余的资金 :param df: :return: """ sh_index = df fig, ax = plt.subplots(nrows=3, ncols=1) ax[0].plot(range(len(sh_index['date'])), sh_index['close'], 'b--') # 打印BS操作 tuple_bs = list(zip(range(len(sh_index['date'])), sh_index['BS'], sh_index['close'])) tuple_b = list(filter(lambda x: x[1] == 'buy', tuple_bs)) ax[0].plot(list(map(lambda x: x[0], tuple_b)), list(map(lambda x: x[2], tuple_b)), 'r*', label='buy') tuple_s = list(filter(lambda x: x[1] == 'sale', tuple_bs)) ax[0].plot(list(map(lambda x: x[0], tuple_s)), list(map(lambda x: x[2], tuple_s)), 'g*', label='sale') # 打印对比 ax[1].plot(range(len(sh_index['date'])), sh_index['origin_money'], 'b--', label='no_use_strategy') ax[1].plot(range(len(sh_index['date'])), sh_index['strategy_money'], 'r--', label='use_strategy') # 打印stk数量和剩余money ax[2].plot(range(len(sh_index['date'])), normal01(sh_index['money_remain']), 'b--', label='剩余money') # ax[2].plot(range(len(sh_index['date'])), normal01(sh_index['amount_remain']), 'r--', label='剩余stk数量') # 整理x轴label x_label = sh_index.apply(lambda x: str(x['date'])[2:].replace('-', ''), axis=1) ax[0] = add_axis(ax[0], x_label, 40, rotation=45, fontsize=8) ax[1] = add_axis(ax[1], x_label, 40, rotation=45, fontsize=8) ax[2] = add_axis(ax[2], x_label, 40, rotation=45, fontsize=8) # ax[3] = addXticklabel(ax[3], x_label, 40, rotation=45, fontsize=8) for ax_sig in ax: ax_sig.legend(loc='best') plt.show()
def plot(self): if self.debug: print('开始可视化结果...') df = self.data_minute.dropna().reset_index(drop=True) df['stk_money_ratio'] = df.apply( lambda x: x['stk_amount'] * x['close'] / (x['money_total']), axis=1) # 筛选买卖点 df_b = df[df['opt_result'] == 'buy'] df_s = df[df['opt_result'] == 'sale'] df_opt = df[df.apply(lambda x: (x['opt_result'] == 'buy') | (x['opt_result'] == 'sale'), axis=1)] fig, ax = plt.subplots(ncols=1, nrows=4) ax[0].plot(df.index, df['close'], 'k*--', label='收盘价') ax[0].plot(df_opt.index, df_opt['close'], 'y-', label=u'买卖阶梯', linewidth=2.5) ax[0].plot(df_b.index, df_b['close'], 'g*', label='买入点', markersize=10) ax[0].plot(df_s.index, df_s['close'], 'r*', label='卖出点', markersize=10) # ax[0].legend(loc='best') # 展示收益比 c_origin = np.array(df['close']) ratio_origin = list((c_origin / c_origin[0])) c_reseau = np.array(df['money_total']) ratio_reseau = list((c_reseau / c_reseau[0])) ax[1].plot(df.index, ratio_origin, 'y--', label=u'不操作收益曲线') ax[1].plot(df.index, ratio_reseau, 'r--', label=u'网格收益曲线') # ax[1].legend(loc='best') ax[2].plot(df.index, df['stk_money_ratio'], 'b--', label=u'仓位') # ax[2].legend(loc='best') ax[3].plot(df.index, df['total_earn'], 'b--', label=u'网格收益') # 增加横轴 ax[3] = add_axis(ax[3], df['date'], 20, 8) for ax_ in ax: ax_.legend(loc='best') plt.show()
stk_code = '000001' sh_index = ts.get_k_data(code=stk_code,start='2014-01-01') sh_index['M10'] = sh_index['close'].rolling(window=10, center=False).mean() sh_index['C-M10'] = sh_index.apply(lambda x: x['close']-x['M10'], axis=1) sh_index['M20'] = sh_index['close'].rolling(window=20, center=False).mean() sh_index['C-M20'] = sh_index.apply(lambda x: x['close']-x['M20'], axis=1) sh_index = sh_index.dropna(how='any') # 展示C-M20的分布 count, bins = np.histogram(sh_index['C-M20'], 100, normed=True) plt.bar(range(len(count)), count) plt.show() fig, ax = plt.subplots(nrows=2, ncols=1) ax[0].plot(range(0, len(sh_index['date'])), sh_index['close'], 'r-', label='20日均线', linewidth=1) ax[1].plot(range(0, len(sh_index['date'])), sh_index['C-M20'], 'g*', label='C-20日均线', linewidth=1) ax[1].plot(range(0, len(sh_index['date'])), np.zeros(len(sh_index)), 'b', label='零线') # 准备下标 ax[0] = add_axis(ax[0], sh_index['date'], 40) ax[1] = add_axis(ax[1], sh_index['date'], 40) for ax_sig in ax: ax_sig.legend(loc='best') plt.show()
sh_index['M20'] = sh_index['close'].rolling(window=180, center=False).mean() sh_index['M5'] = sh_index['close'].rolling(window=30, center=False).mean() sh_index['M5-M20'] = sh_index.apply(lambda x: x['M5'] - x['M20'], axis=1) sh_index['MM9'] = sh_index['M5-M20'].rolling(window=5, center=False).mean() sh_index['MACD'] = sh_index.apply(lambda x: x['M5-M20'] - x['MM9'], axis=1) sh_index = sh_index.dropna(how='any') """ -------------------------------- 图示结果 ------------------------------------ """ fig, ax = plt.subplots(nrows=2, ncols=1) ax[0].plot(range(len(sh_index['date'])), sh_index['close'], 'b--') # ax[0].plot(range(len(sh_index['date'])), sh_index['M5-M20'], 'r--') ax[1].plot(range(len(sh_index['date'])), sh_index['MACD'], 'r--') # 整理x轴label x_label = sh_index.apply(lambda x: str(x['date'])[2:].replace('-', ''), axis=1) ax[0] = add_axis(ax[0], x_label, 40, rotation=45, fontsize=8) ax[1] = add_axis(ax[1], x_label, 40, rotation=45, fontsize=8) # ax[2] = addXticklabel(ax[2], x_label, 40, rotation=45, fontsize=8) # ax[3] = addXticklabel(ax[3], x_label, 40, rotation=45, fontsize=8) for ax_sig in ax: ax_sig.legend(loc='best') plt.show() end = 0
# encoding=utf-8
def gen_idx_pic(stk_df, stk_code=''): """ 打印常用指标 """ # 按升序排序 stk_df = stk_df.sort_values(by='date', ascending=True) """ 增加指标 'RSI5', 'RSI12', 'RSI30' 'SAR' 'slowk', 'slowd' 'upper', 'middle', 'lower' 'MOM' """ stk_df_ = add_stk_index_to_df(stk_df).tail(60) GenPic.set_background_color(bc='w') result_analysis = [] # 检查SAR attention = False sar_tail_origin = stk_df_.tail(2) sar_tail = sar_tail_origin.copy() sar_tail['compare'] = sar_tail_origin.apply( lambda x: x['SAR'] - x['close'], axis=1) title_tmp = stk_code + ' ' + code2name(stk_code) if sar_tail.head(1)['compare'].values[0] * sar_tail.tail( 1)['compare'].values[0] < 0: if sar_tail.tail(1)['SAR'].values[0] < sar_tail.tail( 1)['close'].values[0]: title_tmp = stk_code + ' ' + code2name( stk_code) + ' 注意 SAR 指标翻转,后续数天可能上涨!' result_analysis.append(title_tmp + RankNote.print_day_close_rank(stk_df)) GenPic.set_background_color(bc='b_r') else: title_tmp = stk_code + ' ' + code2name( stk_code) + ' 注意 SAR 指标翻转,后续数天可能下跌!' result_analysis.append(title_tmp + RankNote.print_day_close_rank(stk_df)) GenPic.set_background_color(bc='b_g') attention = True fig, ax = plt.subplots(nrows=5, ncols=1) ax[0].plot(range(0, len(stk_df_['date'])), stk_df_['RSI5'], 'b--', label='RSI5线', linewidth=1) ax[0].plot(range(0, len(stk_df_['date'])), stk_df_['RSI12'], 'r--', label='RSI12线', linewidth=1) ax[0].plot(range(0, len(stk_df_['date'])), stk_df_['RSI30'], 'g*--', label='RSI30', linewidth=0.5, markersize=1) ax[0].plot(range(0, len(stk_df_['date'])), [20 for a in range(len(stk_df_['date']))], 'b--', linewidth=0.3) ax[0].plot(range(0, len(stk_df_['date'])), [80 for a in range(len(stk_df_['date']))], 'b--', linewidth=0.3) ax[0].set_ylim(0, 100) ax[1].plot(range(0, len(stk_df_['date'])), stk_df_['SAR'], 'r--', label='SAR', linewidth=0.5, markersize=1) ax[1].plot(range(0, len(stk_df_['date'])), stk_df_['close'], 'g*--', label='close', linewidth=0.5, markersize=1) ax[2].plot(range(0, len(stk_df_['date'])), stk_df_['slowk'], 'g*--', label='slowk', linewidth=0.5, markersize=1) ax[2].plot(range(0, len(stk_df_['date'])), stk_df_['slowd'], 'r*--', label='slowd', linewidth=0.5, markersize=1) ax[2].plot(range(0, len(stk_df_['date'])), [20 for a in range(len(stk_df_['date']))], 'b--', linewidth=0.3) ax[2].plot(range(0, len(stk_df_['date'])), [80 for a in range(len(stk_df_['date']))], 'b--', linewidth=0.3) ax[2].set_ylim(0, 100) ax[3].plot(range(0, len(stk_df_['date'])), stk_df_['upper'], 'r*--', label='布林上线', linewidth=0.5, markersize=1) ax[3].plot(range(0, len(stk_df_['date'])), stk_df_['middle'], 'b*--', label='布林均线', linewidth=0.5, markersize=1) ax[3].plot(range(0, len(stk_df_['date'])), stk_df_['lower'], 'g*--', label='布林下线', linewidth=0.5, markersize=1) ax[4].plot(range(0, len(stk_df_['date'])), stk_df_['MOM'], 'g*--', label='MOM', linewidth=0.5, markersize=1) # 准备下标 xlabel_series = stk_df_.apply(lambda x: x['date'][2:].replace('-', ''), axis=1) ax[4] = add_axis(ax[4], xlabel_series, 40, rotation=45, fontsize=10) # 设置默认标题 title_tmp = stk_code + ' ' + code2name(stk_code) + '/上次更新时间:%s' % str( get_current_datetime_str()) ax[0].set_title(title_tmp) for ax_sig in ax: ax_sig.legend(loc='best', fontsize=5) fig.tight_layout() # 调整整体空白 plt.subplots_adjust(wspace=0, hspace=0) # 调整子图间距 ax[0].set_title(title_tmp) return fig, ax, attention, result_analysis
def genStkIdxPicForQQ(stk_df, stk_code=''): """ 打印常用指标 """ # 按升序排序 stk_df = stk_df.sort_values(by='date', ascending=True) """ 增加指标 'RSI5', 'RSI12', 'RSI30' 'SAR' 'slowk', 'slowd' 'upper', 'middle', 'lower' 'MOM' """ stk_df = add_stk_index_to_df(stk_df).tail(60) fig, ax = plt.subplots(nrows=5, ncols=1) ax[0].plot(range(0, len(stk_df['date'])), stk_df['RSI5'], 'b--', label='RSI5线', linewidth=1) ax[0].plot(range(0, len(stk_df['date'])), stk_df['RSI12'], 'r--', label='RSI12线', linewidth=1) ax[0].plot(range(0, len(stk_df['date'])), stk_df['RSI30'], 'g*--', label='RSI30', linewidth=0.5, markersize=1) ax[0].plot(range(0, len(stk_df['date'])), [20 for a in range(len(stk_df['date']))], 'b--', linewidth=0.3) ax[0].plot(range(0, len(stk_df['date'])), [80 for a in range(len(stk_df['date']))], 'b--', linewidth=0.3) ax[0].set_ylim(0, 100) ax[1].plot(range(0, len(stk_df['date'])), stk_df['SAR'], 'r--', label='SAR', linewidth=0.5, markersize=1) ax[1].plot(range(0, len(stk_df['date'])), stk_df['close'], 'g*--', label='close', linewidth=0.5, markersize=1) ax[2].plot(range(0, len(stk_df['date'])), stk_df['slowk'], 'g*--', label='slowk', linewidth=0.5, markersize=1) ax[2].plot(range(0, len(stk_df['date'])), stk_df['slowd'], 'r*--', label='slowd', linewidth=0.5, markersize=1) ax[2].plot(range(0, len(stk_df['date'])), [20 for a in range(len(stk_df['date']))], 'b--', linewidth=0.3) ax[2].plot(range(0, len(stk_df['date'])), [80 for a in range(len(stk_df['date']))], 'b--', linewidth=0.3) ax[2].set_ylim(0, 100) ax[3].plot(range(0, len(stk_df['date'])), stk_df['upper'], 'r*--', label='布林上线', linewidth=0.5, markersize=1) ax[3].plot(range(0, len(stk_df['date'])), stk_df['middle'], 'b*--', label='布林均线', linewidth=0.5, markersize=1) ax[3].plot(range(0, len(stk_df['date'])), stk_df['lower'], 'g*--', label='布林下线', linewidth=0.5, markersize=1) ax[4].plot(range(0, len(stk_df['date'])), stk_df['MOM'], 'g*--', label='MOM', linewidth=0.5, markersize=1) # 准备下标 xlabel_series = stk_df.apply(lambda x: x['date'][2:].replace('-', ''), axis=1) ax[0] = add_axis(ax[0], xlabel_series, 40, rotation=45) ax[1] = add_axis(ax[1], xlabel_series, 40, rotation=45) ax[2] = add_axis(ax[2], xlabel_series, 40, rotation=45) ax[3] = add_axis(ax[3], xlabel_series, 40, rotation=45) ax[4] = add_axis(ax[4], xlabel_series, 40, rotation=45) for ax_sig in ax: ax_sig.legend(loc='best', fontsize=5) fig.tight_layout() # 调整整体空白 plt.subplots_adjust(wspace=0, hspace=0) # 调整子图间距 # 检查SAR attention = False sar_tail = stk_df.tail(2) sar_tail['compare'] = sar_tail.apply(lambda x: x['SAR'] - x['close'], axis=1) if sar_tail.head(1)['compare'].values[0] * sar_tail.tail( 1)['compare'].values[0] < 0: plt.title(stk_code + ' 注意 SAR 指标异动!') attention = True return fig, ax, attention
def genStkIdxPic(stk_df, stk_code, current_date, root_save_dir, pic_name='stk_idx.png'): """ 打印常用指标 """ # 按升序排序 stk_df = stk_df.sort_values(by='date', ascending=True) """ 增加指标 'RSI5', 'RSI12', 'RSI30' 'SAR' 'slowk', 'slowd' 'upper', 'middle', 'lower' 'MOM' """ stk_df = add_stk_index_to_df(stk_df).tail(60) fig, ax = plt.subplots(nrows=5, ncols=1) ax[0].plot(range(0, len(stk_df['date'])), stk_df['RSI5'], 'b--', label='RSI5线', linewidth=1) ax[0].plot(range(0, len(stk_df['date'])), stk_df['RSI12'], 'r--', label='RSI12线', linewidth=1) ax[0].plot(range(0, len(stk_df['date'])), stk_df['RSI30'], 'g*--', label='RSI30', linewidth=0.5, markersize=1) ax[1].plot(range(0, len(stk_df['date'])), stk_df['SAR'], 'g*--', label='SAR', linewidth=0.5, markersize=1) ax[2].plot(range(0, len(stk_df['date'])), stk_df['slowk'], 'g*--', label='slowk', linewidth=0.5, markersize=1) ax[2].plot(range(0, len(stk_df['date'])), stk_df['slowd'], 'r*--', label='slowd', linewidth=0.5, markersize=1) ax[3].plot(range(0, len(stk_df['date'])), stk_df['upper'], 'r*--', label='布林上线', linewidth=0.5, markersize=1) ax[3].plot(range(0, len(stk_df['date'])), stk_df['middle'], 'b*--', label='布林均线', linewidth=0.5, markersize=1) ax[3].plot(range(0, len(stk_df['date'])), stk_df['lower'], 'g*--', label='布林下线', linewidth=0.5, markersize=1) ax[4].plot(range(0, len(stk_df['date'])), stk_df['MOM'], 'g*--', label='MOM', linewidth=0.5, markersize=1) # 准备下标 xlabel_series = stk_df.apply(lambda x: x['date'][2:].replace('-', ''), axis=1) ax[0] = add_axis(ax[0], xlabel_series, 40, rotation=45) ax[1] = add_axis(ax[1], xlabel_series, 40, rotation=45) ax[2] = add_axis(ax[2], xlabel_series, 40, rotation=45) ax[3] = add_axis(ax[3], xlabel_series, 40, rotation=45) ax[4] = add_axis(ax[4], xlabel_series, 40, rotation=45) for ax_sig in ax: ax_sig.legend(loc='best', fontsize=5) # 保存图片 save_dir = root_save_dir + current_date + '/' + str(stk_code) + '/' if not os.path.exists(save_dir): os.makedirs(save_dir) plt.tight_layout() plt.savefig(save_dir + pic_name, dpi=300) plt.close() return save_dir + pic_name