Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

final code #28

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,878 changes: 2,878 additions & 0 deletions .ipynb_checkpoints/Efficient_Frontier_rch-checkpoint.ipynb

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions .ipynb_checkpoints/dashboard-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 2
}
3,000 changes: 3,000 additions & 0 deletions .ipynb_checkpoints/dashboard_rch-checkpoint.ipynb

Large diffs are not rendered by default.

229 changes: 229 additions & 0 deletions .ipynb_checkpoints/efficieny_frontier_rch-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"ename": "ModuleNotFoundError",
"evalue": "No module named 'quandl'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-2-076b88a2f767>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# import needed modules\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0mquandl\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mpandas\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mpd\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mmatplotlib\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpyplot\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mplt\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'quandl'"
]
}
],
"source": [
"# import needed modules\n",
"import quandl\n",
"import pandas as pd\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'quandl' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-3-2aa8bb83b75a>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# get adjusted closing prices of 5 selected companies with Quandl\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mquandl\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mApiConfig\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mapi_key\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'1tETqu7ZxhRNAZ2sWNVS'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[0mselected\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;34m'CNP'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'F'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'WMT'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'GE'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'TSLA'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m data = quandl.get_table('WIKI/PRICES', ticker = selected,\n\u001b[0;32m 5\u001b[0m \u001b[0mqopts\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m \u001b[1;34m'columns'\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;34m'date'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'ticker'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'adj_close'\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m}\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mNameError\u001b[0m: name 'quandl' is not defined"
]
}
],
"source": [
"# get adjusted closing prices of 5 selected companies with Quandl\n",
"quandl.ApiConfig.api_key = '1tETqu7ZxhRNAZ2sWNVS'\n",
"selected = ['CNP', 'F', 'WMT', 'GE', 'TSLA']\n",
"data = quandl.get_table('WIKI/PRICES', ticker = selected,\n",
" qopts = { 'columns': ['date', 'ticker', 'adj_close'] },\n",
" date = { 'gte': '2014-1-1', 'lte': '2016-12-31' }, paginate=True)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'data' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-4-3853dc989bbf>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# reorganise data pulled by setting date as index with\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;31m# columns of tickers and their corresponding adjusted prices\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mclean\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdata\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mset_index\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'date'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 4\u001b[0m \u001b[0mtable\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mclean\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpivot\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'ticker'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mNameError\u001b[0m: name 'data' is not defined"
]
}
],
"source": [
"# reorganise data pulled by setting date as index with\n",
"# columns of tickers and their corresponding adjusted prices\n",
"clean = data.set_index('date')\n",
"table = clean.pivot(columns='ticker')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# calculate daily and annual returns of the stocks\n",
"returns_daily = table.pct_change()\n",
"returns_annual = returns_daily.mean() * 250"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# get daily and covariance of returns of the stock\n",
"cov_daily = returns_daily.cov()\n",
"cov_annual = cov_daily * 250"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# empty lists to store returns, volatility and weights of imiginary portfolios\n",
"port_returns = []\n",
"port_volatility = []\n",
"stock_weights = []"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# set the number of combinations for imaginary portfolios\n",
"num_assets = len(selected)\n",
"num_portfolios = 50000"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# populate the empty lists with each portfolios returns,risk and weights\n",
"for single_portfolio in range(num_portfolios):\n",
" weights = np.random.random(num_assets)\n",
" weights /= np.sum(weights)\n",
" returns = np.dot(weights, returns_annual)\n",
" volatility = np.sqrt(np.dot(weights.T, np.dot(cov_annual, weights)))\n",
" port_returns.append(returns)\n",
" port_volatility.append(volatility)\n",
" stock_weights.append(weights)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# a dictionary for Returns and Risk values of each portfolio\n",
"portfolio = {'Returns': port_returns,\n",
" 'Volatility': port_volatility}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# extend original dictionary to accomodate each ticker and weight in the portfolio\n",
"for counter,symbol in enumerate(selected):\n",
" portfolio[symbol+' Weight'] = [Weight[counter] for Weight in stock_weights]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# make a nice dataframe of the extended dictionary\n",
"df = pd.DataFrame(portfolio)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# get better labels for desired arrangement of columns\n",
"column_order = ['Returns', 'Volatility'] + [stock+' Weight' for stock in selected]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# reorder dataframe columns\n",
"df = df[column_order]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# plot the efficient frontier with a scatter plot\n",
"plt.style.use('seaborn')\n",
"df.plot.scatter(x='Volatility', y='Returns', figsize=(10, 8), grid=True)\n",
"plt.xlabel('Volatility (Std. Deviation)')\n",
"plt.ylabel('Expected Returns')\n",
"plt.title('Efficient Frontier')\n",
"plt.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading