Knowee
Questions
Features
Study Tools

use python to: Write a code to download monthly data of the following tickers for the period from December 2011 to December 2022: ['AAPL','MSFT','GOOG','AMZN','TSLA','BRK-A','NVDA','UNH','JNJ','V','WMT','JPM','PG','XOM','MA','CVX','HD','BAC','PFE','LLY','KO','COST','DIS','AVGO','PEP','TMO','CSCO','CMCSA','ORCL','ABT']. Process monthly returns for the stocks (in a DataFrame), the market (a Series, using ^GSPC), and the risk-free rate (a Series, using ^IRX). Display the three variables.

Question

use python to: Write a code to download monthly data of the following tickers for the period from December 2011 to December 2022: ['AAPL','MSFT','GOOG','AMZN','TSLA','BRK-A','NVDA','UNH','JNJ','V','WMT','JPM','PG','XOM','MA','CVX','HD','BAC','PFE','LLY','KO','COST','DIS','AVGO','PEP','TMO','CSCO','CMCSA','ORCL','ABT']. Process monthly returns for the stocks (in a DataFrame), the market (a Series, using ^GSPC), and the risk-free rate (a Series, using ^IRX). Display the three variables.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a step-by-step guide on how to do this using Python and the pandas_datareader library.

First, you need to install the pandas_datareader library if you haven't done so already. You can do this by running the following command in your terminal:

pip install pandas_datareader

Next, you can use the following code to download the monthly data for the specified tickers:

import pandas_datareader as pdr
import pandas as pd

# Define the tickers and the date range
tickers = ['AAPL','MSFT','GOOG','AMZN','TSLA','BRK-A','NVDA','UNH','JNJ','V','WMT','JPM','PG','XOM','MA','CVX','HD','BAC','PFE','LLY','KO','COST','DIS','AVGO','PEP','TMO','CSCO','CMCSA','ORCL','ABT']
start_date = '2011-12-01'
end_date = '2022-12-31'

# Download the data
data = pdr.get_data_yahoo(tickers, start=start_date, end=end_date, interval = "m")

# Keep only the adjusted close prices
data = data['Adj Close']

Then, you can calculate the monthly returns for the stocks:

# Calculate the monthly returns
returns = data.pct_change()

Next, you can download the data for the market and the risk-free rate and calculate their monthly returns:

# Download the data for the market and the risk-free rate
market_data = pdr.get_data_yahoo('^GSPC', start=start_date, end=end_date, interval = "m")
rf_data = pdr.get_data_yahoo('^IRX', start=start_date, end=end_date, interval = "m")

# Calculate the monthly returns
market_returns = market_data['Adj Close'].pct_change()
rf_returns = rf_data['Adj Close'].pct_change()

Finally, you can display the three variables:

print(returns)
print(market_returns)
print(rf_returns)

Please note that the '^IRX' ticker represents the 13 Week Treasury Bill as a proxy for the risk-free rate. The returns calculated here are simple returns, not log returns. Also, please be aware that this code might take a while to run due to the large amount of data being downloaded.

This problem has been solved

Similar Questions

use python to do:Download daily data for Amazon (AMZN), Netflix (NFLX) and Bank of America (BA) from 2/1/2011 to the present day and retain Adj Close only. Name the dataframe xdata.

Download the Excel spreadsheet from the LMS. It contains historical monthly prices and dividends (paid at the end of the month) for Telstra stock (Ticker: TLS) over a five-year period. For this period, calculate: 1. The return for each month

Create a class Year to read year and find whether is is leap year or not. Create  a class month  which display 12 months . Create a class import which uses above classes and tell the current month and year and also tell whether it is leap or not

Write a function max_city_temp(csv_filename, city) which analyses temperatures recorded in a CSV file, and returns the maximum temperature recorded for the named city.Once again, the first column of the CSV file will be the city name, and the rest of the columns will be months of the year. The first row of the CSV file will provide the column headings. Here is an example file fragment:max_temp_small.csvcity/month,Jan,Feb,Mar,AprMelbourne,41.2,35.5,37.4,29.3Brisbane,31.3,40.2,37.9,29Darwin,34,34,33.2,34.5Here is an example of how max_city_temp() should work:>>> max_city_temp('max_temp_small.csv', 'Brisbane')40.2

Graphical Representation of Statistical DataWhich mode of representation would you choose for the following dataset:The yearly revenue numbers for a company during the period 2010 - 2019. Note: More than one answer may be correct.Frequency PolygonHistogramBar GraphPie Chart

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.