5.31. Example - Candlesticks OHLC (Open, High, Low, Close) Financial Plot

This example shows a candlesticks 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 values for the four prices (open, high, low, and close). Compare this with Example 5.30, “Basic OHLC Plot” and Example 5.32, “Filled Candlesticks OHLC Plot”, which show the same data but with a different presentation.

The data values for this example are read from an external file. Refer to Section 5.30, “Example - Basic OHLC (Open, High, Low, Close) Financial Plot” for more information. Unlike that example, this candlesticks example uses the dates from the data file as the X values in the data array, with the PHPlot data type data-data. The dates read from the file are converted into timestamp values with strtotime() inside read_prices_data_data(). Since each row in a data-data array specifies its X value, the rows do not need to be sorted (as they were in the previous example).

PHPlot will format the X values as dates because SetXLabelType is used to select date/time formatting. Unlike the text-data example, this also lets us use SetXTickIncrement to control the label density along the X axis.

Example 5.31. Candlesticks OHLC Plot

Candlesticks OHLC Financial Plot Example
# 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 file
require_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 X
  values 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]);
    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->SetDataColors(array('SlateBlue', 'black', 'SlateBlue', 'black'));
$plot->SetXLabelType('time', '%Y-%m-%d');
$plot->SetXTickIncrement(7*24*60*60); // 1 week interval
if (method_exists($plot, 'TuneYAutoRange'))
    $plot->TuneYAutoRange(0); // Suppress Y zero magnet (PHPlot >= 6.0.0)

SourceForge.net Logo

This version of the manual was produced for the PHPlot Sourceforge project web service site, which requires the logo on each page.

To download a logo-free copy of the manual, see the PHPlot project downloads area.