Question

In: Computer Science

write a python script to calculate the return of a stock with 10years of historical data/returns

write a python script to calculate the return of a stock with 10years of historical data/returns

Solutions

Expert Solution

#consider the stocks example as netflix data or use a stock of your desire like apple etc,.
#import necessary packages like pandas,numpy and matplotlib for plotting charts

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import pandas_datareader as web

#getting the required data during a certain period of time
netflix = web.get_data_yahoo("NFLX",start = "2010-01-01", end = "2019-01-01")

#see the top 5 rows of the table
print(netflix.head())

#charting the Netflix closing adjusted price
#to observe it we are plotting a graph
netflix['Adj Close'].plot()
plt.xlabel("Date")
plt.ylabel("Adjusted")
plt.title("Netflix Price data")
plt.show() 

#calculate the returns
netflix_daily_returns = netflix['Adj Close'].pct_change()
netflix_monthly_returns = netflix['Adj Close'].resample('M').ffill().pct_change()

#to see the top five rows of the netflix daily returns
print(netflix_daily_returns.head())

print(netflix_monthly_returns.head())
\

fig = plt.figure()
ax1 = fig.add_axes([0.1,0.1,0.8,0.8])
ax1.plot(netflix_daily_returns)
ax1.set_xlabel("Date")
ax1.set_ylabel("Percent")
ax1.set_title("Netflix daily returns data")
plt.show()

fig = plt.figure()
ax1 = fig.add_axes([0.1,0.1,0.8,0.8])
ax1.plot(netflix_monthly_returns)
ax1.set_xlabel("Date")
ax1.set_ylabel("Percent")
ax1.set_title("Netflix monthly returns data")
plt.show()

#calculate the cumulative returns we will use the cumprod() function
netflix_cum_returns = (netflix_daily_returns + 1).cumprod()

#charting the monthly cummulative returns data these are optional to use
fig = plt.figure()
ax1 = fig.add_axes([0.1,0.1,0.8,0.8])
netflix_cum_returns = (netflix_monthly_returns + 1).cumprod()
ax1.plot(netflix_cum_returns)
ax1.set_xlabel("Date")
ax1.set_ylabel("Growth of $1 investment")
ax1.set_title("Netflix Monthly cumulative returns data")
plt.show()


This maybe useful for your guidance and you can try any kind of stocks in the place of netflix stocks.


Related Solutions

write a python script to calculate 401k with compounding interest
write a python script to calculate 401k with compounding interest
Given the following historical returns, calculate the average return and the standard deviation Year Return 1...
Given the following historical returns, calculate the average return and the standard deviation Year Return 1 12% 2 7% 3 10% 4 9%
The school bookstore wants you to write a Python script to calculate the point of sale...
The school bookstore wants you to write a Python script to calculate the point of sale (total cost) of their new 25$ gift cards. They are also running a special, if a customer buys a gift card they can buy all books for 5$ dollars each. The gift card cost is $25.00 plus $5.00 per book. In addition, there is a sales tax which should be applied to the subtotal and it is 8% (multiply the subtotal by 0.08.) Requirements:...
The school bookstore wants you to write a Python script to calculate the point of sale...
The school bookstore wants you to write a Python script to calculate the point of sale (total cost) of their new 25$ gift cards. They are also running a special, if a customer buys a gift card they can buy all books for 5$ dollars each. The gift card cost is $25.00 plus $5.00 per book. In addition, there is a sales tax which should be applied to the subtotal and it is 8% (multiply the subtotal by 0.08.) Requirements:...
The school bookstore wants you to write a Python script to calculate the point of sale...
The school bookstore wants you to write a Python script to calculate the point of sale (total cost) of their new 25$ gift cards. They are also running a special, if a customer buys a gift card they can buy all books for 5$ dollars each. The gift card cost is $25.00 plus $5.00 per book. In addition, there is a sales tax which should be applied to the subtotal and it is 8% (multiply the subtotal by 0.08.) Requirements:...
Write a python script to calculate the average length of the game shortest game , longest...
Write a python script to calculate the average length of the game shortest game , longest game and the overall average length of a snake and ladder game The game length is the number of throws of the die. We are asked to write a python script which calculate that together with the average
Write a python script to calculate the average length of the game, Shortest game, longest game...
Write a python script to calculate the average length of the game, Shortest game, longest game and overall length of the game we need a python script that calculates the average length of a snake and ladder game. ie the number of moves it takes a single player to win the game. Then you run the game several times and will compare the results to come up with the required data(length of the game and number of moves )
1. How to calculate the historical average return and the volatility of stock? 2. How to...
1. How to calculate the historical average return and the volatility of stock? 2. How to calculate the correlation matrix among different stocks? (can you show me with real examples)
return as it is related to risk. What is return, component parts of return, historical returns,...
return as it is related to risk. What is return, component parts of return, historical returns, and the relationship of returns, and considerations in evaluating return?
REALIZED RATES OF RETURN Stocks A and B have the following historical returns: Year Stock A's...
REALIZED RATES OF RETURN Stocks A and B have the following historical returns: Year Stock A's Returns, rA Stock B's Returns, rB 2011 - 15.90% - 16.50% 2012 33.50 21.20 2013 11.25 31.60 2014 - 6.00 - 8.80 2015 34.00 29.35 Calculate the average rate of return for stock A during the period 2011 through 2015. Round your answer to two decimal places. % Calculate the average rate of return for stock B during the period 2011 through 2015. Round...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT