1from datetime import date 2import logging 3 4import pandas_datareader.data as web 5 6from Orange.data import Domain 7from orangecontrib.timeseries import Timeseries 8 9log = logging.getLogger(__name__) 10 11try: 12 from Orange.data.pandas_compat import table_from_frame 13except ImportError: 14 raise RuntimeError("Yahoo Finance requires Orange >= 3.9") 15 16 17def quandl_data(symbol, 18 since=None, 19 until=None, 20 *, 21 collapse='daily', 22 api_key=''): 23 """ 24 25 Parameters 26 ---------- 27 symbol 28 since 29 until 30 collapse: none|daily|weekly|monthly|quarterly|annual 31 api_key 32 33 Returns 34 ------- 35 36 """ 37 if since is None: 38 since = date(1900, 1, 1).isoformat() 39 if until is None: 40 until = date.today().isoformat() 41 42 QUANDL_URL = ('https://www.quandl.com/api/v3/datasets/WIKI/{SYMBOL}/data.csv?' 43 'start_date={START_DATE}&end_date={END_DATE}&order=asc&' 44 'collapse={COLLAPSE}&transform=rdiff&api_key={API_KEY}') 45 url = QUANDL_URL.format(SYMBOL=symbol, 46 START_DATE=since, 47 END_DATE=until, 48 COLLAPSE=collapse, 49 API_KEY=api_key) 50 ts = Timeseries.from_url(url) 51 return ts 52 53 54 55def finance_data(symbol, 56 since=None, 57 until=None, 58 granularity='d'): 59 """Fetch Yahoo Finance data for stock or index `symbol` within the period 60 after `since` and before `until` (both inclusive). 61 62 Parameters 63 ---------- 64 symbol: str 65 A stock or index symbol, as supported by Yahoo Finance. 66 since: date 67 A start date (default: 1900-01-01). 68 until: date 69 An end date (default: today). 70 granularity: 'd' or 'w' or 'm' or 'v' 71 What data to get: daily, weekly, monthly, or dividends. 72 73 Returns 74 ------- 75 data : Timeseries 76 """ 77 if since is None: 78 since = date(1900, 1, 1) 79 if until is None: 80 until = date.today() 81 82 f = web.DataReader(symbol, 'yahoo', since, until) 83 data = Timeseries.from_data_table(table_from_frame(f)) 84 85 # Make Adjusted Close a class variable 86 attrs = [var.name for var in data.domain.attributes] 87 attrs.remove('Adj Close') 88 data = Timeseries.from_table(Domain(attrs, [data.domain['Adj Close']], 89 None, source=data.domain), 90 data) 91 92 data.name = symbol 93 data.time_variable = data.domain['Date'] 94 return data 95