-
Return and Variance 7
-
Lecture1.1
-
Lecture1.2
-
Lecture1.3
-
Lecture1.4
-
Lecture1.5
-
Lecture1.6
-
Lecture1.7
-
-
Solving Equations 5
-
Lecture2.1
-
Lecture2.2
-
Lecture2.3
-
Lecture2.4
-
Lecture2.5
-
-
Capital Allocation Line 6
-
Lecture3.1
-
Lecture3.2
-
Lecture3.3
-
Lecture3.4
-
Lecture3.5
-
Lecture3.6
-
-
Diversification 3
-
Lecture4.1
-
Lecture4.2
-
Lecture4.3
-
-
Investment Sets 3
-
Lecture5.1
-
Lecture5.2
-
Lecture5.3
-
-
Portfolios 7
-
Lecture6.1
-
Lecture6.2
-
Lecture6.3
-
Lecture6.4
-
Lecture6.5
-
Lecture6.6
-
Lecture6.7
-
-
Capital and Security Market Lines 3
-
Lecture7.1
-
Lecture7.2
-
Lecture7.3
-
-
Arbitrage 3
-
Lecture8.1
-
Lecture8.2
-
Lecture8.3
-
-
Dividend Discount Model 2
-
Lecture9.1
-
Lecture9.2
-
-
Fixed Income 4
-
Lecture10.1
-
Lecture10.2
-
Lecture10.3
-
Lecture10.4
-
-
Duration and Immunization 4
-
Lecture11.1
-
Lecture11.2
-
Lecture11.3
-
Lecture11.4
-
Getting Real Stock Data
Solution 1
sum([x.expectedReturn * y for x,y in zip(stocks,investments)])/sum(investments)
Solution 2
weights = [x/sum(investments) for x in investments]
print(sum([x.expectedReturn * y for x,y in zip(stocks,weights)]))
Time to crack into real stock data. You’ll need to install pandas-datareader if you have not already.
!pip install pandas_datareader
You may also need to update your version if you have it downloaded but have not done so recently.
! python3 -m pip install pandas_datareader --upgrade
The way we get stock data is by calling the function pdr.get_yahoo_data(stock, startDate, endDate). The stock is what stock we want data on and the other two are the dates we want data between. Let’s get Ford stock data.
import pandas_datareader as pdr
import datetime
start = datetime.date(2010,1,1)
end = datetime.date(2017,5,1)
stock = pdr.get_data_yahoo('F',start,end)
stock
What gets printed out is a pandas dataframe. Pandas is a library which allows for manipulation of large datasets. The first column is the index column. The index column is used as the identifier for each row. The other columns are the stock data. Open is what the stock was in the beginning of the day, close is the end value, high and low are the highest and lowest values, and finally volume is how much volume of the stock was traded that day.
We can get a single column of a dataframe by indexing with the column name. To get only the close column we would do this:
print(stock["Adj Close"])