Overall Statistics
# region imports
from AlgorithmImports import *
# endregion

class UpcomingIPOsExampleAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self.set_start_date(2020, 1, 1)
        self.set_end_date(2024, 10, 1)
        self.set_cash(100000)

        # Use SPY as proxy to trade the market popularity.
        self.spy = self.add_equity("SPY", Resolution.DAILY).symbol
        # Request IPO data to obtain the number of IPOs to generate trade signals.
        self.add_data(EODHDUpcomingIPOs, "ipos")
        
        # Use a SMA indicator to estimate the trend of the number of IPO events.
        self.sma_ipo = SimpleMovingAverage(60)
        self.sma_ipo.window.size = 2

        self._day = -1
        self.set_warm_up(60)

    def on_data(self, slice: Slice) -> None:
        # Only daily update.
        if self._day == slice.time.day:
            return

        # Update the SMA indicator with the updated number of IPO event upcoming within 7 days.
        ipos = slice.get(EODHDUpcomingIPOs)
        if not ipos:
            self.sma_ipo.update(slice.time, 0)
        else:
            self.sma_ipo.update(slice.time, len(ipos))

        self._day = slice.time.day

        # Compare the last 2 SMA indicator value to estimate if the trend is going up or down.
        if self.sma_ipo.current.value > self.sma_ipo.window[-1].current.value:
            # If the current SMA is higher than the previous day, we consider the market is in increasing popularity and estimate the market will go up.
            self.set_holdings(self.spy, 1)
        else:
            # Otherwise, we estiamte the market will go down due to lower popularity.
            self.set_holdings(self.spy, -1)