数组中的历史数据 - yql

时间:2015-08-09 20:24:11

标签: javascript jquery yql

我有这个脚本来获取去年到今天的历史数据。脚本运行良好,有一个股票(在这个例子中是“mo”)。我构建一个数组,因为我想要6个股票,并且循环需要构建6个表。如果我用我的数组名称替换股票名称“mo”,我会收到错误。

var yyyy = new Date().getFullYear();
    var mm = new Date().getMonth() + 1;

    if (mm < 10) {
        mm = "0" + mm;
    }

    var dd = new Date().getDate();
    var endDate = yyyy + "-" + mm + "-" + dd;
    var startDate = (yyyy - 1) + "-" + mm + "-" + dd;

    $.ajax({
        url: "https://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.historicaldata where symbol = 'mo' and startDate = '" + startDate + "' and endDate = '" + endDate + "'&format=json&diagnostics=true&env=store://datatables.org/alltableswithkeys&callback=",
        dataType: 'json',
        success: function (data) {

            var myTable = "";
            myTable += "<br /><table border='1' cellpadding='0' cellspacing='0' width='500' bgcolor='green'>";
            myTable += "<tr>";
            myTable += "<td>Date</td><td>Open</td><td>Low</td><td>High</td><td>Volume</td><td>Close Price</td>";
            myTable += "</tr>";

            $.each(data.query.results.quote, function (index, item) {

                myTable += "<tr><td>" + item.Date + "</td><td>" + item.Open + "</td><td>" + item.Low + "</td><td>" + item.High + "</td><td>" + item.Volume + "</td><td>" + item.Close + "</td></tr>";

            });

            myTable += "</table>";
            $("#quotes").html(myTable);
        },
        error: function () {
            $("#quotes").html('<p>Something has gone terribly wrong.</p>');
        }
    });

1 个答案:

答案 0 :(得分:0)

您不需要构建6个表查询。您可以使用“IN”关键字。如果您需要“YHOO”,“GOOG”和“AAPL”库存的历史数据,请查询示例

select * from yahoo.finance.historicaldata where symbol IN ("YHOO","GOOG","AAPL") and startDate = "2009-09-11" and endDate = "2010-03-10"

在YQL控制台here

中测试