JSON formatted stock quote API (live or historical)

做~自己de王妃 提交于 2019-11-29 19:14:20

Sure, if brought back and parsed as JSON with javascript, you would be able to do the following and pull out everything you wanted from each returned stock:

var callback = function(_return /* The json returned for yahooapis */) {
    var totalReturned = _return.query.count;
    //OR: var totalReturned = _return.query.results.quote.length;
    for (var i = 0; i < totalReturned; ++i) {
        var stock = _return.query.results.quote[i];
        var symbol = stock.symbol;
        var percent_change = stock.Change_PercentChange;
        var changeRealTime = stock.ChangeRealtime;
        ...
    }
}

--

var url = 'http://query.yahooapis.com/v1/public/yql';
var startDate = '2012-01-01';
var endDate = '2012-01-08';
var data = encodeURIComponent('select * from yahoo.finance.historicaldata where symbol in ("YHOO","AAPL","GOOG","MSFT") and startDate = "' + startDate + '" and endDate = "' + endDate + '"');
$.getJSON(url, 'q=' + data + "&env=http%3A%2F%2Fdatatables.org%2Falltables.env&format=json", callback);

--

YQL Demo

(Add and startDate = "" and endDate = "" to the query with the dates in the format yyyy-mm-dd to do what you want, also make sure to choose JSON as the output format)

--

Some additional information from the comments:

  • In the example above the query was for historical data from yahoo.finance.historicaldata, you can also query yahoo.finance.quotes for realtime -- lagged about 15 minutes)
  • If you want true real-time information query the webservice: e.g. finance.yahoo.com/webservice/v1/symbols/YHOO/quote?format=json (add &view=detail to that query if you want a more detailed output)

As a software developer, I would recommend Alpha Vantage. They offer realtime and historical stock quotes (daily, weekly, monthly, etc.) as RESTful JSON APIs.

It’s completely free with unlimited API calls. It’s realtime as long as the stock is listed on major stock exchanges.

Here is an example API call for the MSFT daily prices and volumes, enriched with split/dividend adjustments. The latest data point is the realtime information for the current trading day.

They also offer technical analysis APIs on top of the market data according to their documentation.

Documentation: https://iextrading.com/developer/docs/#stocks

GET https://api.iextrading.com/1.0/stock/jnj/quote

{
    "symbol": "JNJ",
    "companyName": "Johnson & Johnson",
    "primaryExchange": "New York Stock Exchange",
    "close": 124.69,
    "closeTime": 1531771224535
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!