def fetch_stock_values(stock_code, start_date, end_date): """Fetches daily stock values from Yahoo Finance.""" start_date = date_to_datetime( parse_date(start_date if start_date is not None else -30 * 3600 * 24)) end_date = date_to_datetime( parse_date(end_date if end_date is not None else 0)) if start_date > end_date: raise ValueError("start_date must be equal to or less than end_date") provider = Yahoo() rows = provider.asset_values(stock_code, start_date, end_date, Granularity.day) for row in rows: # TODO: Write a function to handle this for generic cases # TODO: Convert the timestamp to an ISO format # NOTE: The last column is data source. Not sure if this is an elegant # way to handle this. # FIXME: Think of a better way to handle this dt = row[0].isoformat() print(", ".join([dt] + [str(c) for c in row[1:]] + ["yahoo"]))
def test_date_to_datetime(): date = parse_date("2018-01-13") dt_beginning = parse_datetime("2018-01-13 00:00:00") dt_end = parse_datetime("2018-01-13 23:59:59") assert date_to_datetime(date) == dt_beginning assert date_to_datetime(date, True) == dt_end
def fetch_stock_values(stock_code, start_date, end_date): """Fetches daily stock values from Yahoo Finance.""" start_date = date_to_datetime( parse_date(start_date if start_date is not None else -30 * 3600 * 24)) end_date = date_to_datetime( parse_date(end_date if end_date is not None else 0)) if start_date > end_date: raise ValueError('start_date must be equal to or less than end_date') provider = Yahoo() rows = provider.asset_values( stock_code, start_date, end_date, Granularity.day) for row in rows: # TODO: Write a function to handle this for generic cases # TODO: Convert the timestamp to an ISO format # NOTE: The last column is data source. Not sure if this is an elegant # way to handle this. # FIXME: Think of a better way to handle this dt = row[0].isoformat() print(', '.join([dt] + [str(c) for c in row[1:]] + ['yahoo']))
def request_import_stock_values_handler(event, context): codes = ['AMD', 'AMZN', 'BRK-A', 'BRK-B', 'ESRT', 'NVDA', 'SBUX', 'SPY'] start_time = date_to_datetime(parse_date(-3)) end_time = date_to_datetime(parse_date(0)) for code in codes: request_import_stock_values(code, start_time, end_time) log.info('Requested to import stock values: {0}', ', '.join(codes))
def request_import_stock_values_handler(event, context): codes = ["AMD", "AMZN", "BRK-A", "BRK-B", "ESRT", "NVDA", "SBUX", "SPY"] start_time = date_to_datetime(parse_date(-3)) end_time = date_to_datetime(parse_date(0)) for code in codes: request_import_stock_values(code, start_time, end_time) log.info("Requested to import stock values: {0}", ", ".join(codes))
def request_import_stock_values(code): """Enqueue a request to import stock values.""" start_time = date_to_datetime(parse_date(-3)) end_time = date_to_datetime(parse_date(0)) request_import_stock_values_(code, start_time, end_time)