def greater(value1, value2): if isinstance(value1, dict): try: if at.date_encoding(value1['date']) > at.date_encoding( value2['date']): return True except: return False else: if at.date_encoding(value1[0]) > at.date_encoding(value2[0]): return True
def get_series_hist_data(code, days, start_date = '', multi_threads = 20): ''' Gather the stock data for a range of days :param code: str, stock index :param days: int, the number of days :param start_date: str, the start date :param multi_threads: int, the number of threads, default 20 :return: a list for the data of a series of dates ''' if days < 10: days_n = 10 else: days_n = days if start_date != '': start_date = at.date_encoding(start_date) else: start_date = datetime.date.today() def record_thread_result(code, date, q, q_n): result = get_stock_hist_data(code, date) if result == None: q_n.put(result) else: q.put(result) q = queue.Queue() q_n = queue.Queue() list = [] while q.qsize() < days and q_n.qsize() < days_n * 2: threads = [] for days_back in range(0, multi_threads): day_mark = start_date - datetime.timedelta(days_back) threads.append(threading.Thread(target=record_thread_result, args=(code, at.date_decoding(day_mark), q, q_n))) for t in threads: t.start() for n in threads: n.join() start_date -= datetime.timedelta(multi_threads) while not q.empty(): list.append(q.get()) list = at.sort_list_by_date(list) list = list[-days:] return list
def get_index(date, idx, type, duration=0): ''' Return the index of specific type in a specific day :param date: str, date :param idx: str, stock index :param type: str, type :return: the index ''' dict = {'sh': '000001', \ 'sz': '399001'} end = at.date_decoding(at.date_encoding(date) + datetime.timedelta(duration)) matrix = ts.get_k_data(dict[idx], index=True, start=date, end=end) content = [] for i in range(len(matrix)): line = {} line['date'] = matrix.iloc[i]['date'] line['index'] = matrix.iloc[i][type] content.append(line) if len(content) == 1: index = content[0]['index'] content = index return content
def shift_one_day(date): result = at.date_decoding( at.date_encoding(date) + datetime.timedelta(shifter)) return result
def plot_difference_multi_smoothing(self, code, smooth, date='', duration=default_range, leading_smooth=leading_smooth, threads=threads_of_catch, period_length=0, type='show', height=graph_height): ''' Plot the difference with multiple smoothing method :param code: str, stock index :param date: str, date :param duration: int, the analysis duration :param smooth: list, the smooth method :param leading_smooth: int, the leading smooth period :param threads: int, the number of threads used :param type: show or save the figure :return: None ''' if period_length == 0: period_length = duration periods_list = at.split_period(date, duration, period_length) for i in periods_list: date = i['start_date'] duration = i['days'] #(date, code, open, close, actual, theoretical, diff, smoothed) list = self.__diff_lists_multi_smoothing__(code, smooth, date, duration, leading_smooth, threads) if list != None: x_date = [date2num(at.date_encoding(i['date'])) for i in list] max_volume = max([i['volume'] for i in list]) y_price = [(date2num(at.date_encoding(i['date'])), i['open'], i['close'], i['high'], i['low']) for i in list] y_deviation_list = {} for idv in smooth: y_deviation_list['smoothed difference %s' % idv] = [ i['smoothed difference %s' % idv] for i in list ] # y_deviation = [i['smoothed difference'] for i in list] # y_theo = [i['smoothed theoretical'] for i in list] # y_theo = [i for i in y_theo] y_volume = [ (date2num(at.date_encoding(i['date'])), i['open'], i['high'], i['low'], i['close'], i['volume'] / max_volume) for i in list ] support_line = [] for i in range(len(figure_deviation_line)): support_line.append( [figure_deviation_line[i] for k in list]) # fig, price = plt.subplots() fig = plt.figure() sub_plots = gridspec.GridSpec(2, 1, height_ratios=[5, 1]) sub_plots.update(wspace=0.001, hspace=0.001) price = plt.subplot(sub_plots[0]) price.xaxis_date() mondays = WeekdayLocator(MONDAY) alldays = DayLocator() weekFormatter = DateFormatter('%b %d') price.xaxis.set_major_locator(mondays) price.xaxis.set_minor_locator(alldays) price.xaxis.set_major_formatter(weekFormatter) price.xaxis_date() deviation = price.twinx() volume = plt.subplot(sub_plots[1], sharex=price) mpf.candlestick_ochl(price, y_price, width=0.8, colorup='r', colordown='g', alpha=0.8) def pick_clor(color_list, idx): length = len(color_list) try: if idx == 0: return color_list[0] else: return color_list[idx % length] except: return color_list[0] color_list = ['g', 'y', 'b', 'r'] for idvc in range(len(smooth)): deviation.plot( x_date, y_deviation_list['smoothed difference %s' % smooth[idvc]], pick_clor(color_list, idvc)) for j in support_line: deviation.plot(x_date, j, 'y:') price.set_ylabel('price') price.get_xaxis().set_visible(False) deviation.set_ylabel('deviation') mpf.volume_overlay3(volume, y_volume, width=10, colorup='r', colordown='g', alpha=0.8) volume.get_yaxis().set_visible(False) smooth_tag = '-'.join([str(i) for i in smooth]) volume.set_xlabel("%s %s %s" % (code, list[-1]['date'], smooth_tag)) volume.set_ylim(0, 1) if type == "show": plt.show() if type == "save": try: os.mkdir(os.path.join(os.getcwd(), 'graph')) except: pass path = os.path.join( os.getcwd(), 'graph/%s-%s-%i-%s.png' % (code, list[-1]['date'], duration, smooth_tag)) fig.set_size_inches( math.sqrt(int(duration)) * height / 3, height) plt.savefig(path) else: print(the_warning)
import messenger as ms import analyst as al import numpy as np import assistant as at import matplotlib.pyplot as plt h = al.PriceDeviation() h.price_diff_list_save('002415', date='2017-06-28', duration=600, smooth=3) origin_data = h.price_diff_list_load('002415', date='2017-06-28', duration=600, smooth=3) origin_data = origin_data[-200:] result = [] date_list = [] for i in range(30, len(origin_data)): date = origin_data[i]['date'] sub_list = origin_data[i - 30:i] diff_list = [i['smoothed difference'] for i in sub_list] theo_list = [i['smoothed theoretical'] for i in sub_list] corr = np.correlate(diff_list, theo_list) print(date, corr) result.append(corr) date_list.append(at.date_encoding(date)) plt.plot(date_list, result) plt.show()