Candlestick
Candlestick is a type of chart commonly used in financial markets to represent the price movement of an asset, such as stocks or cryptocurrencies. It consists of individual "candles" that display the opening, closing, high, and low prices for a specific time period. Each candle has a rectangular body, representing the opening and closing prices, and thin lines, called wicks or shadows, indicating the highest and lowest prices during that time frame.
Candlestick charts often use colors to visually indicate whether the value within a specific candle has increased or decreased. For example, a positive price movement is typically represented by a green candle, indicating that the closing price is higher than the opening price (and by a red color otherwise). Basically "candlestick" main statistic is an indicator of whether the price has increased.
This guide describes comprehensively the process of building candlestick chart and all the details of its customization.
This notebook uses definitions from DataFrame.
Usage
Binning is commonly used in statistics and data analysis to simplify complex data sets and make them easier to interpret. Histogram (or any other plot with "bin" statistics) helps to give an overview of the sample distribution.
Arguments
Input (mandatory):
x
— candlex
-position (often time or date describing)open
— candle open valuehigh
— candle high valuelow
— candle low valueclose
— candle close value
Generalized signature
The specific signature depends on the function, but all functions related to "candlestick" statistic (which will be discussed further below - different variations of statCandlestick()
, candlestick()
) have approximately the same signature with the arguments above:
The possible types of x
, open
, high
, low
, close
depend on where a certain function is used. They can be simply Iterable
(List
, Set
, etc.) or a reference to a column in a DataFrame
(String
, ColumnAccessor
) or the DataColumn
itself.
Output statistics
name | type | description |
---|---|---|
Stat.x | X | Candle |
Stat.open | Double | Candle open. Equals to input |
Stat.close | Double | Candle close. Equals to input |
Stat.min | Double | Candle minimum. Equals to input |
Stat.lower | Double | Candle lower value (i.e. smaller of |
Stat.upper | Double | Candle lower value (i.e. greater of |
Stat.max | Double | Candle maximum. Equals to input |
Stat.isIncreased | Boolean | Increase indicator: |
StatCandlestick plots
x | open | high | low | close |
---|---|---|---|---|
Jan | 14.2 | 15.5 | 7.5 | 8.0 |
Feb | 6.7 | 9.6 | 6.1 | 8.6 |
Mar | 8.8 | 10.7 | 8.5 | 10.7 |
Apr | 11.2 | 11.7 | 5.4 | 6.5 |
May | 4.0 | 9.9 | 4.0 | 9.8 |
df
has a signature
x | open | high | low | close |
---|
Let's take a look at StatCandlestick
output DataFrame:
Stat | ||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
It has the following signature:
Stat | |||||||
---|---|---|---|---|---|---|---|
x | open | close | min | lower | upper | max | isIncreased |
As you can see, we got a DataFrame
with one ColumnGroup
called Stat
which contains several columns with statics. For statCandlestick
, each row corresponds to one candle. Stat.x
is the candle x
-coordinate. Stat.open
and Stat.close
correspond to candle open and close. Stat.min
and Stat.max
correspond to candle low and high. Stat.lower
and Stat.upper
correspond to candle edges. Stat.isIncreased
shows if value is increased (i.e close > open
).
DataFrame
with "candlestick" statistics is called StatCandlestickFrame
statCandlestick
context transform
statCandlestick(statCandlestickArgs) { /*new plotting context*/ }
modifies a plotting context — instead of original data (no matter was it empty or not) new StatCandlestick
dataset (calculated on given arguments. Inputs and weights can be provided as Iterable
or as dataset column reference — by name as a String
, as a ColumnReference
or as a DataColumn
) is used inside a new context (original dataset and primary context are not affected — you can add layers using initial dataset outside the statCandlestick
context). Since the old dataset is irrelevant, we cannot use references for its columns. But we can refer to the new ones. They are all contained in the Stat
group and can be called inside the new context:
Candlestick layer
Basically, candlestick plot is a box plot where each box represents one candle. Box whisker's ends correspond to low
and high
values; and lower and upper edges to open
and close
(so here we need to determine which is greater and which is lesser — that's what we counted in the statistics). Non-positional attributes (most often color) indicate whether an increase or decrease has occurred. So basically, we can build a candlestick with statCandlestick
and boxes
as follows:
But we can do it even faster with candlestick(statCandlestickArgs)
method:
Let's compare them:
These two plots are almost identical (the only difference in tooltips). Indeed, candlestick just uses statCandlestick and boxes and performs aesthetic mappings under the hood.
candlestick
customization
We can customize candlestick layer: candlestick()
optionally opens a new context, where we can configure it. We can set different color, borderline color, etc. for candles with increasing and decreasing value with special DSL, or make general settings (as in the usual context opened by boxes { ... }
):
However, it can also be done as with other statistical layers API (i.e., through mappings from statistics from StatCandlestick
dataset):
candlestick
plot
candlestick(statCandlestickArgs) and DataFrame.candlestick(statCandlestickArgs) are a family of functions for fast plotting a candlestick.
In case you want to provide inputs using column selection DSL, it’s a bit different from the usual one — you should assign them throw invocation eponymous functions:
Candlestick plot can be configured with .configure {}
extension — it opens a context that combines bars, StatCandlestick
and plot context. That means you can configure boxes settings, mappings using StatCandlestick
dataset and any plot adjustments: