Stock Price

Example on how to get stock price information from yahoo.finance

by Nivaldo

JavaScript

function getData(symbol,shares) {
    var url = "http://query.yahooapis.com/v1/public/yql";
    var data = encodeURIComponent("select * from yahoo.finance.quotes where symbol in ('" + symbol + "')");

    $.getJSON(url, 'q=' + data + "&format=json&diagnostics=true&env=http://datatables.org/alltables.env")
        .done(function (data) {
            console.log(symbol,data.query.results.quote.LastTradePriceOnly*shares);
        return data.query.results.quote.LastTradePriceOnly*shares;
    })
        .fail(function (jqxhr, textStatus, error) {
        var err = textStatus + ", " + error;
            console.log(err);
    });
}

var PGGAX = getData("PGGAX",707.395);
var DWGAX = getData("DWGAX",385.935);
var GAIOX = getData("GAIOX",417.709);
var SMCWX = getData("SMCWX",87.209);

// does not work because the data is asynchronous; need to wrap inside a promise...use q?
console.log(PGGAX + DWGAX + GAIOX + SMCWX);