@quickoldcar: My mistake, I missed a ) transcribing to my post, the original did have it, php would've caught it otherwise. Thanks.
@barand: the first part of the code pulls 2-month stock price data from yahoo finance csv, and populates date and open, high, low, close price keys correspondingly (above snippet uses close). Here's gist of (not very cleanly-written) code:
$url = 'http://real-chart.finance.yahoo.com/table.csv?s='.'KO'.'&a='.$prevmonth.'&b='.$prevday.'&c='.$prevyear.'&d='.$curmonth.'&e='.$curday.'&f='.$curyear.'&g=d&ignore=.csv'; //'KO' refers to ticker symbol of Coca Cola $handle = fopen($url, "r"); $arr['filesize'] = sizeof($handle); $keys = array('line', 'date', 'open', 'high', 'low', 'close', 'volume', 'adj'); //new keys $arr = array(); $buffer = fgets($handle, 5120); if ($buffer == "") return false; //parse each line and place into array $one while (($buffer = fgets($handle, 5120)) !== false) { if (!preg_match('/([0-9\-]{10})[\,]{1}([0-9\.]+)[\,]{1}([0-9\.]+)[\,]{1}([0-9\.]+)[\,]{1}([0-9\.]+)[\,]{1}([0-9\.]+)[\,]{1}([0-9\.]+)\s?/', $buffer, $one)) return false; $one = array_combine($keys, array_values($one)); //rekeying $rawdata[] = $one; }
if (!feof($handle)) { echo "Error: unexpected fgets() fail\n"; return false; } fclose($handle); for ($i=0; $i<sizeof($rawdata); $i++) { $dataarray[$i]['date']=$rawdata[$i]['date']; $dataarray[$i]['high']=$rawdata[$i]['high']; $dataarray[$i]['low']=$rawdata[$i]['low']; $dataarray[$i]['close']=$rawdata[$i]['close']; } $prices = (array_reverse($dataarray));//flip price array order
the charting code comes straight from libchart documentation, which plots daily closing prices (Y axis) and trading date (X axis):
$chart1 = new LineChart();//start macdchart $serie1 = new XYDataSet(); $chart2 = new LineChart(); $serie2 = new XYDataSet(); $serie3 = new XYDataSet(); for ($i=25; $i<sizeof($prices); $i++) { $serie1->addPoint(new Point($prices[$i]['date'], $prices[$i]['close']); }
Hope this is enough detail. Thanks.