python-pour-finance/05-Sources-Données/.ipynb_checkpoints/1-Pandas-Datareader-checkpo...

503 lines
15 KiB
Plaintext
Raw Permalink Normal View History

2023-08-21 15:12:19 +00:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Pandas Datareader\n",
"\n",
"** NOTE: Not every geographical location works well with pandas datareader, your firewall may also block it!**\n",
"\n",
"Functions from pandas_datareader.data and pandas_datareader.wb extract data from various Internet sources into a pandas DataFrame. Currently the following sources are supported:\n",
"\n",
"* Yahoo! Finance\n",
"* Google Finance\n",
"* Enigma\n",
"* St.Louis FED (FRED)\n",
"* Kenneth Frenchs data library\n",
"* World Bank\n",
"* OECD\n",
"* Eurostat\n",
"* Thrift Savings Plan\n",
"* Oanda currency historical rate\n",
"* Nasdaq Trader symbol definitions (remote_data.nasdaq_symbols)\n",
"\n",
"It should be noted, that various sources support different kinds of data, so not all sources implement the same methods and the data elements returned might also differ."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import pandas_datareader.data as web\n",
"\n",
"import datetime\n",
"\n",
"start = datetime.datetime(2015, 1, 1)\n",
"\n",
"end = datetime.datetime(2017, 1, 1)\n",
"\n",
"facebook = web.DataReader(\"FB\", 'google', start, end)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style>\n",
" .dataframe thead tr:only-child th {\n",
" text-align: right;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: left;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Open</th>\n",
" <th>High</th>\n",
" <th>Low</th>\n",
" <th>Close</th>\n",
" <th>Volume</th>\n",
" </tr>\n",
" <tr>\n",
" <th>Date</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2015-01-02</th>\n",
" <td>78.58</td>\n",
" <td>78.93</td>\n",
" <td>77.70</td>\n",
" <td>78.45</td>\n",
" <td>18177475</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2015-01-05</th>\n",
" <td>77.98</td>\n",
" <td>79.25</td>\n",
" <td>76.86</td>\n",
" <td>77.19</td>\n",
" <td>26452191</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2015-01-06</th>\n",
" <td>77.23</td>\n",
" <td>77.59</td>\n",
" <td>75.36</td>\n",
" <td>76.15</td>\n",
" <td>27399288</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2015-01-07</th>\n",
" <td>76.76</td>\n",
" <td>77.36</td>\n",
" <td>75.82</td>\n",
" <td>76.15</td>\n",
" <td>22045333</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2015-01-08</th>\n",
" <td>76.74</td>\n",
" <td>78.23</td>\n",
" <td>76.08</td>\n",
" <td>78.18</td>\n",
" <td>23960953</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Open High Low Close Volume\n",
"Date \n",
"2015-01-02 78.58 78.93 77.70 78.45 18177475\n",
"2015-01-05 77.98 79.25 76.86 77.19 26452191\n",
"2015-01-06 77.23 77.59 75.36 76.15 27399288\n",
"2015-01-07 76.76 77.36 75.82 76.15 22045333\n",
"2015-01-08 76.74 78.23 76.08 78.18 23960953"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"facebook.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Experimental Options\n",
"\n",
"The Options class allows the download of options data from Google Finance.\n",
"\n",
"The get_options_data method downloads options data for specified expiry date and provides a formatted DataFrame with a hierarchical index, so its easy to get to the specific option you want.\n",
"\n",
"Available expiry dates can be accessed from the expiry_dates property."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from pandas_datareader.data import Options\n",
"\n",
"fb_options = Options('FB', 'google')"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"data = fb_options.get_options_data(expiry=fb_options.expiry_dates[0])"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style>\n",
" .dataframe thead tr:only-child th {\n",
" text-align: right;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: left;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th>Last</th>\n",
" <th>Bid</th>\n",
" <th>Ask</th>\n",
" <th>Chg</th>\n",
" <th>PctChg</th>\n",
" <th>Vol</th>\n",
" <th>Open_Int</th>\n",
" <th>Root</th>\n",
" <th>Underlying_Price</th>\n",
" <th>Quote_Time</th>\n",
" </tr>\n",
" <tr>\n",
" <th>Strike</th>\n",
" <th>Expiry</th>\n",
" <th>Type</th>\n",
" <th>Symbol</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">5.0</th>\n",
" <th rowspan=\"2\" valign=\"top\">2018-01-19</th>\n",
" <th>call</th>\n",
" <th>FB180119C00005000</th>\n",
" <td>149.10</td>\n",
" <td>150.00</td>\n",
" <td>150.65</td>\n",
" <td>0.3</td>\n",
" <td>0.20</td>\n",
" <td>50.0</td>\n",
" <td>9528.0</td>\n",
" <td>FB</td>\n",
" <td>155.27</td>\n",
" <td>2017-07-11 23:35:24.466303</td>\n",
" </tr>\n",
" <tr>\n",
" <th>put</th>\n",
" <th>FB180119P00005000</th>\n",
" <td>0.01</td>\n",
" <td>NaN</td>\n",
" <td>0.03</td>\n",
" <td>0.0</td>\n",
" <td>0.00</td>\n",
" <td>NaN</td>\n",
" <td>3053.0</td>\n",
" <td>FB</td>\n",
" <td>155.27</td>\n",
" <td>2017-07-11 23:35:24.466303</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">10.0</th>\n",
" <th rowspan=\"2\" valign=\"top\">2018-01-19</th>\n",
" <th>call</th>\n",
" <th>FB180119C00010000</th>\n",
" <td>143.20</td>\n",
" <td>145.05</td>\n",
" <td>145.75</td>\n",
" <td>-0.5</td>\n",
" <td>-0.35</td>\n",
" <td>40.0</td>\n",
" <td>2855.0</td>\n",
" <td>FB</td>\n",
" <td>155.27</td>\n",
" <td>2017-07-11 23:35:24.466303</td>\n",
" </tr>\n",
" <tr>\n",
" <th>put</th>\n",
" <th>FB180119P00010000</th>\n",
" <td>0.01</td>\n",
" <td>NaN</td>\n",
" <td>0.02</td>\n",
" <td>0.0</td>\n",
" <td>0.00</td>\n",
" <td>NaN</td>\n",
" <td>2056.0</td>\n",
" <td>FB</td>\n",
" <td>155.27</td>\n",
" <td>2017-07-11 23:35:24.466303</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15.0</th>\n",
" <th>2018-01-19</th>\n",
" <th>call</th>\n",
" <th>FB180119C00015000</th>\n",
" <td>135.80</td>\n",
" <td>140.10</td>\n",
" <td>140.80</td>\n",
" <td>0.0</td>\n",
" <td>0.00</td>\n",
" <td>NaN</td>\n",
" <td>240.0</td>\n",
" <td>FB</td>\n",
" <td>155.27</td>\n",
" <td>2017-07-11 23:35:24.466303</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Last Bid Ask Chg PctChg \\\n",
"Strike Expiry Type Symbol \n",
"5.0 2018-01-19 call FB180119C00005000 149.10 150.00 150.65 0.3 0.20 \n",
" put FB180119P00005000 0.01 NaN 0.03 0.0 0.00 \n",
"10.0 2018-01-19 call FB180119C00010000 143.20 145.05 145.75 -0.5 -0.35 \n",
" put FB180119P00010000 0.01 NaN 0.02 0.0 0.00 \n",
"15.0 2018-01-19 call FB180119C00015000 135.80 140.10 140.80 0.0 0.00 \n",
"\n",
" Vol Open_Int Root \\\n",
"Strike Expiry Type Symbol \n",
"5.0 2018-01-19 call FB180119C00005000 50.0 9528.0 FB \n",
" put FB180119P00005000 NaN 3053.0 FB \n",
"10.0 2018-01-19 call FB180119C00010000 40.0 2855.0 FB \n",
" put FB180119P00010000 NaN 2056.0 FB \n",
"15.0 2018-01-19 call FB180119C00015000 NaN 240.0 FB \n",
"\n",
" Underlying_Price \\\n",
"Strike Expiry Type Symbol \n",
"5.0 2018-01-19 call FB180119C00005000 155.27 \n",
" put FB180119P00005000 155.27 \n",
"10.0 2018-01-19 call FB180119C00010000 155.27 \n",
" put FB180119P00010000 155.27 \n",
"15.0 2018-01-19 call FB180119C00015000 155.27 \n",
"\n",
" Quote_Time \n",
"Strike Expiry Type Symbol \n",
"5.0 2018-01-19 call FB180119C00005000 2017-07-11 23:35:24.466303 \n",
" put FB180119P00005000 2017-07-11 23:35:24.466303 \n",
"10.0 2018-01-19 call FB180119C00010000 2017-07-11 23:35:24.466303 \n",
" put FB180119P00010000 2017-07-11 23:35:24.466303 \n",
"15.0 2018-01-19 call FB180119C00015000 2017-07-11 23:35:24.466303 "
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# FRED"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import pandas_datareader.data as web\n",
"\n",
"import datetime\n",
"\n",
"start = datetime.datetime(2010, 1, 1)\n",
"\n",
"end = datetime.datetime(2017, 1, 1)\n",
"\n",
"gdp = web.DataReader(\"GDP\", \"fred\", start, end)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style>\n",
" .dataframe thead tr:only-child th {\n",
" text-align: right;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: left;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>GDP</th>\n",
" </tr>\n",
" <tr>\n",
" <th>DATE</th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2010-01-01</th>\n",
" <td>14681.1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2010-04-01</th>\n",
" <td>14888.6</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2010-07-01</th>\n",
" <td>15057.7</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2010-10-01</th>\n",
" <td>15230.2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2011-01-01</th>\n",
" <td>15238.4</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" GDP\n",
"DATE \n",
"2010-01-01 14681.1\n",
"2010-04-01 14888.6\n",
"2010-07-01 15057.7\n",
"2010-10-01 15230.2\n",
"2011-01-01 15238.4"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gdp.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}