The FinalProject file utilizes the yfinance library to download hourly stock data for five selected symbols—MCD(McDonalds), AAPL(Apple), SPY(S&P 500 ETF), MSFT(Microsoft), and AMZN(Amazon)—spanning the past month. It fetches data for each stock, stores it in a dictionary, and combines the data into a single DataFrame, where the stock symbols serve as column keys. The DataFrame’s columns are flattened for easier readability, and the first few rows of the combined data are printed to provide a quick overview of the stock performance.
In addition to fetching stock data, the code also processes a financial dataset to calculate total income and expenses from a DataFrame. It filters the data based on the 'Type' column, summing the values for income and expenses separately. The code also uses the unique() function to identify distinct components in the dataset and groups the data by these components to sum their corresponding values. Finally, the performance of an income source, 'Source 2', is simulated by investing it in the S&P 500 ETF, with the growth calculated and displayed based on SPY’s return over the past year. This data is visualized with bar charts to help compare the outcomes. Also provides a comprehensive overview of the SPY ETF's performance over the past year. It retrieves the historical data, showing the price one year ago and the current price. The percentage return is calculated to reflect the ETF's growth or decline during that period. Finally, the code demonstrates how an initial investment of $10,000 would have grown based on the SPY return, offering a concrete example of the investment's real-world impact. This gives users a clear understanding of SPY's recent performance and its potential for investment growth.
Initially attempted to access historical stock data for the S&P 500 (SPY) through the Polygon API. However, due to your subscription plan not covering the required data timeframe, was unable to access it. As a result, switched to using the yfinance library, which provides a straightforward way to download historical stock data. Using yfinance successfully retrieved the past year’s data for SPY and calculate key metrics like percentage returns. This approach allowed to continue the analysis without needing to upgrade your Polygon subscription, effectively leveraging the free capabilities of yfinance for stock data retrieval.
The Previous_DayOHLC file retrieves the previous day's stock data for Apple (AAPL) and SPY (S&P 500 ETF) by making an HTTP request to a URL fetched from a function get_stock_url(), which enable us to access our API KEY AND API URL from a different .py file. If valid data is found, it extracts and prints the volume, open, close, high, and low prices for AAPL from the previous trading day. If the request fails or no data is found, it prints an error message or indicates the absence of results.
So, one could store their api key and api url in a different file and run both file FinalProject and Previous_DayOHLC