Hi there folks. I need help to define the stock universe I want to apply my trading idea to.

What code would I use to define the universe of stocks to just those within the SP500? When backtesting, would the universe use the stocks that are no longer in the SP500 today, or does the backtest just grab today's SP500 stocks and go back in time?