def _output_format(self, out, fmt_j=None, fmt_p=None): result = {} if len(self.symbols) == 1 and not out[self.symbols[0]]["chart"]: return pd.DataFrame(out) for symbol in self.symbols: if symbol not in out: raise IEXSymbolError(symbol) d = out.pop(symbol)["chart"] df = pd.DataFrame(d) if self.output_format == "pandas": df["date"] = pd.DatetimeIndex(df["date"]) df = df.set_index(df["date"]) values = ["close", "volume"] if self.close_only is False: values = ["open", "high", "low"] + values df = df[values] if self.single_day is False: sstart = self.start.strftime("%Y-%m-%d") send = self.end.strftime("%Y-%m-%d") df = df.loc[sstart:send] result.update({symbol: df}) if self.output_format == "pandas": if len(result) > 1: result = pd.concat(result.values(), keys=result.keys(), axis=1) else: for sym in list(result): result[sym] = result[sym].to_dict("index") return result[self.symbols[0]] if len(self.symbols) == 1 else result
def _get_endpoint(self, endpoint, params=(), fmt_p=None, fmt_j=None, filter_=None): result = {} if filter_: params.update({"filter": filter_}) self.optional_params = params self.endpoints = [endpoint] data = self.fetch(fmt_j=fmt_j, fmt_p=no_pandas) # IEX Cloud returns multiple symbol requests as as a list of dicts # so convert to dict of dicts if isinstance(data, list): data = data[0] for symbol in self.symbols: if symbol not in data: raise IEXSymbolError(symbol) if endpoint not in data[symbol]: result[symbol] = [] else: result[symbol] = data[symbol][endpoint] return self._output_format_one(result, fmt_p=fmt_p, fmt_j=fmt_j)
def _output_format(self, out, fmt_j=None, fmt_p=None): result = {} for symbol in self.symbols: try: if symbol not in out or not out[symbol]["chart"]: raise IEXSymbolError(symbol) d = out.pop(symbol)["chart"] df = pd.DataFrame(d) if self.output_format == 'pandas': df["date"] = pd.DatetimeIndex(df["date"]) df = df.set_index(df["date"]) values = ["open", "high", "low", "close", "volume"] df = df[values] sstart = self.start.strftime('%Y-%m-%d') send = self.end.strftime('%Y-%m-%d') df = df.loc[sstart:send] result.update({symbol: df}) except: continue if self.output_format is "pandas": if len(result) > 1: result = pd.concat(result.values(), keys=result.keys(), axis=1) else: for sym in list(result): result[sym] = result[sym].to_dict('index') return result[self.symbols[0]] if self.n_symbols == 1 else result
def _get_endpoint(self, endpoint, params={}): self.optional_params = params self.endpoints = [endpoint] data = self.fetch() for symbol in self.symbols: if symbol not in data: raise IEXSymbolError(symbol) elif endpoint in data[symbol]: if data[symbol][endpoint] is None: raise IEXEndpointError(endpoint) else: raise IEXEndpointError(endpoint) return data
def refresh(self): """ Downloads latest data from all Stock endpoints """ self.endpoints = self.ALL_ENDPOINTS_STR_1 self.data_set = self.fetch() self.endpoints = self.ALL_ENDPOINTS_STR_2 data2 = self.fetch() for symbol in self.symbols: if symbol not in self.data_set: raise IEXSymbolError(symbol) self.data_set[symbol].update(data2[symbol])
def get_all(self): """ Returns all endpoints, indexed by endpoint title for each symbol Notes ----- Only allows JSON format (pandas not supported). """ self.optional_params = {} self.endpoints = self._ENDPOINTS[:10] json_data = self.fetch() self.endpoints = self._ENDPOINTS[10:20] json_data_2 = self.fetch() for symbol in self.symbols: if symbol not in json_data: raise IEXSymbolError(symbol) json_data[symbol].update(json_data_2[symbol]) return json_data
def get_endpoints(self, endpoints=[]): """ Universal selector method to obtain specific endpoints from the data set. Parameters ---------- endpoints: str or list Desired valid endpoints for retrieval Notes ----- Only allows JSON format (pandas not supported). Raises ------ IEXEndpointError If an invalid endpoint is specified IEXSymbolError If a symbol is invalid IEXQueryError If issues arise during query """ if isinstance(endpoints, str): return self._get_endpoint(endpoints) elif not endpoints: raise ValueError("Please provide a valid list of endpoints") elif len(endpoints) > 10: raise ValueError("Please input up to 10 valid endpoints") self.optional_params = {} self.endpoints = endpoints json_data = self.fetch() for symbol in self.symbols: if symbol not in json_data: raise IEXSymbolError(symbol) for endpoint in endpoints: if endpoint in json_data[self.symbols[0]]: if json_data[self.symbols[0]][endpoint] is None: raise IEXEndpointError(endpoint) else: raise IEXEndpointError(endpoint) return json_data
def _get_endpoint(self, endpoint, params={}, fmt_p=None, fmt_j=None, filter_=None): result = {} if filter_: params.update({"filter": filter_}) self.optional_params = params self.endpoints = [endpoint] data = self.fetch(fmt_j=fmt_j, fmt_p=no_pandas) for symbol in self.symbols: if symbol not in data: raise IEXSymbolError(symbol) if endpoint not in data[symbol]: result[symbol] = [] else: result[symbol] = data[symbol][endpoint] return self._output_format_one(result, fmt_p=fmt_p, fmt_j=fmt_j)
def get_select_endpoints(self, endpoints=[]): """ Universal selector method to obtain specific endpoints from the data set. Parameters ---------- endpoints: str or list Desired valid endpoints for retrieval Notes ----- Only allows JSON format (pandas not supported). Raises ------ IEXEndpointError If an invalid endpoint is specified IEXQueryError If issues arise during query """ if isinstance(endpoints, str): endpoints = [endpoints] elif not endpoints: raise ValueError("Please provide a valid list of endpoints") result = {} for symbol in self.symbols: temp = {} try: ds = self.data_set[symbol] except KeyError: IEXSymbolError(symbol) for endpoint in endpoints: try: query = ds[endpoint] except KeyError: raise IEXEndpointError(endpoint) temp[endpoint] = query result[symbol] = temp return result
def get_endpoints(self, endpoints=()): """ Universal selector method to obtain specific endpoints from the data set. Parameters ---------- endpoints: str or list Desired valid endpoints for retrieval Notes ----- Only allows JSON format (pandas not supported). Raises ------ IEXEndpointError If an invalid endpoint is specified IEXSymbolError If a symbol is invalid IEXQueryError If issues arise during query """ if isinstance(endpoints, str) and endpoints in self._ENDPOINTS: endpoints = list(endpoints) if not endpoints or not set(endpoints).issubset(self._ENDPOINTS): raise IEXEndpointError("Please provide a valid list of endpoints") elif len(endpoints) > 10: raise ValueError("Please input up to 10 valid endpoints") self.optional_params = {} self.endpoints = endpoints json_data = self.fetch(fmt_p=no_pandas) for symbol in self.symbols: if symbol not in json_data: raise IEXSymbolError(symbol) return json_data[self.symbols[0]] if self.n_symbols == 1 else json_data
def fetch(self): response = super(HistoricalReader, self).fetch() for sym in self.symlist: if sym not in list(response): raise IEXSymbolError(sym) return self._output_format(response)