5.30. Example - Basic OHLC (Open, High, Low, Close) Financial Plot
Prev | Chapter 5. PHPlot Examples | Next

## 5.30. Example - Basic OHLC (Open, High, Low, Close) Financial Plot

This example shows an ohlc plot, which is a basic 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 the next two examples, Example 5.31, “Candlesticks OHLC Plot” and Example 5.32, “Filled Candlesticks OHLC Plot”, which show the same data but with a different presentation.

In this example, the data array is read from an external file in Comma Separated Value (CSV) format. (Financial data in this format is available for download from sites such as Yahoo! Finance.) A portion of the data file can be found below.

This example uses the dates from the data file as row labels in the data array, with text-data data format. For this to work, the rows have to be sorted by increasing date, so the `read_prices_text_data()` first reads the data into a temporary array, sorts by the date, then copies the data into a PHPlot data array. Compare this with the other two OHLC examples, where the same data is used differently.

This example uses TuneYAutoRange to disable the zero magnet, which would otherwise stretch the Y axis range to include 0. For many plots, including 0 on the axis is desirable, but for this financial plot we want to be able to see small changes in the values, and are less interested in the relative amounts. (The zero magnet feature was added at PHPlot-6.0.0, and the script tests for the method availability before using it.)

Example 5.30. Basic OHLC Plot

    <?php
    # PHPlot Example: OHLC (Financial) plot, basic lines, using
    # external data file, text-data format.
    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.
      The rows have to be sorted by date, because the original is reversed.
      This version of the function uses the date as a label, and returns a
      text-data (implied X) PHPlot data array.
    */
    function read_prices_text_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 into array keyed by date for sorting.
        while ($r = fgetcsv($f)) {
            $d[$r[0]] = array($r[1], $r[2], $r[3], $r[4]);
        }
        fclose($f);
        ksort($d);
        // Convert to a PHPlot data array with label and 4 values per row.
        foreach ($d as $date => $r) {
            $data[] = array($date, $r[0], $r[1], $r[2], $r[3]);
        }
        return $data;
    }

    $plot = new PHPlot(800, 600);
    $plot->SetImageBorderType('plain'); // Improves presentation in the manual
    $plot->SetTitle("OHLC (Open/High/Low/Close) Financial Plot\nMSFT Q1 2009");
    $plot->SetDataType('text-data');
    $plot->SetDataValues(read_prices_text_data(DATAFILE));
    $plot->SetPlotType('ohlc');
    $plot->SetDataColors('black');
    $plot->SetXLabelAngle(90);
    $plot->SetXTickPos('none');
    if (method_exists($plot, 'TuneYAutoRange'))
        $plot->TuneYAutoRange(0); // Suppress Y zero magnet (PHPlot >= 6.0.0)
    $plot->DrawGraph();

Here is the top portion of the data file used for the three OHLC examples. This file is called ohlcdata.csv.

    Date,Open,High,Low,Close,Volume,Adj Close
    2009-03-31,17.83,18.79,17.78,18.37,92095500,17.81
    2009-03-30,17.74,17.76,17.27,17.48,49633000,16.95
    2009-03-27,18.54,18.62,18.05,18.13,47670400,17.58
    2009-03-26,18.17,18.88,18.12,18.83,63775100,18.26
    2009-03-25,17.98,18.31,17.52,17.88,73927100,17.34
    ...

Prev | Up | Next
5.29. Example - Horizontal Thin Bar Line Plot | Home | 5.31. Example - Candlesticks OHLC (Open, High, Low, Close) Financial Plot