Created
January 18, 2016 18:57
-
-
Save mavieth/e9c27bc23aae79cb37d9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> | |
| <script> | |
| (function($) { | |
| function getStock(opts, type, complete) { | |
| var defs = { | |
| desc: false, | |
| baseURL: 'http://query.yahooapis.com/v1/public/yql?q=', | |
| query: { | |
| quotes: 'select * from yahoo.finance.quotes where symbol = "{stock}" | sort(field="{sortBy}", descending="{desc}")', | |
| historicaldata: 'select * from yahoo.finance.historicaldata where symbol = "{stock}" and startDate = "{startDate}" and endDate = "{endDate}"' | |
| }, | |
| suffixURL: { | |
| quotes: '&env=store://datatables.org/alltableswithkeys&format=json&callback=?', | |
| historicaldata: '&env=store://datatables.org/alltableswithkeys&format=json&callback=?' | |
| } | |
| }; | |
| opts = opts || {}; | |
| if (!opts.stock) { | |
| complete('No stock defined'); | |
| return; | |
| } | |
| var query = defs.query[type] | |
| .replace('{stock}', opts.stock) | |
| .replace('{sortBy}', defs.sortBy) | |
| .replace('{desc}', defs.desc) | |
| .replace('{startDate}', opts.startDate) | |
| .replace('{endDate}', opts.endDate) | |
| var url = defs.baseURL + query + (defs.suffixURL[type] || ''); | |
| $.getJSON(url, function(data) { | |
| var err = null; | |
| if (!data || !data.query) { | |
| err = true; | |
| } | |
| complete(err, !err && data.query.results); }); | |
| } | |
| window.getStock = getStock; | |
| })(jQuery); | |
| // Usage | |
| getStock({ stock: 'PTRC' }, 'quotes', function(err, data) { | |
| console.log(data); | |
| }); | |
| getStock({ stock: 'PTRC', startDate: '2015-01-01', endDate: '2016-01-17' }, 'historicaldata', function(err, data) { | |
| console.log(data); | |
| }); | |
| var today = new Date(); | |
| var dd = today.getDate(); | |
| var mm = today.getMonth()+1; | |
| var yyyy = today.getFullYear(); | |
| var lastYear = yyyy - 1; | |
| var curMonth = mm; | |
| var curDay = dd; | |
| var curYear = yyyy; | |
| var start = lastYear.toString() + '-' + '01' + '-' + '01'; | |
| var end = curYear.toString() + '-' + curMonth.toString() + '-' + curDay.toString(); | |
| getStock({ stock: 'PTRC', startDate: start, endDate: end }, 'historicaldata', function(err, data) { | |
| console.log(data); | |
| }); | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment