debug_t_dates = [1226901600,] debug_expiration_dates = [1232172000,] debug_symbols = ['FAS'] # if getting data, clear the existing data first or view_smiles # will report errors (multiple hits for unique data) if not DEBUG is True: sqlite3tools.delete_sqlite_table(conn, TABLENAME) if DEBUG is True and debug_symbols is not None: symbols = debug_symbols for symbol in symbols: params = dict() params['symbol'] = symbol sql = SQL.get_t_dates % params t_dates = execute_query_DF(conn, sql)('t_date') if DEBUG is True and debug_t_dates is not None: t_dates = debug_t_dates for t_date in t_dates: print print symbol + ":", print str(epoch2datetime(int(t_date)).date()).rjust(11), params['t_date'] = t_date sql = SQL.get_spot_closing_price % params stock_price_data = execute_query_DF(conn, sql)('price_close_ini') if not len(stock_price_data) == 1: msg = "More than once spot closing price for %s on %s. Bad dog." % (symbol, t_date) print len(stock_price_data) raise Exception, msg else: spot_closing_price = stock_price_data[0]
from datetimetools import * import SQL_view_smiles as SQL import scipy import datetime from matplotlib import pyplot conn = sqlite3.connect("Levered_Financial_ETF_Option_and_Stock_DB.sqlite3") symbols = ['FAS','FAZ','SKF','UYG'] symbols = ['FAS',] for symbol in symbols: params = dict() params['symbol'] = symbol sql = SQL.get_t_dates % params t_dates = execute_query_DF(conn, sql) for t_date in t_dates('t_date'): params['t_date'] = t_date sql = SQL.get_expiration_dates % params expiration_dates = execute_query_DF(conn, sql) for expiration_date in expiration_dates('expiration_date'): # Lets get some data params['expiration_date'] = expiration_date time_to_expiration = (float(expiration_date) - float(t_date)) / seconds_per_year sql = SQL.get_implied_forward % params implied_forward = execute_query_DF(conn, sql)('implied_forward')[0] sql = SQL.get_strike_and_cvol_and_pvol_and_call_delta % params sql = SQL.get_fit_smile % params fit_params = execute_query_DF(conn, sql) if not fit_params.numrows() == 1:
xmax = 500 ymin = .2 ymax = 3.5 pyplot.axis([xmin, xmax, ymin, ymax]) alpha_days = np.arange(0, 500, 2) lb = [.3, .2, 1] ub = [5, 3, 10] if DEBUG is True and debug_symbols is not None: symbols = debug_symbols for symbol in symbols: params = dict() params['symbol'] = symbol sql = SQL.get_t_dates % params t_dates = execute_query_DF(conn, sql)('t_date') if DEBUG is True and debug_t_dates is not None: t_dates = debug_t_dates for t_date in t_dates: params['t_date'] = t_date sql = SQL.get_expiration_dates_and_vols % params data = execute_query_DF(conn, sql) if data.numrows() < 4: continue print t_date expiration_dates = np.array([int(i) for i in data('expiration_date')]) days = (expiration_dates - int(t_date)) / seconds_per_day times_to_expiration = days / 365. vol = data('vol')
pair = ('FAS','FAZ') fit_parameter = 'vol' start_date = datetime.date(2009, 1, 2) end_date = datetime.date(2009, 11, 12) symbol1 = pair[0] symbol2 = pair[1] params = dict() params['symbol1'] = symbol1 params['symbol2'] = symbol2 params['fit_parameter'] = fit_parameter params['start_date'] = int(datetime2epoch(start_date)) params['end_date'] = int(datetime2epoch(end_date)) sql = SQL.get_fit_coeff % params print sql fit_coeff = execute_query_DF(conn, sql) coeff1 = fit_coeff('coeff1') coeff2 = fit_coeff('coeff2') price1 = fit_coeff('price_close1') price2 = fit_coeff('price_close2') t_dates = fit_coeff('t_date') price_close_ini1 = fit_coeff('price_close_ini1') price_close_ini2 = fit_coeff('price_close_ini2') # Linear fit linear_fit = scipy.polyfit(coeff1, coeff2, deg=1) xmin = min(coeff1) xmax = max(coeff1) fit_x = np.arange(xmin, xmax, (xmax - xmin) / 50.) fit_y = scipy.polyval(linear_fit, fit_x)