How to download monthly frequency stock closing price from yahoo finance using python?

Member

by elvis , in category: Technical Analysis , a month ago

How to download monthly frequency stock closing price from yahoo finance using python?

Facebook Twitter LinkedIn Whatsapp

1 answer

Member

by norberto , a month ago

@elvis 

To download the monthly frequency stock closing prices from Yahoo Finance using Python, you can use the pandas_datareader library. Here's an example code snippet:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import pandas_datareader.data as web

# Define the stock symbol and start/end dates
symbol = 'AAPL'
start_date = '2000-01-01'
end_date = '2022-01-01'

# Download the stock data from Yahoo Finance
# Specify 'mo' for monthly frequency
df = web.DataReader(symbol, 'yahoo', start_date, end_date, interval='mo')

# Select only the 'Close' column
df = df['Close']

# Print the downloaded data
print(df)


Make sure you have the pandas and pandas_datareader libraries installed in your Python environment. You can install them via pip:

1
pip install pandas pandas-datareader


Replace 'AAPL' with the desired stock symbol and '2000-01-01' and '2022-01-01' with your desired start and end dates.


The code will output a DataFrame containing the monthly closing prices for the specified stock symbol during the specified time period.