class StockPriceGetter(object): def __init__(self): self.exec_error = False self.db_model = StockPriceDbModel() # example: http://chart.finance.yahoo.com/table.csv?s=MSFT&a=7&b=9&c=2016&d=8&e=9&f=2016&g=d&ignore=.csv self.price_url = 'http://chart.finance.yahoo.com/table.csv?s=%s&a=%d&b=%d&c=%d&d=%d&e=%d&f=%d&g=d&ignore=.csv' def save_prices_for_all_companies(self, start_date, end_date, stop_if_duplicate=True, insert_missing_days=True): print('>>>Saving stock prices from %s to %s') % (start_date, end_date) for comp in self.db_model.get_companies(): data = self.get_prices_for_company_ticker(comp[1], start_date, end_date) if data: if insert_missing_days: new_data = self._refill_yahoo_data(comp[0], data, start_date, end_date) self.db_model.save_refilled_prices_for_company(new_data) else: self.db_model.save_prices_for_company(comp[0], data, stop_if_duplicate, insert_missing_days) # the end print('>>>New stock prices were saved into DB.') def get_prices_for_company_ticker(self, ticker, start_date, end_date): """ :param ticker: string :param start_date: Date :param end_date: Date :return list """ # The API has error - you must give the previous month. start_date = start_date - datetime.timedelta(days=33) # Build URL target_url = self.price_url % (ticker, start_date.month, start_date.day, start_date.year, end_date.day, end_date.month, end_date.year) print target_url # Get CSV file and turn it into list. Data in format: Date,Open,High,Low,Close,Volume,Adj Close try: data = urllib2.urlopen(target_url).read().split('\n') except Exception, e: self.exec_error = True print('not found: ' + target_url) return False # Remove first line (labels) and last line (empty). del(data[0]) del(data[-1]) # Return list return data
def __init__(self): self.exec_error = False self.db_model = StockPriceDbModel() # example: http://chart.finance.yahoo.com/table.csv?s=MSFT&a=7&b=9&c=2016&d=8&e=9&f=2016&g=d&ignore=.csv self.price_url = 'http://chart.finance.yahoo.com/table.csv?s=%s&a=%d&b=%d&c=%d&d=%d&e=%d&f=%d&g=d&ignore=.csv'