def __init__(self, dates, open, high, low, close, vol, start, end): if start > end: (start, end) = (end, start) self.report_log = [] max = None max_date = None min = None min_date = None seq_start = dates[0] seq_end = dates[0] series = [] n = 0 for i in range(len(dates)): d = dates[i] if (d > start) and (d < end): series.append(close[i]) if (d < seq_start): seq_start = d if (d > seq_end): seq_end = d n = n + 1 h = high[i] if max == None: max = h max_date = d else: if h > max: max = h max_date = d l = low[i] if min == None: min = l min_date = d else: if l < min: min = l min_date = d self.report_log.append('%s - %s' % (seq_start, seq_end)) self.report_log.append('%d trading days' % n) self.report_log.append('Max = %s - %s' % (str(max), max_date)) self.report_log.append('Min = %s - %s' % (str(min), min_date)) h = Histogram(series) for l in h.report(): self.report_log.append(l)
def gen_intraday_volatility_plots(symbol, dates, high, low, close, start_date, end_date, gen_title) : (start_idx, end_idx) = get_start_end_idxs(dates, start_date, end_date) # cut slice date_slice = dates[start_idx : end_idx] slice_start_date = dates[start_idx] slice_end_date = dates[end_idx] low_slice = low[start_idx : end_idx] high_slice = high[start_idx : end_idx] close_slice = close[start_idx : end_idx] delta = [] for i in range(len(high_slice)): delta.append(float(100 * (high_slice[i] - low_slice[i]) / close_slice[i]) ) # generate matplotlib plot x = np.array(date_slice) y = np.array(delta) fever_fig = plt.figure() ax = fever_fig.add_subplot(111) ax.plot(x,y) # leg = ax.legend(('Model length'), 'upper center', shadow=True) ax.grid(False) ax.set_ylabel('100 * (High - Low) / Close') title = gen_title(symbol, 'Intra-Day Range as a Percentage of Closing Price', slice_start_date, slice_end_date) ax.set_title(title) # date intervals & markers (formatter, locator) = tick_info(slice_start_date, slice_end_date) ax.xaxis.set_major_formatter(formatter) ax.xaxis.set_major_locator(locator) fever_fig.autofmt_xdate(rotation=90) ax.set_xlim([slice_start_date, slice_end_date]) ax.set_xlabel('Date') # ------------ h = Histogram(delta) left_edge = [] height = [] for bin in h.bins: left_edge.append(float(bin.floor)) height.append(h.bin_contrib_perc(bin)) x = np.array(left_edge) y = np.array(height) dist_fig = plt.figure() ax = dist_fig.add_subplot(111) ax.bar(x, y, width=h.bins[0].range) ax.set_xlim(h.min, h.max) ax.set_ylabel('% of Population') ax.set_xlabel('Intra-Day Range i.t.o Close : 100 * (High - Low) / Close') title = gen_title(symbol, 'Distribution of Intra-Day Range', slice_start_date, slice_end_date) ax.set_title(title) reports = [] reports.append(AnalysisReport(symbol, slice_start_date, slice_end_date, 'Intra-Day Range Fever', '', fever_fig)) reports.append(AnalysisReport(symbol, slice_start_date, slice_end_date, 'Distribution of Intra-day Range', h.report(date_slice, delta), dist_fig)) return reports