Skip to content

Instantly share code, notes, and snippets.

@dtb49
Created July 5, 2017 14:40
Show Gist options
  • Save dtb49/07811513c2c8b0c2034f854dff56500a to your computer and use it in GitHub Desktop.
Save dtb49/07811513c2c8b0c2034f854dff56500a to your computer and use it in GitHub Desktop.
This is a simple stock program that makes calls from both the Yahoo and Google Finance APIs. Originally it was only using the Yahoo API but, since Verizon bought them out they have changed their API. I modified it so that it retrieves the chart from Google Finance.
<!--Beginning of the script-->
<link rel="stylesheet" href="https://www.w3schools.com/lib/w3.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script language="JavaScript">
function start(){
getData();
document.getElementById('link1').click();
}
function getData() {
var url = "http://query.yahooapis.com/v1/public/yql";
var symbol = $("#symbol").val();
var data = encodeURIComponent("select * from yahoo.finance.quotes where symbol in ('" + symbol + "')");
$.getJSON(url, 'q=' + data + "&format=json&diagnostics=true&env=store://datatables.org/alltableswithkeys&callback=?") @*original link broken: http://datatables.org/alltables.env*@
.done(function (data) {
$("#name").text("" + data.query.results.quote.Symbol);
$("#date").text("" + data.query.results.quote.Date);
$("#time").text("" + data.query.results.quote.LastTradeTime);
$("#result").text("" + data.query.results.quote.LastTradePriceOnly);
$("#chg").text("" + data.query.results.quote.Change);
$("#bid").text("" + data.query.results.quote.PreviousClose);
$("#volume").text("" + data.query.results.quote.Volume);
if(data.query.results.quote.PercentChange)
{
if(data.query.results.quote.PercentChange.indexOf("+") != -1){
document.getElementById("chg").className = "w3-text-green";
}
else{
document.getElementById("chg").className = "w3-text-red";
}
}
}).fail(function (jqxhr, textStatus, error) {
var err = textStatus + ", " + error;
$("#result").text('Request failed: ' + err);
});
}
function SendRequest()
{
getData();
document.getElementById('link1').click();
}
function CheckEnter(e)
{
if ((e.keyCode && e.keyCode==13) || (e.which && e.which==13)) {
return SendRequest();
}
return true;
}
function changeChart(select, item, url, symbol)
{
url = url.replace('goog',$("#symbol").val());
symbol = symbol.replace('GOOG',$("#symbol").val());
var div1d=document.getElementById("div1d_"+item);
var div5d=document.getElementById("div5d_"+item);
var div3m=document.getElementById("div3m_"+item);
var div6m=document.getElementById("div6m_"+item);
var div1y=document.getElementById("div1y_"+item);
var div2y=document.getElementById("div2y_"+item);
var div5y=document.getElementById("div5y_"+item);
var divMax=document.getElementById("divMax_"+item);
var divChart=document.getElementById("imgChart_"+item);
if(div1d==null || div5d==null || div3m==null || div6m==null || div1y==null || div2y==null || div5y==null || divMax==null || divChart==null)
return;
div1d.innerHTML="1d";
div5d.innerHTML="5d";
div3m.innerHTML="3m";
div6m.innerHTML="6m";
div1y.innerHTML="1y";
div2y.innerHTML="2y";
div5y.innerHTML="5y";
divMax.innerHTML="Max";
var rand_no = Math.random();
rand_no = rand_no * 100000000;
switch(select)
{
case 0:
div1d.innerHTML ="<b>1d</b>";
divChart.src = "https://www.google.com/finance/chart?cht=g&q="+symbol;
break;
case 1:
div5d.innerHTML="<b>5d</b>";
divChart.src = "https://www.google.com/finance/chart?cht=g&q="+symbol+"&p=5d";
break;
case 2:
div3m.innerHTML="<b>3m</b>";
divChart.src = "https://www.google.com/finance/chart?cht=g&q="+symbol+"&p=3M";
break;
case 3:
div6m.innerHTML="<b>6m</b>";
divChart.src = "https://www.google.com/finance/chart?cht=g&q="+symbol+"&p=6M";
break;
case 5:
div2y.innerHTML="<b>2y</b>";
divChart.src = "https://www.google.com/finance/chart?cht=g&q="+symbol+"&p=2Y";
break;
case 6:
div5y.innerHTML="<b>5y</b>";
divChart.src = "https://www.google.com/finance/chart?cht=g&q="+symbol+"&p=5Y";
break;
case 7:
divMax.innerHTML="<b>msx</b>";
divChart.src = "https://www.google.com/finance/chart?cht=g&q="+symbol+"&p=40Y";
break;
case 4:
default:
div1y.innerHTML="<b>1y</b>";
divChart.src = "https://www.google.com/finance/chart?cht=g&q="+symbol+"&p=1Y";
break;
}
}
</script>
<!--End of the script-->
<iframe class="w3-row w3-mobile" src="@CurrentPage.iFramedContent" width="100%" height="650" scrolling="auto" style="@iframeCss"></iframe>
<div class="w3-mobile" align="center" style="background:url(stock_chart_yahoo_finance/main_body.jpg); width:550px; margin:50px auto; padding:20px;">
<table width="100%" border="0" cellpadding="0" cellspacing="0" align="center" class="w3-mobile" style="overflow-x:auto">
<tbody>
<tr valign="top">
<td class="normalText" align="left">
<input name="txtQuote" id="symbol" onkeypress="return CheckEnter(event);" value="GOOG" type="text" />
<br />
<div id="service" style="padding:10px 0;">
<table style="border: 1px solid black;" width="100">
<tbody>
<tr style="font-size: 14px; font-family: Arial,Helvetica,sans-serif; font-weight: bold;">
<td>Symbol</td>
<td>Date</td>
<td>Last Trade</td>
<td>Trade</td>
<td>Chg</td>
<td>Open</td>
<td>Volume</td>
</tr>
<tr style="font-family: Arial,Helvetica,sans-serif; font-size: 14px; padding: 0px 2px;">
<td id="name">GOOG</td>
<td><p>
<script> document.write(new Date().toLocaleDateString()); </script>
</p></td>
<td id="time">5:00pm</td>
<td id="result">$2.15</td>
<td id="chg"><span id="colorText" class="greenText">+0.00(+0.00%)</span></td>
<td id="bid">2.15</td>
<td id="volume">9000</td>
</tr>
</tbody>
</table>
<br />
<img id="imgChart_0" src="stock_chart_yahoo_finance/yhoo.png" border="0" /><br />
<a class="linkText" href='javascript:changeChart(0,0,%20&quot;y/goog&quot;,%20&quot;GOOG&quot;);'><span id="div1d_0">1d</span></a>&nbsp;&nbsp;
<a class="linkText" href='javascript:changeChart(1,0,%20&quot;y/goog&quot;,%20&quot;GOOG&quot;);'><span id="div5d_0">5d</span></a>&nbsp;&nbsp;
<a class="linkText" href='javascript:changeChart(2,0,%20&quot;y/goog&quot;,%20&quot;GOOG&quot;);'><span id="div3m_0">3m</span></a>&nbsp;&nbsp;
<a class="linkText" href='javascript:changeChart(3,0,%20&quot;y/goog&quot;,%20&quot;GOOG&quot;);'><span id="div6m_0">6m</span></a>&nbsp;&nbsp;
<a class="linkText" href='javascript:changeChart(4,0,%20&quot;y/goog&quot;,%20&quot;GOOG&quot;);'><span id="div1y_0">1y</span></a>&nbsp;&nbsp;
<a class="linkText" href='javascript:changeChart(5,0,%20&quot;y/goog&quot;,%20&quot;GOOG&quot;);'><span id="div2y_0">2y</span></a>&nbsp;&nbsp;
<a class="linkText" href='javascript:changeChart(6,0,%20&quot;y/goog&quot;,%20&quot;GOOG&quot;);'><span id="div5y_0">5y</span></a>&nbsp;&nbsp;
<a id="link1" class="linkText" href='javascript:changeChart(7,0,%20&quot;y/goog&quot;,%20&quot;GOOG&quot;);'><span id="divMax_0"><b>msx</b></span></a>
<br />
</div>
</td>
</tr>
</tbody>
</table>
</div>
<script>
SendRequest();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment