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

## 5.32. Example - Filled Candlesticks OHLC (Open, High, Low, Close) Financial Plot

This example shows a candlesticks2 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.31, “Candlesticks OHLC Plot”, which show the same data but with a different presentation. With the candlesticks2 plot type, all the candlestick bodies are filled in, meaning you must set meaningful data colors in order to be able to tell if a security closes up or down.

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. This example uses the data-data format, with the dates read from the file converted to X values in the data array.

Example 5.32. Filled Candlesticks OHLC Plot

    <?php
    # PHPlot Example: OHLC (Financial) plot, Filled 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]);
        }
        fclose($f);
        return $data;
    }

    $plot = new PHPlot(800, 600);
    $plot->SetImageBorderType('plain'); // Improves presentation in the manual
    $plot->SetTitle("Filled Candlesticks Financial Plot (data-data)\nMSFT Q1 2009");
    $plot->SetDataType('data-data');
    $plot->SetDataValues(read_prices_data_data(DATAFILE));
    $plot->SetPlotType('candlesticks2');
    $plot->SetDataColors(array('red', 'DarkGreen', 'red', 'DarkGreen'));
    $plot->SetXLabelAngle(90);
    $plot->SetXLabelType('time', '%Y-%m-%d');
    $plot->SetXTickIncrement(7*24*60*60); // 1 week interval
    $plot->DrawGraph();

Prev | Up | Next
5.31. Example - Candlesticks OHLC (Open, High, Low, Close) Financial Plot | Home | 5.33. Example - Linepoints Plot with Data Value Labels