def plot_uoa_performance(s, exp, strike): data = r.get_option_historicals(s, exp, strike, 'call') dates = [] prices = [] for point in data: dates.append( datetime.datetime.strptime(point['begins_at'], '%Y-%m-%dT%H:%M:%SZ')) prices.append(float(point['open_price'])) plt.plot(dates, prices) plt.show()
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
def plotPricesForExpriation( ohlc, symbol, expirationDate, dfstrike_prices, span='week', optionType='call', ): """ :param OHLC: open_price, high_price, low_price, close_price :param symbol: :param expirationDate: :param dfstrike_prices: :param optionType: :param span: :return: """ 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)) if df.empty: print('No options\t{}\t{}\t{}'.format(expirationDate, strike_price, span)) return df['strike_price'] = strike_price df[[ 'strike_price', 'open_price', 'high_price', 'low_price', 'close_price' ]] = df[[ 'strike_price', 'open_price', 'high_price', 'low_price', 'close_price' ]].apply(pd.to_numeric) ohlc_price = df[ohlc].iloc[0] df['diff'] = df[ohlc] - ohlc_price df['pct_change'] = 0 if ohlc_price == 0 else round( df['diff'] / ohlc_price, 2) df_all = pd.concat([ df_all, df[[ 'begins_at', 'strike_price', 'close_price', 'high_price', 'pct_change', 'low_price', 'open_price', 'volume' ]] ]) # df_all = df_all[df_all != 0.01] df_all = df_all[df.high_price != df.high_price[0:1] [0]] # filter no changes form the starting date print() diffMaxHighLow(df_all, dfstrike_prices) local = utcToLocal(df_all.iloc[-1:].begins_at.values[-1]) title = 'Percent change {} Expiration:{}, Time:{:{dfmt} {tfmt}}'.format( symbol, expirationDate, local, dfmt='%Y-%m-%d', tfmt='%H:%M') fig = px.line(df_all, x="begins_at", y="pct_change", color="strike_price", line_group="strike_price", hover_name="strike_price", line_shape="spline", render_mode="svg") fig.update_layout(title=title, xaxis_title="Date", yaxis_title="Pct Change", font=dict(family="Courier New, monospace", size=18, color="#7f7f7f")) fig.show() title = 'Close Price {} Expiration:{}, Time:{:{dfmt} {tfmt}}'.format( symbol, expirationDate, local, dfmt='%Y-%m-%d', tfmt='%H:%M') fig = px.line(df_all, x="begins_at", y="close_price", color="strike_price", line_group="strike_price", hover_name="strike_price", line_shape="spline", render_mode="svg") fig.update_layout(title=title, xaxis_title="Date", yaxis_title="Close Price", font=dict(family="Courier New, monospace", size=18, color="#7f7f7f")) fig.show()
#!!! Fill out username and password username = '' password = '' #!!! login = r.login(username,password) #!!! fill out the specific option information symbol = 'V' expirationDate = '2018-11-16' strike = 150 optionType = 'call' span = 'year' #available options are day,week,year, and 5year #!!! historicalData = r.get_option_historicals(symbol,expirationDate,strike,optionType,span) dates = [] closingPrices = [] openPrices = [] for item in historicalData['data_points']: print(item) dates.append(item['begins_at']) closingPrices.append(item['close_price']) openPrices.append(item['open_price']) #!!! I made it so it only got the last 30 days but you could delete these lines to get full year. dates = dates[-30:] closingPrices = closingPrices[-30:]
' break even.. $', round(float(item['break_even_price']), 3), ' current.. $', round(float(stockPrice[0])), ' change needed.. $', round(changeNeeded, 3), ' % change needed.. ', round(changeNeededPercent, 3), "%", ' \n\t\tprevious close.. $', round(float(item['previous_close_price']), 3), ' change.. $', round(changeInCost, 3), ' percent change.. ', round(changeInCostPercent, 3), "%" ' \n\t\t\timplied volatility.. ', impliedVolatility, ' volume.. ', item['volume'], ' open interest.. ', item['open_interest'], ' \n\t\t\thigh price.. ', highPrice, ' low price.. ', lowPrice, ) historicalData = rh.get_option_historicals(stock, date, item['strike_price'], item['type'], 'year') dates = [] closingPrices = [] openPrices = [] for thing in historicalData['data_points']: dates.append(thing['begins_at']) closingPrices.append(float(thing['close_price'])) openPrices.append(float(thing['open_price'])) # Getting data to graph dates = dates[-historicalDays:] closingPrices = closingPrices[-historicalDays:] openPrices = openPrices[-historicalDays:] #
login = r.login(username, password) #!!! fill out the specific option information symbol = 'AAPL' symbol_name = r.get_name_by_symbol(symbol) expirationDate = '2020-07-02' # format is YYYY-MM-DD. strike = 300 optionType = 'call' # available options are 'call' or 'put' or None. interval = 'hour' # available options are '5minute', '10minute', 'hour', 'day', and 'week'. span = 'week' # available options are 'day', 'week', 'year', and '5year'. bounds = 'regular' # available options are 'regular', 'trading', and 'extended'. info = None #!!! historicalData = r.get_option_historicals(symbol, expirationDate, strike, optionType, interval, span, bounds, info) dates = [] closingPrices = [] openPrices = [] for data_point in historicalData: dates.append(data_point['begins_at']) closingPrices.append(data_point['close_price']) openPrices.append(data_point['open_price']) # change the dates into a format that matplotlib can recognize. x = [dt.datetime.strptime(d, '%Y-%m-%dT%H:%M:%SZ') for d in dates] # plot the data.