Trading Evolution

Markets are never wrong - opinions often are

Get latest prices from Yahoo! Finance web service using AngularJS

It is quite easy to get last price, high, low from Yahoo! Finance web service using AngularJS. The web service query looks like this:

//finance.yahoo.com/webservice/v1/symbols/:symbol/quote?format=json&view=detail

All you need to do is to initialize $resource and call 'get' method.

    $scope.getLatestPrices = function () {

        $scope.yahooFinance = $resource('//finance.yahoo.com/webservice/v1/symbols/:symbol/quote?format=json&view=detail',
                                    { callback: 'JSON_CALLBACK' },
                                    { get: { method: 'JSONP', isArray: false } });

        for (var i = 0; i < $scope.stocks.length; i++) {
            var stock = $scope.stocks[i];
            if (!!stock.Symbol) {
                $scope.yahooFinance.get({ symbol: stock.Symbol }, function (data) {

                    for (var i = 0; i < $scope.stocks.length; i++) {
                        var stock = $scope.stocks[i];
                        if (stock.Symbol.toLowerCase() == data.list.resources[0].resource.fields.symbol.toLowerCase()) {
                            stock.Price = Math.round(Number(data.list.resources[0].resource.fields.price) * 100) / 100;
                            break;
                        }
                    }
                });
            }
        }
    }

Working example can be found here: http://www.bowgett.com/Resources/Tools/PortfolioRebalancingTool.aspx

Loading