Skip to content

Commit

Permalink
Merge pull request #146 from alpacahq/feature/fix-flake8-issues
Browse files Browse the repository at this point in the history
Fix issues caught by flake8
  • Loading branch information
ttt733 authored Feb 6, 2020
2 parents 86056bf + af593f1 commit 853c73f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 153 deletions.
126 changes: 28 additions & 98 deletions alpaca_trade_api/alpha_vantage/rest.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)

Expand All @@ -27,7 +23,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/#
Expand All @@ -36,13 +32,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:
Expand All @@ -52,16 +52,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:
Expand Down Expand Up @@ -113,92 +124,11 @@ 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.
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.
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
Expand Down
1 change: 1 addition & 0 deletions alpaca_trade_api/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,6 @@ def __getattr__(self, key):
return val
return super().__getattr__(key)


class Watchlist(Entity):
pass
2 changes: 1 addition & 1 deletion alpaca_trade_api/polygon/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
4 changes: 3 additions & 1 deletion alpaca_trade_api/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
55 changes: 2 additions & 53 deletions tests/test_alpha_vantage/test_rest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flake8: noqa
import requests_mock
import requests
from os import path
Expand All @@ -9,7 +10,7 @@
import os


cli = REST(os.getenv("ALPHAVANTAGE_API_KEY"))
cli = REST("ALPHAVANTAGE_API_KEY")


# def endpoint(params=''):
Expand Down Expand Up @@ -271,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
Expand Down

0 comments on commit 853c73f

Please sign in to comment.