def create_stock(quote, max_iterations, start, end, stocks, index): ''' Create a stock from a quote ''' if (quote['StockExchange'] == None): return success = False for i in range(max_iterations): if not success: url = historical_url(quote['symbol'], start, end) data = request_json(url) if 'error' in data and i == (max_iterations-1): raise Exception('Error in call to historical api', data) elif 'error' not in data: success = True quotes = data['query']['results']['quote'] close_prices = [float(q['Close']) for q in quotes] stock = Stock(quote, close_prices) stock.index = index lock.acquire() stocks.append(stock) lock.release()
def test_yahoo_historical_url(self): """ Test to create url to get historical prices of a single stock """ yahoo_test_url = ( r"https://query.yahooapis.com/v1/public/yql?q=sele" r"ct%20*%20from%20yahoo.finance.historicaldata%20w" r"here%20symbol%20%3D%20%22YHOO%22%20and%20startDa" r"te%20%3D%20%222009-09-11%22%20and%20endDate%20%3" r"D%20%222010-03-07%22&format=json&diagnostics=tru" r"e&env=store%3A%2F%2Fdatatables.org%2Falltableswi" r"thkeys&callback=" ) produced = historical_url("YHOO", "2009-09-11", "2010-03-07") self.assertEqual(yahoo_test_url, produced)