def read_daily_option_quote( browser: ChromeDriver, symbol: str, option_type: str, strike_price: float, exp_date: int, use_barchart: bool = False, suppress_log: bool = False, ) -> DailyOptionQuote: option_quote = DailyOptionQuote(symbol, option_type, strike_price, exp_date, int(get_date_str())) if use_barchart: url = "https://www.barchart.com/stocks/quotes/" url += f"{symbol}/options?expiration={str(exp_date)}&moneyness=allRows" eclass = "bc-options-quotes" web_data = browser.download_data( url=url, wait_base=1, element_class=eclass, suppress_log=suppress_log, ) else: datetime_diff = get_date(str(exp_date)) - get_datetime_start() date_url = int(datetime_diff.total_seconds()) url = f"https://finance.yahoo.com/quote/{symbol}/options?date={date_url}" eid = 'Col1-1-OptionContracts-Proxy' web_data = browser.download_data( url=url, wait_base=1, element_id=eid, suppress_log=suppress_log, ) parse_daily_option_quote(option_quote, web_data.splitlines(), symbol, option_type, strike_price, exp_date, use_barchart) return option_quote
def read_daily_option_info( browser: ChromeDriver, symbol: str, use_barchart: bool = True, suppress_all_log: bool = False, suppress_sub_log: bool = False, ) -> DailyOptionInfo: option_info = DailyOptionInfo(symbol, int(get_date_str())) if use_barchart: all_exp_dates = read_exp_dates( browser, symbol, suppress_log=suppress_all_log, ) eclass = 'bc-futures-options-quotes-totals' pre_eclass = 'bc-datatable' for exp_date in all_exp_dates: url = "https://www.barchart.com/stocks/quotes/" url += f"{symbol}/options?expiration={str(exp_date)}" web_data = browser.download_data( url=url, wait_base=1, pre_element_class=pre_eclass, element_class=eclass, suppress_log=suppress_sub_log, ) parse_daily_option_info(option_info, web_data.splitlines()) else: logger.error('other data sources not implemented') raise ValueError(f"use_barchart={use_barchart}") return option_info
def read_option_activity(browser: ChromeDriver, save_file=False, folder='logs') -> list: retry_timeout = 4 url = 'https://www.barchart.com/options/unusual-activity/stocks?page=all' eid = 'main-content-column' #buttons = ['a.show-all'] buttons = None num_retry = 0 while num_retry < retry_timeout: num_retry += 1 try: web_data = browser.download_data(url=url, wait_base=num_retry, button_css=buttons, element_id=eid) except Exception as e: logger.error(f'error {str(e)} (retry={num_retry}/{retry_timeout})') time.sleep(num_retry) continue else: option_activity_list = parse_option_activity(web_data.splitlines()) if len(option_activity_list) > 0: logger.info( 'retrieved option activity list: # items={:d}'.format( len(option_activity_list))) break else: logger.warning( 'option activity list empty? retry={}/{}'.format( num_retry, retry_timeout)) time.sleep(num_retry) continue if num_retry >= retry_timeout: return [] # save a copy if save_file: # try to remove duplicates if folder exists if os.path.exists(folder): today_str = get_date_str() for item in os.listdir(folder): if item.startswith('OA_%s' % (today_str)): os.remove(os.path.join(folder, item)) else: # otherwise create folder os.makedirs(folder) # write new one filename = os.path.join(folder, f'OA_{get_datetime_str()}.txt.gz') with openw(filename, 'wt') as fout: fout.write('\n'.join(option_activity_list)) logger.info(f'{get_time_log()} save option activity to {filename}') return option_activity_list
def read_stock_quote( browser: ChromeDriver, symbol: str, suppress_log: bool = False, ) -> DailyStockQuote: stock_quote = DailyStockQuote(symbol, int(get_date_str())) url = f"https://finance.yahoo.com/quote/{symbol}" eid = "quote-summary" web_data = browser.download_data( url=url, element_id=eid, suppress_log=suppress_log, ) parse_stock_quote(stock_quote, web_data.splitlines()) return stock_quote