Display stock charts on your own website

Plain stock chart
In searching an easy way to display stock charts on a web page hosted in a web server, two useful scripts were found on the internet:

1. Easy Yahoo stock chart ASP.NET Page
2. Displaying a daily chart for a ticker symbol

Even the scripts were written in different languages, they use a common idea that fetch the Yahoo stock charts and display them locally. The first article gives a basic framework to do this in PHP. It has one missing statements at the beginning of the script. I added that and use the Yahoo link from the second article. Now this script can show 6 months and 3 months and daily chart at once.

<?php
//stock chart shower
//Beginners PHP
//Description : This allows a user to enter a ticker symbol
//and rather than the usual dry quotes displays a chart of
//how the stock has performed over the whole year

$symbol=$_POST['symbol'];

if ($symbol =="")
{
?>
<html><body>
<form action='<?php print $PHP_SELF; ?>' method ='post'>
Enter your ticker symbol :<input type='text' name='symbol' size = 6><br>
<input type='submit' value="show chart">
</form>
</body></html>
<?php
}
else
{
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="image/gif; charset=iso-8859-1">
</head>
<img border="0" src="http://ichart.finance.yahoo.com/z?s=<?php print $symbol; ?>&t=6m&q=c&l=on&z=l&a=v&p=s">
<img border="0" src="http://ichart.finance.yahoo.com/z?s=<?php print $symbol; ?>&t=3m&q=c&l=on&z=m&a=v&p=s">
<img border="0" src="http://ichart.yahoo.com/t?s=<?php print $symbol; ?>">

</body>
</html>
<?php
}
?>

Flash Stock Chart

Open Flash Chart 2 provides a whole library of PHP, PERL, Python, .NET and more languages that help your generate SWF flash charts for free. On its website, there are tons of examples to help you use the library. OFC2 uses JSON as the file format and you can generate cool chart in very short time.

The PHP source code to get data from database and generate JSON file for SWF chart is listed here.

<?php

include_once 'php-ofc-library/open-flash-chart.php';
include_once '../stocks_secureinfo.inc';

$stock_tick = 'LIWA';

$sql = " SELECT price_open, price_high, price_low, price_close, stock_volume, c_year, c_month, c_day from stockdata s ".
" INNER JOIN company c ON s.id_company=c.id_company ".
" INNER JOIN caldate d ON s.id_caldate=d.id_caldate ".
" WHERE c.tick='$stock_tick' ".
" ORDER BY c_year ASC, c_month ASC, c_day ASC ".
" ; ";
$result = mysql_query($sql);

if (!$result) {
die('Invalid query: ' . mysql_error());
}

$data = array();
$volume = array();
$max = 0;
while($row = mysql_fetch_array($result))
{
//
// LOOK: PHP thinks our data is text,
//       force it to be an integer:
//
$data[] = new candle_value(
(float)$row['price_high'],
(float)$row['price_open'],
(float)$row['price_close'],
(float)$row['price_low']);

$volume[] = (float)$row['stock_volume'];

}

$title = new title( $stock_tick );

### Candle graph for stock price
$candle = new candle('#9933CC');
$candle->set_values($data);
$candle->set_tooltip('#x_label#<br>High: #high#<br>Open: #open#<br>Close: #close#<br>Low: #low#');
//$candle->set_on_show(new bar_on_show('drop', .9, 0));

$y = new y_axis();
$y->set_range( 0, 11, 0);

$chart = new open_flash_chart();
$chart->set_title( $title );
$chart->add_element( $candle );
$chart->set_y_axis( $y );

### bar graph for stock volume
$bar = new bar();
$bar->set_values( $volume );
$bar->axis = 'right';

$yr = new y_axis_right();
$yr->set_range( 0, 2000000, 0);

$chart->add_element( $bar );
$chart->set_y_axis_right( $yr );

#echo $chart->toPrettyString();
write2file($chart->toPrettyString(),$stock_tick."_price.json");

function write2file($str,$filename)
{
$fh = fopen($filename, 'w') or die("can't open file");
fwrite($fh, $str);
fclose($fh);
}

?>

The HTML file that used to show the SWF chart is listed below.

<html>
<head>

<script type="text/javascript" src="js/swfobject.js"></script>
<script type="text/javascript">

swfobject.embedSWF(
"open-flash-chart.swf", "my_price_chart",
"1000", "400", "9.0.0", "expressInstall.swf",
{"data-file":"LIWA_price.json"} );

</script>

</head>
<body>

<h1>Hello world!</h1>
<div id="my_price_chart"></div>
<br>
<div id="my_volume_chart"></div>
</body>
</html>

For details to use OFC2 and the above codes, please go to OFC2 website and read more details.

Weekly Chart from StockCharts
StockCharts provides nice stock charts with some useful information. A strategy similar to get charts from Yahoo site can be adopted. The link to point to stock chart looks like http://stockcharts.com/c-sc/sc?s=ARST&p=W&b=5&g=0&i=t05444095590&r=6408. There is a problem when you directly embed the link to your website. It suggests you visit their site to view the chart instead of just show it. To solve the problem, we can download the stock charts locally and save them to PNG files. Then the charts can be used as local images. To do that, we can use Linux wget command.

wget “http://stockcharts.com/c-sc/sc?s=ARST&p=W&b=5&g=0&i=t05444095590&r=6408″ -O arst.png

  • Share/Bookmark

One Response to “Display stock charts on your own website”

  1. [...] This post was mentioned on Twitter by DickDeals. DickDeals said: Display stock charts on your own website http://bit.ly/ceTpNU via @AddToAny [...]

Leave a Response

You must be logged in to post a comment.