def __init__(self): self.circle = 20 self.option_dao = OptionDAO() # get the equity records from 100 date ago. #from_date_str = (datetime.date.today() - datetime.timedelta(100)).strftime('%Y-%m-%d') from_date = (datetime.date.today() - datetime.timedelta(100)) #self.spy_records = YahooEquityDAO().get_all_equity_price_by_symbol('SPY', from_date_str) self.spy_records = EquityDAO().get_all_equity_price_by_symbol( 'SPY', from_date) self.hv_spy = OptionCalculater.get_year_history_volatility_list( self.spy_records, self.circle) self.spy_delta_records = self.get_delta_records( 'SPY', self.spy_records) from_date = TradeTime.get_latest_trade_date() - datetime.timedelta(50) self.vix_index_records = VIXDAO().get_vix_price_by_symbol_and_date( 'VIY00', from_date=from_date) (records_f1, records_f2, records_f3) = VIXDAO().get_following_vix(from_date) self.vixf1_records = records_f1 self.vix_delta_records = map(lambda x, y: [x[0], y[1] - x[1]], self.vix_index_records, self.vixf1_records) self.hv_vix = list(self.calculate_f1_volatilities()) #vxx_records = YahooEquityDAO().get_all_equity_price_by_symbol('VXX', from_date_str) vxx_records = EquityDAO().get_all_equity_price_by_symbol( 'VXX', from_date) self.vxx_delta_records = self.get_delta_records('VXX', vxx_records)
def __init__(self): from_date = (datetime.date.today() - datetime.timedelta(150)) self.spy_records = EquityDAO().get_all_equity_price_by_symbol('SPY', from_date) self.vix_records = VIXDAO().get_vix_price_by_symbol_and_date('VIY00', from_date=from_date) if datetime.date.today() > TradeTime.get_latest_trade_date(): new_spy_price = YahooScraper.get_data_by_symbol('SPY') new_vix_price = YahooScraper.get_data_by_symbol('VIX') self.spy_records.append([datetime.date.today(), new_spy_price]) self.vix_records.append([datetime.date.today(), new_vix_price]) self.dates = map(lambda x: x[0], self.spy_records) self.spy_values = map(lambda x: x[1], self.spy_records) self.vix_values = map(lambda x: x[1], self.vix_records)
def push_to_db(self): self.logger.info('Push equity data to db...') EquityDAO().insert(self.parser.equity_records) self.logger.info('Push option data to db...') OptionDAO().insert(self.parser.option_records) self.logger.info('Push vix data to db...') VIXDAO().insert(self.parser.vix_records)
def update_delta(risk_free_interest_rate=0.005): date_price_records = VIXDAO().get_vix_price_by_symbol('VIY00') date_hv_lst = OptionCalculater.get_year_history_volatility_list( date_price_records) date_price_dic = list_to_hash(date_price_records) date_hv_dic = list_to_hash(date_hv_lst) option_dao = OptionDAO() vix_option_records = option_dao.get_vix_options() conn = BaseDAO.get_connection() cursor = conn.cursor() count = 0 for (symbol, trade_date, left_days, strike_price, option_type) in vix_option_records: underlying_price = date_price_dic.get(trade_date) sigma = date_hv_dic.get(trade_date) if underlying_price is not None and sigma is not None: delta = OptionCalculater.get_delta(underlying_price, strike_price, left_days, risk_free_interest_rate, sigma, option_type[0:1].lower()) option_dao.update_delta_for_vix_options( symbol, trade_date, delta, cursor) count += 1 if count == 1000: conn.commit() count = 0 conn.commit() conn.close()
class UpperSPY(object): def __init__(self): from_date = (datetime.date.today() - datetime.timedelta(150)) self.spy_records = EquityDAO().get_all_equity_price_by_symbol('SPY', from_date) self.vix_records = VIXDAO().get_vix_price_by_symbol_and_date('VIY00', from_date=from_date) if datetime.date.today() > TradeTime.get_latest_trade_date(): new_spy_price = YahooScraper.get_data_by_symbol('SPY') new_vix_price = YahooScraper.get_data_by_symbol('VIX') self.spy_records.append([datetime.date.today(), new_spy_price]) self.vix_records.append([datetime.date.today(), new_vix_price]) self.dates = map(lambda x: x[0], self.spy_records) self.spy_values = map(lambda x: x[1], self.spy_records) self.vix_values = map(lambda x: x[1], self.vix_records) def GET(self): low_rate = 1.25 * np.sqrt(21)/100/np.sqrt(252) high_rate = 1.75 * np.sqrt(21)/100/np.sqrt(252) spy_low = map(lambda x, y: x * (1 + low_rate*y), self.spy_values, self.vix_values) spy_high = map(lambda x, y: x * (1 + high_rate*y), self.spy_values, self.vix_values) append_dates = TradeTime.generate_dates(self.dates[-1], self.dates[-1] + datetime.timedelta(days=50)) shifted_dates = self.dates[21:] + (append_dates[0:21]) append_spy_values = [self.spy_values[-1]] * 21 shifted_spy_values = self.spy_values[21:] + append_spy_values fig = Figure(figsize=[60, 10]) ax = fig.add_axes([.1, .1, .8, .8]) ax.plot(shifted_dates, shifted_spy_values, label='spy', color='black') ax.plot(shifted_dates, spy_low, label='spy low', color='blue') # line1.set_dashes([2, 4]) ax.plot(shifted_dates, spy_high, label='spy high', color='skyblue') # line2.set_dashes([8, 4, 2, 4, 2, 4]) # autodates = AutoDateLocator() # ax.xaxis.set_major_locator(autodates) yearsFmt = DateFormatter('%Y-%m-%d') ax.xaxis.set_major_formatter(yearsFmt) ax.set_xticks(shifted_dates) # ax.xticks(shifted_dates, rotation=90) for tick in ax.get_xticklabels(): tick.set_rotation(45) ax.legend(loc='upper left') ax.grid() canvas = FigureCanvasAgg(fig) buf = cStringIO.StringIO() canvas.print_png(buf) data = buf.getvalue() return data
def plot_vix_3in1(): from_date = TradeTime.get_latest_trade_date() - datetime.timedelta(50) records_index = VIXDAO().get_vix_price_by_symbol_and_date( 'VIY00', from_date=from_date) dates = map(lambda x: x[0], records_index) price_index = map(lambda x: x[1], records_index) (records_f1, records_f2, records_f3) = VIXDAO().get_following_vix(from_date) price_f1 = map(lambda x: x[1], records_f1) price_f2 = map(lambda x: x[1], records_f2) fig, ax = plt.subplots() ax.plot(dates, price_index, label='index') ax.plot(dates, price_f1, label='f1') ax.plot(dates, price_f2, label='f2') plt.grid() plt.legend(bbox_to_anchor=(1.05, 1), loc=8, borderaxespad=0.) plt.show()
def calculate_f1_volatilities(self): symbols = list(set(map(lambda x: x[2], self.vixf1_records))) symbol_dic = {} for symbol in symbols: symbol_records = VIXDAO().get_vix_price_by_symbol_and_date( symbol, self.vixf1_records[0][0]) hv_dic = list_to_hash( OptionCalculater.get_year_history_volatility_list( symbol_records, self.circle)) symbol_dic[symbol] = hv_dic for vix_f1_record in self.vixf1_records[20:]: date = vix_f1_record[0] symbol = vix_f1_record[2] hv = symbol_dic[symbol].get(date) yield [date, hv]
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 init_data(self, symbol): # df_equity = EquityDAO().get_date_price_list(symbol) # price_list = df_equity['price'].to_list() # OptionCalculater().get_year_history_volatility(price_list) # get the equity records from 150 date ago. #from_date_str = (datetime.date.today() - datetime.timedelta(150)).strftime('%Y-%m-%d') from_date = (datetime.date.today() - datetime.timedelta(150)) #equity_records = YahooEquityDAO().get_all_equity_price_by_symbol(symbol, from_date_str) equity_records = EquityDAO().get_all_equity_price_by_symbol(symbol, from_date) current_quity_price = equity_records[-1][1] option_iv_records = OptionDAO().get_corresponding_implied_volatilities(symbol, current_quity_price) first_tradetime = option_iv_records[0][0] circle = 30 equity_start_date = first_tradetime - datetime.timedelta(circle) # trade_day_circle = len(filter(lambda x: x[0] >= equity_start_date and x[0] < first_tradetime, equity_records)) hv_records = OptionCalculater.get_year_history_volatility_list(filter(lambda x: x[0] >= equity_start_date, equity_records), circle) self.equity_records = filter(lambda x: x[0] >= first_tradetime, equity_records) self.hv_records = hv_records self.iv_records = option_iv_records if symbol.upper() == 'SPY': self.iv_records = VIXDAO().get_vix_price_by_symbol('VIY00')
def ingest_vix_records(): records = CBOEScraper.get_vix_records() VIXDAO().insert(records)
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
file_path = os.path.join(self.daily_path, 'vix_data', 'vix.json') with open(file_path) as fs: json_data = json.load(fs) for record in json_data['data']: vix = VIX.loads(record) yield vix def load_all(self): for symbol in Symbols.get_option_symbols(): equity = self.load_equity_data_by_symbol(symbol) self.equity_records.append(equity) option_list = list( self.load_option_data_by_symbol(symbol, equity.tradeTime)) self.option_records.extend(option_list) self.vix_records = list(self.load_vix_data_by()) if __name__ == '__main__': #parser = RawDataParser(PathMgr.get_data_path(str(datetime.date.today()))); parser = RawDataParser(PathMgr.get_raw_data_path('2017-11-25')) #parser.load_equity_data_by_symbol('UNG') #parser.load_option_data_by_symbol('UNG') #vix_list = list(parser.load_vix_data_by_symbol()) #print len(vix_list) #print vix_list[8].to_json() #parser.load_all() #print parser.vix_records records = list(parser.load_vix_data_by()) from dataaccess.vixdao import VIXDAO VIXDAO().insert(records)
def add_missing_data_for_vix(self): viq17 = VIX(symbol='VIQ17', lastPrice=12.160, dailyHighPrice=12.560, dailyLowPrice=11.900, dailyLastPrice=12.160,\ previousPrice=12.325, dailyPreviousPrice=12.325, volume=10365,dailyDate1dAgo= '2017-08-16') VIXDAO().insert([viq17])