def plot_line_percent(stock_code, buy_date, start=6, end=2, win_status='green'): """ 百分比方式绘图 :return: """ from abupy import code_to_symbol stock_code_str = code_to_symbol(stock_code).value kl_pd, df_req_start, df_req_end = load_kline_df(stock_code_str) temp = kl_pd[kl_pd.date == buy_date]['key'] if kl_pd[kl_pd.date == buy_date]['key'].size == 0: return kl_pd_key = kl_pd[kl_pd.date == buy_date]['key'].values[0] kl_pd_back = kl_pd.iloc[kl_pd_key - start:kl_pd_key + end, :] # 获得数据 kl_pd_back['key2'] = list(range(0, len(kl_pd_back))) pre_close = kl_pd[kl_pd.key == (kl_pd_key - 1)]['close'].values[0] # 前一天收盘价 rate = 100 / pre_close kl_pd_back['close2'] = kl_pd_back['close'].map(lambda x: x * rate) plt.plot(kl_pd_back.key2, kl_pd_back.close2, label='p_change', color=win_status)
def plot_result_volumn_sum(best_result_tuple, count=None): """ 可视化绘制回测结果累计交易量 :param count: 要绘制的数量 """ orders_pd = best_result_tuple.orders_pd[ 0:count if count is not None else 1000] for key, order in orders_pd.iterrows(): buy_date = order.buy_date stock_code = order.symbol win_status = 'red' if order.sell_type == 'win' else 'green' from abupy import code_to_symbol stock_code = code_to_symbol(stock_code).value kl_pd, df_req_start, df_req_end = load_kline_df(stock_code) kl_pd_key = kl_pd[kl_pd.date == buy_date]['key'].values[0] kl_pd_back = kl_pd.iloc[kl_pd_key - 10:kl_pd_key + 1, :] # 获得数据 kl_pd_back['key2'] = list(range(0, len(kl_pd_back))) kl_pd_back['volume_sum'] = kl_pd_back['volume'].cumsum() pre_data = kl_pd_back[kl_pd.key == (kl_pd_key - 1)]['volume_sum'].values[0] rate = 100 / pre_data kl_pd_back['volume2'] = kl_pd_back['volume_sum'].map( lambda x: 500 if ((x * rate) > 500) else (x * rate)) plt.plot(kl_pd_back.key2, kl_pd_back.volume2, label='p_change', color=win_status) # plt.show() plt.show()
def plot_result_price(best_result_tuple, count=None): """ 可视化绘制回测结果价格 :param orders: AbuOrder对象序列 :param kl_pd: 金融时间序列,pd.DataFrame对象 :return: """ orders_pd = best_result_tuple.orders_pd[ 0:count if count is not None else 1000] for key, order in orders_pd.iterrows(): buy_date = order.buy_date stock_code = order.symbol win_status = 'red' if order.sell_type == 'win' else 'green' from abupy import code_to_symbol stock_code = code_to_symbol(stock_code).value kl_pd, df_req_start, df_req_end = load_kline_df(stock_code) kl_pd_key = kl_pd[kl_pd.date == buy_date]['key'].values[0] kl_pd_back = kl_pd.iloc[kl_pd_key - 30:kl_pd_key + 10, :] # 获得数据 kl_pd_back['key2'] = list(range(0, len(kl_pd_back))) # buy_price = kl_pd[kl_pd.date == buy_date]['close'].values[0] pre_close = kl_pd[kl_pd.key == (kl_pd_key - 1)]['close'].values[0] rate = 100 / pre_close kl_pd_back['close2'] = kl_pd_back['close'].map(lambda x: x * rate) # plt.plot(kl_pd_back.key2, kl_pd_back.p_change, label='p_change',color = win_status) plt.plot(kl_pd_back.key2, kl_pd_back.close2, label='p_change', color=win_status) # plt.show() plt.show()
def plot_order_wave_myself(order, start=100, end=30): """ 绘制周期图 :param order: AbuOrder对象序列 """ stockCode = order.symbol from abupy import code_to_symbol sh_stock_code = code_to_symbol(stockCode).value kl_pd, df_req_start, df_req_end = load_kline_df(sh_stock_code) mask_date = kl_pd['date'] == order.buy_date st_key = kl_pd[mask_date]['key'] if order.sell_type == 'keep': rv_pd = kl_pd[kl_pd.key >= st_key.values[0]] # 买入到卖出之间的数据 else: mask_sell_date = kl_pd['date'] == order.sell_date # 卖出日期 st_sell_key = kl_pd[mask_sell_date]['key'] # 卖出日期key rv_pd = kl_pd[kl_pd.key >= st_key.values[0]] rv_pd = rv_pd[rv_pd.key < st_sell_key.values[0] + 1] from abupy import ABuMarketDrawing kl_pd = kl_pd[kl_pd.key >= (st_key.values[0] - start)] kl_pd = kl_pd[kl_pd.key < (st_sell_key.values[0] + end)] kl_pd.name = stockCode benchmark = IndexSymbol.SH bench_pd = ABuSymbolPd.make_kl_df( benchmark, data_mode=EMarketDataSplitMode.E_DATA_SPLIT_UNDO, n_folds=None, start=None, end=None) bench_mask_date = bench_pd['date'] == order.buy_date # 买入日期 bench_st_key = bench_pd[bench_mask_date]['key'] # 买入日期key if order.sell_type == 'keep': rv_pd = bench_pd[bench_pd.key >= bench_st_key.values[0]] # 买入到卖出之间的数据 else: bench_mask_sell_date = bench_pd['date'] == order.sell_date # 卖出日期 bench_st_sell_key = bench_pd[bench_mask_sell_date]['key'] # 卖出日期key rv_pd = bench_pd[bench_pd.key >= bench_st_key.values[0]] rv_pd = rv_pd[rv_pd.key < bench_st_sell_key.values[0] + 1] bench_pd = bench_pd[bench_pd.key >= (bench_st_key.values[0] - start)] bench_pd = bench_pd[bench_pd.key < (bench_st_sell_key.values[0] + end)] from abupy.UtilBu.AbuJiJinDataUtil import jiJinPlotWave jiJinPlotWave(kl_pd=kl_pd, bench_pd=bench_pd, jiJinCodes=[stockCode], windowBuy=18, windowSell=18, poly=50, showWindowBuy=True, showWindowSell=True, keepDays=rv_pd)
def plot_order_limit_info_myself(order, index, ax_cnt=4, start=60, end=100): """ 绘制单个订单卖出信息最近几天信息 :param order: AbuOrder对象序列 """ stock_code = order.symbol from abupy import code_to_symbol sh_stock_code = code_to_symbol(stock_code).value kl_pd, df_req_start, df_req_end = load_kline_df(sh_stock_code) all_pd = kl_pd fig_dims = (ax_cnt, 1) mask_date = all_pd['date'] == order.buy_date st_key = all_pd[mask_date]['key'] all_pd = all_pd[all_pd.key > st_key.values[0] - start] all_pd = all_pd[all_pd.key < st_key.values[0] + end] plt.subplot2grid(fig_dims, (index, 0)) # 绘制价格曲线 plt.plot(all_pd.index, all_pd['close'], label='close') plot_order_share_myself(all_pd, st_key, order, order.symbol)
# ABuEnv.g_data_fetch_mode = EMarketDataFetchMode.E_DATA_FETCH_FORCE_LOCAL #强制本地,可多线程 ABuEnv.g_market_source = EMarketSourceType.E_MARKET_SOURCE_tx #作用同上点击效果。腾讯数据源(美股,A股,港股) ABuEnv.g_market_target = EMarketTargetType.E_MARKET_TARGET_CN register_matplotlib_converters() stock_code = '600988' #中欧医疗健康混合A # stock_code = '162703' #广发小盘成长混合(LOF) stock_name = 'xxx' start_date = '2020-02-12' # 设置起始日期 end_date = '2020-02-20' # 设置终止日期 # stock_k = ts.get_hist_data(stock_code, start=start_date, end=end_date) from abupy import code_to_symbol, IndexSymbol, EMarketDataSplitMode, EMarketDataFetchMode, EMarketSourceType sh_stock_code = code_to_symbol(stock_code).value stock_k, df_req_start, df_req_end = load_kline_df(sh_stock_code) #读取已存在的数据 kl_pd = ABuSymbolPd.make_kl_df( stock_code, data_mode=EMarketDataSplitMode.E_DATA_SPLIT_UNDO, benchmark=None, n_folds=1) # temp1 = ts.get_hist_data('161005',start='2020-02-03',end='2020-02-22',ktype='D') #ktype:数据类型,D=日k线 W=周 M=月 5=5分钟 15=15分钟 30=30分钟 60=60分钟,默认为D kl_pd = get_and_store_jijin_data('519005') kl_pd = kl_pd.tail(300) kl_pd['date2'] = [ int(datetime.datetime.strptime(dateTemp, '%Y-%m-%d').strftime('%Y%m%d')) for dateTemp in kl_pd.date ]
def plot_order_candle_myself(order, start=60, end=10): """ 绘制订单蜡烛图 :param order: AbuOrder对象序列 """ stock_code = order.symbol from abupy import code_to_symbol sh_stock_code = code_to_symbol(stock_code).value all_pd, df_req_start, df_req_end = load_kline_df(sh_stock_code) mask_date = all_pd['date'] == order.buy_date st_key = all_pd[mask_date]['key'] if order.sell_type == 'keep': rv_pd = all_pd[all_pd.key >= st_key.values[0]] # 买入到卖出之间的数据 st_sell_key = all_pd[mask_date]['key'] else: mask_sell_date = all_pd['date'] == order.sell_date # 卖出日期 st_sell_key = all_pd[mask_sell_date]['key'] # 卖出日期key rv_pd = all_pd[all_pd.key >= st_key.values[0]] rv_pd = rv_pd[rv_pd.key < st_sell_key.values[0] + 1] tempList = [] for k in rv_pd.index: tempList.append(k) if hasattr(order, 'bigWave2Close2'): all_pd['bigWave2Close2'] = order.bigWave2Close2 # all_pd['ema10'] = get_EMA(all_pd.close, 10) # all_pd['ema60'] = get_EMA(all_pd.close, 60) # tempList.append(order.bigWave2Close2Date) from abupy import ABuMarketDrawing all_pd = all_pd[all_pd.key >= (st_key.values[0] - start)] all_pd = all_pd[all_pd.key < (st_sell_key.values[0] + end)] all_pd.name = stock_code benchmark = IndexSymbol.SH bench_pd = ABuSymbolPd.make_kl_df( benchmark, data_mode=EMarketDataSplitMode.E_DATA_SPLIT_UNDO, n_folds=None, start=None, end=None) bench_mask_date = bench_pd['date'] == order.buy_date # 买入日期 bench_st_key = bench_pd[bench_mask_date]['key'] # 买入日期key # bench_pd = bench_pd[bench_pd.key > bench_st_key.values[0] - start] # bench_pd = bench_pd[bench_pd.key < bench_st_key.values[0] + end] if order.sell_type == 'keep': rv_pd = bench_pd[bench_pd.key >= bench_st_key.values[0]] # 买入到卖出之间的数据 bench_st_sell_key = bench_st_key # 卖出日期key else: bench_mask_sell_date = bench_pd['date'] == order.sell_date # 卖出日期 bench_st_sell_key = bench_pd[bench_mask_sell_date]['key'] # 卖出日期key rv_pd = bench_pd[bench_pd.key >= bench_st_key.values[0]] rv_pd = rv_pd[rv_pd.key < bench_st_sell_key.values[0] + 1] bench_pd = bench_pd[bench_pd.key >= (bench_st_key.values[0] - start)] bench_pd = bench_pd[bench_pd.key <= (bench_st_sell_key.values[0] + end)] ABuMarketDrawing.plot_candle_form_klpd(all_pd, html_bk=False, view_indexs=tempList, bench=bench_pd, order=order, save=True)