plot_ohlc
The plot_ohlc
method creates open, high, low, close (OHLC) plots using data from tables.
Syntax
plot_ohlc(
series_name: str,
t: Union[Table, SelectableDataSet],
x: Union[str, List[DateTime]],
open: Union[str, list[int], list[float], list[DateTime]],
high: Union[str, list[int], list[float], list[DateTime]],
low: Union[str, list[int], list[float], list[DateTime]],
close: Union[str, list[int], list[float], list[DateTime]],
by: list[str] = None,
) -> Figure
Parameters
Parameter | Type | Description |
---|---|---|
series_name | str | The name you want to use to identify the series on the plot itself. |
t | Union[Table, SelectableDataSet] | The table (or other selectable data set) that holds the data to be plotted. |
x | Union[str, List[DateTime]] | The name of the column of data to be used for the X value. |
open | Union[str, List[int], List[float], List[DateTime]] | The name of the column of data to be used for the open value. |
high | Union[str, list[int], list[float], list[DateTime]] | The name of the column of data to be used for the high value. |
low | Union[str, list[int], list[float], list[DateTime]] | The name of the column of data to be used for the low value. |
close | Union[str, list[int], list[float], list[DateTime]] | The name of the column of data to be used for the close value. |
by optional | list[str] | Columns that hold grouping data. |
Returns
An open, high, low, close (OHLC) plot with a single axes.
Examples
The following example plots data from a Deephaven table.
from deephaven import read_csv
from deephaven import agg
from deephaven.plot.figure import Figure
crypto_trades = read_csv(
"https://media.githubusercontent.com/media/deephaven/examples/main/CryptoCurrencyHistory/CSV/CryptoTrades_20210922.csv"
)
agg_list = [
agg.last(cols=["CloseTime = Timestamp"]),
agg.first(cols=["OpenTime = Timestamp"]),
agg.max_(cols=["High = Price"]),
agg.min_(cols=["Low = Price"]),
]
btc_bin = crypto_trades.where(filters=["Instrument=`BTC/USD`"]).update(
formulas=["TimestampBin = lowerBin(Timestamp, MINUTE)"]
)
t_ohlc = (
btc_bin.agg_by(agg_list, by=["TimestampBin"])
.join(table=btc_bin, on=["CloseTime = Timestamp"], joins=["Close = Price"])
.join(table=btc_bin, on=["OpenTime = Timestamp"], joins=["Open = Price"])
)
plot_ohlc = (
Figure()
.figure_title("BTC OHLC - Aug 22 2021")
.plot_ohlc(
series_name="BTC",
t=t_ohlc,
x="TimestampBin",
open="Open",
high="High",
low="Low",
close="Close",
)
.show()
)
Shared Axes
Just like XY series plots, the Open, High, Low and Close plot can also be used to present multiple series on the same chart, including the use of multiple X or Y axes. An example of this follows:
from deephaven import agg as agg
from deephaven.plot.figure import Figure
btc_bin = crypto_trades.where(filters=["Instrument=`BTC/USD`"]).update(
formulas=["TimestampBin = lowerBin(Timestamp, MINUTE)"]
)
eth_bin = crypto_trades.where(filters=["Instrument=`ETH/USD`"]).update(
formulas=["TimestampBin = lowerBin(Timestamp, MINUTE)"]
)
agg_list = [
agg.last(cols=["CloseTime = Timestamp"]),
agg.first(cols=["OpenTime = Timestamp"]),
agg.max_(["High = Price"]),
agg.min_(["Low = Price"]),
]
btc_ohlc = (
btc_bin.agg_by(agg_list, by=["TimestampBin"])
.join(table=btc_bin, on=["CloseTime = Timestamp"], joins=["Close = Price"])
.join(table=btc_bin, on=["OpenTime = Timestamp"], joins=["Open = Price"])
)
eth_ohlc = (
eth_bin.agg_by(agg_list, by=["TimestampBin"])
.join(table=eth_bin, on=["CloseTime = Timestamp"], joins=["Close = Price"])
.join(table=eth_bin, on=["OpenTime = Timestamp"], joins=["Open = Price"])
)
plot_ohlc = (
Figure()
.figure_title("BTC and ETC OHLC - Aug 22 2021")
.plot_ohlc(
series_name="BTC",
t=t_ohlc,
x="TimestampBin",
open="Open",
high="High",
low="Low",
close="Close",
)
.x_twin()
.plot_ohlc(
series_name="ETH",
t=t_ohlc,
x="TimestampBin",
open="Open",
high="High",
low="Low",
close="Close",
)
.show()
)