def get_equity_prices(self, symbol, from_date, new_price): # equity_records = EquityDAO().get_all_equity_price_by_symbol(symbol, from_date) equity_records = YahooEquityDAO().get_all_equity_price_by_symbol('SPY', from_date.strftime('%Y-%m-%d'),'Closeprice') if new_price is not None: equity_records.append([datetime.date.today(), new_price]) equity_prices = map(lambda x: x[1], equity_records) return equity_prices
def get_spy_price_list(date_str_list): price_list = [] dao = YahooEquityDAO() conn = BaseDAO.get_connection() cursor = conn.cursor() for date_str in date_str_list: price_list.append( dao.get_equity_price_by_date('SPY', date_str, cursor=cursor)) conn.close return price_list
def get_historical_volatility(self, new_price, from_date=None): if from_date is None: from_date = (datetime.date.today() - datetime.timedelta(150)) else: from_date = from_date - datetime.timedelta(37) # ensure more than 21 days # equity_records = EquityDAO().get_all_equity_price_by_symbol('SPY', from_date) equity_records = YahooEquityDAO().get_all_equity_price_by_symbol('SPY', from_date.strftime('%Y-%m-%d'), 'Closeprice') if new_price is not None: equity_records.append([datetime.date.today(), new_price]) hv_records = OptionCalculater.get_year_history_volatility_list(equity_records, 21) return hv_records
def __init__(self): self.vix_records = YahooEquityDAO().get_all_equity_price_by_symbol( '^VIX', from_date_str='2010-12-17') self.vxv_records = YahooEquityDAO().get_all_equity_price_by_symbol( '^VXV', from_date_str='2010-12-17') self.dates = map(lambda x: x[0], self.vix_records)[7:] vix_values = map(lambda x: x[1], self.vix_records) self.vix_ma10 = pd.Series(vix_values).rolling( window=7).mean().tolist()[7:] vxv_values = map(lambda x: x[1], self.vxv_records) self.vxv_ma10 = pd.Series(vxv_values).rolling( window=7).mean().tolist()[7:]
def catch_up_missing_data(): logger.info('run catch up missing_data') retry_count = 30 # retry for 5 hours for i in range(retry_count): symbols = YahooEquityDAO().get_missing_records_symbols() if len(symbols) == 0: break else: if i == retry_count-1: raise Exception('Unable to ingest missing data from yahoo website for %s times..'% retry_count) else: time.sleep(600) # sleep 10 minutes, then retry YahooScraper.ingest_recently_historyical_etf(symbols=symbols) YahooEquityDAO().save_all(symbols) logger.info('completed')
def process_for_yahoo_historical_data(): logger.info('process for yahoo historical data...') if datetime.date.today().weekday() == 6: YahooScraper.ingest_all_historical_etf() else: YahooScraper.ingest_recently_historyical_etf() YahooEquityDAO().save_all()
def plot_spy_vs_aieq(from_date=datetime.date(2017, 1, 1)): spy_records = YahooEquityDAO().get_all_equity_price_by_symbol( 'SPY', from_date.strftime('%Y-%m-%d')) aieq_records = YahooEquityDAO().get_all_equity_price_by_symbol( 'AIEQ', from_date.strftime('%Y-%m-%d')) dates = map(lambda x: x[0], aieq_records) spy_values = map(lambda x: x[1], spy_records[-len(dates):]) aieq_values = map(lambda x: x[1], aieq_records) fig, ax1 = plt.subplots() ax2 = ax1.twinx() ax1.plot(dates, spy_values, 'r-', label='spy') ax2.plot(dates, aieq_values, 'b-', label='aieq') ax1.legend(loc='upper left') ax2.legend(loc='upper right') plt.legend(bbox_to_anchor=(1.05, 1), loc=8, borderaxespad=0.) plt.show()
def GET(self): credits_df = NYSECreditDAO().get_all_margin_debt() spy_df = YahooEquityDAO().get_equity_monthly_by_symbol('SPY', ['lastdate', 'adjcloseprice']) #print credits_df #print spy_df credits_df = credits_df[credits_df.lastDate >= '2008-01-01'] spy_df = spy_df[spy_df.lastdate >= datetime.date(2008, 1, 1)] dates = map(lambda x: datetime.datetime.strptime(x, '%Y-%m-%d'), credits_df['lastDate']) debt = credits_df['margin_debt'] spy_prices = spy_df['adjcloseprice'][0:len(dates)] fig = Figure(figsize=[12, 8]) ax1 = fig.add_axes([.1, .1, .8, .8]) ax2 = ax1.twinx() ax1.plot(dates, debt, 'r-', label='credit margin debt') ax2.plot(dates, spy_prices, 'b-', label='SPY') ax1.legend(loc='upper left') ax2.legend(loc='upper center') ax1.grid() canvas = FigureCanvasAgg(fig) # write image data to a string buffer and get the PNG image bytes buf = cStringIO.StringIO() canvas.print_png(buf) data = buf.getvalue() return data
def plot_yahoo_equity_line(symbol): spy_records = YahooEquityDAO().get_all_equity_price_by_symbol(symbol) dates = map(lambda x: x[0], spy_records) spy_values = map(lambda x: x[1], spy_records) fig, ax = plt.subplots() ax.plot(dates, spy_values) plt.show()
class PairTrading(object): def __init__(self): self.yahoo_dao = YahooEquityDAO() def show_ratio(self, equity1, equity2): records1 = self.yahoo_dao.get_all_equity_price_by_symbol(equity1, from_date_str='2008-01-01') records2 = self.yahoo_dao.get_all_equity_price_by_symbol(equity2, from_date_str='2008-01-01') dates = map(lambda x: x[0], records1) prices1 = map(lambda x: x[1], records1) prices2 = map(lambda x: x[1], records2) score, pvalue, _ = coint(prices1, prices2) print(pvalue) ratios = map(lambda x, y: x/y, prices1, prices2) # ratios = prices1/prices2 fig, ax = plt.subplots() ax.plot(dates, ratios) plt.axhline(np.average(ratios)) plt.show() # def zscore(series): # return (series - series.mean()) / np.std(series) @staticmethod def zscore(lst): ave = np.average(lst) std = np.std(lst) return map(lambda x: (x-ave)/std, lst)
def __init__(self, init_cash=1000000, buy_tax=0.001, sell_tax=0.001): self.position = {} self.init_cash = init_cash self.cash = init_cash self.buy_tax = buy_tax self.sell_tax = sell_tax self.yahoo_equity_dao = YahooEquityDAO() self.option_dao = OptionDAO()
def GET(self): records = YahooEquityDAO().get_start_end_date_by_symbols() last_trade_date = TradeTime.get_latest_trade_date() error = False for record in records: if record[2] < last_trade_date: error = True dic = Symbols.get_yahoo_mapping_dic() return render.start_end_date(records, last_trade_date, error, dic)
def _test_vol(): import datetime from dataaccess.yahooequitydao import YahooEquityDAO from_date_str = (datetime.date.today() - datetime.timedelta(100)).strftime('%Y-%m-%d') equity_records = YahooEquityDAO().get_all_equity_price_by_symbol( 'SPY', from_date_str) results = OptionCalculater.get_year_history_volatility_list(equity_records) print results
def GET(self): from_date = TradeTime.get_latest_trade_date() - datetime.timedelta(30) vix_records = VIXDAO().get_vix_price_by_symbol_and_date('VIY00', from_date=from_date) dates = map(lambda x: x[0], vix_records) vix_prices = map(lambda x: x[1], vix_records) vxv_records = YahooEquityDAO().get_all_equity_price_by_symbol('^VXV', from_date_str=from_date.strftime('%Y-%m-%d')) vxmt_records = YahooEquityDAO().get_all_equity_price_by_symbol('^VXMT', from_date_str=from_date.strftime('%Y-%m-%d')) vxv_prices = map(lambda x: x[1], vxv_records) vxmt_prices = map(lambda x: x[1], vxmt_records) fig = Figure(figsize=[12, 8]) ax = fig.add_axes([.1, .1, .8, .8]) ax.plot(dates, vix_prices, label='vix') ax.plot(dates, vxv_prices, label='vxv') ax.plot(dates, vxmt_prices, label='vxmt') ax.legend(loc='upper left') ax.grid() canvas = FigureCanvasAgg(fig) buf = cStringIO.StringIO() canvas.print_png(buf) data = buf.getvalue() return data
def agg1mtodaily(symbol, start_time=datetime.datetime(1971, 1, 1, 0, 0, 0), end_time=datetime.datetime(9999, 1, 1, 0, 0, 0)): records = EquityMinDAO().get_records(symbol, start_time, end_time) sub_records = [] combined_records = [] for record in records: sub_records.append(record) if record[0].hour == 15: sub_records.append(record) combined_record = AGG30Min.combine_records(symbol, sub_records) combined_records.append(combined_record) sub_records = [] YahooEquityDAO().save_from_equities(combined_records)
def get_selected_strike_price(self, selected_symbol, strike_prices): selected_strike_price = self.query_dic.get('strike_price') if selected_strike_price is None: if selected_symbol == '^VIX': current_equity_price = YahooEquityDAO().get_latest_price(selected_symbol) else: current_equity_price = EquityDAO().get_latest_price(selected_symbol) min_delta = sys.maxint for strike_price in strike_prices: delta = abs(strike_price - current_equity_price) if delta < min_delta: min_delta = delta selected_strike_price = strike_price return selected_strike_price
def spy_vs_vix(from_date, ma_window_short=5, ma_window_long=21): spy_records = YahooEquityDAO().get_all_equity_price_by_symbol( 'SPY', from_date.strftime('%Y-%m-%d')) vix_records = YahooEquityDAO().get_all_equity_price_by_symbol( '^VIX', from_date.strftime('%Y-%m-%d')) dates = map(lambda x: x[0], spy_records)[ma_window_long:] spy_values = map(lambda x: x[1], spy_records)[ma_window_long:] vix_values = map(lambda x: x[1], vix_records) vix_values_short = MACD.get_all_ema(vix_values, ma_window_short)[ma_window_long:] vix_values_long = MACD.get_all_ema(vix_values, ma_window_long)[ma_window_long:] # vix_values1 = pd.Series(vix_values).rolling(window=ma_window_short).mean().tolist()[ma_window_long:] # vix_values_mean = pd.Series(vix_values).rolling(window=ma_window_long).mean().tolist()[ma_window_long:] value = 25 # buy_or_hold = map(lambda x,y: 0 if x>value or y > value else 1, vix_values1, vix_values_mean) buy_or_hold = map(lambda x, y: 0 if x > value or y > value else 1, vix_values_short, vix_values_long) bull_regime = [] signal = -1 for i in range(len(buy_or_hold)): date = dates[i] value = buy_or_hold[i] if signal != value: bull_regime.append([date, value]) signal = value print bull_regime fig, ax1 = plt.subplots() ax2 = ax1.twinx() ax1.plot(dates, spy_values, 'r-', label='spy') # ax2.plot(dates, vix_values1, 'b-', label='vix') # ax2.plot(dates, vix_values_mean, 'g-', label='vix') ax2.plot(dates, buy_or_hold) ax1.legend(loc='upper left') ax2.legend(loc='upper right') plt.legend(bbox_to_anchor=(1.05, 1), loc=8, borderaxespad=0.) plt.show()
def load_spy_records(self): rows = YahooEquityDAO().get_all_equity_price_by_symbol( 'SPY', from_date_str='1993-01-01', price_field='openPrice, closePrice') dates = map(lambda x: x[0], rows) spy_prices = map(lambda x: x[2], rows) days_delta = [] night_delta = [] for i in range(len(rows)): days_delta.append(rows[i][2] - rows[i][1]) if i == 0: night_delta.append(0) else: night_delta.append(rows[i][1] - rows[i - 1][2]) return [dates, spy_prices, days_delta, night_delta]
def history(self, symbol, field, window): fields_dic = { 'open': 'openPrice', 'close': 'adjclosePrice', 'high': 'highPrice', 'low': 'lowPrice', 'price': 'adjclosePrice' } fields = fields_dic.keys() if field.lower() not in field: raise Exception('the field should be in %s...' % fields) price_field = fields_dic[field] yahoo_symbol = Symbols.get_mapped_symbol(symbol) from_date = TradeTime.get_from_date_by_window(window) rows = YahooEquityDAO().get_all_equity_price_by_symbol( yahoo_symbol, from_date.strftime('%Y-%m-%d'), price_field) return rows
def spy_option(): from dataaccess.yahooequitydao import YahooEquityDAO records = YahooEquityDAO().get_all_equity_price_by_symbol( 'SPY', from_date_str='2017-07-21') price_list = map(lambda x: x[1], records) #from dataaccess.equitydao import EquityDAO #df = EquityDAO().select_by_symbols(['SPY']) #print df #price_list = df['lastPrice'].tolist() #print price_list underlying_price = price_list[-1] print 'underlying_price=%s' % underlying_price strike_price = 245 interest_rate = 0.005 left_days = 24 sigma = OptionCalculater.get_year_history_volatility(price_list) print 'sigma=%s' % sigma bs_price = OptionCalculater.get_black_scholes_option_price( underlying_price, strike_price, left_days / 365.0, interest_rate, sigma, 'c') print 'bs_price=%s' % bs_price current_price = 1.53 iv = OptionCalculater.get_implied_volatility(current_price, underlying_price, strike_price, left_days, interest_rate, 'c') print 'implied volatility:%s' % iv delta = OptionCalculater.get_delta(underlying_price, strike_price, left_days, interest_rate, sigma, 'c') print 'delta=%s' % delta gamma = OptionCalculater.get_gamma(underlying_price, strike_price, left_days, interest_rate, sigma, 'c') print 'gamma=%s' % gamma vega = OptionCalculater.get_vega(underlying_price, strike_price, left_days, interest_rate, sigma, 'c') print 'vega=%s' % vega theta = OptionCalculater.get_theta(underlying_price, strike_price, left_days, interest_rate, sigma, 'c') print 'theta=%s' % theta rho = OptionCalculater.get_rho(underlying_price, strike_price, left_days, interest_rate, sigma, 'c') print 'rho=%s' % rho
def GET(self, from_date_str=None): default_from_date = TradeTime.get_latest_trade_date() - datetime.timedelta(60) if from_date_str is None or from_date_str == '': from_date = default_from_date else: try: input_from_date = datetime.datetime.strptime(from_date_str, '%Y-%m-%d').date() from_date = TradeTime.get_from_date_by_window(22, input_from_date) except Exception: from_date = default_from_date records_svxy = YahooEquityDAO().get_all_equity_price_by_symbol('SVXY', from_date.strftime('%Y-%m-%d'), 'Closeprice') dates = map(lambda x: x[0], records_svxy) price_svxy = map(lambda x: x[1], records_svxy) # shift append_dates = TradeTime.generate_dates(dates[-1], dates[-1] + datetime.timedelta(days=50)) dates = dates[21:] + append_dates[1:22] price_svxy = price_svxy[21:] + [price_svxy[-1]] * 21 if from_date < default_from_date: dates = dates[:42] price_svxy = price_svxy[:42] fig = Figure(figsize=[24, 4]) ax = fig.add_axes([.1, .1, .8, .8]) ax.plot(dates, price_svxy, label='SVXY') ax.legend(loc='upper left') ax.grid() ax.xaxis.set_major_formatter(DateFormatter('%y%m%d')) ax.set_xticks(dates) for tick in ax.get_xticklabels(): tick.set_rotation(45) canvas = FigureCanvasAgg(fig) buf = cStringIO.StringIO() canvas.print_png(buf) data = buf.getvalue() return data
def __init__(self): self.yahoo_dao = YahooEquityDAO() self.ignore_symbols = [ 'BIL', 'IEF', 'XIV', 'VIX', 'GLD', 'SLV', 'TLT', 'ZIV' ]
class ETFSelection(object): def __init__(self): self.yahoo_dao = YahooEquityDAO() self.ignore_symbols = [ 'BIL', 'IEF', 'XIV', 'VIX', 'GLD', 'SLV', 'TLT', 'ZIV' ] @staticmethod def get_combinations(lst): combinations = [] length = len(lst) for i in range(length - 1): j = i + 1 while i < j: if j == length: break else: combinations.append([lst[i], lst[j]]) j += 1 return combinations def get_correlation_coefficient(self, diff1, diff2): correlation_coefficient = pd.Series(diff1).corr(pd.Series(diff2)) return correlation_coefficient # correlation coefficient sample # a = [1, 2, 3, 4 , 5] # b = [2, 4, 6 ,8, 10] # pd.Series(a).corr(pd.Series(b)) def get_low_volatility_symbols(self, current_date=None, window=120, liquidity_filter_count=50, my_filter_count=30): symbols = self.yahoo_dao.filter_liquidity_symbols( count=liquidity_filter_count, ignore_symbols=self.ignore_symbols) symbol_volatilities = self.yahoo_dao.get_symbol_volatilities( symbols, window, current_date) symbol_volatilities.sort(key=lambda x: x[1]) return map(lambda x: x[0], symbol_volatilities[:my_filter_count]) def get_high_sharp_ratio_symbols(self, current_date=None, window=120, liquidity_filter_count=50, my_filter_count=30): symbols = self.yahoo_dao.filter_liquidity_symbols( count=liquidity_filter_count, ignore_symbols=self.ignore_symbols) symbol_sharp_ratio = self.yahoo_dao.get_symbol_sharp_ratio( symbols, window, current_date) symbol_sharp_ratio.sort(key=lambda x: x[1], reverse=True) return map(lambda x: x[0], symbol_sharp_ratio[:my_filter_count]) # obsoleted def get_all_corr_dic(self): symbols = self.yahoo_dao.filter_liquidity_symbols(count=50) # conn = self.yahoo_dao.get_connection() # cursor = conn.cursor() # diffs = map(lambda x: self.yahoo_dao.get_monthly_diff_price_by_symbol(x, cursor), symbols) diffs = self.yahoo_dao.get_all_monthly_diff_price_by_symbols(symbols) symbols_pair_list = ETFSelection.get_combinations(symbols) diffs_pair_list = ETFSelection.get_combinations(diffs) corr_dic = {} for i in range(len(symbols_pair_list)): [s1, s2] = symbols_pair_list[i] [diff1, diff2] = diffs_pair_list[i] key = '%s-%s' % (s1, s2) reverse_key = '%s-%s' % (s2, s1) corr = self.get_correlation_coefficient(diff1, diff2) corr_dic[key] = corr corr_dic[reverse_key] = corr return corr_dic def get_all_corr(self, current_date=None, liquidity_filter_count=50, second_filter_count=30, second_filter_by=None): if second_filter_by is not None: symbols = second_filter_by( current_date=current_date, liquidity_filter_count=liquidity_filter_count, my_filter_count=second_filter_count) #symbols = self.get_low_volatility_symbols(current_date=current_date, liquidity_filter_count=liquidity_filter_count, vol_filter_count=second_filter_count) else: symbols = self.yahoo_dao.filter_liquidity_symbols( current_date=current_date, count=liquidity_filter_count, ignore_symbols=self.ignore_symbols) diffs = self.yahoo_dao.get_all_monthly_diff_price_by_symbols( symbols, end_date=current_date) df = None for i in range(len(symbols)): new_df = pd.DataFrame({symbols[i]: diffs[i]}) if df is None: df = new_df else: df = df.join(new_df) # print df correlation_coefficient = df.corr() return correlation_coefficient def get_low_corr_symbols(self, current_date=None, count=8, liquidity_filter_count=30, second_filter_count=30, second_filter_by=None): df = self.get_all_corr(current_date, liquidity_filter_count=liquidity_filter_count, second_filter_count=second_filter_count, second_filter_by=second_filter_by) corr_sum = df[df.columns].sum() - 1 # print corr_sum records = map(lambda x, y: [x, y], corr_sum.index, corr_sum.tolist()) records = filter(lambda x: x[1] > 0, records) records.sort(key=lambda x: x[1]) # corr_sum.sort_vaues(assending=False) return map(lambda x: x[0], records[:count]) def get_monthly_end_date(self, start_date=datetime.date(2011, 1, 1)): df = self.yahoo_dao.get_equity_monthly_by_symbol('SPY', ['lastDate']) dates = df['lastDate'].values.tolist() dates = filter(lambda x: x > start_date, dates) return dates def get_monthly_symbols(self, start_date=datetime.date(2011, 1, 1), liquidity_filter_count=50, second_filter_count=None, second_filter_by=None): results = {} for date in self.get_monthly_end_date(start_date): symbols = self.get_low_corr_symbols( date, liquidity_filter_count=liquidity_filter_count, second_filter_count=second_filter_count) results[date] = map(byteify, symbols) # print date, results[date] return results def get_monthly_symbols_with_volatilities(self, start_date=datetime.date( 2011, 1, 1), liquidity_filter_count=50, volatility_filter_count=None): results = {} for date in self.get_monthly_end_date(start_date): symbols = self.get_low_corr_symbols( date, liquidity_filter_count=liquidity_filter_count, second_filter_count=volatility_filter_count, second_filter_by=self.get_low_volatility_symbols) results[date] = map(byteify, symbols) # print date, results[date] return results def get_monthly_symbols_with_sharp_ratio(self, start_date=datetime.date( 2011, 1, 1), liquidity_filter_count=50, sharp_ratio_filter_count=None): results = {} for date in self.get_monthly_end_date(start_date): symbols = self.get_low_corr_symbols( date, liquidity_filter_count=liquidity_filter_count, second_filter_count=sharp_ratio_filter_count, second_filter_by=self.get_high_sharp_ratio_symbols) results[date] = map(byteify, symbols) # print date, results[date] return results @staticmethod def get_symbols_mapping(): symbols = Symbols.get_all_tradeable_symbols() items = map(lambda x: '\'%s\':symbol(\'%s\')' % (x, x), symbols) return '{%s}' % ','.join(items)
def get_historical_prices(self): yahooEquityDAO = YahooEquityDAO() for symbol in self.symbols: rows = yahooEquityDAO.get_all_equity_price_by_symbol(symbol) yield map(lambda x: x[1], rows)
def __init__(self): self.yahoo_dao = YahooEquityDAO()
import datetime import matplotlib.pyplot as plt from dataaccess.nysecreditdao import NYSECreditDAO from dataaccess.yahooequitydao import YahooEquityDAO def plot_lines(credits_df, spy_df, ss_df): dates = map(lambda x: datetime.datetime.strptime(x, '%Y-%m-%d'), credits_df['lastDate']) # debt = credits_df['margin_debt'] spy_prices = spy_df['adjcloseprice'][0:len(dates)] ss_prices = ss_df['adjcloseprice'][0:len(dates)] fig, ax = plt.subplots() ax.plot(dates, spy_prices, 'b-', label='SPX') ax.plot(dates, ss_prices, 'r-', label='SH') plt.legend(bbox_to_anchor=(1.05, 1), loc=8, borderaxespad=0.) plt.show() if __name__ == '__main__': credits_df = NYSECreditDAO().get_all_margin_debt() #date_str_list = map(lambda x: x.date_str, credits) spy_df = YahooEquityDAO().get_equity_monthly_by_symbol( '^GSPC', ['lastdate', 'adjcloseprice']) ss_df = YahooEquityDAO().get_equity_monthly_by_symbol( '000001.ss', ['lastdate', 'adjcloseprice']) plot_lines(credits_df, spy_df, ss_df)
def process_for_yahoo_historical_data(): logger.info('process for yahoo historical data...') YahooScraper.ingest_recently_historyical_etf() YahooEquityDAO().save_all()
def process_for_yahoo_historical_data(): YahooScraper.ingest_recently_historyical_etf() YahooEquityDAO().save_all()
def _get_equity_price_list_as_dic(symbol): records = YahooEquityDAO().get_all_equity_price_by_symbol(symbol) result = {} for [d, v] in records: result[int(d.strftime('%Y%m%d'))] = v return result
def GET(self, from_date_str=None): default_from_date = TradeTime.get_latest_trade_date() - datetime.timedelta(60) if from_date_str is None or from_date_str == '': from_date = default_from_date else: try: input_from_date = datetime.datetime.strptime(from_date_str, '%Y-%m-%d').date() from_date = TradeTime.get_from_date_by_window(22, input_from_date) except Exception: from_date = default_from_date # records_index = VIXDAO().get_vix_price_by_symbol_and_date('VIY00', from_date=from_date) records_index = YahooEquityDAO().get_all_equity_price_by_symbol('^VIX', from_date.strftime('%Y-%m-%d'),'Closeprice') (records_f1, records_f2, records_f3) = VIXDAO().get_following_vix(from_date) new_spy_price = None if datetime.date.today() > TradeTime.get_latest_trade_date(): try: new_spy_price = YahooScraper.get_data_by_symbol('SPY') except Exception as e: new_spy_price = CNBCScraper.get_data_by_symbol('SPY') try: new_vix_price = YahooScraper.get_data_by_symbol('VIX') except Exception as e: new_vix_price = CNBCScraper.get_data_by_symbol('VIX') new_vix_features = CBOEScraper.get_vix_future() [new_f1, new_f2, new_f3] = [new_vix_features[0][2], new_vix_features[1][2], new_vix_features[2][2]] records_index.append([datetime.date.today(), new_vix_price]) records_f1.append([datetime.date.today(), new_f1]) records_f2.append([datetime.date.today(), new_f2]) records_f3.append([datetime.date.today(), new_f3]) dates = map(lambda x: x[0], records_index) price_index = map(lambda x: x[1], records_index) price_f1 = map(lambda x: x[1], records_f1) price_f2 = map(lambda x: x[1], records_f2) price_f3 = map(lambda x: x[1], records_f3) hv_records = self.get_historical_volatility(new_spy_price, from_date)[-len(dates):] hv_prices = map(lambda x: x[1]*100, hv_records) spy_prices = self.get_equity_prices('SPY', from_date, new_spy_price) [spy_low, spy_high] = self.get_low_and_upper(spy_prices, price_index) # shift append_dates = TradeTime.generate_dates(dates[-1], dates[-1] + datetime.timedelta(days=50)) dates = dates[21:] + (append_dates[0:21]) hv_prices = self.shift_values(hv_prices) spy_prices = self.shift_values(spy_prices) price_index = self.shift_values(price_index) price_f1 = self.shift_values(price_f1) price_f2 = self.shift_values(price_f2) price_f3 = self.shift_values(price_f3) if from_date < default_from_date: dates = dates[:42] hv_prices = hv_prices[:42] spy_prices = spy_prices[:42] spy_low = spy_low[:42] price_index = price_index[:42] price_f1 = price_f1[:42] price_f2 = price_f2[:42] price_f3 = price_f3[:42] fig = Figure(figsize=[24, 8]) ax1 = fig.add_axes([.1, .1, .8, .8]) ax2 = ax1.twinx() ax1.plot(dates, hv_prices, label='historical volatility', color='black') ax1.plot(dates, price_index, label='vix index', color='blue') ax1.plot(dates, price_f1, label='vix first month', color='deepskyblue') ax1.plot(dates, price_f2, label='vix second month', color='lightskyblue') ax1.plot(dates, price_f3, label='vix third month', color='lightblue') ax1.legend(loc='upper left') ax2.plot(dates, spy_prices, 'red', label='SPY') ax2.plot(dates, spy_low, 'orange', label='spy_low') # ax2.plot(dates, spy_high, 'yellow', label='spy_high') ax2.legend(loc='upper right') ax1.grid() ax1.xaxis.set_major_formatter(DateFormatter('%y%m%d')) ax1.set_xticks(dates) for tick in ax1.get_xticklabels(): tick.set_rotation(45) canvas = FigureCanvasAgg(fig) buf = cStringIO.StringIO() canvas.print_png(buf) data = buf.getvalue() return data