def export_data(request, api_detail, parent_symbol, symbol, data_type): # print('Request Data: ', api_detail, parent_symbol, symbol, data_type) response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment;filename=av_data.csv' # opts = queryset.model._meta field_names = ['Time Series', 'Open', 'High', 'Low', 'Close', 'Volume'] writer = csv.writer(response) writer.writerow(field_names) if api_detail == '1': ts = TimeSeries(key=settings.API_KEY, output_format='json') if data_type == 'Intraday': api_data, meta_data = ts.get_intraday(symbol=symbol, interval='1min', outputsize='full') elif data_type == 'Weekly': api_data, meta_data = ts.get_weekly(symbol=symbol) elif data_type == 'Daily': api_data, meta_data = ts.get_daily(symbol=symbol) else: api_data, meta_data = ts.get_monthly(symbol=symbol) for key, value in api_data.items(): csv_list = [key] for k, v in value.items(): csv_list.append(v) writer.writerow(csv_list) return response
def Time_series(name, time, output, type): #ts = TimeSeries(key=API_key, output_format='pandas') ts = TimeSeries(key=API_key) if (type == 'Intraday'): data, meta_data = ts.get_intraday(symbol=name, interval=time, outputsize=output) #print_time_series(data,name,time,type) return data elif (type == 'Day Adjusted'): data, meta_data = ts.get_daily_adjusted(symbol=name, outputsize=output) print_time_series(data, name, time, type) elif (type == 'Weekly'): data, meta_data = ts.get_weekly(symbol=name) print_time_series(data, name, time, type) elif (type == 'Weekly Adjusted'): data, meta_data = ts.get_weekly_adjusted(symbol=name) print_time_series(data, name, time, type) elif (type == 'Monthly'): data, meta_data = ts.get_monthly(symbol=name) #print_time_series(data,name,time,type) description(data, name, time, type) elif (type == 'Monthly Adjusted'): data, meta_data = ts.get_monthly_adjusted(symbol=name) print_time_series(data, name, time, type)
def getValues(symbol, currency='Stock'): ''' get the full time series from a given stock or cryptocurrency :param symbol: symbol of desired stock or crypto :param currency: 'stock' or 'crypto' :return: (x,y) x is the dates, and y is the stock value ''' if currency == 'Stock': ts = TimeSeries(key=api_key, output_format='pandas') try: data, meta_data = ts.get_weekly(symbol) except KeyError: return None, None x = data.index.values # get index values (dates) y = data.iloc[:, 3].values # get third column which is closing price return x, y elif currency == 'Cryptocurrency': cc = CryptoCurrencies(key=api_key, output_format='pandas') try: data, meta_data = cc.get_digital_currency_weekly(symbol, market='USD') except KeyError: return None, None x = data.index.values # get index values (dates) y = data.iloc[:, 3].values # get third column which is closing price return x, y
def getAVdata(stock, interval, akey): while True: try: ts = TimeSeries(key=alphakeys[akey], output_format='pandas') if interval == 'daily': tdata, meta_data = ts.get_daily(symbol=stock, outputsize='full') elif interval == 'weekly': tdata, meta_data = ts.get_weekly(symbol=stock, outputsize='full') elif interval == 'monthly': tdata, meta_data = ts.get_monthly(symbol=stock, outputsize='full') else: tdata, meta_data = ts.get_intraday(symbol=stock, interval=interval, outputsize='full') break except Exception as e: print('Exception!!!: Stock %s Alphakey: %s Interval: %s' % (stock, alphakeys[akey], interval)) akey = (akey + 1) % len(alphakeys) print(e, '\n') continue print(stock, alphakeys[akey], interval) tdata = tdata.rename(columns={'1. open': 'Open', '2. high': 'High', '3. low': 'Low', \ '4. close': 'Close', '5. volume': 'Volume'}) tdata = tdata.dropna() return tdata, akey
def get(self, request): ALPHA_VANTAGE_API_KEY = os.environ['ALPHA_VANTAGE_API_KEY'] ts = TimeSeries(key='ALPHA_VANTAGE_API_KEY') data, meta_data = ts.get_weekly(symbol='^BVSP.BS', outputsize='full') with open('data.json', 'w') as f: f.write(json.dumps(data, indent=4)) with open('meta_data.json', 'w') as f: f.write(json.dumps(meta_data, indent=4)) return TemplateResponse(request, self.template_name, {})
def weeklyPivotInfo(symbol): ts = TimeSeries(key=key, output_format='pandas') data, meta_data = ts.get_weekly(symbol=symbol) high = data.iloc[ 1, 1] # ['1. open', '2. high', '3. low', '4. close', '5. volume'] low = data.iloc[1, 2] close = data.iloc[1, 3] pivotPointCalculation = (high + low + close) / 3 pivotPoint = round(pivotPointCalculation, 2) print(pivotPoint) return pivotPoint
def stockData(self): ts = TimeSeries(key=self.apiKey, output_format='pandas') if (self.timePeriod == "interday"): data, meta_data = ts.get_intraday(symbol=self.stockName) elif (self.timePeriod == "daily"): data, meta_data = ts.get_daily(symbol=self.stockName) elif (self.timePeriod == "weekly"): data, meta_data = ts.get_weekly(symbol=self.stockName) elif (self.timePeriod == "monthly"): data, meta_data = ts.get_monthly(symbol=self.stockName) return data
def get_stock_data(ticker): """Get the weekly stock data for the ticker :param ticker: Ticker to generate data for :type ticker: str :returns: weekly stock data for ticker :rtype: pd.df """ print(f'Getting stock data for: {ticker}') ts = TimeSeries(key=alpha_key) data = ts.get_weekly(ticker) data = format(data) return data
def Feed_Weekly(self): collectionname = 'Weekly' for com in self.Company: try: ts = TimeSeries(key=self.APIKEYS[2], output_format='pandas') data, meta_data = ts.get_weekly(com) data = pd.DataFrame(data) data.reset_index(inplace=True) mongodb.UpdateValue(collectionname, com, data.to_dict(orient='list')) # mongodb.WriteValue(collectionname, com, data.to_dict(orient='list')) # print(data) except Exception as e: print('Company Ignore due to high service call' + '\nError : ' + str(e))
def ogweekly(name, k): key = k ts = TimeSeries(key, output_format='pandas') #ti = TechIndicators(key) #sym = input('Enter stock symbol: ') global dataw sym = name dataw, meta_data = ts.get_weekly(symbol=sym) print("model for week data") #print(data) fn = name + ".week.csv" with open(fn, 'w') as ff: dataw.to_csv(ff)
def output(self): """ Retrieve stock data @return TableWrapper of stock data """ stock_symbols = self.ap_paramList[0]() timeseries_retriever = TimeSeries(key=DataFetcher.getConfigItem( 'stocks', 'api_key'), output_format='pandas', indexing_type='date') data_dict = OrderedDict() metadata_dict = OrderedDict() for symbol in stock_symbols: # Extract data if self.data_type == 'daily': data, metadata = timeseries_retriever.get_daily( symbol, outputsize='full') elif self.data_type == 'daily_adjusted': data, metadata = timeseries_retriever.get_daily_adjusted( symbol, outputsize='full') elif self.data_type == 'monthly': data, metadata = timeseries_retriever.get_monthly(symbol) elif self.data_type == 'monthly_adjusted': data, metadata = timeseries_retriever.get_monthly_adjusted( symbol) elif self.data_type == 'weekly': data, metadata = timeseries_retriever.get_weekly(symbol) elif self.data_type == 'weekly_adjusted': data, metadata = timeseries_retriever.get_weekly_adjusted( symbol) elif self.data_type == 'intraday': data, metadata = timeseries_retriever.get_weekly_adjusted( symbol, self.interval, outputsize='full') # Convert index to pandas datetime if self.data_type == 'intraday': data.index = pd.to_datetime(data.index).tz_localize( metadata['6. Time Zone']) else: data.index = pd.to_datetime(data.index) data_dict[symbol] = data[self.start_date:self.end_date] metadata_dict[symbol] = metadata return TableWrapper(data_dict, meta_data=metadata_dict)
def stockchart_1year(symbol1, symbol2): # Make the interval be daily title = '1 Year' ts = TimeSeries(key='QBGZM8IV1P2X2VJQ', output_format='pandas') data1, meta_data = ts.get_weekly(symbol=symbol1) data2, meta_data = ts.get_weekly(symbol=symbol2) newdata1 = data1.drop(data1.index[0:len(data1.index) - 53]) newdata2 = data2.drop(data2.index[0:len(data2.index) - 53]) newdata1 = modify_data(newdata1) newdata2 = modify_data(newdata2) # print(newdata1) return graph(symbol1, symbol2, newdata1, newdata2, meta_data, ts, title)
def get_data_weekly(symbol, savingtoCsv=True): # gets data over of a week # loading necessary modules from alpha_vantage.timeseries import TimeSeries # getting api key API_KEY, waiting_times = api_key_finder() # creating necessary object ts = TimeSeries(key=API_KEY, output_format='pandas', indexing_type='integer') # reading data into pandas data, meta_data = ts.get_weekly(symbol=symbol) # writing data to database and csv write_to_database(data, 'Weekly', symbol, 'weekly', savingtoCsv) return data, meta_data
def getTimeSeries(self, function, symbols, interval='1min', outputsize="compact", datatype="pandas"): #check if datatype is valid and create instance of time series if datatype != 'pandas' and datatype != 'json' and datatype != 'csv': print( "Invalid Datatype: Vaible options are 'pandas', 'json', 'csv'") return -1 else: ts = TimeSeries(key=self.key, output_format=datatype) #check if outputsize is a valid input if outputsize != 'compact' and outputsize != 'full': print( "Invalide Output Size: Viable options are 'compact' and 'full'" ) return -1 #determine what time series the user wants and build request if function == 'intraday': valid_intervals = ['1min', '5min', '15min', '30min', '60min'] if interval not in valid_intervals: print( "Invalid Interval: Viable options are '1min', '5min', '15min', '30min', '60min'" ) return -1 else: data, meta_data = ts.get_intraday(symbol=symbols, interval=interval, outputsize=outputsize) elif function == 'daily': data, meta_data = ts.get_daily(symbol=symbols, outputsize=outputsize) elif function == 'weekly': data, meta_data = ts.get_weekly(symbol=symbols) elif function == 'monthly': data, meta_data = ts.get_monthly(symbol=symbols) else: print( "Invalid Function: Viable options are 'intraday', 'daily', 'weekly', and 'monthly'" ) return -1 return data
def get_weekly_price(self): ''' object -> None This method gets a json file with prices ''' ts = TimeSeries(key='WYXA08Z6LYUO5HL1', retries=2) # Get json object with the intraday data and another with the call's metadata price = ts.get_weekly(self.name)[0] self.history = {} for i in price: if len(self.history) < 25: self.history[i] = price[i] else: break
def getData(symbol, timeSeries, chartType, startDate, endDate): ts = TimeSeries(key=API_KEY, output_format='pandas') if timeSeries == '1': data, meta_data = ts.get_intraday(symbol=symbol, interval='60min', outputsize='full') f = 'H' if timeSeries == '2': data, meta_data = ts.get_daily(symbol=symbol, outputsize='compact') f = 'D' if timeSeries == '3': data, meta_data = ts.get_weekly(symbol=symbol) f = 'W' if timeSeries == '4': data, meta_data = ts.get_monthly(symbol=symbol) f = 'M' data_date_changed = data[endDate:startDate] if chartType == "1": line_chart = pygal.Bar(x_label_rotation=20, width=1000, height=400) line_chart.title = 'Stock Data for {}: {} to {}'.format( symbol, startDate, endDate) labels = data_date_changed.index.to_list() line_chart.x_labels = reversed(labels) line_chart.add("Open", data_date_changed['1. open']) line_chart.add("High", data_date_changed['2. high']) line_chart.add("Low", data_date_changed['3. low']) line_chart.add("Close", data_date_changed['4. close']) line_chart.render_in_browser() if chartType == "2": line_chart = pygal.Line(x_label_rotation=20, spacing=80) line_chart.title = 'Stock Data for {}: {} to {}'.format( symbol, startDate, endDate) labels = data_date_changed.index.to_list() line_chart.x_labels = reversed(labels) line_chart.add("Open", data_date_changed['1. open']) line_chart.add("High", data_date_changed['2. high']) line_chart.add("Low", data_date_changed['3. low']) line_chart.add("Close", data_date_changed['4. close']) line_chart.render_in_browser()
def load_data_history(ticker='SPY', freq='15min', start='2014', end='2016'): """ Loads data from Yahoo. After loading it renames columns to shorter format, which is what Backtest expects. Set `adjust close` to True to correct all fields with with divident info provided by Yahoo via Adj Close field. Defaults are in place for convenience. """ if isinstance(ticker, list): return pd.Panel({ t: load_data_history(ticker=t, start=start, adjust_close=adjust_close) for t in ticker }) ts = TimeSeries(key='8YLDYU6Z9IAZNIS4') if freq == 'daily': data, meta_data = ts.get_daily(ticker, outputsize='full') elif freq == 'weekly': data, meta_data = ts.get_weekly(ticker, outputsize='full') elif freq == 'monthly': data, meta_data = ts.get_monthly(ticker, outputsize='full') else: data, meta_data = ts.get_intraday( ticker, interval=freq, outputsize='full') #supported 1min, 15min, 30min, 60min #r = data['Close'] ohlc_cols = ['Open', 'High', 'Low', 'Close'] #data[ohlc_cols] = data[ohlc_cols].mul(r, axis=0) data = data.rename( columns={ 'Open': 'O', 'High': 'H', 'Low': 'L', 'Close': 'C', 'Adj Close': 'AC', 'Volume': 'V' }) return data
def get_historical_data(key, time_slice, symbol): """Wrapper function to source historical EOD stock data Args: key (str): alphavantage api key time_slice (str): Aggregate level to fetch. Options are: - daily - weekly - monthly symbol (str): Symbol used, including the exchange Returns: DataFrame: EOD dataframe """ # Instantiate Session ts = TimeSeries(key=key, output_format="pandas", indexing_type="integer") # Retrieve Data if time_slice == "daily": df, metadata = ts.get_daily(symbol, outputsize="full") elif time_slice == "weekly": df, metadata = ts.get_weekly(symbol) elif time_slice == "monthly": df, metadata = ts.get_monthly(symbol) # Replace 0's with NA's because they're almost certainly false df.replace(0, np.nan, inplace=True) # Fix crappy column header that contain numbers & periods df.columns = [ remove_numbers(x).replace(". ", "") for x in df.columns.tolist() ] # Fix Date type df["date"] = pd.to_datetime(df["date"]) return df
class StockTicker: def __init__(self): with open("../config/alpha_vantage_api_key.json", "r") as f: api_key = json.load(f) self.ts = TimeSeries(key=api_key["key"], output_format='pandas') self.sym = "" def get_ticker(self): if datetime.datetime.today().weekday() not in [5, 6]: try: data, meta_data = self.ts.get_intraday(symbol=self.sym, outputsize="compact", interval="1min") print(data) return data except ValueError: return False else: try: data, meta_data = self.ts.get_weekly(symbol=self.sym) return yf.Ticker(self.sym+".NS").history(period='1d').iloc[0, 3] except ValueError: return False def set_sym(self, symbol): self.sym = symbol
def collectData(self, outputsize='compact') -> pd.DataFrame: ts = TimeSeries(key=key, output_format='pandas') data, metadata = ts.get_weekly(self.symbol) return pd.DataFrame(data)
class AVInterface: """ AlphaVantage interface class, provides methods to call AlphaVantage API and return the result in useful format handling possible errors. """ def __init__(self, apiKey, config): self.read_configuration(config) self._last_call_ts = dt.datetime.now() self.TS = TimeSeries( key=apiKey, output_format="pandas", indexing_type="integer", treat_info_as_error=True, retries=0, ) self.TI = TechIndicators(key=apiKey, output_format="pandas", retries=0) logging.info("AlphaVantage initialised.") def read_configuration(self, config): self.enable = config["alpha_vantage"]["enable"] self.api_timeout = config["alpha_vantage"]["api_timeout"] def get_prices(self, market_id, interval): """ Return the price time series of the requested market with the interval granularity. Return None if the interval is invalid """ if ( interval == AVInterval.MIN_1 or interval == AVInterval.MIN_5 or interval == AVInterval.MIN_15 or interval == AVInterval.MIN_30 or interval == AVInterval.MIN_60 ): return self.intraday(market_id, interval) elif interval == AVInterval.DAILY: return self.daily(market_id) elif interval == AVInterval.WEEKLY: return self.weekly(market_id) # TODO implement monthly call else: return None def daily(self, marketId): """ Calls AlphaVantage API and return the Daily time series for the given market - **marketId**: string representing an AlphaVantage compatible market id - Returns **None** if an error occurs otherwise the pandas dataframe """ self._wait_before_call() market = self._format_market_id(marketId) try: data, meta_data = self.TS.get_daily(symbol=market, outputsize="full") return data except Exception as e: logging.error("AlphaVantage wrong api call for {}".format(market)) logging.debug(e) logging.debug(traceback.format_exc()) logging.debug(sys.exc_info()[0]) return None def intraday(self, marketId, interval): """ Calls AlphaVantage API and return the Intraday time series for the given market - **marketId**: string representing an AlphaVantage compatible market id - **interval**: string representing an AlphaVantage interval type - Returns **None** if an error occurs otherwise the pandas dataframe """ self._wait_before_call() if interval is AVIntervals.DAILY: logging.error("AlphaVantage Intraday does not support DAILY interval") return None market = self._format_market_id(marketId) try: data, meta_data = self.TS.get_intraday( symbol=market, interval=interval.value, outputsize="full" ) return data except Exception as e: logging.error("AlphaVantage wrong api call for {}".format(market)) logging.debug(e) logging.debug(traceback.format_exc()) logging.debug(sys.exc_info()[0]) return None def weekly(self, marketId): """ Calls AlphaVantage API and return the Weekly time series for the given market - **marketId**: string representing an AlphaVantage compatible market id - Returns **None** if an error occurs otherwise the pandas dataframe """ self._wait_before_call() market = self._format_market_id(marketId) try: data, meta_data = self.TS.get_weekly(symbol=market) return data except Exception as e: logging.error("AlphaVantage wrong api call for {}".format(market)) logging.debug(e) logging.debug(traceback.format_exc()) logging.debug(sys.exc_info()[0]) return None def quote_endpoint(self, market_id): """ Calls AlphaVantage API and return the Quote Endpoint data for the given market - **market_id**: string representing the market id to fetch data of - Returns **None** if an error occurs otherwise the pandas dataframe """ self._wait_before_call() market = self._format_market_id(market_id) try: data, meta_data = self.TS.get_quote_endpoint( symbol=market, outputsize="full" ) return data except: logging.error("AlphaVantage wrong api call for {}".format(market)) return None # Technical indicators def macdext(self, marketId, interval): """ Calls AlphaVantage API and return the MACDEXT tech indicator series for the given market - **marketId**: string representing an AlphaVantage compatible market id - **interval**: string representing an AlphaVantage interval type - Returns **None** if an error occurs otherwise the pandas dataframe """ self._wait_before_call() market = self._format_market_id(marketId) try: data, meta_data = self.TI.get_macdext( market, interval=interval.value, series_type="close", fastperiod=12, slowperiod=26, signalperiod=9, fastmatype=2, slowmatype=1, signalmatype=0, ) if data is None: return None data.index = range(len(data)) return data except Exception as e: logging.error("AlphaVantage wrong api call for {}".format(market)) logging.debug(e) logging.debug(traceback.format_exc()) logging.debug(sys.exc_info()[0]) return None def macd(self, marketId, interval): """ Calls AlphaVantage API and return the MACDEXT tech indicator series for the given market - **marketId**: string representing an AlphaVantage compatible market id - **interval**: string representing an AlphaVantage interval type - Returns **None** if an error occurs otherwise the pandas dataframe """ self._wait_before_call() market = self._format_market_id(marketId) try: data, meta_data = self.TI.get_macd( market, interval=interval.value, series_type="close", fastperiod=12, slowperiod=26, signalperiod=9, ) return data except Exception as e: logging.error("AlphaVantage wrong api call for {}".format(market)) logging.debug(e) logging.debug(traceback.format_exc()) logging.debug(sys.exc_info()[0]) return None # Utils functions def _format_market_id(self, marketId): """ Convert a standard market id to be compatible with AlphaVantage API. Adds the market exchange prefix (i.e. London is LON:) """ return "{}:{}".format("LON", marketId.split("-")[0]) def _wait_before_call(self): """ Wait between API calls to not overload the server """ while (dt.datetime.now() - self._last_call_ts) <= dt.timedelta( seconds=self.api_timeout ): time.sleep(0.5) self._last_call_ts = dt.datetime.now()
key = json.load(open("alpha-vantage-api.key.json"))[ "key"] # '7FEDJMHY3CM2KPEC' # Your API Key # https://github.com/RomelTorres/alpha_vantage # Chose your output format or default to JSON (python dict) ts = TimeSeries(key, output_format='pandas') # 'pandas' or 'json' or 'csv' #------------------------------------------------------------------------------- # (only for example purposes) Pull data from API and prepare it for plotting on line chart SYMBOL = "IBM" # Get the data, returns a tuple # quote_data is a pandas dataframe, quote_meta_data is a dict # https://github.com/RomelTorres/alpha_vantage/blob/develop/alpha_vantage/timeseries.py # quote_data, quote_meta_data = ts.get_intraday(symbol=SYMBOL,interval='1min', outputsize='compact') # quote_data, quote_meta_data = ts.get_daily(symbol=SYMBOL, outputsize='compact') # "full" quote_data, quote_meta_data = ts.get_weekly(symbol=SYMBOL) # print(quote_meta_data) df = quote_data.copy() # print(df.head()) df = df.transpose() # print(df.head()) df.rename(index={ "1. open": "open", "2. high": "high", "3. low": "low", "4. close": "close", "5. volume": "volume" },
0, len(title_list) ): #loops through all title_list(list of stocks mentions in title) if (current_phrase_parsed[y].lower() == title_list[z] ): # checks if these phrases mention a stock if ( ranked_list[x][0] < 3.0 ): # checks if mentioned stock is error (if word has low score and therfore not an actual stock mention) title_list[z] = "" # makes it blank false_positive(title_list) for x in range(0, len(title_list)): try: stock_tic = str(title_list[z]) if (stock_tic != ""): data, meta_data = ts.get_weekly(symbol=stock_tic) data['4. close'].plot() plt.title('Intraday Times Series for the ' + stock_tic + ' stock (1 min)') plt.show() continue except: print("error") pass print(title_list) ranked_phrases = [] ranked_length = 0 print(submission.title) submission_text = submission.selftext
from alpha_vantage.techindicators import TechIndicators from urllib2 import HTTPError import time import numpy as np import sys from alpha_vantage.timeseries import TimeSeries text_file = open("weekly.txt", "r") symbols = text_file.read().split() bull_flag = [] unprocessed = [] ts = TimeSeries(key='MKAV2AXSQSPRE2R0', output_format='pandas') for s in symbols: try: data, meta_data = ts.get_weekly(symbol=s) close_price = data["close"] for i in range(-20, -3): if (data["close"][i] >= 1.1 * data["open"][i]): bull_range = data["close"][i] - data["open"][i] current_position_since_bull = data["close"][-1] - data["open"][ i] if (current_position_since_bull > 0.5 * bull_range): bull_flag.append(s) break except: print s, ", Unexpected error:", sys.exc_info()[0] unprocessed.append(s) continue output_file = open("unprocessed_bull_flag_weekly.txt", "w")
class AVInterface(StocksInterface): """ AlphaVantage interface class, provides methods to call AlphaVantage API and return the result in useful format handling possible errors. """ def initialise(self) -> None: logging.info("Initialising AVInterface...") api_key = self._config.get_credentials()["av_api_key"] self.TS = TimeSeries(key=api_key, output_format="pandas", treat_info_as_error=True) self.TI = TechIndicators(key=api_key, output_format="pandas", treat_info_as_error=True) def _to_av_interval(self, interval: Interval) -> AVInterval: """ Convert the Broker Interval to AlphaVantage compatible intervals. Return the converted interval or None if a conversion is not available """ if interval == Interval.MINUTE_1: return AVInterval.MIN_1 elif interval == Interval.MINUTE_5: return AVInterval.MIN_5 elif interval == Interval.MINUTE_15: return AVInterval.MIN_15 elif interval == Interval.MINUTE_30: return AVInterval.MIN_30 elif interval == Interval.HOUR: return AVInterval.MIN_60 elif interval == Interval.DAY: return AVInterval.DAILY elif interval == Interval.WEEK: return AVInterval.WEEKLY elif interval == Interval.MONTH: return AVInterval.MONTHLY else: logging.error( "Unable to convert interval {} to AlphaVantage equivalent". format(interval.value)) raise ValueError("Unsupported Interval value: {}".format(interval)) def get_prices(self, market: Market, interval: Interval, data_range: int) -> MarketHistory: data = None av_interval = self._to_av_interval(interval) if (av_interval == AVInterval.MIN_1 or av_interval == AVInterval.MIN_5 or av_interval == AVInterval.MIN_15 or av_interval == AVInterval.MIN_30 or av_interval == AVInterval.MIN_60): data = self.intraday(market.id, av_interval) elif av_interval == AVInterval.DAILY: data = self.daily(market.id) elif av_interval == AVInterval.WEEKLY: data = self.weekly(market.id) # TODO implement monthly call else: raise ValueError("Unsupported Interval.{}".format(interval.name)) history = MarketHistory( market, data.index, data["2. high"].values, data["3. low"].values, data["4. close"].values, data["5. volume"].values, ) return history def daily(self, marketId: str) -> pandas.DataFrame: """ Calls AlphaVantage API and return the Daily time series for the given market - **marketId**: string representing an AlphaVantage compatible market id - Returns **None** if an error occurs otherwise the pandas dataframe """ self._wait_before_call(self._config.get_alphavantage_api_timeout()) market = self._format_market_id(marketId) try: data, meta_data = self.TS.get_daily(symbol=market, outputsize="full") return data except Exception as e: print(e) logging.error("AlphaVantage wrong api call for {}".format(market)) logging.debug(e) logging.debug(traceback.format_exc()) logging.debug(sys.exc_info()[0]) return None def intraday(self, marketId: str, interval: AVInterval) -> pandas.DataFrame: """ Calls AlphaVantage API and return the Intraday time series for the given market - **marketId**: string representing an AlphaVantage compatible market id - **interval**: string representing an AlphaVantage interval type - Returns **None** if an error occurs otherwise the pandas dataframe """ self._wait_before_call(self._config.get_alphavantage_api_timeout()) market = self._format_market_id(marketId) try: data, meta_data = self.TS.get_intraday(symbol=market, interval=interval.value, outputsize="full") return data except Exception as e: logging.error("AlphaVantage wrong api call for {}".format(market)) logging.debug(e) logging.debug(traceback.format_exc()) logging.debug(sys.exc_info()[0]) return None def weekly(self, marketId: str) -> pandas.DataFrame: """ Calls AlphaVantage API and return the Weekly time series for the given market - **marketId**: string representing an AlphaVantage compatible market id - Returns **None** if an error occurs otherwise the pandas dataframe """ self._wait_before_call(self._config.get_alphavantage_api_timeout()) market = self._format_market_id(marketId) try: data, meta_data = self.TS.get_weekly(symbol=market) return data except Exception as e: logging.error("AlphaVantage wrong api call for {}".format(market)) logging.debug(e) logging.debug(traceback.format_exc()) logging.debug(sys.exc_info()[0]) return None def quote_endpoint(self, market_id: str) -> pandas.DataFrame: """ Calls AlphaVantage API and return the Quote Endpoint data for the given market - **market_id**: string representing the market id to fetch data of - Returns **None** if an error occurs otherwise the pandas dataframe """ self._wait_before_call(self._config.get_alphavantage_api_timeout()) market = self._format_market_id(market_id) try: data, meta_data = self.TS.get_quote_endpoint(symbol=market, outputsize="full") return data except Exception: logging.error("AlphaVantage wrong api call for {}".format(market)) return None # Technical indicators def get_macd(self, market: Market, interval: Interval, datapoints_range: int) -> MarketMACD: av_interval = self._to_av_interval(interval) data = self.macdext(market.id, av_interval) macd = MarketMACD( market, data.index, data["MACD"].values, data["MACD_Signal"].values, data["MACD_Hist"].values, ) return macd def macdext(self, marketId: str, interval: AVInterval) -> pandas.DataFrame: """ Calls AlphaVantage API and return the MACDEXT tech indicator series for the given market - **marketId**: string representing an AlphaVantage compatible market id - **interval**: string representing an AlphaVantage interval type - Returns **None** if an error occurs otherwise the pandas dataframe """ self._wait_before_call(self._config.get_alphavantage_api_timeout()) market = self._format_market_id(marketId) data, meta_data = self.TI.get_macdext( market, interval=interval.value, series_type="close", fastperiod=12, slowperiod=26, signalperiod=9, fastmatype=2, slowmatype=1, signalmatype=0, ) return data def macd(self, marketId: str, interval: AVInterval) -> pandas.DataFrame: """ Calls AlphaVantage API and return the MACDEXT tech indicator series for the given market - **marketId**: string representing an AlphaVantage compatible market id - **interval**: string representing an AlphaVantage interval type - Returns **None** if an error occurs otherwise the pandas dataframe """ self._wait_before_call(self._config.get_alphavantage_api_timeout()) market = self._format_market_id(marketId) data, meta_data = self.TI.get_macd( market, interval=interval.value, series_type="close", fastperiod=12, slowperiod=26, signalperiod=9, ) return data # Utils functions def _format_market_id(self, marketId: str) -> str: """ Convert a standard market id to be compatible with AlphaVantage API. Adds the market exchange prefix (i.e. London is LON:) """ # TODO MarketProvider/IGInterface should return marketId without "-UK" return "{}:{}".format("LON", marketId.split("-")[0])
def get_weekly(symbol: str): ts = TimeSeries(key=config.AV_KEY, output_format="pandas") data, _ = ts.get_weekly(symbol) return data
class AlphaVantageClient: def __init__(self, api_key=None): if api_key is None: default_api_key = utils.get_api_key('AlphaVantage') if default_api_key is None: raise ValueError('No AlphaVantage API Key found.') else: self.ts = TimeSeries(key=default_api_key, output_format='pandas') else: self.ts = TimeSeries(key=api_key, output_format='pandas') def get_data(self, symbol, freq='daily', adjusted=True, interval='15min', outputsize='full'): """ Return time series in pandas formet. Keyword Arguments: symbol: the symbol for the equity we want to get its data freq: frequency of data, supported values are 'daily', 'weekly', 'monthly' (default 'daily'). Currently not support for 'intraday' adjusted: adjust the OHLC value (default True) interval: time interval between two conscutive values, used when freq is intraday supported values are '1min', '5min', '15min', '30min', '60min' (default '15min') outputsize: The size of the call, supported values are 'compact' and 'full; the first returns the last 100 points in the data series, and 'full' returns the full-length intraday times series, commonly above 1MB (default 'compact') """ key = '{}-{}'.format(freq, adjusted) # elif key == 'intraday-True': # data, _ = self.ts.get_intraday(symbol=symbol, interval=interval, outputsize=outputsize) # elif key == 'intraday-False': # data, _ = self.ts.get_intraday(symbol=symbol, interval=interval, outputsize=outputsize) if key == 'daily-True': data, _ = self.ts.get_daily_adjusted(symbol=symbol, outputsize=outputsize) elif key == 'daily-False': data, _ = self.ts.get_daily(symbol=symbol, outputsize=outputsize) elif key == 'weekly-True': data, _ = self.ts.get_weekly_adjusted(symbol=symbol) elif key == 'weekly-False': data, _ = self.ts.get_weekly(symbol=symbol) elif key == 'monthly-True': data, _ = self.ts.get_monthly_adjusted(symbol=symbol) elif key == 'monthly-False': data, _ = self.ts.get_monthly(symbol=symbol) else: raise Warning( 'Freq: {} or Adjusted: {} is not valid. Default to Daily Adjusted.' ) data, _ = self.ts.get_daily_adjusted(symbol=symbol, outputsize=outputsize) if freq == 'intraday': data.columns = ['Open', 'High', 'Low', 'Close', 'Volume'] else: columns_name = [ 'Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume', 'Dividends', 'Stock Splits' ] data.columns = columns_name[:len(data.columns)] data = data.rename(index={'date': 'Date'}) return data.sort_index(ascending=True) def get_latest_data(self, symbol): """ Return the latest price and volume information for a security of your choice Keyword Arguments: symbol: the symbol for the equity we want to get its data """ data, _ = self.ts.get_quote_endpoint(symbol=symbol) columns_name = [ 'Open', 'High', 'Low', 'Price', 'Volume', 'Date', 'Previous Close', 'Change', 'Change Pct' ] data = data.iloc[:, 1:] data.columns = columns_name data = data.set_index(['Date']) return data
args = parser.parse_args() if args.interval not in valid_intervals: raise ValueError('Invalid time interval entered') # Create time series object for alpha vantage data key = open('avkey.txt', 'r').read() ts = TimeSeries(key=key, output_format='csv') # Get data from alpha vantage print('Getting data from Alpha Vantage...') if args.interval in valid_intervals[:3]: dataset, dataset_meta = ts.get_intraday(symbol=args.symbol, interval=args.interval, outputsize='full') elif args.interval == '1day': dataset, dataset_meta = ts.get_daily(symbol=args.symbol, outputsize='full') elif args.interval == '1week': dataset, dataset_meta = ts.get_weekly(symbol=args.symbol) else: dataset, dataset_meta = ts.get_monthly(symbol=args.symbol) # Write data to files and upload to google drive set_file = '{}-{}.csv'.format(args.symbol, args.interval) print('Writing data to {}'.format(set_file)) data_to_csv(set_file, dataset) #google_doc_upload(set1_file, 'csv', ) #google_doc_upload(set2_file, 'csv', ) #google_doc_upload(set3_file, 'csv', )
class AlphaVantage: def __init__(self, last_date = None): self.key = config.get_alphavantage_key() self.last_date = last_date self.ts = TimeSeries(key=self.key, retries=5, output_format='pandas', indexing_type='date') def compact_quotes(self, sym): try: df, m = self.ts.get_daily(sym, outputsize='compact') except: print 'Compact quote get caused exception' return None if df is not None: df = df.rename(columns = {'1. open': 'open', '2. high': 'high', '3. low': 'low', '4. close': 'close', '5. volume': 'volume'}) df = df[['open', 'high', 'low', 'close', 'volume']] return df def full_quotes(self, sym): try: df, m = self.ts.get_daily(sym, outputsize='full') except: print 'Full quote get caused exception' return None if df is not None: df = df.rename(columns = {'1. open': 'open', '2. high': 'high', '3. low': 'low', '4. close': 'close', '5. volume': 'volume'}) df = df[['open', 'high', 'low', 'close', 'volume']] return df def compact_quotes_intraday(self, sym): try: df, m = self.ts.get_intraday(sym, interval='30min', outputsize='compact') except: print 'Compact quote intraday get caused exception' return None if df is not None: df = df.rename(columns = {'1. open': 'open', '2. high': 'high', '3. low': 'low', '4. close': 'close', '5. volume': 'volume'}) df = df[['open', 'high', 'low', 'close', 'volume']] return df def full_quotes_intraday(self, sym): try: df, m = self.ts.get_intraday(sym, interval='30min', outputsize='full') except: print 'Full quote intraday get caused exception' return None if df is not None: df = df.rename(columns = {'1. open': 'open', '2. high': 'high', '3. low': 'low', '4. close': 'close', '5. volume': 'volume'}) df = df[['open', 'high', 'low', 'close', 'volume']] return df def quotes_weekly(self, sym): try: df, m = self.ts.get_weekly(sym) except: print 'Weekly quote get caused exception' return None if df is not None: df = df.rename(columns = {'1. open': 'open', '2. high': 'high', '3. low': 'low', '4. close': 'close', '5. volume': 'volume'}) df = df[['open', 'high', 'low', 'close', 'volume']] return df def quotes_monthly(self, sym): try: df, m = self.ts.get_monthly(sym) except: print 'Weekly quote get caused exception' return None if df is not None: df = df.rename(columns = {'1. open': 'open', '2. high': 'high', '3. low': 'low', '4. close': 'close', '5. volume': 'volume'}) df = df[['open', 'high', 'low', 'close', 'volume']] return df
class REST(object): def __init__(self, api_key): self._api_key = get_alpha_vantage_credentials(api_key) self._session = requests.Session() self._timeseries = TimeSeries(key=self._api_key) self._cryptocurrencies = CryptoCurrencies(key=self._api_key) self._foreignexchange = ForeignExchange(key=self._api_key) self._sectorperformance = SectorPerformances(key=self._api_key) self._techindicators = TechIndicators(key=self._api_key) def _request(self, method, params=None): url = 'https://www.alphavantage.co/query?' params = params or {} params['apikey'] = self._api_key resp = self._session.request(method, url, params=params) resp.raise_for_status() return resp.json() def get(self, params=None): ''' Customizable endpoint, where you can pass all keywords/paramters from the documentation: https://www.alphavantage.co/documentation/# Returns: pandas, csv, or json ''' return self._request('GET', params=params) def historic_quotes(self, symbol, adjusted=False, outputsize='full', cadence='daily', output_format=None): ''' Returns the one of the TIME_SERIES_* endpoints of the Alpha Vantage API. Params: symbol: The ticker to return adjusted: Return the adjusted prices cadence: Choose between ['daily', 'weekly', 'monthly'], to return the cadence output_format: Choose between['json', 'csv', 'pandas'] Returns: pandas, csv, or json ''' if output_format: self._timeseries.output_format = output_format if cadence == 'daily': data, _ = self._timeseries.get_daily_adjusted( symbol=symbol, outputsize=outputsize) if adjusted else self._timeseries.get_daily(symbol=symbol, outputsize=outputsize) if cadence == 'weekly': data, _ = self._timeseries.get_weekly_adjusted( symbol=symbol) if adjusted else self._timeseries.get_weekly(symbol=symbol) if cadence == 'monthly': data, _ = self._timeseries.get_monthly_adjusted( symbol=symbol) if adjusted else self._timeseries.get_monthly(symbol=symbol) return data def intraday_quotes(self, symbol, interval='5min', outputsize='full', output_format=None): ''' Returns the TIME_SERIES_INTRADAY endpoint of the Alpha Vantage API. Params: symbol: The ticker to return interval: Choose between['1min', '5min', '15min', '30min', '60min'] output_format: Choose between['json', 'csv', 'pandas'] Returns: pandas, csv, or json ''' if output_format: self._timeseries.output_format = output_format data, _ = self._timeseries.get_intraday( symbol=symbol, interval=interval, outputsize=outputsize) return data def current_quote(self, symbol): ''' Returns the GLOBAL_QUOTE endpoint of the Alpha Vantage API. Params: symbol: The ticker to return output_format: Choose between['json', 'csv', 'pandas'] Returns: pandas, csv, or json ''' data, _ = self._timeseries.get_quote_endpoint(symbol=symbol) return data def last_quote(self, symbol): return self.current_quote(symbol) def company(self, symbol, datatype='json'): return self.search_endpoint(symbol, datatype=datatype) def search_endpoint(self, keywords, datatype='json'): '''Search endpoint returns a list of possible companies that correspond to keywords Params: datatype: csv, json, or pandas keywords: ex. keywords=microsoft Returns: pandas, csv, or json ''' params = {'function': 'SYMBOL_SEARCH', 'keywords': keywords, 'datatype': datatype} return self.get(params) def historic_fx_quotes(self, from_symbol, to_symbol, outputsize='full', cadence='daily', output_format=None): ''' Returns the one of the FX_* endpoints of the Alpha Vantage API. Params: from_currency: The symbol to convert to_currency: The symbol to convert to cadence: Choose between ['daily', 'weekly', 'monthly'], to return the cadence output_format: Choose between['json', 'csv', 'pandas'] Returns: pandas, csv, or json ''' if output_format: self._foreignexchange.output_format = output_format if cadence == 'daily': data, _ = self._foreignexchange.get_currency_exchange_daily( from_symbol=from_symbol, to_symbol=to_symbol, outputsize=outputsize) if cadence == 'weekly': data, _ = self._foreignexchange.get_currency_exchange_weekly( from_symbol=from_symbol, to_symbol=to_symbol, outputsize=outputsize) if cadence == 'monthly': data, _ = self._foreignexchange.get_currency_exchange_monthly( from_symbol=from_symbol, to_symbol=to_symbol, utputsize=outputsize) return data def intraday_fx_quotes(self, from_symbol, to_symbol, interval='5min', outputsize='full', output_format=None): ''' Returns the FX_INTRADAY endpoint of the Alpha Vantage API. Params: from_currency: The symbol to convert to_currency: The symbol to convert to interval: Choose between['1min', '5min', '15min', '30min', '60min'] output_format: Choose between['json', 'csv', 'pandas'] Returns: pandas, csv, or json ''' if output_format: self._foreignexchange.output_format = output_format data, _ = self._foreignexchange.get_currency_exchange_intraday( from_symbol=from_symbol, to_symbol=to_symbol, interval=interval, outputsize=outputsize) return data def exchange_rate(self, from_currency, to_currency): ''' Returns the exchange rate of two currencies, digital or physical. CURRENCY_EXCHANGE_RATE endpoint of the Alpha Vantage API Params: from_currency: The symbol to convert to_currency: The symbol to convert to Returns: json ''' params = {'function': "CURRENCY_EXCHANGE_RATE", 'from_currency': from_currency, 'to_currency': to_currency} data = self.get(params) return data def historic_cryptocurrency_quotes(self, symbol, market, cadence='daily', output_format=None): ''' Returns the one of the DIGITAL_CURRENCY_* endpoints of the Alpha Vantage API. Params: symbol: The cryptocurrency to return market: The market it's being sold on cadence: Choose between ['daily', 'weekly', 'monthly'], to return the cadence output_format: Choose between['json', 'csv', 'pandas'] Returns: pandas, csv, or json ''' if output_format: self._cryptocurrencies.output_format = output_format if cadence == 'daily': data, _ = self._cryptocurrencies.get_digital_currency_daily( symbol=symbol, market=market) if cadence == 'weekly': data, _ = self._cryptocurrencies.get_digital_currency_weekly( symbol=symbol, market=market) if cadence == 'monthly': data, _ = self._cryptocurrencies.get_digital_currency_monthly( symbol=symbol, market=market) return data def techindicators(self, techindicator='SMA', output_format='json', **kwargs): ''' Returns the one of the technical indicator endpoints of the Alpha Vantage API. Params: techindicator: The technical indicator of choice params: Each technical indicator has additional optional parameters Returns: pandas, csv, or json ''' if output_format: self._techindicators.output_format = output_format params = {'function': techindicator} for key, value in kwargs.items(): params[key] = value data = self.get(params) return data def sector(self): ''' Returns the sector performances Returns: pandas, csv, or json ''' data, _ = self._sectorperformance.get_sector() return data