📚 The CoCalc Library - books, templates and other resources
License: OTHER
#1# ystockquote : Python module - retrieve stock quote data from Yahoo Finance2#3# Copyright (c) 2007,2008,2013 Corey Goldberg ([email protected])4#5# license: GNU LGPL6#7# This library is free software; you can redistribute it and/or8# modify it under the terms of the GNU Lesser General Public9# License as published by the Free Software Foundation; either10# version 2.1 of the License, or (at your option) any later version.11#12# Requires: Python 2.7/3.2+131415__version__ = '0.2.2'161718try:19# py320from urllib.request import Request, urlopen21from urllib.parse import urlencode22except ImportError:23# py224from urllib2 import Request, urlopen25from urllib import urlencode262728def _request(symbol, stat):29url = 'http://finance.yahoo.com/d/quotes.csv?s=%s&f=%s' % (symbol, stat)30req = Request(url)31resp = urlopen(req)32return str(resp.read().decode('utf-8').strip())333435def get_all(symbol):36"""37Get all available quote data for the given ticker symbol.3839Returns a dictionary.40"""41values = _request(symbol, 'l1c1va2xj1b4j4dyekjm3m4rr5p5p6s7').split(',')42return dict(43price=values[0],44change=values[1],45volume=values[2],46avg_daily_volume=values[3],47stock_exchange=values[4],48market_cap=values[5],49book_value=values[6],50ebitda=values[7],51dividend_per_share=values[8],52dividend_yield=values[9],53earnings_per_share=values[10],54fifty_two_week_high=values[11],55fifty_two_week_low=values[12],56fifty_day_moving_avg=values[13],57two_hundred_day_moving_avg=values[14],58price_earnings_ratio=values[15],59price_earnings_growth_ratio=values[16],60price_sales_ratio=values[17],61price_book_ratio=values[18],62short_ratio=values[19],63)646566def get_price(symbol):67return _request(symbol, 'l1')686970def get_change(symbol):71return _request(symbol, 'c1')727374def get_volume(symbol):75return _request(symbol, 'v')767778def get_avg_daily_volume(symbol):79return _request(symbol, 'a2')808182def get_stock_exchange(symbol):83return _request(symbol, 'x')848586def get_market_cap(symbol):87return _request(symbol, 'j1')888990def get_book_value(symbol):91return _request(symbol, 'b4')929394def get_ebitda(symbol):95return _request(symbol, 'j4')969798def get_dividend_per_share(symbol):99return _request(symbol, 'd')100101102def get_dividend_yield(symbol):103return _request(symbol, 'y')104105106def get_earnings_per_share(symbol):107return _request(symbol, 'e')108109110def get_52_week_high(symbol):111return _request(symbol, 'k')112113114def get_52_week_low(symbol):115return _request(symbol, 'j')116117118def get_50day_moving_avg(symbol):119return _request(symbol, 'm3')120121122def get_200day_moving_avg(symbol):123return _request(symbol, 'm4')124125126def get_price_earnings_ratio(symbol):127return _request(symbol, 'r')128129130def get_price_earnings_growth_ratio(symbol):131return _request(symbol, 'r5')132133134def get_price_sales_ratio(symbol):135return _request(symbol, 'p5')136137138def get_price_book_ratio(symbol):139return _request(symbol, 'p6')140141142def get_short_ratio(symbol):143return _request(symbol, 's7')144145146def get_historical_prices(symbol, start_date, end_date):147"""148Get historical prices for the given ticker symbol.149Date format is 'YYYY-MM-DD'150151Returns a nested list (first item is list of column headers).152"""153params = urlencode({154's': symbol,155'a': int(start_date[5:7]) - 1,156'b': int(start_date[8:10]),157'c': int(start_date[0:4]),158'd': int(end_date[5:7]) - 1,159'e': int(end_date[8:10]),160'f': int(end_date[0:4]),161'g': 'd',162'ignore': '.csv',163})164url = 'http://ichart.yahoo.com/table.csv?%s' % params165req = Request(url)166resp = urlopen(req)167content = str(resp.read().decode('utf-8').strip())168days = content.splitlines()169return [day.split(',') for day in days]170171