nicknochnack / MLTradingBot

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

rkaushik29 opened this issue · comments

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?

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}')

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

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

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?

@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

Thanks a lot! It works!