0

I am able to pull and display stock price information from the Google Finance API, but would like to add the stock prices and display a sum of the combined amounts. Given the following code:

HTML

<link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.0-rc2/css/bootstrap.css">
 <script type="text/javascript" src="//code.jquery.com/jquery-1.9.1.js"></script>
  <body>
  <div class="container">
  <h1>Stock Portfolio</h1>
  <h2>Total Portfolio Value:</h2>
  <div class="total"></div>
  <h3>Stocks</h3>
  <div class="stockTick"></div>
  <div class="stockTick2"></div>
</div>

jQuery

$(document).ready(function(){

  $.getJSON('https://finance.google.com/finance/info?client=ig&q=NYSE:FB&callback=?',function(response){
    var stockInfo = response[0];
    var stockString =
   '<div class="stockWrapper">';
      stockString +='Company: <span class="stockSymbol"><b>'+stockInfo.t+'</b></span><br />';
      stockString +='Price: <span class="stockPrice">'+stockInfo.l+'</span><br />';
      stockString +='Change: <span class="stockChange">'+stockInfo.c+'</span><br />';
      stockString +=' Reported at: <span>at</span> <span class="stockTime">'+stockInfo.ltt+'</span><hr>';
    stockString +='</div>';
    $('.stockTick').prepend(stockString);
  });
  $.getJSON('https://finance.google.com/finance/info?client=ig&q=NYSE:USMD&callback=?',function(response){
    var stockInfo = response[0];
    var stockString2 =
   '<div class="stockWrapper">';
      stockString2 +='Company: <span class="stockSymbol"><b>'+stockInfo.t+'</b></span><br />';
      stockString2 +='Price: <span class="stockPrice">'+stockInfo.l+'</span><br />';
      stockString2 +='Change: <span class="stockChange">'+stockInfo.c+'</span><br />';
      stockString2 +=' Reported at: <span>at</span> <span class="stockTime">'+stockInfo.ltt+'</span>';
    stockString2 +='</div>';
    $('.stockTick2').prepend(stockString2);
  });
});

Here it is in a jsfiddle

Matt
  • 1,561
  • 5
  • 26
  • 61

1 Answers1

1

FaceBook (FB) and USMD are not on NYSE, they are on the NASDAQ

Here are the Url's that will return JSON data.

https://finance.google.com/finance/info?client=ig&q=NASDAQ:FB https://finance.google.com/finance/info?client=ig&q=NASDAQ:USMD

To get a total of the stock price you can do something like this in the response function $('.total').text(parseFloat($('#total').text()) + parseFloat(stockInfo.l)); (using your fiddle example)

Richard Hubley
  • 2,180
  • 22
  • 29
  • oddly enough, changing the market that they're on does not affect the display. Also, that doesn't answer the question. I'm trying to figure out how to add the two values from the stocks. So I'm looking for add `.stockTick` + `.stockTick2` – Matt Nov 01 '13 at 19:02
  • I'm working out a way to pull the values from each of the feeds and add them. So far, this is what I'm working with: `var stockTickPrice = (stockInfo[0].l) var stockTickPrice2 = (stockInfo2[0].l) var total = (stockTickPrice + stockTickPrice2); alert(total);` – Matt Nov 02 '13 at 18:55
  • added code to update the div.total in your fiddle with the total stock price. – Richard Hubley Nov 13 '13 at 18:21
  • Thanks @Richard Hubley, that worked! And to get the total value of each stock, I just multiplied by the number of shares. `$('.total').text(parseFloat($('.stockPrice').text()) + parseFloat(stockInfo.l) * 108)` – Matt Dec 17 '13 at 19:02
  • Corrected `$('.total').append(parseFloat($('.stockPrice').text())*16 + $('.stockPrice2').text()*116);` – Matt Dec 18 '13 at 02:58