Menu

SampleOHLC

PHPlot Sample - OHLC (Open/High/Low/Close) Financial Plots

Editor's note: This page was originally written up on 2010-11-01. The new plot types described here were implemented in PHPlot-5.3.0 with changes. The examples shown here will not work exactly as shown, and the results will differ.

OHLC is a financial plot that shows the price of a security over time. An experimental version of PHPlot adds 3 OHLC plot types. Note: These plot types are not currently available in any version of PHPlot, including CVS. They may or may not be implemented in a future version.

Reference: [feature-requests:#30]

The plot types are:

  • Basic OHLC: A vertical line shows the low and high prices, with tick marks showing the open price on the left, and closing price on the right.
  • Candlestick: A thin rectangle (body) shows the open and close prices, with upper and lower wicks showing the high and low prices. The body is open if the security closed up, and filled solid if it closed down.
  • Candlestick-Filled: This is the same as candlestick, but the body is always filled. Colors are used to distinguish closing up (for example green or white) versus closing down (for example red or black).

Four data colors are used for these plots as shown in the table below. Note that the default PHPlot data colors are probably not appropriate for these plots, so you will have to set them with SetDataColors().

Color Index Color Usage for plot type
Basic OHLC Candlestick Candlestick-Filled
0 Vertical stroke (if closed down) Candle body fill (if closed down) Candle body (if closed down)
1 Vertical stroke (if closed up) Candle body outline (if closed up) Candle body (if closed up)
2 Tick marks for open and close (if closed down) Candle wicks for high and low (if closed down) Candle wicks for high and low (if closed down)
3 Tick marks for open and close (if closed up) Candle wicks for high and low (if closed up) Candle wicks for high and low (if closed up)

Line thickness can be controlled for all 3 plot types with SetLineWidths. The first setting is used to control the line width for the candlestick bodies, or vertical line in the basic OHLC plot. The second setting is used to control the line width for the candlestick wicks, or tick marks in the basic OHLC plot.

The candlestick body width, and the basic OHLC plot tick width, are calculated by PHPlot based on the plot density. The calculated value is used either for one-half the width of the candlestick body, or the tick widths. There is a minimum width (2 pixels), a maximum width (8 pixels), and a width factor (0.3) that determines how much of the available width is used.

width = max(2, min(8, 0.3 * plot_area_width / num_data_rows))

At present, these three parameters can be controlled by directly setting PHPlot class variables. No member functions are defined for this purpose. (This is the same situation as the class variables which allow adjusting bar width in bar charts.)

Examples of the three new plot types are below. Note the data is in an external file which can be found at the bottom.

Basic OHLC Plot:

Plot Image - Basic OHLC

Basic OHLC Plot Code:

:::php 
<?php
# OHLC Financial Plots - Experimental Samples

# Open and read comma-separated data file into $data array:
$f = fopen('ohlc.data', 'r') or exit(1);
while ($row = fgetcsv($f)) $data[] = $row;
fclose($f);

require_once 'phplot.php';
$plot = new PHPlot(480, 360);
$plot->SetTitle("Demo plot: Basic OHLC");
$plot->SetDataType('text-data');
$plot->SetDataValues($data);
$plot->SetPlotType('ohlc');
$plot->SetLineWidths(array(2, 2));
$plot->SetDataColors(array('red', 'black', 'red', 'black'));
$plot->SetXTickPos('none');
$plot->SetXLabelAngle(90);
$plot->DrawGraph();

~~~~~ 

### Candlesticks Plot: ###

[[img src=ohlc_candlestick.png alt="Plot Image - Candlestick OHLC"]]

### Candlesticks Plot Code: ###

:::php
<?php

OHLC Financial Plots - Experimental Samples

Open and read comma-separated data file into $data array:

$f = fopen('ohlc.data', 'r') or exit(1);
while ($row = fgetcsv($f)) $data[] = $row;
fclose($f);

require_once 'phplot.php';
$plot = new PHPlot(480, 360);
$plot->SetTitle("Demo plot: Candlestick");
$plot->SetDataType('text-data');
$plot->SetDataValues($data);
$plot->SetPlotType('candlesticks');
$plot->SetLineWidths(array(2, 2));
$plot->SetDataColors('black');
$plot->SetXTickPos('none');
$plot->SetXLabelAngle(90);
$plot->DrawGraph();

### Filled Candlesticks Plot: ###

[[img src=ohlc_candlestickfilled.png alt="Plot Image - Filled Candlestick OHLC"]]

### Filled Candlesticks Plot Code: ###

:::php
<?php

OHLC Financial Plots - Experimental Samples

Open and read comma-separated data file into $data array:

$f = fopen('ohlc.data', 'r') or exit(1);
while ($row = fgetcsv($f)) $data[] = $row;
fclose($f);

require_once 'phplot.php';
$plot = new PHPlot(480, 360);
$plot->SetTitle("Demo plot: Filled Candlestick");
$plot->SetDataType('text-data');
$plot->SetDataValues($data);
$plot->SetPlotType('candlesticks-filled');
$plot->SetLineWidths(array(2, 2));
$plot->SetDataColors(array('red', 'black', 'red', 'black'));
$plot->SetXTickPos('none');
$plot->SetXLabelAngle(90);
$plot->DrawGraph();

### Data: ###

For the 3 sample plots above, the data values have been placed in an external
file ohlc.data which is in comma-separated-value (CSV) format.
Of course, the data values can be provided in the script or via any other
mechanism. For OHLC plots, 4 Y values per X are used, for the Open, High,
Low, and Close prices. Each row must have exactly 4 Y values. There cannot
be missing values, and multiple data sets are not supported.

"2009-12-01",29.52,30.05,29.41,30.01
"2009-12-02",29.90,29.99,29.65,29.78
"2009-12-03",29.84,30.20,29.76,29.83
"2009-12-04",30.05,30.37,29.83,29.98
"2009-12-07",29.78,30.08,29.68,29.79
"2009-12-08",29.52,29.74,29.38,29.57
"2009-12-09",29.47,29.81,29.25,29.71
"2009-12-10",29.71,29.96,29.66,29.87
"2009-12-11",29.97,30.00,29.79,29.85
"2009-12-14",29.91,30.16,29.90,30.11
"2009-12-15",29.89,30.21,29.88,30.02
"2009-12-16",30.07,30.41,30.04,30.10
"2009-12-17",29.95,29.96,29.57,29.60
"2009-12-18",29.84,30.45,29.80,30.36
"2009-12-21",30.40,30.84,30.37,30.52
"2009-12-22",30.60,30.93,30.54,30.82
"2009-12-23",30.71,30.95,30.69,30.92
"2009-12-24",30.88,31.00,30.76,31.00
"2009-12-28",31.00,31.18,30.89,31.17
"2009-12-29",31.35,31.50,31.23,31.39
"2009-12-30",31.15,31.29,30.80,30.96
"2009-12-31",30.98,30.99,30.48,30.48
~~~~


Related

Feature Requests: #30

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.