Skip to content

Instantly share code, notes, and snippets.

@florinel-chis
Created September 13, 2023 11:18
Show Gist options
  • Save florinel-chis/c7eccb93bec374cd8b628080db363dee to your computer and use it in GitHub Desktop.
Save florinel-chis/c7eccb93bec374cd8b628080db363dee to your computer and use it in GitHub Desktop.
yfinance get TSLA daily data for the past 3 years
import yfinance as yf
import pandas as pd
from datetime import datetime, timedelta
# Define the ticker symbol
ticker_symbol = "TSLA"
# Calculate the date from 3 years ago
end_date = datetime.today().strftime('%Y-%m-%d')
start_date = (datetime.today() - timedelta(days=3*365)).strftime('%Y-%m-%d')
# Fetch the data
data = yf.download(ticker_symbol, start=start_date, end=end_date)
# The data is now in a pandas DataFrame. You can view it, save it, or manipulate it as needed.
print(data)
# If you want to save the data to a CSV file, you can do so with the following:
data.to_csv("TSLA_3_years_data.csv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment