How to generate a CSV with python that contains stock price of AAPL each minute for the…
- User
How to generate a CSV with python that contains stock price of AAPL each minute for the last two years?
- Assistant
To generate a CSV file with minute-by-minute stock prices of AAPL for the last two years using Python, you can use the
yfinancelibrary to fetch the data andpandasto handle and export it. First, you'll need to install the required libraries if you haven't already:bashpip install yfinance pandasNow, you can use the following script to fetch and save the data:
pythonimport yfinance as yf import pandas as pd # Define the ticker symbol and the time period ticker = "AAPL" start_date = (pd.to_datetime("today") - pd.DateOffset(years=2)).strftime("%Y-%m-%d") end_date = pd.to_datetime("today").strftime("%Y-%m-%d") # Fetch the historical data data = yf.download(ticker, start=start_date, end=end_date, interval="1m") # Save the data to a CSV file data.to_csv(f"{ticker}_1m_last_2_years.csv") print(f"Data saved to {ticker}_1m_last_2_years.csv")
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.