Beispiel #1
0
def option_filter(ticker, moneyness_thresh, dte_thresh):
    fwd_date = dt.datetime.today() + dt.timedelta(days=dte_thresh)
    tape = Options(ticker, 'yahoo')
    data = tape.get_options_data(month=fwd_date.month,
                                 year=fwd_date.year).reset_index()
    data['Moneyness'] = np.abs(
        data['Strike'] - data['Underlying_Price']) / data['Underlying_Price']

    data['DTE'] = (data['Expiry'] - dt.datetime.today()).dt.days
    data = data[[
        'Strike', 'DTE', 'Type', 'IV', 'Vol', 'Open_Int', 'Moneyness', 'Root',
        'Underlying_Price', 'Last', 'Bid', 'Ask'
    ]]
    data['Mid'] = data['Ask'] - data['Bid']

    filtered_data = data[(data['Moneyness'] <= moneyness_thresh) & (
        data['DTE'] <= dte_thresh)].reset_index()[data.columns]
    put_ivs = filtered_data[filtered_data.Type == 'put'].pivot(
        index='Strike', columns='DTE', values='IV').dropna()
    call_ivs = filtered_data[filtered_data.Type == 'put'].pivot(
        index='Strike', columns='DTE', values='IV').dropna()
    hv_data = current_volatility([ticker])

    put_ivs['Close'] = hv_data['close'][0]
    call_ivs['Close'] = hv_data['close'][0]
    put_ivs['Daily HV'] = hv_data['daily_ann'][0]
    call_ivs['Daily HV'] = hv_data['daily_ann'][0]
    put_ivs['Intra HV'] = hv_data['intra_ann'][0]
    call_ivs['Intra HV'] = hv_data['intra_ann'][0]
    put_ivs['Overnight HV'] = hv_data['ovrnt_ann'][0]
    call_ivs['Overnight HV'] = hv_data['ovrnt_ann'][0]
    put_ivs['Daily Dollar Vol'] = hv_data['daily_dollar_vol'][0]
    call_ivs['Daily Dollar Vol'] = hv_data['daily_dollar_vol'][0]

    put_ivs['Moneyness'] = np.abs(put_ivs.index -
                                  put_ivs['Close']) / put_ivs['Close']
    call_ivs['Moneyness'] = np.abs(call_ivs.index -
                                   call_ivs['Close']) / call_ivs['Close']

    call_ivs.index.name = ticker + ' Call Strike'
    put_ivs.index.name = ticker + ' Put Strike'
    return call_ivs, put_ivs
Beispiel #2
0
def H2_read_option_ws(symbol_store, dy):
    """
    This function stands for hourly job 2, read option with symbol.
    Input here: 
        -symbol_store = a list of symbols that shall be updated daily
        -dy = directory in string about where to find and store data.
    
    5-19-18: changed from google to yahoo for function H2_read_option_ws
    and also, I dont think we need H1 function anymore.
    """

    print("H2 google finance options info reading...")
    errorlist = []
    spfile = dy + "/sp500.json"
    spfile_f = formulate_directory(spfile)
    index = 1
    for symbol in symbol_store:
        try:
            data = Options(symbol,
                           'yahoo')  # read the option data from google finance
            exp_list = [data.expiry_dates[0]]
            print('1', symbol, max(exp_list))
            for i in range(10):
                data = Options(
                    symbol,
                    'yahoo')  # read the option data from google finance
                exp_list.append(data.expiry_dates[0])

            df = data.get_options_data(expiry=max(exp_list))
            row_num = df.shape[0]
            for j in range(10):
                df_tmp = data.get_options_data(expiry=max(exp_list))
                if df_tmp.shape[0] > row_num:
                    print("find larger df for %s, row num change %d -> %d" %
                          (symbol, row_num, df_tmp.shape[0]))
                    df = df_tmp.copy()
                    row_num = df_tmp.shape[0]
            df.to_csv(spfile_f, sep=',', mode='a')
            print(index, " Done for :", symbol, "\n")
        except TypeError:
            traceback.print_exc()
            errorlist.append(symbol)
            continue  #shall this be continue?
        except KeyError:
            traceback.print_exc()
            print('2', symbol, max(exp_list))
            print("error with stock:  ", symbol)
            errorlist.append(symbol)
            continue
        except Exception:
            traceback.print_exc()
            print('3', symbol, max(exp_list))
            print("error with stock:  ", symbol)
            errorlist.append(symbol)
            continue
        index += 1
        factor = rd.random() * 15
        time.sleep(0.1 * factor)
    path_error = dy + "/errorlist.txt"
    f = open(path_error, 'w')  #shall use 'a' for append
    for elements in errorlist:
        f.write(str(elements))
    f.close()
    print("H2 options info reading finished.")
    print("number of symbols with error: " + str(len(errorlist)) + "\n\n")
Beispiel #3
0
# PANDAS
import pandas_datareader.data as web
import datetime

start = datetime.datetime(2015,1,1) 
end = datetime.datetime(2017,1,1)

facebook = web.DataReader('FB', 'google', start, end)
facebook.head()

from pandas_datareader.data import Options
fb_options = Options('FB', 'google')
options_df = fb_options.get_options_data(expiry=fb_options.expiry_dates[0])
options_df.head()

# QUADL
import quandl
mydata = quandl.get('EIA/PET_RWTC_D')
# can use , returns='numpy'
import matplotlib.pyplot as plt
mydata.plot()

mydata = quandl.get('ZILLOW/C9_ZRIFAH')

mydata = quandl.get('WIKI/AAPL')
mydata.head()

    def _ProcessOne(self, sym, expir, time_out=None):
        """
        process one symbol, one expiry date, one strike
        time_out: maximum seconds waiting for the request
        """
        if time_out == None:
            time_out = self.req_timeout_1   # the default timeout length

        tmpOption = Options(sym, 'yahoo')

        logging.info("{} - {}: Requesting".format(sym, expir))
        # set timeout for single request
        with timeout(seconds = time_out):
            mydata = tmpOption.get_options_data(expiry = expir)
            
        logging.info("{} - {}: Finished request, insert to table...".format(sym, expir))
        mydata = mydata.reset_index()

        # within dataset "mydata", go through each row and insert to DB 
        for i in range(len(mydata.index)):
            onerow = mydate.iloc[[i]]
            # clean data, sometime bid and ask contains strange values
            tmpBid = onerow.Bid[i]
            tmpAsk = onerow.Ask[i]
            if type(onerow.Bid[i]) is not numpy.float64:
                tmpBid = 0.0
            if type(onerow.Ask[i]) is not numpy.float64:
                tmpAsk = 0.0
            
            # clean data, sometime open_interest is not a number
            tmpOpenInt = onerow.Open_Int[i]
            if onerow.Open_Int[i] == '-':
                tmpOpenInt = 0                
            # replace missing value '-' with '0'
            # insert_str = insert_str.replace('NaT', '0')
            # insert_str = insert_str.replace(' -,', ' 0,')


            # for index symbols (^VIX ==> VIX)
            tmpsym = sym
            if tmpsym[0] == "^":
                tmpsym = tmpsym[1:]

            # Raw exception handeling function
            self,_HandelingRaw(i, onerow)

            # gen sql insert statement
            insert_str = """INSERT INTO OPT_{} (underlying_symbol, option_symbol,
            strike, expiry, option_type, quote_date, last, bid, ask, vol,
            open_int, IV, underlying_price) VALUES 
            ('{}', '{}', {}, '{}', '{}', '{}', {}, {}, {}, {}, {}, {}, {});""".format(tmpsym,\
                    onerow.Root[i], onerow.Symbol[i], onerow.Strike[i], \
                    str(onerow.Expiry[i].date()), onerow.Type[i], str(onerow.Quote_Time[i].date()),\
                    onerow.Last[i], tmpBid, tmpAsk,\
                    onerow.Vol[i], tmpOpenInt, float(onerow.IV[i].strip('%'))/100,\
                    onerow.Underlying_Price[i])
            # push into db
             
            try:
                self.rowcnt += self.cur.execute(insert_str) 
            except Exception as err:
                # exception while inserting to database
                print("Exception occured while inserting to database")
                print(insert_str)
                print(mydata.iloc[[i]])
                logging.error("Exception occured while inserting to database")
                logging.error(insert_str)
                logging.error(mydata.iloc[[i]])
                raise

            self.conn.commit()
            # end of for loop

        logging.info("{} - {}: Inserted to table, {} rows affected.".format(sym, expir, self.rowcnt))
        print("{} - {}: Inserted to table, {} rows affected.".format(sym, expir, self.rowcnt))
        self.rowcnt = 0
Beispiel #5
0
import pandas_datareader as web
from pandas_datareader.data import Options
import datetime

import quandl

start = datetime.datetime(2015, 1, 1)
end = datetime.datetime(2019, 1, 1)

########## Yahoo & Google ##########
# Equity Prices
facebook = web.DataReader("FB", "yahoo", start, end)
# Option Prices
fb_options = Options("FB", "yahoo")
fb_options.get_options_data(expiry=fb_optipns.expirt_dates[0])

########## Quandl ##########

mydata = quandl.get("EIA/PET_RWTC_D", returns="numpy")
realEstate_SF = quandl.get("ZILLOW/C13_ZRIFAH")
stockApple = quandl.get("WIKI/AAPL.1")  # .1 gives the first column

Beispiel #6
0
def greek_calc(ticker,
               dte_ub,
               dte_lb,
               prem_price_use='Mid',
               delta_filter=0.2,
               expiry_set=0):
    fwd_date = dt.datetime.today() + dt.timedelta(days=dte_ub)
    tape = Options(ticker, 'yahoo')
    options_chain = tape.get_options_data(month=fwd_date.month,
                                          year=fwd_date.year).reset_index()
    options_chain = options_chain[[
        'Strike', 'Expiry', 'Type', 'Last', 'Bid', 'Ask', 'Vol', 'Open_Int',
        'IV', 'Underlying_Price'
    ]]
    df = options_chain
    df['DTE'] = (df['Expiry'] - dt.datetime.today()).dt.days
    df['Mid'] = (df['Ask'] + df['Bid']) / 2
    df = df[(df['DTE'] <= dte_ub) & (df['DTE'] >= dte_lb)]
    df = df.reset_index()[df.columns]

    year = 365
    premiums = df[prem_price_use].values  # 'Last' or 'Mid'
    strikes = df['Strike'].values
    time_to_expirations = df['DTE'].values
    ivs = df['IV'].values
    underlying = df['Underlying_Price'].values[0]
    types = df['Type'].values

    # Make sure nothing thows up
    assert len(premiums) == len(strikes)
    assert len(strikes) == len(time_to_expirations)

    sigmas = []
    deltas = []
    gammas = []
    thetas = []
    vegas = []
    for premium, strike, time_to_expiration, flag in zip(
            premiums, strikes, time_to_expirations, types):

        # Constants
        P = premium
        S = underlying
        K = strike
        t = time_to_expiration / float(year)
        r = 0.005 / 100
        q = 0 / 100
        try:
            sigma = py_vollib.black_scholes_merton.implied_volatility.implied_volatility(
                P, S, K, t, r, q, flag[0])
            sigmas.append(sigma)
        except:
            sigma = 0.0
            sigmas.append(sigma)

        try:
            delta = py_vollib.black_scholes_merton.greeks.analytical.delta(
                flag[0], S, K, t, r, sigma, q)
            deltas.append(delta)
        except:
            delta = 0.0
            deltas.append(delta)

        try:
            gamma = py_vollib.black_scholes_merton.greeks.analytical.gamma(
                flag[0], S, K, t, r, sigma, q)
            gammas.append(gamma)
        except:
            gamma = 0.0
            gammas.append(gamma)

        try:
            theta = py_vollib.black_scholes_merton.greeks.analytical.theta(
                flag[0], S, K, t, r, sigma, q)
            thetas.append(theta)
        except:
            theta = 0.0
            thetas.append(theta)

        try:
            vega = py_vollib.black_scholes_merton.greeks.analytical.vega(
                flag[0], S, K, t, r, sigma, q)
            vegas.append(vega)
        except:
            vega = 0.0
            vegas.append(vega)

    ivs = np.array(sigmas)
    df['Calc IV'] = ivs
    df['Delta'] = deltas
    df['Gamma'] = gammas
    df['Theta'] = thetas
    df['Vega'] = vegas
    df = df.dropna()

    expiry_filter = df.sort_values('DTE')['DTE'].drop_duplicates().values[min(
        expiry_set, len(df.sort_values('DTE')['DTE'].drop_duplicates()))]

    calls = df[(abs(df['Delta']) >= delta_filter) & (df['Type'] == 'call') &
               (df['DTE'] == expiry_filter)].reset_index()[df.columns]
    puts = df[(abs(df['Delta']) >= delta_filter) & (df['Type'] == 'put') &
              (df['DTE'] == expiry_filter)].reset_index()[df.columns]

    return calls, puts
Beispiel #7
0
import numpy as np
import pandas as pd
import pandas_datareader.data as web
import datetime
from pandas_datareader.data import Options

start = datetime.datetime(2010, 1, 1)
end = datetime.datetime(2013, 1, 27)
goog = web.DataReader('GOOG', 'yahoo', start, end)
goog.ix['2010-01-04']

goog_o = Options('GOOG', 'yahoo')
goog_o.get_options_data(expiry=goog_o.expiry_dates[0])

df = web.DataReader("XOM", "yahoo", start, end)
#This pulls data for Exxon from the Morningstar API
#print(df.head())

df.reset_index(inplace=True)
df.set_index("Date", inplace=True)
df = df.drop("Symbol", axis=1)

print(df.head())

df['High'].plot()
plt.legend()
plt.show()

options_prices = Options("XOM", "yahoo")
options_df = options_prices.get_options_data(
    expiry=options_prices.expiry_dates[0])
print(options_df.tail())

#########################
#dictionary to a dataframe using pandas
import pandas as pd

web_stats = {
    'Day': [1, 2, 3, 4, 5, 6, 7, 8, 9],
    'Visitors': [43, 34, 65, 56, 29, 76, 78, 45, 23],
    'Bounce Rate': [65, 67, 78, 65, 45, 52, 34, 65, 87]
}

df = pd.DataFrame(web_stats)
df.head()
print(df.tail())