Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for news and sentiments #358

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 52 additions & 0 deletions alpha_vantage/alphaintelligence.py
@@ -0,0 +1,52 @@
from .alphavantage import AlphaVantage as av


class AlphaIntelligence(av):


"""This class implements all the api calls to alpha intelligence
"""
@av._output_format
@av._call_api_on_func
def get_news_sentiment(self,tickers, topics=None, time_from=None, time_to=None,sort=None,limit=None):
"""live and historical market news & sentiment data from a large & growing selection of premier news
outlets around the world, covering stocks, cryptocurrencies, forex, and a wide range of topics such
as fiscal policy, mergers & acquisitions, IPOs, etc
Visit alpha vantage documentation for details on keyboard arguments.
Keyword Arguments:
tickers: stock/crypto/forex symbols of your choice
topics: news topics of your choice
time_from: time as UTC (ETC + 5 = UTC) as YYYYMMDDTHHMM
time_to: time as UTC (ETC + 5 = UTC) as YYYYMMDDTHHMM
sort: sort results based on EARLIEST, LATEST, RELEVANCE; default=Latest
limit: number of matching results, default=50
Returns: Pandas dataframe

"""
_FUNCTION_KEY = "NEWS_SENTIMENT"
return _FUNCTION_KEY, 'feed', None

@av._output_format
@av._call_api_on_func
def get_top_gainers(self):
"""returns the top 20 gainers in the US market as pandas dataframe
"""
_FUNCTION_KEY = "TOP_GAINERS_LOSERS"
return _FUNCTION_KEY, 'top_gainers', None

@av._output_format
@av._call_api_on_func
def get_top_losers(self):
"""returns the top 20 losers in the US market as pandas dataframe
"""
_FUNCTION_KEY = "TOP_GAINERS_LOSERS"
return _FUNCTION_KEY, 'top_losers', None


@av._output_format
@av._call_api_on_func
def get_most_actively_traded(self):
"""returns the top 20 most active traded tickers in the US market as pandas dataframe
"""
_FUNCTION_KEY = "TOP_GAINERS_LOSERS"
return _FUNCTION_KEY, 'most_actively_traded', None
26 changes: 15 additions & 11 deletions alpha_vantage/alphavantage.py
Expand Up @@ -26,7 +26,7 @@ class AlphaVantage(object):
_RAPIDAPI_URL = "https://alpha-vantage.p.rapidapi.com/query?"

def __init__(self, key=None, output_format='json',
treat_info_as_error=True, indexing_type='date', proxy=None, rapidapi=False):
treat_info_as_error=True, index=True, indexing_type='date', proxy=None, rapidapi=False):
""" Initialize the class

Keyword Arguments:
Expand All @@ -35,6 +35,7 @@ def __init__(self, key=None, output_format='json',
server not able to answer the call.
treat_info_as_error: Treat information from the api as errors
output_format: Either 'json', 'pandas' os 'csv'
index: Yes or No; Giving option to override indexing if not interested
indexing_type: Either 'date' to use the default date string given
by the alpha vantage api call or 'integer' if you just want an
integer indexing on your dataframe. Only valid, when the
Expand Down Expand Up @@ -70,6 +71,7 @@ def __init__(self, key=None, output_format='json',
# Not all the calls accept a data type appended at the end, this
# variable will be overridden by those functions not needing it.
self._append_type = True
self.index = index
self.indexing_type = indexing_type
self.proxy = proxy or {}

Expand Down Expand Up @@ -282,17 +284,19 @@ def _format_wrapper(self, *args, **kwargs):
dtype='object')
return data_pandas, meta_data

if 'integer' in self.indexing_type:
# Set Date as an actual column so a new numerical index
# will be created, but only when specified by the user.
data_pandas.reset_index(level=0, inplace=True)
data_pandas.index.name = 'index'
if self.index:
if 'integer' in self.indexing_type:
# Set Date as an actual column so a new numerical index
# will be created, but only when specified by the user.
data_pandas.reset_index(level=0, inplace=True)
data_pandas.index.name = 'index'
else:
data_pandas.index.name = 'date'
# convert to pandas._libs.tslibs.timestamps.Timestamp
data_pandas.index = pandas.to_datetime(data_pandas.index)
return data_pandas, meta_data
else:
data_pandas.index.name = 'date'
# convert to pandas._libs.tslibs.timestamps.Timestamp
data_pandas.index = pandas.to_datetime(
data_pandas.index)
return data_pandas, meta_data
return data_pandas, meta_data
elif 'csv' in self.output_format.lower():
return call_response, None
else:
Expand Down
56 changes: 56 additions & 0 deletions test_alpha_vantage/test_integration_alphavantage.py
Expand Up @@ -232,3 +232,59 @@ def test_get_digital_currency_weekly(self):
output_format='pandas',
symbol='BTC',
market='CNY')



def test_news_sentiment(self):

## Ouput will be a pandas dataframe
# # Test dictionary as output
# ns = AlphaIntelligence(key=TestAlphaVantage._API_KEY_TEST)
# self._assert_result_is_format(ns.get_news_sentiment,
# tickers=TestAlphaVantage._API_EQ_NAME_TEST)
# Test panda as output
ns = AlphaIntelligence(key=TestAlphaVantage._API_KEY_TEST,
output_format='pandas')
self._assert_result_is_format(ns.get_news_sentiment, output_format='pandas',
tickers=TestAlphaVantage._API_EQ_NAME_TEST)



def test_top_gainers(self):

## Ouput will be a pandas dataframe
# Test dictionary as output
# ns = AlphaIntelligence(key=TestAlphaVantage._API_KEY_TEST)
# self._assert_result_is_format(ns.get_top_gainers)

# Test panda as output
ns = AlphaIntelligence(key=TestAlphaVantage._API_KEY_TEST,
output_format='pandas')
self._assert_result_is_format(ns.get_top_gainers, output_format='pandas')


def test_top_losers(self):

## Ouput will be a pandas dataframe
# Test dictionary as output
# ns = AlphaIntelligence(key=TestAlphaVantage._API_KEY_TEST)
# self._assert_result_is_format(ns.get_top_gainers)

# Test panda as output
ns = AlphaIntelligence(key=TestAlphaVantage._API_KEY_TEST,
output_format='pandas')
self._assert_result_is_format(ns.get_top_losers, output_format='pandas')


def test_top_active(self):

## Ouput will be a pandas dataframe
# Test dictionary as output
# ns = AlphaIntelligence(key=TestAlphaVantage._API_KEY_TEST)
# self._assert_result_is_format(ns.get_top_gainers)

# Test panda as output
ns = AlphaIntelligence(key=TestAlphaVantage._API_KEY_TEST,
output_format='pandas')
self._assert_result_is_format(ns.get_most_actively_traded, output_format='pandas')