imars.com - Stéphane Lavergne's web site
Go to site navigation

ChartOHLC

Plot OHLC stock charts.

Summary

Introduction

Legal

Copyright (C) 2008 Stephane Lavergne http://www.imars.com/

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Description

Unique PHP implementation of stock charting. I couldn't find any bar chart implementations in PHP, so ChartOHLC was born. See http://www.imars.com/trading/ for real-life examples of charts made with this class.

Usage

Couldn't be simpler, for my needs anyway. :-)

// Let's define a nice clean colour scheme first:
$bgColor = 0xFFFFFF;
$gridColor = 0xE0E0E0;
$titleColor = 0x009000;
$upColor = $titleColor;
$downColor = false;
$borderColor = 0xFF8080;
$labelColor = 0x000000;
$volColor = 0x8080FF;

// Now, build the chart where $data is our array:
require_once 'ChartOHLC.inc';
$chart = new ChartOHLC(600, 300, $bgColor, $data);
$chart->title("MSFT - 5 min", $titleColor);
$chart->title("(C) 2008 imars.com but freely redistributable", $gridColor, true);
$chart->plotVolume($volColor, $borderColor, $labelColor, 50);
$chart->plotPriceLegends($borderColor, $labelColor, $gridColor, true);
$chart->plotPrice($upColor, $downColor);

// Dump to stdout and destroy:
header('Content-Type: image/png');
$chart->png();
unset($chart);

TODO

The code has not been optimized at all yet, not even cleaned up. It just does the job for now and I'll clean it up later on.

Constants

LEGEND_HEIGHT

const LEGEND_HEIGHT

Minimum distance in pixels between vertical legend labels.

General Methods

ChartOHLC

function ChartOHLC($x, $y, $bgColor, $data, $wantPrice = true, $wantVolume = true, $decimals = 2)

Create a new OHLC chart.

Parameters

x
Width in pixels
y
Height in pixels
bgColor
Integer value for the background color. (i.e. “0xFFFFFF”)
data
Indexed array of bar information. Each element is an indexed array listing, in order: timestamp integer, open, high, low, close price floats, and volume integer.
wantPrice
Bool whether you intend to plot a price legend. (Optional.)
wantVolume
Bool whether you intend to plot volume (mandatory legend.) (Optional.)
decimals
Number of decimals to standardize to in price displays. (Optional.)

__destruct

function __destruct()

Destroy this instance.

setBarWidth

function setBarWidth($width)

Change the density of the chart. By default, each bar is 4 pixels wide: 1 pixel for the open dot, 1 pixel for the bar itself, 1 pixel for the close dot, and 1 spacer.

I personally prefer 3-6 depending on the purpose of the chart. 2-3 looks very cramped, although it's cheaper than buying additional monitors. :-)

Careful

If you're changing the bar width with this method, be sure to call it before invoking any of the plotting methods.

Parameters

width
New width in pixels.

Returns

The width parameter.

getLow

function getLow()

Get lowest price in the entire data set.

If called after setBarWidth(), it will be the viewable area only.

If called after plotPriceLegends(), it will be for the viewable area only, but subject to the extending done to reach round numbers on the legend.

Returns

The lowest price.

getHigh

function getHigh()

Get highest price in the entire data set.

If called after setBarWidth(), it will be the viewable area only.

If called after plotPriceLegends(), it will be for the viewable area only, but subject to the extending done to reach round numbers on the legend.

Returns

The highest price.

png

function png($filename = null)

Generate PNG image. The result is either returned in a string or saved to a file, depending on the presence of the $filename argument.

Parameters

filename
File to save PNG image to. (Optional.)

Returns

The direct result of GD's imagepng(), which should be the PNG data in a string if $filename was omitted.

Plotting Methods

title

function title($title, $color, $right = false)

Add a title at the top of the chart. The first time it is invoked, it reduces the height of the OHLC chart area in order to fit a line of text at the top. In subsequent invocations, additional titles are appended at the end of the existing title.

Caveats

While multiple left-justified titles can be appended, only one right-justified title can be printed.

Parameters

title
Text to display.
color
24-bit colour integer to print with. (i.e. 0x00FF00 for green)
right
Bool whether you want the title right-justified. (Optional.)

Returns

No return value.

plotPriceLegends

function plotPriceLegends($borderColor, $priceColor, $gridColor = false, $doTime = true, $mGridColor = false)

Display legend labels and background grid. To stack your prices on top of the grid, be sure to invoke this before plotPrice().

Parameters

borderColor
24-bit colour integer for the border
priceColor
24-bit colour integer for the price
gridColor
24-bit colour integer for the grid or false if undesired. (Optional.)
doTime
Bool whether to also process the X (time) axis. (Optional.)
mGridColor
24-bit colour integer for the major grid lines or false if undesired. (Optional.)

Returns

No return value.

plotPrice

function plotPrice($upColor, $downColor = false)

Plot OHLC price bars. Omitting a down colour draws all bars in the same colour.

Parameters

upColor
24-bit colour integer
downColor
24-bit colour integer (Optional.)

Returns

No return value.

plotVolume

function plotVolume($color, $borderColor, $titleColor, $height)

Create and plot a volume histogram area.

Caveats

It is necessary to do this before invoking plotPriceLegends() and plotPrice() because it reduces the height of the OHLC chart area.

Parameters

color
24-bit colour integer for the histogram.
borderColor
24-bit colour integer for the border.
titleColor
24-bit colour integer for the inline “Volume” title.
height
Height in pixels.

plotTrend

function plotTrend($x1, $y1, $x2, $y2, $color)

Draw a trend line. The line starts at the origin coordinates, then becomes dashed after the second coordinates.

Caveats

The coordinates are relative to the entire image not just the price area. This is to help users (such as myself) with external sources of coordinates. (In my case, JavaScript mouse click events.)

Parameters

x1
X of origin pixel
y1
Y of origin pixel
x2
X of second pixel
y2
Y of second pixel
color
24-bit colour integer for the line

Returns

No return value.

plotPriceStudy

function plotPriceStudy($data, $color, $smooth = 0)

Draws a broken line. The array of data is expected to have exactly as many elements as the bars passed to ChartOHLC at creation time. Each value in the array must be in the displayable price range, or it will be omitted.

Caveats

The displayable price range will not be stretched to accomodate values outside of it, as it was determined in plotPriceLegends() which should be called before other plotting methods including this one.

Parameters

data
Indexed array of price values to plot.
color
24-bit colour integer for the broken line.
smooth
Smoothing: 0=none, 1=mild, 2=more. (Optional.)

Returns

No return value.

plotLevelStudy

function plotLevelStudy($levels, $color)

FIXME

Post a comment:

(not displayed, optional)

(optional)

Don't write in these two boxes:

(URLs will become hyperlinks, please omit "http://".)

Site navigation