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

AttributeError: 'REST' object has no attribute 'get_news' #4

Open
rkaushik29 opened this issue Jan 30, 2024 · 6 comments
Open

AttributeError: 'REST' object has no attribute 'get_news' #4

rkaushik29 opened this issue Jan 30, 2024 · 6 comments

Comments

@rkaushik29
Copy link

I don't see a get_news method exposed under REST even in the Alpaca Trade API documentation. I'm appalled it worked in this project. Anyone else facing this?

@rkaushik29
Copy link
Author

Not sure if OP is active here: if anyone faces this issue, use this method to get news from Alpaca (include it in the MLTrader class defined in this project:

    def get_alpaca_news(self):
        base_url = 'https://data.alpaca.markets/v1beta1/news'
        params = {
            'symbols': ','.join(self.symbol)
        }

        headers = {
            'Apca-Api-Key-Id': API_KEY,
            'Apca-Api-Secret-Key': API_SECRET
        }

        response = requests.get(base_url, headers=headers, params=params)

        if response.status_code == 200:
            data = response.json()

            # Extract headlines
            headlines = [news_item['headline'] for news_item in data['news']]
            return headlines
        else:
            raise Exception(f'Failed to retrieve data: {response.status_code} - {response.reason}')

@bognar-dev
Copy link

THe problem with is is that it will not return data from the right date but jsut the latest news

@dnesting
Copy link

dnesting commented Jan 31, 2024

I'm doing a variation on this (alpaca-py==0.14.0), which seems to work OK. The API looks to be in flux.

Example:

import datetime

from alpaca.data import requests
from alpaca.data.historical import news

news_client = news.NewsClient(api_key=API_KEY, secret_key=API_SECRET)

end = datetime.datetime.now().date()
start = end - datetime.timedelta(days=3)

req = requests.NewsRequest(
  symbols='AAPL',
  start=str(start),
  end=str(end),
)

while True:
  news_set = news_client.get_news(req)
  for news in news_set.news:
    headline = news.headline
    ...
  else:
    # Pagination
    if news_set.next_page_token:
      req.page_token = news_set.next_page_token
      continue
  break

@S0ft1c
Copy link

S0ft1c commented Jan 31, 2024

Not sure if OP is active here: if anyone faces this issue, use this method to get news from Alpaca (include it in the MLTrader class defined in this project:

    def get_alpaca_news(self):
        base_url = 'https://data.alpaca.markets/v1beta1/news'
        params = {
            'symbols': ','.join(self.symbol)
        }

        headers = {
            'Apca-Api-Key-Id': API_KEY,
            'Apca-Api-Secret-Key': API_SECRET
        }

        response = requests.get(base_url, headers=headers, params=params)

        if response.status_code == 200:
            data = response.json()

            # Extract headlines
            headlines = [news_item['headline'] for news_item in data['news']]
            return headlines
        else:
            raise Exception(f'Failed to retrieve data: {response.status_code} - {response.reason}')

I have the same issue.

Sorry, I didn't understand in what place exactly I need to paste it (I stupidly pasted it in a class and it did nothing)

Can you please explain how to use this function?

@bognar-dev
Copy link

bognar-dev commented Jan 31, 2024

@S0ft1c
I did a similar thing to @dnesting

This is the function for get_alpaca_news() I use in the class as a member function:

    def get_alpaca_news(self,today, three_days_prior):
        newsRequest = NewsRequest(
            symbols=self.symbol,
            start=three_days_prior,
            end=today,
            limit=20
        )
        news = self.newsClient.get_news(newsRequest)
        headlines = []
        summaries = []

        for news_item in news.news:
            headlines.append(news_item.headline)
            summaries.append(news_item.summary)

        return headlines

You also need to change the function call in the get_sentiment() function

def get_sentiment(self):
        today, three_days_prior = self.get_dates()
        news = self.get_alpaca_news(today, three_days_prior)
        probability, sentiment = estimate_sentiment(news)
        return probability, sentiment

And in the initialise function you need to add the news client as a class variable

def initialize(self, symbol: str = "SPY", cash_at_risk: float = .5):

        self.symbol = symbol
        self.sleeptime = "24H"
        self.last_trade = None
        self.cash_at_risk = cash_at_risk
        self.newsClient = NewsClient(ALPACA_CREDS["API_KEY"],ALPACA_CREDS["API_SECRET"])

If you need more help let me know

@S0ft1c
Copy link

S0ft1c commented Jan 31, 2024

Thanks a lot! It works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants