nicknochnack / MLTradingBot

708 stars 280 forks source link

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

Closed herzphi closed 5 months ago

herzphi commented 5 months ago

Using your instructions from the ReadMe leads to a attribution error: AttributeError: 'REST' object has no attribute 'get_news'. The full traceback:

(venv) MLTradingBot % python tradingbot.py Starting backtest for MLTrader... INFO:backtest_stats:Starting backtest... Progress |--------------------------------------------------------------------------------------------------------| 0.10% [Elapsed: 0:00:02 ETA: 0:38:27] 2024-01-20 14:56:56,805: root: ERROR: 'REST' object has no attribute 'get_news' 2024-01-20 14:56:56,809: root: ERROR: Traceback (most recent call last): File "/Users/herzphi/Library/Mobile Documents/com~apple~CloudDocs/Programming/MLTradingBot/venv/lib/python3.11/site-packages/lumibot/strategies/strategy_executor.py", line 831, in run self._run_trading_session() File "/Users/herzphi/Library/Mobile Documents/com~apple~CloudDocs/Programming/MLTradingBot/venv/lib/python3.11/site-packages/lumibot/strategies/strategy_executor.py", line 782, in _run_trading_session self._on_trading_iteration() File "/Users/herzphi/Library/Mobile Documents/com~apple~CloudDocs/Programming/MLTradingBot/venv/lib/python3.11/site-packages/lumibot/strategies/strategy_executor.py", line 274, in func_output result = func_input(self, *args, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/herzphi/Library/Mobile Documents/com~apple~CloudDocs/Programming/MLTradingBot/venv/lib/python3.11/site-packages/lumibot/strategies/strategy_executor.py", line 298, in func_output result = func_input(self, *args, *kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/herzphi/Library/Mobile Documents/com~apple~CloudDocs/Programming/MLTradingBot/venv/lib/python3.11/site-packages/lumibot/strategies/strategy_executor.py", line 87, in func_output return func_input(self, args, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/herzphi/Library/Mobile Documents/com~apple~CloudDocs/Programming/MLTradingBot/venv/lib/python3.11/site-packages/lumibot/strategies/strategy_executor.py", line 378, in _on_trading_iteration on_trading_iteration() File "/Users/herzphi/Library/Mobile Documents/com~apple~CloudDocs/Programming/MLTradingBot/venv/lib/python3.11/site-packages/lumibot/tools/decorators.py", line 62, in func_output frame, result = call_function_get_frame(func_input, *args, *kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/herzphi/Library/Mobile Documents/com~apple~CloudDocs/Programming/MLTradingBot/venv/lib/python3.11/site-packages/lumibot/tools/decorators.py", line 30, in call_function_get_frame result = func(args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/Users/herzphi/Library/Mobile Documents/com~apple~CloudDocs/Programming/MLTradingBot/tradingbot.py", line 50, in on_trading_iteration probability, sentiment = self.get_sentiment() ^^^^^^^^^^^^^^^^^^^^ File "/Users/herzphi/Library/Mobile Documents/com~apple~CloudDocs/Programming/MLTradingBot/tradingbot.py", line 41, in get_sentiment news = self.api.get_news(symbol=self.symbol, ^^^^^^^^^^^^^^^^^ AttributeError: 'REST' object has no attribute 'get_news'

Exception in thread MLTrader: Traceback (most recent call last): File "/Users/herzphi/Library/Mobile Documents/com~apple~CloudDocs/Programming/MLTradingBot/venv/lib/python3.11/site-packages/lumibot/strategies/strategy_executor.py", line 831, in run self._run_trading_session() File "/Users/herzphi/Library/Mobile Documents/com~apple~CloudDocs/Programming/MLTradingBot/venv/lib/python3.11/site-packages/lumibot/strategies/strategy_executor.py", line 782, in _run_trading_session self._on_trading_iteration() File "/Users/herzphi/Library/Mobile Documents/com~apple~CloudDocs/Programming/MLTradingBot/venv/lib/python3.11/site-packages/lumibot/strategies/strategy_executor.py", line 274, in func_output result = func_input(self, *args, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/herzphi/Library/Mobile Documents/com~apple~CloudDocs/Programming/MLTradingBot/venv/lib/python3.11/site-packages/lumibot/strategies/strategy_executor.py", line 298, in func_output result = func_input(self, *args, *kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/herzphi/Library/Mobile Documents/com~apple~CloudDocs/Programming/MLTradingBot/venv/lib/python3.11/site-packages/lumibot/strategies/strategy_executor.py", line 87, in func_output return func_input(self, args, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/herzphi/Library/Mobile Documents/com~apple~CloudDocs/Programming/MLTradingBot/venv/lib/python3.11/site-packages/lumibot/strategies/strategy_executor.py", line 378, in _on_trading_iteration on_trading_iteration() File "/Users/herzphi/Library/Mobile Documents/com~apple~CloudDocs/Programming/MLTradingBot/venv/lib/python3.11/site-packages/lumibot/tools/decorators.py", line 62, in func_output frame, result = call_function_get_frame(func_input, *args, *kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/herzphi/Library/Mobile Documents/com~apple~CloudDocs/Programming/MLTradingBot/venv/lib/python3.11/site-packages/lumibot/tools/decorators.py", line 30, in call_function_get_frame result = func(args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/Users/herzphi/Library/Mobile Documents/com~apple~CloudDocs/Programming/MLTradingBot/tradingbot.py", line 50, in on_trading_iteration probability, sentiment = self.get_sentiment() ^^^^^^^^^^^^^^^^^^^^ File "/Users/herzphi/Library/Mobile Documents/com~apple~CloudDocs/Programming/MLTradingBot/tradingbot.py", line 41, in get_sentiment news = self.api.get_news(symbol=self.symbol, ^^^^^^^^^^^^^^^^^ AttributeError: 'REST' object has no attribute 'get_news'

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/threading.py", line 1045, in _bootstrap_inner self.run() File "/Users/herzphi/Library/Mobile Documents/com~apple~CloudDocs/Programming/MLTradingBot/venv/lib/python3.11/site-packages/lumibot/strategies/strategy_executor.py", line 844, in run raise RuntimeError("Exception encountered, stopping BackTest.") from e RuntimeError: Exception encountered, stopping BackTest.

Creating indicators plot... (venv) herzphi@MacBook-Pro-von-Philipp MLTradingBot %

Tcby04 commented 5 months ago

Here is my non fancy way to fix this.

import requests
from requests.auth import HTTPBasicAuth
def get_sentiment(self): 
        today, three_days_prior = self.get_dates()

        url = "https://data.alpaca.markets/v1beta1/news"
        params = {
            'symbols': self.symbol,
            'start': three_days_prior,
            'end': today
        }
        headers = {
            "APCA-API-KEY-ID": API_KEY,
            "APCA-API-SECRET-KEY": API_SECRET
        }

        response = requests.get(url, params=params, headers=headers)
        if response.status_code == 200:
            news_data = response.json()

            # Extracting news from the 'news' key in the response
            news_items = news_data.get('news', [])
            if news_items:
                news_headlines = [item["headline"] for item in news_items]
                probability, sentiment = estimate_sentiment(news_headlines)
                return probability, sentiment
            else:
                print("No news items found in the response")
                return None, None
        else:
            print(f"Failed to fetch news: {response.status_code}")
            return None, None

herzphi commented 5 months ago

I installed the wrong package. The problem mentioned above is solved by installing the repository: https://github.com/alpacahq/alpaca-trade-api-python.git instead of just installing alpaca-trade-api.

grantreighard commented 5 months ago

Funny, I didn't see this issue so I ended up fixing the project up with the newer Alpaca package and added some QoL things: https://github.com/grantreighard/MLTradingBot