Last active
July 28, 2016 06:08
-
-
Save txthai/0f8505a7a0f95be132db07b0b4616982 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"import pandas_datareader.data as web\n", | |
"import datetime\n", | |
"\n", | |
"def set_variables():\n", | |
" ticker_symbols = ['^GSPC', '^DJI', '^IXIC']\n", | |
" start_date = '2000-01-01'\n", | |
" end_date = '2016-06-30'\n", | |
" data_source = 'yahoo'\n", | |
" directory = './'\n", | |
" file_extension = '.csv'\n", | |
" return ticker_symbols, start_date, end_date, data_source, directory, file_extension\n", | |
"\n", | |
"def pull_data():\n", | |
" ticker_symbols, start_date, end_date, data_source, directory, file_extension = set_variables()\n", | |
" start = string_to_date_time(start_date)\n", | |
" end = string_to_date_time(end_date)\n", | |
" for ticker_symbol in ticker_symbols:\n", | |
" df = web.DataReader(ticker_symbol, data_source, start, end)\n", | |
" save_data(df, directory, ticker_symbol, start_date, end_date, file_extension)\n", | |
"\n", | |
"def string_to_date_time(s):\n", | |
" sy = int(s[:4])\n", | |
" sm = int(s[5:7])\n", | |
" sd = int(s[8:])\n", | |
" return datetime.datetime(sy, sm, sd)\n", | |
"\n", | |
"def save_data(df, directory, ticker_symbol, start_date, end_date, file_extension):\n", | |
" sdt = strip_hyphens(start_date)\n", | |
" edt = strip_hyphens(end_date)\n", | |
" filename = directory + ticker_symbol + '_' + sdt + '-' + edt + file_extension\n", | |
" df.to_csv(filename, index_label=\"Date\")\n", | |
"\n", | |
"def strip_hyphens(s):\n", | |
" return s.replace(\"-\", \"\")\n", | |
"\n", | |
"if __name__ == \"__main__\":\n", | |
" pull_data()" | |
] | |
}, | |
{ | |
"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.5.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment