Skip to content

Commit 853c73f

Browse files
authored
Merge pull request #146 from alpacahq/feature/fix-flake8-issues
Fix issues caught by flake8
2 parents 86056bf + af593f1 commit 853c73f

File tree

5 files changed

+35
-153
lines changed

5 files changed

+35
-153
lines changed

alpaca_trade_api/alpha_vantage/rest.py

Lines changed: 28 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import requests
22
from alpha_vantage.timeseries import TimeSeries
3-
from alpha_vantage.cryptocurrencies import CryptoCurrencies
4-
from alpha_vantage.foreignexchange import ForeignExchange
53
from alpha_vantage.sectorperformance import SectorPerformances
64
from alpha_vantage.techindicators import TechIndicators
75
from alpaca_trade_api.common import get_alpha_vantage_credentials
@@ -13,8 +11,6 @@ def __init__(self, api_key):
1311
self._api_key = get_alpha_vantage_credentials(api_key)
1412
self._session = requests.Session()
1513
self._timeseries = TimeSeries(key=self._api_key)
16-
self._cryptocurrencies = CryptoCurrencies(key=self._api_key)
17-
self._foreignexchange = ForeignExchange(key=self._api_key)
1814
self._sectorperformance = SectorPerformances(key=self._api_key)
1915
self._techindicators = TechIndicators(key=self._api_key)
2016

@@ -27,7 +23,7 @@ def _request(self, method, params=None):
2723
return resp.json()
2824

2925
def get(self, params=None):
30-
''' Customizable endpoint, where you can pass all
26+
''' Customizable endpoint, where you can pass all
3127
keywords/paramters from the documentation:
3228
https://www.alphavantage.co/documentation/#
3329
@@ -36,13 +32,17 @@ def get(self, params=None):
3632
'''
3733
return self._request('GET', params=params)
3834

39-
def historic_quotes(self, symbol, adjusted=False, outputsize='full', cadence='daily', output_format=None):
40-
''' Returns the one of the TIME_SERIES_* endpoints of the Alpha Vantage API.
35+
def historic_quotes(
36+
self, symbol, adjusted=False, outputsize='full',
37+
cadence='daily', output_format=None
38+
):
39+
''' Returns one of the TIME_SERIES_* endpoints
40+
of the Alpha Vantage API.
4141
4242
Params:
4343
symbol: The ticker to return
4444
adjusted: Return the adjusted prices
45-
cadence: Choose between ['daily', 'weekly', 'monthly'], to return the cadence
45+
cadence: Choose between ['daily', 'weekly', 'monthly']
4646
output_format: Choose between['json', 'csv', 'pandas']
4747
4848
Returns:
@@ -52,16 +52,27 @@ def historic_quotes(self, symbol, adjusted=False, outputsize='full', cadence='da
5252
self._timeseries.output_format = output_format
5353
if cadence == 'daily':
5454
data, _ = self._timeseries.get_daily_adjusted(
55-
symbol=symbol, outputsize=outputsize) if adjusted else self._timeseries.get_daily(symbol=symbol, outputsize=outputsize)
55+
symbol=symbol, outputsize=outputsize
56+
) if adjusted else self._timeseries.get_daily(
57+
symbol=symbol, outputsize=outputsize
58+
)
5659
if cadence == 'weekly':
5760
data, _ = self._timeseries.get_weekly_adjusted(
58-
symbol=symbol) if adjusted else self._timeseries.get_weekly(symbol=symbol)
61+
symbol=symbol
62+
) if adjusted else self._timeseries.get_weekly(
63+
symbol=symbol
64+
)
5965
if cadence == 'monthly':
6066
data, _ = self._timeseries.get_monthly_adjusted(
61-
symbol=symbol) if adjusted else self._timeseries.get_monthly(symbol=symbol)
67+
symbol=symbol
68+
) if adjusted else self._timeseries.get_monthly(
69+
symbol=symbol
70+
)
6271
return data
6372

64-
def intraday_quotes(self, symbol, interval='5min', outputsize='full', output_format=None):
73+
def intraday_quotes(
74+
self, symbol, interval='5min', outputsize='full', output_format=None
75+
):
6576
''' Returns the TIME_SERIES_INTRADAY endpoint of the Alpha Vantage API.
6677
6778
Params:
@@ -113,92 +124,11 @@ def search_endpoint(self, keywords, datatype='json'):
113124
'keywords': keywords, 'datatype': datatype}
114125
return self.get(params)
115126

116-
def historic_fx_quotes(self, from_symbol, to_symbol, outputsize='full', cadence='daily', output_format=None):
117-
''' Returns the one of the FX_* endpoints of the Alpha Vantage API.
118-
119-
Params:
120-
from_currency: The symbol to convert
121-
to_currency: The symbol to convert to
122-
cadence: Choose between ['daily', 'weekly', 'monthly'], to return the cadence
123-
output_format: Choose between['json', 'csv', 'pandas']
124-
125-
Returns:
126-
pandas, csv, or json
127-
'''
128-
if output_format:
129-
self._foreignexchange.output_format = output_format
130-
if cadence == 'daily':
131-
data, _ = self._foreignexchange.get_currency_exchange_daily(
132-
from_symbol=from_symbol, to_symbol=to_symbol, outputsize=outputsize)
133-
if cadence == 'weekly':
134-
data, _ = self._foreignexchange.get_currency_exchange_weekly(
135-
from_symbol=from_symbol, to_symbol=to_symbol, outputsize=outputsize)
136-
if cadence == 'monthly':
137-
data, _ = self._foreignexchange.get_currency_exchange_monthly(
138-
from_symbol=from_symbol, to_symbol=to_symbol, utputsize=outputsize)
139-
return data
140-
141-
def intraday_fx_quotes(self, from_symbol, to_symbol, interval='5min', outputsize='full', output_format=None):
142-
''' Returns the FX_INTRADAY endpoint of the Alpha Vantage API.
143-
144-
Params:
145-
from_currency: The symbol to convert
146-
to_currency: The symbol to convert to
147-
interval: Choose between['1min', '5min', '15min', '30min', '60min']
148-
output_format: Choose between['json', 'csv', 'pandas']
149-
150-
Returns:
151-
pandas, csv, or json
152-
'''
153-
if output_format:
154-
self._foreignexchange.output_format = output_format
155-
data, _ = self._foreignexchange.get_currency_exchange_intraday(
156-
from_symbol=from_symbol, to_symbol=to_symbol, interval=interval, outputsize=outputsize)
157-
return data
158-
159-
def exchange_rate(self, from_currency, to_currency):
160-
''' Returns the exchange rate of two currencies, digital or physical.
161-
CURRENCY_EXCHANGE_RATE endpoint of the Alpha Vantage API
162-
163-
Params:
164-
from_currency: The symbol to convert
165-
to_currency: The symbol to convert to
166-
167-
Returns:
168-
json
169-
'''
170-
params = {'function': "CURRENCY_EXCHANGE_RATE",
171-
'from_currency': from_currency, 'to_currency': to_currency}
172-
data = self.get(params)
173-
return data
174-
175-
def historic_cryptocurrency_quotes(self, symbol, market, cadence='daily', output_format=None):
176-
''' Returns the one of the DIGITAL_CURRENCY_* endpoints of the Alpha Vantage API.
177-
178-
Params:
179-
symbol: The cryptocurrency to return
180-
market: The market it's being sold on
181-
cadence: Choose between ['daily', 'weekly', 'monthly'], to return the cadence
182-
output_format: Choose between['json', 'csv', 'pandas']
183-
184-
Returns:
185-
pandas, csv, or json
186-
'''
187-
if output_format:
188-
self._cryptocurrencies.output_format = output_format
189-
if cadence == 'daily':
190-
data, _ = self._cryptocurrencies.get_digital_currency_daily(
191-
symbol=symbol, market=market)
192-
if cadence == 'weekly':
193-
data, _ = self._cryptocurrencies.get_digital_currency_weekly(
194-
symbol=symbol, market=market)
195-
if cadence == 'monthly':
196-
data, _ = self._cryptocurrencies.get_digital_currency_monthly(
197-
symbol=symbol, market=market)
198-
return data
199-
200-
def techindicators(self, techindicator='SMA', output_format='json', **kwargs):
201-
''' Returns the one of the technical indicator endpoints of the Alpha Vantage API.
127+
def techindicators(
128+
self, techindicator='SMA', output_format='json', **kwargs
129+
):
130+
''' Returns one of the technical indicator endpoints of the
131+
Alpha Vantage API.
202132
203133
Params:
204134
techindicator: The technical indicator of choice

alpaca_trade_api/entity.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,5 +152,6 @@ def __getattr__(self, key):
152152
return val
153153
return super().__getattr__(key)
154154

155+
155156
class Watchlist(Entity):
156157
pass

alpaca_trade_api/polygon/rest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def last_quote(self, symbol):
151151
raw = self.get(path)
152152
# TODO status check
153153
return Quote(raw['last'])
154-
154+
155155
def previous_day_bar(self, symbol):
156156
path = '/aggs/ticker/{}/prev'.format(symbol)
157157
raw = self.get(path, version='v2')

alpaca_trade_api/rest.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,9 @@ def add_watchlist(self, watchlist_name):
416416
return [Watchlist(o) for o in resp]
417417

418418
def add_to_watchlist(self, watchlist_id, symbol):
419-
resp = self.post('/watchlists/{}'.format(watchlist_id), data=dict(symbol=symbol))
419+
resp = self.post(
420+
'/watchlists/{}'.format(watchlist_id), data=dict(symbol=symbol)
421+
)
420422
return Watchlist(resp)
421423

422424
def update_watchlist(self, watchlist_id, name=None, symbols=None):

tests/test_alpha_vantage/test_rest.py

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# flake8: noqa
12
import requests_mock
23
import requests
34
from os import path
@@ -9,7 +10,7 @@
910
import os
1011

1112

12-
cli = REST(os.getenv("ALPHAVANTAGE_API_KEY"))
13+
cli = REST("ALPHAVANTAGE_API_KEY")
1314

1415

1516
# def endpoint(params=''):
@@ -271,58 +272,6 @@ def test_search_endpoint(self, mock_request):
271272
self.assertIsInstance(
272273
data, dict, 'Result Data must be a dict')
273274

274-
@requests_mock.Mocker()
275-
def test_historic_fx_quotes(self, mock_request):
276-
""" Test that api call returns a json file as requested
277-
"""
278-
cli = REST(TestAlphaVantage._API_KEY_TEST)
279-
url = "https://www.alphavantage.co/query?function=FX_DAILY&from_symbol=USD&to_symbol=EUR&outputsize=full&apikey=test"
280-
path_file = self.get_file_from_url("mock_foreign_exchange_historical")
281-
with open(path_file) as f:
282-
mock_request.get(url, text=f.read())
283-
data = cli.historic_fx_quotes('USD', 'EUR')
284-
self.assertIsInstance(
285-
data, dict, 'Result Data must be a dict')
286-
287-
@requests_mock.Mocker()
288-
def test_intraday_fx_quotes(self, mock_request):
289-
""" Test that api call returns a json file as requested
290-
"""
291-
cli = REST(TestAlphaVantage._API_KEY_TEST)
292-
url = "https://www.alphavantage.co/query?function=FX_INTRADAY&from_symbol=USD&to_symbol=EUR&outputsize=full&apikey=test"
293-
path_file = self.get_file_from_url("mock_intraday_fx")
294-
with open(path_file) as f:
295-
mock_request.get(url, text=f.read())
296-
data = cli.intraday_fx_quotes('USD', 'EUR')
297-
self.assertIsInstance(
298-
data, dict, 'Result Data must be a dict')
299-
300-
@requests_mock.Mocker()
301-
def test_exchange_rate(self, mock_request):
302-
""" Test that api call returns a json file as requested
303-
"""
304-
cli = REST(TestAlphaVantage._API_KEY_TEST)
305-
url = "https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency=USD&to_currency=EUR&apikey=test"
306-
path_file = self.get_file_from_url("mock_foreign_exchange")
307-
with open(path_file) as f:
308-
mock_request.get(url, text=f.read())
309-
data = cli.exchange_rate('USD', 'EUR')
310-
self.assertIsInstance(
311-
data, dict, 'Result Data must be a dict')
312-
313-
@requests_mock.Mocker()
314-
def test_historic_cryptocurrency_quotes(self, mock_request):
315-
""" Test that api call returns a json file as requested
316-
"""
317-
cli = REST(TestAlphaVantage._API_KEY_TEST)
318-
url = "https://www.alphavantage.co/query?function=DIGITAL_CURRENCY_DAILY&symbol=BTC&market=CNY&apikey=test&datatype=json"
319-
path_file = self.get_file_from_url("mock_crypto_currencies")
320-
with open(path_file) as f:
321-
mock_request.get(url, text=f.read())
322-
data = cli.historic_cryptocurrency_quotes('BTC', 'CNY')
323-
self.assertIsInstance(
324-
data, dict, 'Result Data must be a dict')
325-
326275
@requests_mock.Mocker()
327276
def test_techindicators(self, mock_request):
328277
""" Test that api call returns a json file as requested

0 commit comments

Comments
 (0)