Blame | Last modification | View Log | RSS feed
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /><title>5.31. Example - Candlesticks OHLC (Open, High, Low, Close) Financial Plot</title><link rel="stylesheet" type="text/css" href="phplotdoc.css" /><meta name="generator" content="DocBook XSL Stylesheets V1.78.1" /><link rel="home" href="index.html" title="PHPlot Reference Manual" /><link rel="up" href="examples.html" title="Chapter 5. PHPlot Examples" /><link rel="prev" href="ex-ohlcbasic.html" title="5.30. Example - Basic OHLC (Open, High, Low, Close) Financial Plot" /><link rel="next" href="ex-ohlccandlesticks2.html" title="5.32. Example - Filled Candlesticks OHLC (Open, High, Low, Close) Financial Plot" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">5.31. Example - Candlesticks OHLC (Open,High, Low, Close) Financial Plot</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ex-ohlcbasic.html">Prev</a> </td><th width="60%" align="center">Chapter 5. PHPlot Examples</th><td width="20%" align="right"> <a accesskey="n" href="ex-ohlccandlesticks2.html">Next</a></td></tr></table><hr /></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="ex-ohlccandlesticks"></a>5.31. Example - Candlesticks OHLC (Open, High, Low, Close) Financial Plot</h2></div></div></div><p>This example shows a <a class="link" href="conc-plottypes.html#plottype-candlesticks" title="3.4.5. Plot Type: candlesticks (OHLC Candlesticks Plot)">candlesticks</a>plot, which is a form of the Open, High, Low, Close (OHLC) financial plot.Each X is a point in time or interval, and there are 4 corresponding Y valuesfor the four prices (open, high, low, and close).Compare this with <a class="xref" href="ex-ohlcbasic.html#example-ohlcbasic" title="Example 5.30. Basic OHLC Plot">Example 5.30, “Basic OHLC Plot”</a>and <a class="xref" href="ex-ohlccandlesticks2.html#example-ohlccandlesticks2" title="Example 5.32. Filled Candlesticks OHLC Plot">Example 5.32, “Filled Candlesticks OHLC Plot”</a>,which show the same data but with a different presentation.</p><p>The data values for this example are read from an external file.Refer to <a class="xref" href="ex-ohlcbasic.html" title="5.30. Example - Basic OHLC (Open, High, Low, Close) Financial Plot">Section 5.30, “Example - Basic OHLC (Open, High, Low, Close) Financial Plot”</a> for more information.Unlike that example, this candlesticks example uses the dates from thedata file as the X values in the data array, with the PHPlot data typedata-data. The dates read from the file are converted into timestamp valueswith <code class="function">strtotime()</code> inside<code class="function">read_prices_data_data()</code>.Since each row in a data-data array specifies its X value, the rows do notneed to be sorted (as they were in the previous example).</p><p>PHPlot will format the X values as dates because <a class="xref" href="SetXLabelType.html" title="SetXLabelType"><span class="refentrytitle">SetXLabelType</span></a>is used to select date/time formatting. Unlike the text-data example, thisalso lets us use <a class="xref" href="SetXTickIncrement.html" title="SetXTickIncrement"><span class="refentrytitle">SetXTickIncrement</span></a> to control the labeldensity along the X axis.</p><div class="example"><a id="example-ohlccandlesticks"></a><p class="title"><strong>Example 5.31. Candlesticks OHLC Plot</strong></p><div class="example-contents"><div class="informalfigure"><div class="mediaobject"><img src="examples/ohlccandlesticks.png" alt="Candlesticks OHLC Financial Plot Example" /></div></div><pre class="programlisting"><?php# PHPlot Example: OHLC (Financial) plot, Candlesticks plot, using# external data file, data-data format with date-formatted labels.define('DATAFILE', 'examples/ohlcdata.csv'); // External data filerequire_once 'phplot.php';/*Read historical price data from a CSV data downloaded from Yahoo! Finance.The first line is a header which must contain: Date,Open,High,Low,Close[...]Each additional line has a date (YYYY-MM-DD), then 4 price values.Convert to PHPlot data-data data array with empty labels and time_t Xvalues and return the data array.*/function read_prices_data_data($filename){$f = fopen($filename, 'r');if (!$f) {fwrite(STDERR, "Failed to open data file: $filename\n");return FALSE;}// Read and check the file header.$row = fgetcsv($f);if ($row === FALSE || $row[0] != 'Date' || $row[1] != 'Open'|| $row[2] != 'High' || $row[3] != 'Low' || $row[4] != 'Close') {fwrite(STDERR, "Incorrect header in: $filename\n");return FALSE;}// Read the rest of the file and convert.while ($d = fgetcsv($f)) {$data[] = array('', strtotime($d[0]), $d[1], $d[2], $d[3], $d[4]);}fclose($f);return $data;}$plot = new PHPlot(800, 600);$plot->SetImageBorderType('plain'); // Improves presentation in the manual$plot->SetTitle("Candlesticks Financial Plot (data-data)\nMSFT Q1 2009");$plot->SetDataType('data-data');$plot->SetDataValues(read_prices_data_data(DATAFILE));$plot->SetPlotType('candlesticks');$plot->SetDataColors(array('SlateBlue', 'black', 'SlateBlue', 'black'));$plot->SetXLabelAngle(90);$plot->SetXLabelType('time', '%Y-%m-%d');$plot->SetXTickIncrement(7*24*60*60); // 1 week intervalif (method_exists($plot, 'TuneYAutoRange'))$plot->TuneYAutoRange(0); // Suppress Y zero magnet (PHPlot >= 6.0.0)$plot->DrawGraph();</pre></div></div><br class="example-break" /></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ex-ohlcbasic.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="examples.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ex-ohlccandlesticks2.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">5.30. Example - Basic OHLC (Open, High, Low, Close) Financial Plot </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 5.32. Example - Filled Candlesticks OHLC (Open, High, Low, Close) Financial Plot</td></tr></table></div></body></html>