plot_price
function backtide.analysis.price.plot_price(data, price_col="close", indicators=None, run=None, title=None, legend="upper left", figsize=(900, 600), filename=None, display=True)[source]
Create a price line chart.
Optionally, overlay the prices with indicators or with trade markers.
| Parameters |
data : pd.DataFrame | pl.DataFrame
Input data containing columns
price_col : str, default="close"symbol, open, high, low, close
and dt with the datetime.
Column name in
indicators : BaseIndicator | Sequence[BaseIndicator] | dict[str, BaseIndicator] | None, default=Nonedata to plot on the y-axis.
Indicators to overlay on the price chart. If dict, it must map a name
(used in the legend) to an indicator instance.
run : RunResult | None, default=None
When provided, overlays entry/exit markers from the run's trades on top
of the price line. Triangles mark entries (up for long, down for short)
and crosses mark exits (green for winners, red for losers). Trades whose
symbols are not present in
title : str | dict | None, default=Nonedata are silently skipped.
Title for the plot.
legend : str | dict | None, default="upper left"
Legend for the plot. See the user guide for an extended
description of the choices.
figsize : tuple[int, int], default=(900, 600)
Figure's size in pixels, format as (x, y).
filename : str | Path | None, default=None
Save the plot using this name. The type of the file depends on the
provided name (
display : bool | None, default=True.html, .png, .pdf, etc...). If filename has no
file type, the plot is saved as .html. If None, the plot isn't saved.
Whether to render the plot. If
None, it returns the figure.
|
| Returns |
Figure | None
The Plotly figure object. Only returned if
display=None.
|
See Also
Create a candlestick chart.
Create a trading volume bar chart.
Create a VWAP (Volume-Weighted Average Price) chart.
Example
>>> from backtide.storage import query_bars
>>> from backtide.analysis import plot_price
>>> from backtide.indicators import BollingerBands, SimpleMovingAverage
>>> from backtide.storage import query_strategy_runs, query_experiments
>>> df = query_bars(["AAPL", "MSFT"], "1d")
>>> # Compare the price of two symbols
>>> plot_price(df)
>>> # Add a line indicator to the price chart
>>> aapl = df[df["symbol"] == "AAPL"]
>>> plot_price(aapl, indicators=SimpleMovingAverage())
>>> # Add a band indicator to the price chart
>>> plot_price(aapl, indicators=BollingerBands())
>>> # Add trades to the chart
>>> exp = query_experiments().iloc[0]
>>> run = query_strategy_runs(exp.id)[-1]
>>> plot_price(aapl, run=run)