From a842e2b566fff2877b927ae77b9bcc607eef8ba4 Mon Sep 17 00:00:00 2001 From: ttt733 Date: Thu, 6 Feb 2020 00:22:40 -0600 Subject: [PATCH 1/5] Fix issues caught by flake8 --- alpaca_trade_api/alpha_vantage/rest.py | 82 +++++++++++++++++++------- alpaca_trade_api/entity.py | 1 + alpaca_trade_api/polygon/rest.py | 2 +- alpaca_trade_api/rest.py | 4 +- 4 files changed, 66 insertions(+), 23 deletions(-) diff --git a/alpaca_trade_api/alpha_vantage/rest.py b/alpaca_trade_api/alpha_vantage/rest.py index 83f2440b..146ff2bc 100644 --- a/alpaca_trade_api/alpha_vantage/rest.py +++ b/alpaca_trade_api/alpha_vantage/rest.py @@ -27,7 +27,7 @@ def _request(self, method, params=None): return resp.json() def get(self, params=None): - ''' Customizable endpoint, where you can pass all + ''' Customizable endpoint, where you can pass all keywords/paramters from the documentation: https://www.alphavantage.co/documentation/# @@ -36,13 +36,17 @@ def get(self, params=None): ''' 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. + def historic_quotes( + self, symbol, adjusted=False, outputsize='full', + cadence='daily', output_format=None + ): + ''' Returns 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 + cadence: Choose between ['daily', 'weekly', 'monthly'] output_format: Choose between['json', 'csv', 'pandas'] Returns: @@ -52,16 +56,27 @@ def historic_quotes(self, symbol, adjusted=False, outputsize='full', cadence='da 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) + 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) + 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) + 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): + def intraday_quotes( + self, symbol, interval='5min', outputsize='full', output_format=None + ): ''' Returns the TIME_SERIES_INTRADAY endpoint of the Alpha Vantage API. Params: @@ -113,13 +128,16 @@ def search_endpoint(self, keywords, datatype='json'): '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. + def historic_fx_quotes( + self, from_symbol, to_symbol, outputsize='full', + cadence='daily', output_format=None + ): + ''' Returns 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 + cadence: Choose between ['daily', 'weekly', 'monthly'] output_format: Choose between['json', 'csv', 'pandas'] Returns: @@ -129,16 +147,28 @@ def historic_fx_quotes(self, from_symbol, to_symbol, outputsize='full', cadence= 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) + 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) + 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) + from_symbol=from_symbol, + to_symbol=to_symbol, + outputsize=outputsize + ) return data - def intraday_fx_quotes(self, from_symbol, to_symbol, interval='5min', outputsize='full', output_format=None): + 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: @@ -153,7 +183,11 @@ def intraday_fx_quotes(self, from_symbol, to_symbol, interval='5min', outputsize 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) + from_symbol=from_symbol, + to_symbol=to_symbol, + interval=interval, + outputsize=outputsize + ) return data def exchange_rate(self, from_currency, to_currency): @@ -172,13 +206,16 @@ def exchange_rate(self, from_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. + def historic_cryptocurrency_quotes( + self, symbol, market, cadence='daily', output_format=None + ): + ''' Returns 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 + cadence: Choose between ['daily', 'weekly', 'monthly'] output_format: Choose between['json', 'csv', 'pandas'] Returns: @@ -197,8 +234,11 @@ def historic_cryptocurrency_quotes(self, symbol, market, cadence='daily', output 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. + def techindicators( + self, techindicator='SMA', output_format='json', **kwargs + ): + ''' Returns one of the technical indicator endpoints of the + Alpha Vantage API. Params: techindicator: The technical indicator of choice diff --git a/alpaca_trade_api/entity.py b/alpaca_trade_api/entity.py index da06e67a..0daf2fd5 100644 --- a/alpaca_trade_api/entity.py +++ b/alpaca_trade_api/entity.py @@ -152,5 +152,6 @@ def __getattr__(self, key): return val return super().__getattr__(key) + class Watchlist(Entity): pass diff --git a/alpaca_trade_api/polygon/rest.py b/alpaca_trade_api/polygon/rest.py index ca38775b..889b0a19 100644 --- a/alpaca_trade_api/polygon/rest.py +++ b/alpaca_trade_api/polygon/rest.py @@ -151,7 +151,7 @@ def last_quote(self, symbol): raw = self.get(path) # TODO status check return Quote(raw['last']) - + def previous_day_bar(self, symbol): path = '/aggs/ticker/{}/prev'.format(symbol) raw = self.get(path, version='v2') diff --git a/alpaca_trade_api/rest.py b/alpaca_trade_api/rest.py index 17c2f453..5030d295 100644 --- a/alpaca_trade_api/rest.py +++ b/alpaca_trade_api/rest.py @@ -416,7 +416,9 @@ def add_watchlist(self, watchlist_name): return [Watchlist(o) for o in resp] def add_to_watchlist(self, watchlist_id, symbol): - resp = self.post('/watchlists/{}'.format(watchlist_id), data=dict(symbol=symbol)) + resp = self.post( + '/watchlists/{}'.format(watchlist_id), data=dict(symbol=symbol) + ) return Watchlist(resp) def update_watchlist(self, watchlist_id, name=None, symbols=None): From 8f167106e5ea676f1b5d28d5d49f6afb0266174e Mon Sep 17 00:00:00 2001 From: ttt733 Date: Thu, 6 Feb 2020 00:25:40 -0600 Subject: [PATCH 2/5] Remove flake8 validation for Alpha Vantage test --- tests/test_alpha_vantage/test_rest.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_alpha_vantage/test_rest.py b/tests/test_alpha_vantage/test_rest.py index 0cf5f574..63e1433b 100644 --- a/tests/test_alpha_vantage/test_rest.py +++ b/tests/test_alpha_vantage/test_rest.py @@ -1,3 +1,4 @@ +# flake8: noqa import requests_mock import requests from os import path From 18b784ed09a92d2c17249629f351b3e279e07a3d Mon Sep 17 00:00:00 2001 From: ttt733 Date: Thu, 6 Feb 2020 00:27:50 -0600 Subject: [PATCH 3/5] Fix issue with Alpha Vantage test --- tests/test_alpha_vantage/test_rest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_alpha_vantage/test_rest.py b/tests/test_alpha_vantage/test_rest.py index 63e1433b..c1353cf1 100644 --- a/tests/test_alpha_vantage/test_rest.py +++ b/tests/test_alpha_vantage/test_rest.py @@ -10,7 +10,7 @@ import os -cli = REST(os.getenv("ALPHAVANTAGE_API_KEY")) +cli = REST("ALPHAVANTAGE_API_KEY") # def endpoint(params=''): From 26a6bece4210b70c0e6b05b07317de60f0a2479e Mon Sep 17 00:00:00 2001 From: ttt733 Date: Thu, 6 Feb 2020 00:29:24 -0600 Subject: [PATCH 4/5] Remove crypto and FX endpoints for Alpha Vantage --- alpaca_trade_api/alpha_vantage/rest.py | 106 ------------------------- tests/test_alpha_vantage/test_rest.py | 52 ------------ 2 files changed, 158 deletions(-) diff --git a/alpaca_trade_api/alpha_vantage/rest.py b/alpaca_trade_api/alpha_vantage/rest.py index 146ff2bc..2e4af8dc 100644 --- a/alpaca_trade_api/alpha_vantage/rest.py +++ b/alpaca_trade_api/alpha_vantage/rest.py @@ -128,112 +128,6 @@ def search_endpoint(self, keywords, datatype='json'): '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 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'] - 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, - outputsize=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 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'] - 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 ): diff --git a/tests/test_alpha_vantage/test_rest.py b/tests/test_alpha_vantage/test_rest.py index c1353cf1..5c73105a 100644 --- a/tests/test_alpha_vantage/test_rest.py +++ b/tests/test_alpha_vantage/test_rest.py @@ -272,58 +272,6 @@ def test_search_endpoint(self, mock_request): self.assertIsInstance( data, dict, 'Result Data must be a dict') - @requests_mock.Mocker() - def test_historic_fx_quotes(self, mock_request): - """ Test that api call returns a json file as requested - """ - cli = REST(TestAlphaVantage._API_KEY_TEST) - url = "https://www.alphavantage.co/query?function=FX_DAILY&from_symbol=USD&to_symbol=EUR&outputsize=full&apikey=test" - path_file = self.get_file_from_url("mock_foreign_exchange_historical") - with open(path_file) as f: - mock_request.get(url, text=f.read()) - data = cli.historic_fx_quotes('USD', 'EUR') - self.assertIsInstance( - data, dict, 'Result Data must be a dict') - - @requests_mock.Mocker() - def test_intraday_fx_quotes(self, mock_request): - """ Test that api call returns a json file as requested - """ - cli = REST(TestAlphaVantage._API_KEY_TEST) - url = "https://www.alphavantage.co/query?function=FX_INTRADAY&from_symbol=USD&to_symbol=EUR&outputsize=full&apikey=test" - path_file = self.get_file_from_url("mock_intraday_fx") - with open(path_file) as f: - mock_request.get(url, text=f.read()) - data = cli.intraday_fx_quotes('USD', 'EUR') - self.assertIsInstance( - data, dict, 'Result Data must be a dict') - - @requests_mock.Mocker() - def test_exchange_rate(self, mock_request): - """ Test that api call returns a json file as requested - """ - cli = REST(TestAlphaVantage._API_KEY_TEST) - url = "https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency=USD&to_currency=EUR&apikey=test" - path_file = self.get_file_from_url("mock_foreign_exchange") - with open(path_file) as f: - mock_request.get(url, text=f.read()) - data = cli.exchange_rate('USD', 'EUR') - self.assertIsInstance( - data, dict, 'Result Data must be a dict') - - @requests_mock.Mocker() - def test_historic_cryptocurrency_quotes(self, mock_request): - """ Test that api call returns a json file as requested - """ - cli = REST(TestAlphaVantage._API_KEY_TEST) - url = "https://www.alphavantage.co/query?function=DIGITAL_CURRENCY_DAILY&symbol=BTC&market=CNY&apikey=test&datatype=json" - path_file = self.get_file_from_url("mock_crypto_currencies") - with open(path_file) as f: - mock_request.get(url, text=f.read()) - data = cli.historic_cryptocurrency_quotes('BTC', 'CNY') - self.assertIsInstance( - data, dict, 'Result Data must be a dict') - @requests_mock.Mocker() def test_techindicators(self, mock_request): """ Test that api call returns a json file as requested From af593f137cee1d22a62d2cf094c456770a9ed464 Mon Sep 17 00:00:00 2001 From: ttt733 Date: Thu, 6 Feb 2020 00:29:57 -0600 Subject: [PATCH 5/5] Remove crypto and FX connections --- alpaca_trade_api/alpha_vantage/rest.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/alpaca_trade_api/alpha_vantage/rest.py b/alpaca_trade_api/alpha_vantage/rest.py index 2e4af8dc..2c28e9e9 100644 --- a/alpaca_trade_api/alpha_vantage/rest.py +++ b/alpaca_trade_api/alpha_vantage/rest.py @@ -1,7 +1,5 @@ import requests from alpha_vantage.timeseries import TimeSeries -from alpha_vantage.cryptocurrencies import CryptoCurrencies -from alpha_vantage.foreignexchange import ForeignExchange from alpha_vantage.sectorperformance import SectorPerformances from alpha_vantage.techindicators import TechIndicators from alpaca_trade_api.common import get_alpha_vantage_credentials @@ -13,8 +11,6 @@ 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)