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.30. Example - Basic 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-horizthinbarline.html" title="5.29. Example - Horizontal Thin Bar Line Plot" /><link rel="next" href="ex-ohlccandlesticks.html" title="5.31. Example - 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.30. Example - Basic OHLC (Open, High, Low, Close) Financial Plot</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ex-horizthinbarline.html">Prev</a> </td><th width="60%" align="center">Chapter 5. PHPlot Examples</th><td width="20%" align="right"> <a accesskey="n" href="ex-ohlccandlesticks.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-ohlcbasic"></a>5.30. Example - Basic OHLC (Open, High, Low, Close) Financial Plot</h2></div></div></div><p>
|
|
|
3 |
This example shows an <a class="link" href="conc-plottypes.html#plottype-ohlc" title="3.4.9. Plot Type: ohlc (Basic OHLC Plot)">ohlc</a> plot, which
|
|
|
4 |
is a basic 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 the next two examples,
|
|
|
8 |
<a class="xref" href="ex-ohlccandlesticks.html#example-ohlccandlesticks" title="Example 5.31. Candlesticks OHLC Plot">Example 5.31, “Candlesticks OHLC Plot”</a>
|
|
|
9 |
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>,
|
|
|
10 |
which show the same data but with a different presentation.
|
|
|
11 |
</p><p>
|
|
|
12 |
In this example, the data array is read from an external file in Comma
|
|
|
13 |
Separated Value (CSV) format. (Financial data in this format is available
|
|
|
14 |
for download from sites such as Yahoo! Finance.)
|
|
|
15 |
A portion of the data file can be found below.
|
|
|
16 |
</p><p>
|
|
|
17 |
This example uses the dates from the data file as row labels in the data
|
|
|
18 |
array, with text-data data format. For this to work, the rows have to be sorted
|
|
|
19 |
by increasing date, so the <code class="function">read_prices_text_data()</code>
|
|
|
20 |
first reads the data into a temporary array, sorts by the date, then copies
|
|
|
21 |
the data into a PHPlot data array. Compare this with the other two OHLC
|
|
|
22 |
examples, where the same data is used differently.
|
|
|
23 |
</p><p>
|
|
|
24 |
This example uses <a class="xref" href="TuneYAutoRange.html" title="TuneYAutoRange"><span class="refentrytitle">TuneYAutoRange</span></a> to disable the
|
|
|
25 |
<span class="emphasis"><em>zero magnet</em></span>, which would otherwise stretch the Y axis
|
|
|
26 |
range to include 0. For many plots, including 0 on the axis is desirable,
|
|
|
27 |
but for this financial plot we want to be able to see small changes in the
|
|
|
28 |
values, and are less interested in the relative amounts.
|
|
|
29 |
(The zero magnet feature was added at PHPlot-6.0.0, and the script tests
|
|
|
30 |
for the method availability before using it.)
|
|
|
31 |
</p><div class="example"><a id="example-ohlcbasic"></a><p class="title"><strong>Example 5.30. Basic OHLC Plot</strong></p><div class="example-contents"><div class="informalfigure"><div class="mediaobject"><img src="examples/ohlcbasic.png" alt="Basic OHLC Financial Plot Example" /></div></div><pre class="programlisting"><?php
|
|
|
32 |
# PHPlot Example: OHLC (Financial) plot, basic lines, using
|
|
|
33 |
# external data file, text-data format.
|
|
|
34 |
define('DATAFILE', 'examples/ohlcdata.csv'); // External data file
|
|
|
35 |
require_once 'phplot.php';
|
|
|
36 |
|
|
|
37 |
/*
|
|
|
38 |
Read historical price data from a CSV data downloaded from Yahoo! Finance.
|
|
|
39 |
The first line is a header which must contain: Date,Open,High,Low,Close[...]
|
|
|
40 |
Each additional line has a date (YYYY-MM-DD), then 4 price values.
|
|
|
41 |
The rows have to be sorted by date, because the original is reversed.
|
|
|
42 |
This version of the function uses the date as a label, and returns a
|
|
|
43 |
text-data (implied X) PHPlot data array.
|
|
|
44 |
*/
|
|
|
45 |
function read_prices_text_data($filename)
|
|
|
46 |
{
|
|
|
47 |
$f = fopen($filename, 'r');
|
|
|
48 |
if (!$f) {
|
|
|
49 |
fwrite(STDERR, "Failed to open data file: $filename\n");
|
|
|
50 |
return FALSE;
|
|
|
51 |
}
|
|
|
52 |
// Read and check the file header.
|
|
|
53 |
$row = fgetcsv($f);
|
|
|
54 |
if ($row === FALSE || $row[0] != 'Date' || $row[1] != 'Open'
|
|
|
55 |
|| $row[2] != 'High' || $row[3] != 'Low' || $row[4] != 'Close') {
|
|
|
56 |
fwrite(STDERR, "Incorrect header in: $filename\n");
|
|
|
57 |
return FALSE;
|
|
|
58 |
}
|
|
|
59 |
// Read the rest of the file into array keyed by date for sorting.
|
|
|
60 |
while ($r = fgetcsv($f)) {
|
|
|
61 |
$d[$r[0]] = array($r[1], $r[2], $r[3], $r[4]);
|
|
|
62 |
}
|
|
|
63 |
fclose($f);
|
|
|
64 |
ksort($d);
|
|
|
65 |
// Convert to a PHPlot data array with label and 4 values per row.
|
|
|
66 |
foreach ($d as $date => $r) {
|
|
|
67 |
$data[] = array($date, $r[0], $r[1], $r[2], $r[3]);
|
|
|
68 |
}
|
|
|
69 |
return $data;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
$plot = new PHPlot(800, 600);
|
|
|
73 |
$plot->SetImageBorderType('plain'); // Improves presentation in the manual
|
|
|
74 |
$plot->SetTitle("OHLC (Open/High/Low/Close) Financial Plot\nMSFT Q1 2009");
|
|
|
75 |
$plot->SetDataType('text-data');
|
|
|
76 |
$plot->SetDataValues(read_prices_text_data(DATAFILE));
|
|
|
77 |
$plot->SetPlotType('ohlc');
|
|
|
78 |
$plot->SetDataColors('black');
|
|
|
79 |
$plot->SetXLabelAngle(90);
|
|
|
80 |
$plot->SetXTickPos('none');
|
|
|
81 |
if (method_exists($plot, 'TuneYAutoRange'))
|
|
|
82 |
$plot->TuneYAutoRange(0); // Suppress Y zero magnet (PHPlot >= 6.0.0)
|
|
|
83 |
$plot->DrawGraph();
|
|
|
84 |
</pre></div></div><br class="example-break" /><p>
|
|
|
85 |
Here is the top portion of the data file used for the three OHLC examples.
|
|
|
86 |
This file is called <a class="ulink" href="ohlcdata.csv" target="_top">ohlcdata.csv</a>.
|
|
|
87 |
</p><pre class="programlisting">Date,Open,High,Low,Close,Volume,Adj Close
|
|
|
88 |
2009-03-31,17.83,18.79,17.78,18.37,92095500,17.81
|
|
|
89 |
2009-03-30,17.74,17.76,17.27,17.48,49633000,16.95
|
|
|
90 |
2009-03-27,18.54,18.62,18.05,18.13,47670400,17.58
|
|
|
91 |
2009-03-26,18.17,18.88,18.12,18.83,63775100,18.26
|
|
|
92 |
2009-03-25,17.98,18.31,17.52,17.88,73927100,17.34
|
|
|
93 |
...
|
|
|
94 |
</pre><p>
|
|
|
95 |
</p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ex-horizthinbarline.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-ohlccandlesticks.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">5.29. Example - Horizontal Thin Bar Line Plot </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 5.31. Example - Candlesticks OHLC (Open, High, Low, Close) Financial Plot</td></tr></table></div></body></html>
|