def create_index_figure_column(code, dtype, start_date, end_date): delta_days = (end_date - start_date).days if delta_days <= 0: return None start_date = start_date.strftime('%Y-%m-%d') end_date = end_date.strftime('%Y-%m-%d') if dtype == 'K线图': obj = CIndex(code) df = obj.get_k_data_in_range(start_date, end_date) source = ColumnDataSource(df) mapper = linear_cmap(field_name='pchange', palette=['red', 'green'], low=0, high=0, low_color = 'green', high_color = 'red') p = figure(plot_height=500, plot_width=1200, tools="", toolbar_location=None, sizing_mode="scale_both", x_range=(0, len(df))) p.xaxis.axis_label = "时间" p.yaxis.axis_label = "点数" p.xaxis.major_label_overrides = {i: mdate for i, mdate in enumerate(df['date'])} p.segment(x0='index', y0='low', x1='date', y1='high', line_width=2, color='black', source=source) p.vbar(x='index', bottom='open', top='close', width = 50 / delta_days, color=mapper, source=source) p.add_tools(HoverTool(tooltips=[("数量", "@close"), ("时间", "@date")])) volume_p = figure(plot_height=150, plot_width=1200, tools="", toolbar_location=None, sizing_mode="scale_both") volume_p.x_range = p.x_range volume_p.vbar(x='date', top='volume', width = 50 / delta_days, color=mapper, source=source) return column(p, volume_p) else: obj = BullStockRatio(code) df = obj.get_k_data_between(start_date, end_date) source = ColumnDataSource(df) p = figure(plot_height=500, plot_width=1200, tools="", toolbar_location=None, sizing_mode="scale_both") p.xaxis.axis_label = "时间" p.yaxis.axis_label = "比例" p.line(x = 'index', y = 'ratio', line_width = 3, line_alpha = 1.0, source = source) p.xaxis.major_label_overrides = {i: mdate for i, mdate in enumerate(df['date'])} p.add_tools(HoverTool(tooltips=[("比例", "@ratio"), ("时间", "@date")])) return column(p)
def get_index_data(start_date, end_date, index_code): iobj = CIndex(index_code, dbinfo=ct.OUT_DB_INFO, redis_host='127.0.0.1') i_data = iobj.get_k_data_in_range(start_date, end_date) i_data = i_data[[ 'open', 'high', 'low', 'close', 'volume', 'amount', 'date' ]] return i_data
def choose_plate(edate = '2016-10-11', ndays = 90): rindustry_info_client = RIndexIndustryInfo(redis_host='127.0.0.1') today_industry_df = rindustry_info_client.get_k_data(edate) pchange_df = today_industry_df.sort_values(by = 'pchange', ascending = False).head(3) mchange_df = today_industry_df.sort_values(by = 'mchange', ascending = False).head(3) plate_code_list = list(set(pchange_df.code.tolist()).intersection(pchange_df.code.tolist())) if len(plate_code_list) == 0: logger.info("no interested plate for date:%s" % edate) return list() sdate = get_day_nday_ago(edate, ndays, '%Y-%m-%d') #get sh index data sh_index_obj = CIndex('000001', redis_host='127.0.0.1') sh_index_info = sh_index_obj.get_k_data_in_range(sdate, edate) sh_index_pchange = 100 * (sh_index_info.loc[len(sh_index_info) - 1, 'close'] - sh_index_info.loc[0, 'preclose']) / sh_index_info.loc[0, 'preclose'] #get industry data all_industry_df = rindustry_info_client.get_k_data_in_range(sdate, edate) all_industry_df = all_industry_df.loc[all_industry_df.code.isin(plate_code_list)] industry_static_info = DataFrame(columns={'code', 'sai', 'pchange', ct.KL, ct.QL, ct.JL, ct.FL}) #choose better industry redisobj = create_redis_obj("127.0.0.1") today_industry_info = IndustryInfo.get(redisobj) for code, industry in all_industry_df.groupby('code'): industry = industry.reset_index(drop = True) industry['sri'] = 0 industry['sri'] = industry['pchange'] - sh_index_info['pchange'] industry['sai'] = 0 industry.at[(industry.pchange > 0) & (sh_index_info.pchange < 0), 'sai'] = industry.loc[(industry.pchange > 0) & (sh_index_info.pchange < 0), 'sri'] industry_sai = len(industry.loc[industry.sai > 0]) industry_pchange = 100 * (industry.loc[len(industry) - 1, 'close'] - industry.loc[0, 'preclose']) / industry.loc[0, 'preclose'] code_list = json.loads(today_industry_info.loc[today_industry_info.code == code, 'content'].values[0]) info_dict, good_code_list = choose_stock(code_list, sdate, edate) industry_static_info = industry_static_info.append(DataFrame([[code, industry_sai, industry_pchange, info_dict[ct.KL], info_dict[ct.QL], info_dict[ct.JL], info_dict[ct.FL]]], columns = ['code', 'sai', 'pchange', ct.KL, ct.QL, ct.JL, ct.FL]), sort = 'True') industry_static_info = industry_static_info.reset_index(drop = True) industry_static_info = industry_static_info.sort_values(by=['pchange'], ascending=False) return good_code_list
def get_data(code, start_date, end_date): cstock_obj = CIndex(code, dbinfo=ct.OUT_DB_INFO, redis_host='127.0.0.1') data = cstock_obj.get_k_data_in_range(start_date, end_date) data = data.set_index('date') if is_df_has_unexpected_data(data): return None data.index = pd.to_datetime(data.index) data = data.dropna(how='any') return data
def get_index_df(self, code, start_date, end_date): cindex_client = CIndex(code) df = cindex_client.get_k_data_in_range(start_date, end_date) df['time'] = df.index.tolist() df = df[[ 'time', 'open', 'high', 'low', 'close', 'volume', 'amount', 'date' ]] return df
def get_index_data(self, start_date, end_date, index_code): iobj = CIndex(index_code, dbinfo = self.dbinfo, redis_host = self.redis_host) i_data = iobj.get_k_data_in_range(start_date, end_date) i_data = i_data.sort_values(by=['date'], ascending=True) i_data = i_data.reset_index(drop = True) i_data['time'] = i_data.index.tolist() i_data = i_data[['time', 'open', 'high', 'low', 'close', 'volume', 'amount', 'date']] return i_data
def get_data(self, start_date, end_date, index_code): df = self.ris.get_k_data_in_range(start_date, end_date) iobj = CIndex(index_code, dbinfo=ct.OUT_DB_INFO, redis_host='127.0.0.1') i_data = iobj.get_k_data_in_range(start_date, end_date) i_data['time'] = i_data.index.tolist() i_data = i_data[[ 'time', 'open', 'high', 'low', 'close', 'volume', 'amount', 'date' ]] return df, i_data
base_color = '#e6daa6' name = "shanghai_%s_image" % cdate i_data = i_data[[ 'date', 'open', 'high', 'low', 'close', 'volume', 'amount' ]] fig, index_ax = plt.subplots(figsize=(16, 10)) fig.subplots_adjust(bottom=0.2) candlestick2_ochl(index_ax, i_data['open'], i_data['close'], i_data['high'], i_data['low'], width=1.0, colorup='r', colordown='g') index_ax.set_ylabel(name) index_ax.set_xticks(range(0, len(i_data['date']), 10)) plt.plot() fig.autofmt_xdate() plt.show() index_obj = CIndex('000001', dbinfo=ct.OUT_DB_INFO, redis_host='127.0.0.1') df = index_obj.get_k_data() cdates = df[(df.pchange > 4) & (df.date > '1999-01-01')].date.tolist() for mdate in cdates: start_date = mdate end_date = get_day_nday_after(start_date, num=40, dformat="%Y-%m-%d") index_data = index_obj.get_k_data_in_range(start_date, end_date) plot_index(start_date, index_data)