98 |
- |
1 |
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
|
|
|
2 |
<!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>
|
|
|
3 |
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>
|
|
|
4 |
plot, which is a form of the Open, High, Low, Close (OHLC) financial plot.
|
|
|
5 |
Each X is a point in time or interval, and there are 4 corresponding Y values
|
|
|
6 |
for the four prices (open, high, low, and close).
|
|
|
7 |
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>
|
|
|
8 |
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>,
|
|
|
9 |
which show the same data but with a different presentation.
|
|
|
10 |
</p><p>
|
|
|
11 |
The data values for this example are read from an external file.
|
|
|
12 |
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.
|
|
|
13 |
Unlike that example, this candlesticks example uses the dates from the
|
|
|
14 |
data file as the X values in the data array, with the PHPlot data type
|
|
|
15 |
data-data. The dates read from the file are converted into timestamp values
|
|
|
16 |
with <code class="function">strtotime()</code> inside
|
|
|
17 |
<code class="function">read_prices_data_data()</code>.
|
|
|
18 |
Since each row in a data-data array specifies its X value, the rows do not
|
|
|
19 |
need to be sorted (as they were in the previous example).
|
|
|
20 |
</p><p>
|
|
|
21 |
PHPlot will format the X values as dates because <a class="xref" href="SetXLabelType.html" title="SetXLabelType"><span class="refentrytitle">SetXLabelType</span></a>
|
|
|
22 |
is used to select date/time formatting. Unlike the text-data example, this
|
|
|
23 |
also lets us use <a class="xref" href="SetXTickIncrement.html" title="SetXTickIncrement"><span class="refentrytitle">SetXTickIncrement</span></a> to control the label
|
|
|
24 |
density along the X axis.
|
|
|
25 |
</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
|
|
|
26 |
# PHPlot Example: OHLC (Financial) plot, Candlesticks plot, using
|
|
|
27 |
# external data file, data-data format with date-formatted labels.
|
|
|
28 |
define('DATAFILE', 'examples/ohlcdata.csv'); // External data file
|
|
|
29 |
require_once 'phplot.php';
|
|
|
30 |
|
|
|
31 |
/*
|
|
|
32 |
Read historical price data from a CSV data downloaded from Yahoo! Finance.
|
|
|
33 |
The first line is a header which must contain: Date,Open,High,Low,Close[...]
|
|
|
34 |
Each additional line has a date (YYYY-MM-DD), then 4 price values.
|
|
|
35 |
Convert to PHPlot data-data data array with empty labels and time_t X
|
|
|
36 |
values and return the data array.
|
|
|
37 |
*/
|
|
|
38 |
function read_prices_data_data($filename)
|
|
|
39 |
{
|
|
|
40 |
$f = fopen($filename, 'r');
|
|
|
41 |
if (!$f) {
|
|
|
42 |
fwrite(STDERR, "Failed to open data file: $filename\n");
|
|
|
43 |
return FALSE;
|
|
|
44 |
}
|
|
|
45 |
// Read and check the file header.
|
|
|
46 |
$row = fgetcsv($f);
|
|
|
47 |
if ($row === FALSE || $row[0] != 'Date' || $row[1] != 'Open'
|
|
|
48 |
|| $row[2] != 'High' || $row[3] != 'Low' || $row[4] != 'Close') {
|
|
|
49 |
fwrite(STDERR, "Incorrect header in: $filename\n");
|
|
|
50 |
return FALSE;
|
|
|
51 |
}
|
|
|
52 |
// Read the rest of the file and convert.
|
|
|
53 |
while ($d = fgetcsv($f)) {
|
|
|
54 |
$data[] = array('', strtotime($d[0]), $d[1], $d[2], $d[3], $d[4]);
|
|
|
55 |
}
|
|
|
56 |
fclose($f);
|
|
|
57 |
return $data;
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
$plot = new PHPlot(800, 600);
|
|
|
61 |
$plot->SetImageBorderType('plain'); // Improves presentation in the manual
|
|
|
62 |
$plot->SetTitle("Candlesticks Financial Plot (data-data)\nMSFT Q1 2009");
|
|
|
63 |
$plot->SetDataType('data-data');
|
|
|
64 |
$plot->SetDataValues(read_prices_data_data(DATAFILE));
|
|
|
65 |
$plot->SetPlotType('candlesticks');
|
|
|
66 |
$plot->SetDataColors(array('SlateBlue', 'black', 'SlateBlue', 'black'));
|
|
|
67 |
$plot->SetXLabelAngle(90);
|
|
|
68 |
$plot->SetXLabelType('time', '%Y-%m-%d');
|
|
|
69 |
$plot->SetXTickIncrement(7*24*60*60); // 1 week interval
|
|
|
70 |
if (method_exists($plot, 'TuneYAutoRange'))
|
|
|
71 |
$plot->TuneYAutoRange(0); // Suppress Y zero magnet (PHPlot >= 6.0.0)
|
|
|
72 |
$plot->DrawGraph();
|
|
|
73 |
</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>
|