def main(): #codes = session.execute('select distinct(code) from day_k_data;').fetchall() codes = [['MOMO']] rate = {} start_date = '2017-01-04' date_delta = -date_today_delta(start_date, '%Y-%m-%d') norm_type = 'character' quota_index = 0 codes = [code[0] for code in codes] #date_deltas = range(50,200,20) #start_dates = [date_add(start_date,i) for i in range(10,50,10)] #df = pd.read_sql('select * from day_k_data where code="MOMO" order by date asc;',engine) #rate = single_test(df,start_dates,date_deltas,norm_type,quota_index) #open_market_dates = session.execute('select distinct(code),date from day_k_data;').fetchall() for code in codes[:1]: print code try: df = pd.read_sql( 'select * from day_k_data where code="' + code + '" order by date asc;', engine) for i in range(len(df['date'])): df.date[i] = df.date[i].split(' ')[0] r = back_test(df, start_date, date_delta, norm_type, quota_index) if r: rate[code] = r except Exception, e: print e, 'line 212' continue
def write_to_database(options_info): for code in options_info: for date in options_info[code]: if date_today_delta(date) <= 0: continue dfop = pd.DataFrame(options_info[code][date]).set_index('17Strike') dfop.insert(0,'date',date_today) dfop = dfop.drop_duplicates() dfop.to_sql('option_price',engine,if_exists='append')
def write_to_database(options_info): df_back = pd.DataFrame() for date in options_info: if date_today_delta(date) <= 0: continue dfop = pd.DataFrame(options_info[date]) dfop.insert(0, 'date', date_today) if df_back.empty: df_back = dfop df_back = pd.concat([df_back, dfop], axis=0) df_back = df_back.set_index('17Strike') df_back.to_sql('option_greeks', engine, if_exists='append')
#coding:utf-8 import numpy as np from scipy.stats import norm import datetime, commfunction # 设定参数 r = 0.03975 # risk-free interest rate expire_day = 'Jul 21, 2017' # time to expire t = float(commfunction.date_today_delta( expire_day, format='%b %d, %Y')) / 365 # time to expire (30 days) q = 0 # dividend yield S0 = 40.59 # underlying price X = 39 # strike price mktprice_call = 2.56 # market price #mktprice_put = 5.7 # market price # 用二分法求implied volatility,暂只针对call option sigma = 0.65487 # initial volatility def call_implied_sigma(S0, X, r, t, sigma, mktprice, q=0): C = 0 upper = 1 lower = 0 while abs(C - mktprice) > 1e-4: d1 = (np.log(S0 / X) + (r - q + sigma**2 / 2) * t) / (sigma * np.sqrt(t)) d2 = d1 - sigma * np.sqrt(t) C = S0 * np.exp(-q * t) * norm.cdf(d1) - X * np.exp( -r * t) * norm.cdf(d2) if C - mktprice > 0:
options_info[code][date][column_name].append('') return options_info for stock in stocks: stock = 'YY' options_info = get_option_pg_datas( [stock], pre='http://www.nasdaq.com/symbol/', tail='/option-chain/greeks?dateindex=-1') option_price_history = pd.read_sql( 'select * from option_greeks where `16Root`="' + stock + '" order by date asc;', engine) options_info = to_dataframe(options_info) for d in options_info['18Puts']: if date_today_delta(d) <= 0: continue for s in list(options_info[options_info['18Puts'] == d]['17Strike']): t_price = options_info[options_info['18Puts'] == d] t_price = t_price[t_price['17Strike'] == s].set_index('date') h_prices = option_price_history[option_price_history['18Puts'] == d] h_prices = h_prices[h_prices['17Strike'] == s].set_index('date') iv = pd.concat( [h_prices[['15IV', '24IV']], t_price[['15IV', '24IV']]], axis=0) print stock, d, s print iv try: iv.plot() plt.legend(('Calls_IV', 'PUTS_IV'), loc='upper left')