Skip to content

Commit

Permalink
Merge pull request #315 from RomelTorres/revert-297-develop
Browse files Browse the repository at this point in the history
Revert "Refactoring of internal APIs and adding LISTING_STATUS function"
  • Loading branch information
AlphaVantageSupport committed Jun 14, 2021
2 parents 76120da + 5ddab31 commit db37ed6
Show file tree
Hide file tree
Showing 15 changed files with 862 additions and 824 deletions.
16 changes: 5 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ You may also get a key from [rapidAPI](https://rapidapi.com/alphavantage/api/alp
ts = TimeSeries(key='YOUR_API_KEY',rapidapi=True)
```

Internally there is a retries counter, that can be used to minimize connection errors (in case that the API is not able to respond in time), the default is set to
5 but can be increased or decreased whenever needed.
```python
ts = TimeSeries(key='YOUR_API_KEY',retries='YOUR_RETRIES')
```
The library supports giving its results as json dictionaries (default), pandas dataframe (if installed) or csv, simply pass the parameter output_format='pandas' to change the format of the output for all the API calls in the given class. Please note that some API calls do not support the csv format (namely ```ForeignExchange, SectorPerformances and TechIndicators```) because the API endpoint does not support the format on their calls either.

```python
Expand Down Expand Up @@ -99,17 +104,6 @@ plt.show()
Giving us as output:
![alt text](images/docs_ts_msft_example.png?raw=True "MSFT minute value plot example")

### Fundamental data
It is also possible to query fundamental data.

```python
from alpha_vantage.fundamentaldata import FundamentalData

fd = FundamentalData(key='YOUR_API_KEY', output_format='pandas')
data, meta_data = fd.get_listing_status()
print(data)
```

### Technical indicators
The same way we can get pandas to plot technical indicators like Bollinger Bands®

Expand Down
617 changes: 276 additions & 341 deletions alpha_vantage/alphavantage.py

Large diffs are not rendered by default.

350 changes: 255 additions & 95 deletions alpha_vantage/async_support/alphavantage.py

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion alpha_vantage/async_support/fundamentaldata.py

This file was deleted.

32 changes: 16 additions & 16 deletions alpha_vantage/cryptocurrencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@


class CryptoCurrencies(av):
"""This class implements all the crypto currencies API calls."""

@av.output_format()
@av.call_api_on_func()
"""This class implements all the crypto currencies api calls
"""
@av._output_format
@av._call_api_on_func
def get_digital_currency_daily(self, symbol, market):
""" Returns the daily historical time series for a digital currency
(e.g., BTC) traded on a specific market (e.g., CNY/Chinese Yuan),
Expand All @@ -21,9 +21,9 @@ def get_digital_currency_daily(self, symbol, market):
"""
_FUNCTION_KEY = 'DIGITAL_CURRENCY_DAILY'
return _FUNCTION_KEY, 'Time Series (Digital Currency Daily)', 'Meta Data'
@av.output_format()
@av.call_api_on_func()

@av._output_format
@av._call_api_on_func
def get_digital_currency_weekly(self, symbol, market):
""" Returns the weekly historical time series for a digital currency
(e.g., BTC) traded on a specific market (e.g., CNY/Chinese Yuan),
Expand All @@ -39,9 +39,9 @@ def get_digital_currency_weekly(self, symbol, market):
"""
_FUNCTION_KEY = 'DIGITAL_CURRENCY_WEEKLY'
return _FUNCTION_KEY, 'Time Series (Digital Currency Weekly)', 'Meta Data'
@av.output_format()
@av.call_api_on_func()

@av._output_format
@av._call_api_on_func
def get_digital_currency_monthly(self, symbol, market):
""" Returns the monthly historical time series for a digital currency
(e.g., BTC) traded on a specific market (e.g., CNY/Chinese Yuan),
Expand All @@ -57,9 +57,9 @@ def get_digital_currency_monthly(self, symbol, market):
"""
_FUNCTION_KEY = 'DIGITAL_CURRENCY_MONTHLY'
return _FUNCTION_KEY, 'Time Series (Digital Currency Monthly)', 'Meta Data'
@av.output_format()
@av.call_api_on_func()

@av._output_format
@av._call_api_on_func
def get_digital_currency_exchange_rate(self, from_currency, to_currency):
""" Returns the realtime exchange rate for any pair of digital
currency (e.g., BTC) or physical currency (e.g., USD).
Expand All @@ -73,9 +73,9 @@ def get_digital_currency_exchange_rate(self, from_currency, to_currency):
"""
_FUNCTION_KEY = 'CURRENCY_EXCHANGE_RATE'
return _FUNCTION_KEY, 'Realtime Currency Exchange Rate', None
@av.output_format()
@av.call_api_on_func()

@av._output_format
@av._call_api_on_func
def get_digital_crypto_rating(self, symbol):
""" Returns the Fundamental Crypto Asset Score for a digital currency
(e.g., BTC), and when it was last updated.
Expand Down
23 changes: 13 additions & 10 deletions alpha_vantage/foreignexchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def __init__(self, *args, **kwargs):
raise ValueError("Output format {} is not compatible with the ForeignExchange class".format(
self.output_format.lower()))

@av.output_format()
@av.call_api_on_func()
@av._output_format
@av._call_api_on_func
def get_currency_exchange_rate(self, from_currency, to_currency):
""" Returns the realtime exchange rate for any pair of physical
currency (e.g., EUR) or physical currency (e.g., USD).
Expand All @@ -32,8 +32,8 @@ def get_currency_exchange_rate(self, from_currency, to_currency):
_FUNCTION_KEY = 'CURRENCY_EXCHANGE_RATE'
return _FUNCTION_KEY, 'Realtime Currency Exchange Rate', None

@av.output_format()
@av.call_api_on_func()
@av._output_format
@av._call_api_on_func
def get_currency_exchange_intraday(self, from_symbol, to_symbol, interval='15min', outputsize='compact'):
""" Returns the intraday exchange rate for any pair of physical
currency (e.g., EUR) or physical currency (e.g., USD).
Expand All @@ -55,8 +55,8 @@ def get_currency_exchange_intraday(self, from_symbol, to_symbol, interval='15min
_FUNCTION_KEY = 'FX_INTRADAY'
return _FUNCTION_KEY, "Time Series FX ({})".format(interval), 'Meta Data'

@av.output_format()
@av.call_api_on_func()
@av._output_format
@av._call_api_on_func
def get_currency_exchange_daily(self, from_symbol, to_symbol, outputsize='compact'):
""" Returns the daily exchange rate for any pair of physical
currency (e.g., EUR) or physical currency (e.g., USD).
Expand All @@ -75,8 +75,8 @@ def get_currency_exchange_daily(self, from_symbol, to_symbol, outputsize='compac
_FUNCTION_KEY = 'FX_DAILY'
return _FUNCTION_KEY, "Time Series FX (Daily)", 'Meta Data'

@av.output_format()
@av.call_api_on_func()
@av._output_format
@av._call_api_on_func
def get_currency_exchange_weekly(self, from_symbol, to_symbol, outputsize='compact'):
""" Returns the weekly exchange rate for any pair of physical
currency (e.g., EUR) or physical currency (e.g., USD).
Expand All @@ -95,8 +95,8 @@ def get_currency_exchange_weekly(self, from_symbol, to_symbol, outputsize='compa
_FUNCTION_KEY = 'FX_WEEKLY'
return _FUNCTION_KEY, "Time Series FX (Weekly)", 'Meta Data'

@av.output_format()
@av.call_api_on_func()
@av._output_format
@av._call_api_on_func
def get_currency_exchange_monthly(self, from_symbol, to_symbol, outputsize='compact'):
""" Returns the monthly exchange rate for any pair of physical
currency (e.g., EUR) or physical currency (e.g., USD).
Expand All @@ -107,6 +107,9 @@ def get_currency_exchange_monthly(self, from_symbol, to_symbol, outputsize='comp
For example: from_symbol=EUR or from_symbol=USD.
to_symbol: The destination currency for the exchange rate.
For example: to_symbol=USD or to_symbol=JPY.
interval: time interval between two conscutive values,
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 monthly times
Expand Down
91 changes: 43 additions & 48 deletions alpha_vantage/fundamentaldata.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,27 @@
from datetime import datetime

from .alphavantage import AlphaVantage as av

from datetime import datetime
search_date = datetime.now().date().strftime('%Y-%m-%d')


class FundamentalData(av):
"""This class implements all the api calls to fundamental data"""


"""This class implements all the api calls to fundamental data
"""
def __init__(self, *args, **kwargs):
"""Inherit AlphaVantage base class with its default arguments."""
"""
Inherit AlphaVantage base class with its default arguments.
"""
super(FundamentalData, self).__init__(*args, **kwargs)
self._append_type = False
if self.output_format.lower() == 'csv':
raise ValueError(
"Output format {} is not compatible with the FundamentalData class.".format(
self.output_format.lower()))

@av.output_format()
@av.call_api_on_func('csv')
def get_listing_status(self, date=None, state='active'):
"""
Returns a list of active or delisted US stocks and ETFs, either as of the latest trading
day or at a specific time in history. The endpoint is positioned to facilitate equity
research on asset lifecycle and survivorship.
"""

_FUNCTION_KEY = 'LISTING_STATUS'
return _FUNCTION_KEY, None, None

@av.output_format()
@av.call_api_on_func()
raise ValueError("Output format {} is not compatible with the FundamentalData class".format(
self.output_format.lower()))

@av._output_format
@av._call_api_on_func
def get_company_overview(self, symbol):
"""Returns the company information, financial ratios,
"""
Returns the company information, financial ratios,
and other key metrics for the equity specified.
Data is generally refreshed on the same day a company reports its latest
earnings and financials.
Expand All @@ -43,10 +32,11 @@ def get_company_overview(self, symbol):
_FUNCTION_KEY = 'OVERVIEW'
return _FUNCTION_KEY, None, None

@av.output_format()
@av.call_api_on_func()
@av._output_format
@av._call_api_on_func
def get_income_statement_annual(self, symbol):
"""Returns the annual and quarterly income statements for the company of interest.
"""
Returns the annual and quarterly income statements for the company of interest.
Data is generally refreshed on the same day a company reports its latest
earnings and financials.
Expand All @@ -55,11 +45,12 @@ def get_income_statement_annual(self, symbol):
"""
_FUNCTION_KEY = 'INCOME_STATEMENT'
return _FUNCTION_KEY, 'annualReports', 'symbol'
@av.output_format()
@av.call_api_on_func()

@av._output_format
@av._call_api_on_func
def get_income_statement_quarterly(self, symbol):
"""Returns the annual and quarterly income statements for the company of interest.
"""
Returns the annual and quarterly income statements for the company of interest.
Data is generally refreshed on the same day a company reports its latest
earnings and financials.
Expand All @@ -68,11 +59,12 @@ def get_income_statement_quarterly(self, symbol):
"""
_FUNCTION_KEY = 'INCOME_STATEMENT'
return _FUNCTION_KEY, 'quarterlyReports', 'symbol'
@av.output_format()
@av.call_api_on_func()

@av._output_format
@av._call_api_on_func
def get_balance_sheet_annual(self, symbol):
"""Returns the annual and quarterly balance sheets for the company of interest.
"""
Returns the annual and quarterly balance sheets for the company of interest.
Data is generally refreshed on the same day a company reports its latest
earnings and financials.
Expand All @@ -81,11 +73,12 @@ def get_balance_sheet_annual(self, symbol):
"""
_FUNCTION_KEY = 'BALANCE_SHEET'
return _FUNCTION_KEY, 'annualReports', 'symbol'
@av.output_format()
@av.call_api_on_func()

@av._output_format
@av._call_api_on_func
def get_balance_sheet_quarterly(self, symbol):
"""Returns the annual and quarterly balance sheets for the company of interest.
"""
Returns the annual and quarterly balance sheets for the company of interest.
Data is generally refreshed on the same day a company reports its latest
earnings and financials.
Expand All @@ -94,11 +87,12 @@ def get_balance_sheet_quarterly(self, symbol):
"""
_FUNCTION_KEY = 'BALANCE_SHEET'
return _FUNCTION_KEY, 'quarterlyReports', 'symbol'
@av.output_format()
@av.call_api_on_func()

@av._output_format
@av._call_api_on_func
def get_cash_flow_annual(self, symbol):
"""Returns the annual and quarterly cash flows for the company of interest.
"""
Returns the annual and quarterly cash flows for the company of interest.
Data is generally refreshed on the same day a company reports its latest
earnings and financials.
Expand All @@ -107,11 +101,12 @@ def get_cash_flow_annual(self, symbol):
"""
_FUNCTION_KEY = 'CASH_FLOW'
return _FUNCTION_KEY, 'annualReports', 'symbol'
@av.output_format()
@av.call_api_on_func()

@av._output_format
@av._call_api_on_func
def get_cash_flow_quarterly(self, symbol):
"""Returns the annual and quarterly cash flows for the company of interest.
"""
Returns the annual and quarterly cash flows for the company of interest.
Data is generally refreshed on the same day a company reports its latest
earnings and financials.
Expand Down
21 changes: 14 additions & 7 deletions alpha_vantage/sectorperformance.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,27 @@
class SectorPerformances(av):
"""This class implements all the sector performance api calls
"""

def __init__(self, *args, **kwargs):
"""
Inherit AlphaVantage base class with its default arguments
"""
super(SectorPerformances, self).__init__(*args, **kwargs)
self._append_type = False
if self.output_format.lower() == 'csv':
raise ValueError(
"Output format {} is not compatible with the SectorPerformances class".format(
self.output_format.lower()))

@av.output_format(formatting='sector')
@av.call_api_on_func()
raise ValueError("Output format {} is not comatible with the SectorPerformances class".format(
self.output_format.lower()))

def percentage_to_float(self, val):
""" Transform a string of the form f.f% into f.f/100
Keyword Arguments:
val: The string to convert
"""
return float(val.strip('%')) / 100

@av._output_format_sector
@av._call_api_on_func
def get_sector(self):
"""This API returns the realtime and historical sector performances
calculated from S&P500 incumbents.
Expand Down

0 comments on commit db37ed6

Please sign in to comment.