Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public class CandlestickSeries

  1. Object
  2. Series
  3. XYSeries
  4. CandlestickSeries

Open/high/low/close prices drawn as candlesticks, OHLC bars or Heikin-Ashi candles.

A rising period (close at or above open) is drawn in the theme’s positive colour and a falling one in its negative colour. Pair it with a SessionTimeAxis to skip weekends, or with a TimeAxis to keep calendar spacing.

Chart chart = new Chart();
long day = 24L * 60 * 60 * 1000;
long[] timestamps = {0, day, 4 * day};
double[] open = {10, 11, 12}, high = {12, 13, 14}, low = {9, 10, 11}, close = {11, 12, 13};
CandlestickSeries prices = new CandlestickSeries("ACME");
prices.setOhlc(null, open, high, low, close);
chart.setXAxis(new SessionTimeAxis(Axis.BOTTOM, timestamps));
chart.addSeries(prices);

Candles OHLC bars Heikin-Ashi candles

Fields

public static final int CANDLES = 0Filled bodies with wicks.
public static final int OHLC_BARS = 1A vertical high-low line with open and close ticks.
public static final int HEIKIN_ASHI = 2Candles of Heikin-Ashi averaged prices, which smooth out noise to show the trend.

Constructors

public CandlestickSeries(String name)Creates an empty series.

Methods

public CandlestickSeries setOhlc(double[] x, double[] open, double[] high, double[] low, double[] close)Replaces all prices.
public CandlestickSeries addOhlc(double x, double open, double high, double low, double close)Appends one period, for streaming quotes.
public XYSeries add(double x, double y)Appends a point.
public CandlestickSeries updateLast(double price)Updates the last period in place, as a live tick does to the current candle.
public CandlestickSeries setStyle(int style)Chooses candles, OHLC bars or Heikin-Ashi candles.
public int getStyle()The drawing style.
public CandlestickSeries setBodyFraction(float f)The fraction of each slot a body takes, 0.1 to 1.
public CandlestickSeries setColors(int up, int down)Overrides the theme’s rising and falling colours.
public double getOpen(int i)Opening price of period i.
public double getHigh(int i)Highest price of period i.
public double getLow(int i)Lowest price of period i.
public double getClose(int i)Closing price of period i.
public double[][] heikinAshi()Computes Heikin-Ashi values, {open, high, low, close} arrays.
protected void dataChanged()Drops the cached Heikin-Ashi values: they depend on every candle, so any change to the data (an add, a slide, a set, an updateLast) makes them stale, not only a change of length.
public double[] getYBounds()The extent of the data along y, {min, max}, or null when empty or not cartesian.
public double[] getXBounds()The extent of the data along x, {min, max}, or null when empty or not cartesian.
public void paint(ChartContext ctx)Paints the series.
public ChartPoint hitTest(ChartContext ctx, int x, int y, int radius)Finds the data point nearest a screen position.
public String formatTooltip(ChartContext ctx, ChartPoint p)The tooltip row for a point: the series or point name and its formatted value.
public void paintLegendSymbol(Graphics g, int x, int y, int size)Paints the legend swatch.
protected String tableValue(int i)The text of point i in the accessible data table.

Inherited methods

Field details

CANDLES

public static final int CANDLES = 0
Filled bodies with wicks.

OHLC_BARS

public static final int OHLC_BARS = 1
A vertical high-low line with open and close ticks.

HEIKIN_ASHI

public static final int HEIKIN_ASHI = 2
Candles of Heikin-Ashi averaged prices, which smooth out noise to show the trend.

Constructor details

CandlestickSeries

public CandlestickSeries(String name)
Creates an empty series.

Parameters

name String
the series name

Method details

setOhlc

public CandlestickSeries setOhlc(double[] x, double[] open, double[] high, double[] low, double[] close)
Replaces all prices.

Parameters

x double[]
the x value of each period (a time, or an index for SessionTimeAxis); null for 0, 1, 2…
open double[]
opening prices
high double[]
highest prices
low double[]
lowest prices
close double[]
closing prices

Returns

this series

addOhlc

public CandlestickSeries addOhlc(double x, double open, double high, double low, double close)
Appends one period, for streaming quotes.

Parameters

x double
the period’s x value
open double
the opening price
high double
the highest price
low double
the lowest price
close double
the closing price

Returns

this series

add

public XYSeries add(double x, double y)
Appends a point. With setMaxPoints the oldest point drops off, for real time data.

updateLast

public CandlestickSeries updateLast(double price)
Updates the last period in place, as a live tick does to the current candle.

Parameters

price double
the latest traded price

Returns

this series

setStyle

public CandlestickSeries setStyle(int style)
Chooses candles, OHLC bars or Heikin-Ashi candles.

Parameters

style int
CANDLES, OHLC_BARS or HEIKIN_ASHI

Returns

this series

getStyle

public int getStyle()
The drawing style.

setBodyFraction

public CandlestickSeries setBodyFraction(float f)
The fraction of each slot a body takes, 0.1 to 1.

Parameters

f float
the fraction

Returns

this series

setColors

public CandlestickSeries setColors(int up, int down)
Overrides the theme’s rising and falling colours.

Parameters

up int
colour of rising periods
down int
colour of falling periods

Returns

this series

getOpen

public double getOpen(int i)
Opening price of period i.

getHigh

public double getHigh(int i)
Highest price of period i.

getLow

public double getLow(int i)
Lowest price of period i.

getClose

public double getClose(int i)
Closing price of period i.

heikinAshi

public double[][] heikinAshi()
Computes Heikin-Ashi values, {open, high, low, close} arrays.

Returns

the averaged prices for every period

dataChanged

protected void dataChanged()
Drops the cached Heikin-Ashi values: they depend on every candle, so any change to the data (an add, a slide, a set, an updateLast) makes them stale, not only a change of length.

getYBounds

public double[] getYBounds()
The extent of the data along y, {min, max}, or null when empty or not cartesian.

getXBounds

public double[] getXBounds()
The extent of the data along x, {min, max}, or null when empty or not cartesian.

paint

public void paint(ChartContext ctx)
Paints the series.

Parameters

ctx ChartContext
the current frame: graphics, plot area, axes and animation progress

hitTest

public ChartPoint hitTest(ChartContext ctx, int x, int y, int radius)
Finds the data point nearest a screen position.

Parameters

ctx ChartContext
the frame the series was last painted in
x int
chart relative x
y int
chart relative y
radius int
how far from the point a touch may land and still hit it

Returns

the point, or null

formatTooltip

public String formatTooltip(ChartContext ctx, ChartPoint p)
The tooltip row for a point: the series or point name and its formatted value. Series with several values per point (open/high/low/close, ranges, box plots) override this.

Parameters

ctx ChartContext
the last paint context
p ChartPoint
the point

Returns

one line of text

paintLegendSymbol

public void paintLegendSymbol(Graphics g, int x, int y, int size)
Paints the legend swatch.

Parameters

g Graphics
graphics
x int
swatch left
y int
swatch top
size int
swatch size

tableValue

protected String tableValue(int i)
The text of point i in the accessible data table.

Parameters

i int
the point index

Returns

the formatted value