def test_find_tradable_options(self): info = r.find_options_by_expiration(self.symbol, self.expiration_date) first = info[0] assert (first['expiration_date'] == self.expiration_date) assert (len(info) > 100) info = r.find_options_by_expiration(self.symbol, self.expiration_date, info='strike_price') first = info[0] assert (type(first) == str) assert (len(info) > 100) info = r.find_options_by_expiration(self.symbol, self.expiration_date, info='expiration_date') assert (len(set(info)) == 1)
def test_find_tradable_options(self): info = r.find_options_by_expiration(self.symbol, self.expiration_date) first = info[0] self.assertEqual(first['expiration_date'], self.expiration_date) self.assertGreater(len(info), 100) info = r.find_options_by_expiration(self.symbol, self.expiration_date, info='strike_price') first = info[0] self.assertEqual(type(first), str) self.assertGreater(len(info), 100) info = r.find_options_by_expiration(self.symbol, self.expiration_date, info='expiration_date') self.assertEqual(len(set(info)), 1)
def get_option(ticker, date, delta_threshold, option_type=None, info=None): options = r.find_options_by_expiration(ticker, date, optionType=option_type) closest_delta = -1.0 if option_type == 'call' else 1.0 best_option = None for o in options: if o['delta'] is None: continue if abs(float(o['delta']) - delta_threshold) < abs(closest_delta - delta_threshold): closest_delta = float(o['delta']) best_option = o return best_option
def getStrikePrices(symbol, expirationDate, optionType='call', volume_limit=0): #options = r.find_options_for_list_of_stocks_by_expiration_date([symbol], expirationDate=expirationDate,optionType=optionType) options = r.find_options_by_expiration([symbol], expirationDate=expirationDate, optionType=optionType) options = (filter(lambda x: x['volume'] != '', options)) dfoptions = pd.DataFrame((filter(lambda x: x['volume'] > volume_limit, options))) if dfoptions.empty: print('Volume is 0 for options') else: #dfoptions[['strike_price','high_price','low_price']] = dfoptions[['strike_price','high_price','low_price']].apply(pd.to_numeric) #dfstrike_prices = dfoptions.sort_values(by='volume',ascending=False)[0:5][['strike_price','volume']] #dfstrike_prices.sort_values(by='strike_price',inplace=True) dfstrike_prices = dfoptions.sort_values( by='volume', ascending=False)[0:5][['strike_price', 'volume']].apply(pd.to_numeric) dfstrike_prices.sort_values(by='strike_price', ascending=True, inplace=True) return dfstrike_prices
def getStrikesOHLCChangesExpiration(symbol, expiration_dates, optionType, span, volume_limit=0): df_expirationDates = pd.DataFrame() for expirationDate in expiration_dates[0:5]: print('{}\t'.format(expirationDate), end='') #options = r.find_options_for_list_of_stocks_by_expiration_date([symbol], expirationDate, optionType) options = r.find_options_by_expiration([symbol], expirationDate, optionType) options = (filter(lambda x: x['volume'] != '', options)) dfoptions = pd.DataFrame((filter(lambda x: x['volume'] > volume_limit, options))) if dfoptions.empty: print('Volume is 0 for options') else: dfoptions[['strike_price', 'high_price', 'low_price']] = dfoptions[ ['strike_price', 'high_price', 'low_price']].apply(pd.to_numeric) dfstrike_prices = dfoptions.sort_values(by='volume', ascending=False)[0:5][['strike_price', 'volume']] dfstrike_prices.sort_values(by='strike_price', inplace=True) # dfstrike_prices df_all = pd.DataFrame() for idx, row in dfstrike_prices.iterrows(): strike_price = row.strike_price # print('Strike price: {}'.format(strike_price)) # df = pd.DataFrame( # r.get_option_historicals(symbol, expirationDate, strike_price, optionType, span)['data_points']) df = pd.DataFrame( r.get_option_historicals(symbol, expirationDate, strike_price, optionType, span)) df['strike_price'] = strike_price df_all = pd.concat([df_all, df[ ['begins_at', 'strike_price', 'open_price', 'high_price', 'low_price', 'close_price', 'volume']]]) df_all[['strike_price','open_price', 'high_price', 'low_price','close_price']] = df_all[ ['strike_price', 'open_price','high_price', 'low_price','close_price']].apply(pd.to_numeric) df_all = df_all[df_all.high_price != 0.01] df_all['expiration_date'] = expirationDate print() diffMaxHighLow(df_all, dfstrike_prices) df_expirationDates = pd.concat([df_expirationDates, df_all]) return df_expirationDates
pwd = keyring.get_password("robinhood", "my_very_own_rh_username") login = r.login(MY_RH_USERNAME, pwd) latest_prices = r.get_latest_price(tickers) prices = dict(zip(tickers, list(map(float, latest_prices)))) today = datetime.date.today() friday = today + datetime.timedelta((4 - today.weekday()) % 7) # If it's late in the week and you want to look at next week, uncomment the below line # friday = friday + datetime.timedelta(7) expiration = "{}-{}-{}".format(friday.year, friday.month, friday.day) putData = r.find_options_by_expiration(tickers, expirationDate=expiration, optionType='put') putsToLookAt = defaultdict(lambda: []) for put in putData: tick = put["chain_symbol"] curr_stock_price = prices[tick] strike = float(put["strike_price"]) if strike > curr_stock_price: # We don't sell puts for strikes above the current stock price continue premium = float(put["bid_price"] ) # only the bid price is guaranteed, rest is theoretical if premium < strike * (MIN_PREMIUM_PERCENTAGE / 100): # Not worth it continue