Overall Statistics
# region imports
from AlgorithmImports import *
# endregion

class TiingoNewsDataAlgorithm(QCAlgorithm):
    _word_scores = {'good': 1, 'great': 1, 'best': 1, 'growth': 1,
                   'bad': -1, 'terrible': -1, 'worst': -1, 'loss': -1}

    def initialize(self) -> None:
        self.set_start_date(2024, 9, 1)
        self.set_end_date(2024, 12, 31)
        self.set_cash(100000)
        
        self.add_universe(self.universe.etf("SPY"))

    def on_data(self, slice: Slice):
        holdings_by_symbol = {}
        for symbol, news in slice.get(TiingoNews).items():
            # Assign a sentiment score to the news article by the word-score map
            title_words = news.description.lower()
            #self.debug(f'{symbol} {title_words=}')

    def on_securities_changed(self, changes: SecurityChanges) -> None:
        for security in changes.added_securities:
            # Requesting Tiingo news data to obtain the updated news articles to calculate the sentiment score
            security.tiingo = self.add_data(TiingoNews, security.symbol).symbol
        for security in changes.removed_securities:
            if hasattr(security, 'tiingo'):
                self.remove_security(security.tiingo)