def GET(self, symbol, days): try: days = int(days) except Exception: days = 63 from_date = TradeTime.get_latest_trade_date() - datetime.timedelta(days) equity_records = EquityDAO().get_all_equity_price_by_symbol(symbol, from_date) if datetime.date.today() > TradeTime.get_latest_trade_date(): new_spy_price = YahooScraper.get_data_by_symbol(symbol) equity_records.append([datetime.date.today(), new_spy_price]) dates = map(lambda x: x[0], equity_records) equity_prices = map(lambda x: x[1], equity_records) fig = Figure(figsize=[16, 4]) ax = fig.add_axes([.1, .1, .8, .9]) ax.plot(dates, equity_prices, label='price') 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) # conver to canvas canvas = FigureCanvasAgg(fig) buf = cStringIO.StringIO() canvas.print_png(buf) data = buf.getvalue() return data
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)
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 init_option_symbols(self): expiration_date = self.find_expiration_date() start_date_price = EquityDAO().get_equity_price_by_date( self.symbol, self.start_date) days_to_current_date = (datetime.datetime.today() - self.start_date).days self.call_symbol = OptionDAO().find_symbol(self.symbol, expiration_date, start_date_price * (1+self.delta),\ current_date=self.start_date, days_to_current_date=days_to_current_date, option_type='Call') self.put_symbol = OptionDAO().find_symbol(self.symbol, expiration_date, start_date_price * (1 - self.delta), \ current_date=self.start_date, days_to_current_date=days_to_current_date, option_type='Put')
def plot_equity_lines(symbols): df = EquityDAO().select_by_symbols(symbols) print df fig, ax = plt.subplots() x = None for the_symbol in symbols: sub_df = df[df.symbol == the_symbol] x = sub_df['tradeTime'] y = sub_df['lastPrice'] ax.plot(x, y) plt.show()
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 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')