def getstockopts(stockname, save=True): opts = Options(stockname, 'yahoo') expirydates = opts.expiry_dates dateexp = datetime(2017, 12, 15) #manually set to this calldata = opts.get_call_data(expiry=dateexp) putdata = opts.get_put_data(expiry=dateexp) prices = web.DataReader(stockname, 'yahoo', today) call_impt = calldata[['Last', 'Open_Int', 'Underlying_Price']] call_indexed_impt = call_impt.reset_index() put_impt = putdata[['Last', 'Open_Int', 'Underlying_Price']] put_indexed_impt = put_impt.reset_index() datstr = str(today.date()) comb_indexed_impt = pd.concat([call_indexed_impt, put_indexed_impt]) comb_indexed_impt.index = [datstr] * len(comb_indexed_impt.index) prices_indexed = prices.reset_index() prices_indexed.index = [datstr] * len(prices_indexed.index) if save: foldlists = os.listdir(datfold) foldname = stockname + '/' if stockname not in foldlists: os.mkdir(datfold + foldname) os.mkdir(datfold + foldname + 'options/') os.mkdir(datfold + foldname + 'prices/') stockoptlist = os.listdir(datfold + foldname + 'options/') stockpricelist = os.listdir(datfold + foldname + 'prices/') stockoptname = stockname + '-options.csv' stockpricename = stockname + '-prices.csv' if stockoptname not in stockoptlist: comb_indexed_impt.to_csv(datfold + foldname + 'options/' + stockoptname) else: comb_indexed_impt.to_csv(datfold + foldname + 'options/' + stockoptname, mode='a', header=False) if stockpricename not in stockpricelist: prices_indexed.to_csv(datfold + foldname + 'prices/' + stockpricename) else: prices_indexed.to_csv(datfold + foldname + 'prices/' + stockpricename, mode='a', header=False) print('stock : ' + stockname + ' DONE')
# stock, option, hedge = option_tree(10, 10, 2, 'ameRican', 'Put') # print(stock) # print(option) # print(hedge) ############################################################################################# ##################################### Output the Result ##################################### ############################################################################################# # Get Bid Price, Ask Price for Call and Put Options goog = Options('GOOG', 'yahoo') call_data = goog.get_call_data(6, 2018) bid_call = call_data['Bid'] ask_call = call_data['Ask'] put_data = goog.get_put_data(6, 2018) bid_put = put_data['Bid'] ask_put = put_data['Ask'] # Calculate the Value of Option # value_call_option = [] # Value of Option for Call Option # value_put_option = [] # Value of Option for Put Option # # for i in range(len(bid_call)): # value_call_option.append((bid_call[i] + ask_call[i]) / 2) # value_put_option.append((bid_put[i] + ask_put[i]) / 2) # Get Options' Strike Prices for Call and Put call_data.to_csv('fileCall.csv') temp_call_file = pd.read_csv('fileCall.csv') strike_call = temp_call_file[