def __init__(self): self.vix_records = YahooEquityDAO().get_all_equity_price_by_symbol( '^VXV', from_date_str='2010-12-1') self.vxv_records = YahooEquityDAO().get_all_equity_price_by_symbol( '^VXMT', from_date_str='2010-12-1') self.vix_values = map(lambda x: x[1], self.vix_records) self.vxv_values = map(lambda x: x[1], self.vxv_records)
def __init__(self, vol_circle=10): from_date_str = '2006-07-17' spy_records = YahooEquityDAO().get_all_equity_price_by_symbol( 'SPY', from_date_str) self.vol_list = OptionCalculator.get_year_history_volatility_list( spy_records, vol_circle) self.vix_records = YahooEquityDAO().get_all_equity_price_by_symbol( '^VIX', from_date_str)[vol_circle:] self.vol_values = map(lambda x: x[1] * 100, self.vol_list) self.vix_values = map(lambda x: x[1], self.vix_records)
def __init__(self): self.spy_monthly_records = YahooEquityDAO( ).get_equity_monthly_price_by_symbol('SPY') self.spy_monthly_indicator = self.get_spy_monthly_indicator() self.vix_records = YahooEquityDAO().get_all_equity_price_by_symbol( '^VIX', from_date_str='2006-07-17') self.vxv_records = YahooEquityDAO().get_all_equity_price_by_symbol( '^VXV', from_date_str='2006-07-17') self.vix_values = map(lambda x: x[1], self.vix_records) self.vxv_values = map(lambda x: x[1], self.vxv_records)
def plot_2symbol(self, symbol1, symbol2, from_date_str='2006-07-17'): fig, ax = plt.subplots() records1 = YahooEquityDAO().get_all_equity_price_by_symbol( symbol1, from_date_str=from_date_str) records2 = YahooEquityDAO().get_all_equity_price_by_symbol( symbol2, from_date_str=from_date_str) values1 = map(lambda x: x[1], records1) values2 = map(lambda x: x[1], records2) dates = map(lambda x: x[0], records1) ax.plot(dates, values1, 'r-', label=symbol1) ax.plot(dates, values2, 'b-', label=symbol2) plt.legend(bbox_to_anchor=(1.05, 1), loc=8, borderaxespad=0.) plt.show()
def plot_xiv_vxx(self): fig, ax1 = plt.subplots() xiv_records = YahooEquityDAO().get_all_equity_price_by_symbol( 'XIV', from_date_str='2013-12-17') vxx_records = YahooEquityDAO().get_all_equity_price_by_symbol( 'VXX', from_date_str='2013-12-17') dates = map(lambda x: x[0], xiv_records)[0:756] xiv_values = map(lambda x: x[1], xiv_records)[0:756] vxx_values = map(lambda x: x[1], vxx_records)[0:756] ax2 = ax1.twinx() ax1.plot(dates, xiv_values, 'r-', label='xiv') ax2.plot(dates, vxx_values, 'b-', label='vxx') plt.legend(bbox_to_anchor=(1.05, 1), loc=8, borderaxespad=0.) plt.show()
def __init__(self): self.vix_records = YahooEquityDAO().get_all_equity_price_by_symbol( '^VIX', from_date_str='2006-07-17') self.vxv_records = YahooEquityDAO().get_all_equity_price_by_symbol( '^VXV', from_date_str='2006-07-17') # self.vix_records = YahooEquityDAO().get_all_equity_price_by_symbol('^VIX', from_date_str='2010-01-01') # self.vxv_records = YahooEquityDAO().get_all_equity_price_by_symbol('^VXV', from_date_str='2010-01-01') self.vix_values = map(lambda x: x[1], self.vix_records) self.vxv_values = map(lambda x: x[1], self.vxv_records) self.xiv_records = YahooEquityDAO().get_all_equity_price_by_symbol( 'XIV', from_date_str='2006-07-17') self.xiv_values = map(lambda x: x[1], self.xiv_records) self.vxx_records = YahooEquityDAO().get_all_equity_price_by_symbol( 'VXX', from_date_str='2006-07-17') self.vxx_values = map(lambda x: x[1], self.vxx_records)
def history_min(self, symbol, window): # yahoo_symbol = Symbols.get_mapped_symbol(symbol, Symbols.YahooSymbolMapping) us_dt = datetime.datetime.now(tz=pytz.timezone('US/Eastern')) end_time = datetime.datetime(us_dt.year, us_dt.month, us_dt.day, us_dt.hour, us_dt.minute, us_dt.second) days_window = window/391 + 2 from_date = TradeTime.get_from_date_by_window(days_window) start_time = datetime.datetime(from_date.year, from_date.month, from_date.day, 0, 0) rows = YahooEquityDAO().get_min_time_and_price(symbol, start_time, end_time) return rows[-window:]
def history_min(self, symbol, window, current_date_time): # us_dt = datetime.datetime.now(tz=pytz.timezone('US/Eastern')) # end_time = datetime.datetime(us_dt.year, us_dt.month, us_dt.day, us_dt.hour, us_dt.minute, us_dt.second) end_time = current_date_time days_window = window/391 + 2 from_date = TradeTime.get_from_date_by_window(days_window, current_date_time.date()) start_time = datetime.datetime(from_date.year, from_date.month, from_date.day, 0, 0) rows = YahooEquityDAO().get_min_time_and_price(symbol, start_time, end_time) return rows[-window:]
def plot_vxx(self): fig, ax = plt.subplots() vxx_records = YahooEquityDAO().get_all_equity_price_by_symbol( 'VXX', from_date_str='2010-12-17') vxx_values = map(lambda x: x[1], vxx_records) dates = map(lambda x: x[0], vxx_records) ax.plot(dates, vxx_values, 'r-', label='vxx') plt.legend(bbox_to_anchor=(1.05, 1), loc=8, borderaxespad=0.) plt.show()
def history(self, symbol, field, window): fields_dic = {'open': 'openPrice', 'close': 'adjclosePrice', 'high': 'highPrice', 'low': 'lowPrice', 'price': 'adjclosePrice', 'unadj':'closePrice'} 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, Symbols.YahooSymbolMapping) 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 plot_vxv_minus_vix(self): fig, ax = plt.subplots() dates = map(lambda x: x[0], self.vix_records) values = map(lambda x, y: x - y, self.vxv_values, self.vix_values) xiv_records = YahooEquityDAO().get_all_equity_price_by_symbol( 'XIV', from_date_str='2006-07-17') xiv_values = map(lambda x: x[1], xiv_records) ax.plot(dates, values, 'r-', label='vxv-vix') ax.plot(dates, xiv_values, 'b-', label='xiv') plt.legend(bbox_to_anchor=(1.05, 1), loc=8, borderaxespad=0.) plt.show()
def history(self, symbol, field, window, current_date): fields_dic = {'open': 'openPrice', 'close': 'adjclosePrice', 'high': 'highPrice', 'low': 'lowPrice', 'price': 'adjclosePrice', 'unadj':'closePrice'} 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, Symbols.YahooSymbolMapping) from_date = TradeTime.get_from_date_by_window(window+1, current_date) # window + 1: get one day more data. rows = YahooEquityDAO().get_equity_prices_by_start_end_date(yahoo_symbol, from_date, current_date, price_field) return rows[0:window] # remove current date data.
def plot_symbol_with_ma(self, symbol, ma_window): fig, ax = plt.subplots() records = YahooEquityDAO().get_all_equity_price_by_symbol( symbol, from_date_str='2006-07-17') values = map(lambda x: x[1], records) dates = map(lambda x: x[0], records)[ma_window:] values1 = pd.Series(values).rolling(window=5).mean().tolist()[5:] ma_values = pd.Series(values).rolling( window=ma_window).mean().tolist()[ma_window:] ax.plot(dates, values1[15:], 'r-', label=symbol) ax.plot(dates, ma_values, 'b-', label='%s_ma%s' % (symbol, ma_window)) plt.legend(bbox_to_anchor=(1.05, 1), loc=8, borderaxespad=0.) plt.show()
def condition_momentum(self, symbol, momentum_window=10): records = YahooEquityDAO().get_all_equity_price_by_symbol( symbol, from_date_str='2006-07-17') values = map(lambda x: x[1], records) result = [] for i, value in enumerate(values): if i < momentum_window: result.append(True) else: if value > values[i - momentum_window]: result.append(True) else: result.append(False) return result
def plot_vxv_ma_minus_vix_ma(self, ma_window): fig, ax = plt.subplots() dates = map(lambda x: x[0], self.vix_records)[ma_window:] vxv_ma = pd.Series(self.vxv_values).rolling( window=ma_window).mean().tolist()[ma_window:] vix_ma = pd.Series(self.vix_values).rolling( window=ma_window).mean().tolist()[ma_window:] values = map(lambda x, y: x - y, vxv_ma, vix_ma) xiv_records = YahooEquityDAO().get_all_equity_price_by_symbol( 'XIV', from_date_str='2006-07-17') xiv_values = map(lambda x: x[1], xiv_records)[ma_window:] ax.plot(dates, values, 'r-', label='vxv_ma-vix_ma, ma=%s' % ma_window) ax.plot(dates, xiv_values, 'b-', label='xiv') plt.legend(bbox_to_anchor=(1.05, 1), loc=8, borderaxespad=0.) plt.show()
def plot_z_score_with_xiv(self, ma_window_short=7, ma_window_long=20): rate = map(lambda x, y: x / y, self.vxv_values, self.vix_values) rate_ma_short = pd.Series(rate).rolling( window=ma_window_short).mean().tolist() rate_ma_long = pd.Series(rate).rolling( window=ma_window_long).mean().tolist() rate_std = pd.Series(rate).rolling( window=ma_window_long).std().tolist() zscore = map(lambda x, y, z: (x - y) / z, rate_ma_short[ma_window_long:], rate_ma_long[ma_window_long:], rate_std[ma_window_long:]) dates = map(lambda x: x[0], self.vxv_records)[ma_window_long:] xiv_records = YahooEquityDAO().get_all_equity_price_by_symbol( 'XIV', from_date_str='2006-07-17') xiv_values = map(lambda x: x[1], xiv_records[ma_window_long:]) fig, ax1 = plt.subplots() ax2 = ax1.twinx() ax1.plot(dates, zscore, 'r-', label='z score') ax2.plot(dates, xiv_values, 'b-', label='xiv') plt.legend(bbox_to_anchor=(1.05, 1), loc=8, borderaxespad=0.) plt.show()
def _get_daily_records(symbol, start, end): return YahooEquityDAO().get_equity_prices_by_start_end_date(symbol, start, end)
def _get_min_records(symbol, start, end): return YahooEquityDAO().get_min_time_and_price(symbol, start, end)
risk_free_interest_rate, sigma, flag='c'): theta = vollib.black_scholes.greeks.numerical.theta( flag, underlying_price, strike_price, left_days / 365.0, risk_free_interest_rate, sigma) return theta @staticmethod def get_rho(underlying_price, strike_price, left_days, risk_free_interest_rate, sigma, flag='c'): rho = vollib.black_scholes.greeks.numerical.rho( flag, underlying_price, strike_price, left_days / 365.0, risk_free_interest_rate, sigma) return rho if __name__ == '__main__': import datetime from dataaccess.db 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 = OptionCalculator.get_year_history_volatility_list(equity_records) print results
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 history_30_min(self, symbol, window): return YahooEquityDAO().get_latest_equity_30_min_prices(symbol, window)
def history_30_min(self, symbol, window, current_date_time): return YahooEquityDAO().get_latest_equity_30_min_prices(symbol, window, end_time=current_date_time)
def __init__(self): self.xiv_records = YahooEquityDAO().get_all_equity_price_by_symbol( 'XIV', from_date_str='2006-07-17') self.xiv_values = map(lambda x: x[1], self.xiv_records)