def read_data(self, needStore = False): if os.path.exists('i_data.json'): with open('k_data.json', 'r') as f: k_data = pd.read_json(f.read(), orient = 'records', lines = True) k_data.date = k_data.date.dt.strftime('%Y-%m-%d') with open('d_data.json', 'r') as f: d_data = pd.read_json(f.read(), orient = 'records', lines = True) with open('i_data.json', 'r') as f: i_data = pd.read_json(f.read(), orient = 'records', lines = True) i_data.date = i_data.date.dt.strftime('%Y-%m-%d') else: obj = CStock(self.code, dbinfo = ct.OUT_DB_INFO, redis_host = '127.0.0.1') k_data = obj.get_k_data() k_data.date = pd.to_datetime(k_data.date).dt.strftime('%Y-%m-%d') d_data = obj.get_chip_distribution() iobj = CIndex(self.index_code, dbinfo = ct.OUT_DB_INFO, redis_host = '127.0.0.1') i_data = iobj.get_k_data() cdates = k_data.date.tolist() i_data = i_data.loc[i_data.date.isin(cdates)] i_data = i_data.reset_index(drop = True) k_data = k_data[['date', 'open', 'high', 'low', 'close', 'volume', 'amount', 'outstanding', 'totals', 'adj', 'aprice', 'uprice', 'sprice', 'mprice', 'lprice', 'profit']] k_data = k_data.rename(columns = {"date": "time"}) i_data = i_data[['date', 'open', 'high', 'low', 'close', 'volume', 'amount']] i_data = i_data.rename(columns = {"date": "time"}) if needStore: with open('k_data.json', 'w') as f: f.write(k_data.to_json(orient='records', lines=True)) with open('d_data.json', 'w') as f: f.write(d_data.to_json(orient='records', lines=True)) with open('i_data.json', 'w') as f: f.write(i_data.to_json(orient='records', lines=True)) return k_data, d_data, 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)