Skip to content
Browse files

gallery-2010.11.17-21-32 albertosantini gallery-yql-finance

  • Loading branch information...
1 parent 9eb1a93 commit bf4abbfb72288253ec329965eafacfb0e5574d28 YUI Builder committed Nov 17, 2010
View
5 src/gallery-yql-finance/build.properties
@@ -0,0 +1,5 @@
+builddir=../../../builder/componentbuild
+component=gallery-yql-finance
+component.jsfiles=yql-finance.js
+component.requires=substitute,yql
+
View
8 src/gallery-yql-finance/build.xml
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project name="YQL Finance" default="local">
+ <description>YQL Finance Build File</description>
+ <property file="build.properties" />
+ <import file="${builddir}/3.x/bootstrap.xml"
+ description="Default Build Properties and Targets" />
+</project>
+
View
137 src/gallery-yql-finance/js/yql-finance.js
@@ -0,0 +1,137 @@
+
+ var YQLFinance = function (symbols, callback, params) {
+ if (!params) {
+ params = {};
+ }
+
+ this._params = params;
+ this._callback = callback;
+ };
+
+ YQLFinance.prototype = {
+
+ _callback: null,
+ _params: null,
+
+ makeArray: function (symbols, columns, rows) {
+ var i, j, k, count, nSymbols, nCols, prices = {};
+
+ count = rows.length;
+ nSymbols = symbols.length;
+ nCols = columns.length;
+
+ for (i = 0; i < count; i += nSymbols) {
+ for (j = 0; j < nSymbols; j += 1) {
+ prices[symbols[j]] = prices[symbols[j]] || {};
+ for (k = 0; k < nCols; k += 1) {
+ prices[symbols[j]][columns[k]] =
+ prices[symbols[j]][columns[k]] || [];
+ prices[symbols[j]][columns[k]]
+ .push(rows[i + j][columns[k]]);
+ }
+ }
+ }
+
+ return prices;
+ },
+
+ getQuotes: function (symbols, callback, params) {
+ var sql = 'select {COLUMNS} from yahoo.finance.quotes ' +
+ 'where symbol in ({SYMBOLS})',
+ defaultColumns = ["Change"],
+ query, symbolList = "", i, n;
+
+ params = params || {};
+ params.columns = params.columns || defaultColumns;
+
+ n = symbols.length;
+ for (i = 0; i < n; i += 1) {
+ symbolList += '"' + symbols[i] + '"';
+ if (i !== (n - 1)) {
+ symbolList += ',';
+ }
+ }
+ query = Y.substitute(sql, {
+ COLUMNS: params.columns.join(),
+ SYMBOLS: symbolList
+ });
+
+ Y.log('yql query: ' + query);
+ Y.YQL(query, function (data) {
+ var prices = null;
+
+ if (!data.hasOwnProperty("error") &&
+ data.query.hasOwnProperty("results") &&
+ data.query.results) {
+ prices = Y.YQL.Finance.makeArray(symbols,
+ params.columns, data.query.results.quote);
+ }
+ callback(prices);
+ });
+ },
+
+ getHistoricalQuotes: function (symbols, callback, params) {
+ var histUrl = '"http://ichart.finance.yahoo.com/table.csv?' +
+ 's={SYMBOL}' +
+ '&g={FREQUENCY}' + '"',
+ sql = 'select {COLUMNS} from csv where url in ' +
+ '({HIST_URLS})' +
+ ' and columns="Date,Open,High,Low,Close,Volume,AdjClose"' +
+ ' and Date >= "{START_DATE}" and Date <= "{END_DATE}"' +
+ ' | sort(field="{SORT_COLS}", descending="{SORT_DESC}")',
+ defaultColumns = ["Date", "Open", "High", "Low", "Close",
+ "Volume", "AdjClose"],
+ query, histUrls = "", i, n,
+ now, ddNow, mmNow, yyNow, startDate, endDate;
+
+ now = new Date();
+ ddNow = now.getDate();
+ mmNow = now.getMonth();
+ yyNow = now.getFullYear();
+ startDate = yyNow + "-" + (mmNow + 1) + "-" + "01";
+ endDate = yyNow + "-" + (mmNow + 1) + "-" + ddNow;
+
+ params = params || {};
+ params.columns = params.columns || defaultColumns;
+ params.frequency = params.frequency || "d";
+ params.startDate = params.startDate || startDate;
+ params.endDate = params.endDate || endDate;
+ params.sortCols = params.sortCols || "Date";
+ params.sortDesc = params.sortDesc || "false";
+
+ n = symbols.length;
+ for (i = 0; i < n; i += 1) {
+ histUrls += Y.substitute(histUrl, {
+ SYMBOL: encodeURIComponent(symbols[i]),
+ FREQUENCY: params.frequency
+ });
+ if (i !== (n - 1)) {
+ histUrls += ',';
+ }
+ }
+ query = Y.substitute(sql, {
+ HIST_URLS: histUrls,
+ COLUMNS: params.columns.join(),
+ START_DATE: params.startDate,
+ END_DATE: params.endDate,
+ SORT_COLS: params.sortCols,
+ SORT_DESC: params.sortDesc
+ });
+
+ Y.log('yql query: ' + query);
+ Y.YQL(query, function (data) {
+ var prices = null;
+
+ if (!data.hasOwnProperty("error") &&
+ data.query.hasOwnProperty("results") &&
+ data.query.results) {
+ prices = Y.YQL.Finance.makeArray(symbols,
+ params.columns, data.query.results.row);
+ }
+ callback(prices);
+ });
+ }
+ };
+
+ Y.YQL.Finance = new YQLFinance();
+
View
78 src/gallery-yql-finance/tests/yql-finance.html
@@ -0,0 +1,78 @@
+<!DOCTYPE html>
+
+<html lang="en">
+<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+ <title>Test gallery-yql-finance</title>
+
+<style>
+ .highlight { background-color: yellow; }
+</style>
+
+<script src="http://yui.yahooapis.com/3.2.0/build/yui/yui-min.js"></script>
+</head>
+
+<body>
+
+ <h1>Test gallery-yql-finance</h1>
+
+ <div id="quotes"></div>
+ <br />
+ <div id="histQuotes"></div>
+ <br />
+ <div id="histQuotes2"></div>
+
+<script type="text/javascript">
+/*global YUI */
+
+"use strict";
+
+YUI({
+ modules: {
+ "gallery-yql-finance": {
+ fullpath: "../../../build/gallery-yql-finance/gallery-yql-finance.js",
+ requires: ["substitute", "yql"]
+ }
+ }
+}).use("node", "json", "gallery-yql-finance", function (Y) {
+ var symbols = ["IBM", "MSFT", "YHOO", "^GSPC", "EUR=X"];
+
+ function display(container, prices) {
+ var asset, content = "", c = Y.one(container);
+
+ for (asset in prices) {
+ if (prices.hasOwnProperty(asset)) {
+ content += asset + " -> ";
+ content += Y.JSON.stringify(prices[asset]);
+ content += "<br />";
+ }
+ }
+ c.setContent(content);
+ }
+
+ Y.YQL.Finance.getHistoricalQuotes(symbols, function (prices) {
+ display("#histQuotes", prices);
+ }, {
+ columns: ["Close", "Date"]
+ });
+
+ Y.YQL.Finance.getHistoricalQuotes(symbols, function (prices) {
+ display("#histQuotes2", prices);
+ }, {
+ columns: ["Close", "Date"],
+ frequency: "w",
+ startDate: "2010-10-01",
+ endDate: "2010-10-30"
+ });
+
+ Y.YQL.Finance.getQuotes(symbols, function (prices) {
+ display("#quotes", prices);
+ }, {
+ columns: ["Change", "LastTradeDate"]
+ });
+});
+</script>
+
+</body>
+</html>
+

0 comments on commit bf4abbf

Please sign in to comment.
Something went wrong with that request. Please try again.