Easy, interactive financial charts in Python: Just 11 lines of code, no JavaScript required
Overview
This guide demonstrates how to display Databento’s market data with TradingView’s popular Lightweight Charts library, entirely in Python. It also shows how to display indicators in the charts.
TradingView is a popular charting platform and Databento is a provider of high-quality financial market data. This guide makes use of the lightweight-charts-python library, which provides a user-friendly, Pythonic interface to a simplified version of TradingView — so you don’t need to learn or use JavaScript to take advantage of TradingView’s interactive features.
If you want to explore advanced features of Lightweight Charts, you can find the documentation here.
We will make use of data from Databento’s ohlcv-1m schema. First, install databento
and the lightweight-charts-python
library:
pip install databento
pip install lightweight-charts
Import both libraries and pandas:
import pandas as pd
import databento as db
from lightweight_charts import Chart
Declare a main function and set your Databento API key:
if __name__ = "__main__":
key = "YOUR_DB_API_KEY_HERE"
key = "YOUR_DB_API_KEY_HERE"
client = Historical(key=key)
Getting Data from Databento
Fetch the OHLCV data using Databento’s simple API. Databento has a simple usage-based pricing plan for new users that charges per data request. Depending on your needs, it might be a good idea to choose a subscription plan for flat-rate access or store the data locally to reduce costs that may be incurred on repeat requests.
data = client.timeseries.get_range(
dataset="XNAS.ITCH",
symbols=["NVDA"],
schema="ohlcv-1m",
start="2024-05-13T08:00",
end="2024-05-13T23:59",
limit=1000,
)
# Optional: Save the data to file
# path = "instrument_data.dbn"
# data.to_file(path=path)
Displaying Data with Lightweight Charts
Convert the data to a dataframe and and initialize lightweight_charts
. If you saved the data to file in the previous step, you can load the data with the DBNStore
helper class:
# Optional: Load data from file
# from Databento import DBNStore
# data = DBNStore.from_file(path="instrument_data.dbn")
df = data.to_df()
# Setting toolbox to True allows you to make drawings on the chart
chart = Chart(toolbox=True)
chart.set(df)
# Set block to True, otherwise the chart window will automatically close
chart.show(block = True)
If everything goes alright, you get a basic chart as depicted below:
Plotting Lines
There are various plot types that can be used to overlay derived data and analytics on the chart. Using an example from another Databento article, we will plot the VWAP of NVDA during the session. Note that additional code from this point onwards will be included before calling the chart.set()
and chart.show()
methods.
# Calculate vwap and put the result in a new dataframe
df["pvt"] = df["close"] * df["volume"]
df["pvt"] = df["close"] * df["volume"]
df["vwap"] = df["pvt"].cumsum() / df["volume"].cumsum()
ohlcv_vwap_df = pd.DataFrame({
"vwap": df["vwap"]
}, index=df.index)
# Create a line bound to the chart, and set data to the line
line = chart.create_line(name="vwap", color="blue", width=3)
line.set(ohlcv_vwap_df)
# Important: Put the code above before these two lines
chart.set(df)
chart.show(block=True)
Applying Custom Styles
The lightweight-charts-python
library supports customization of chart elements like font, colors, watermark, and gridlines. Below is an example using Databento branding:
chart.layout(background_color='#F2F2FF', text_color='#090008', font_size=13,
font_family='Helvetica')
chart.candle_style(up_color='#A09EFA', down_color='#474BD7',
border_up_color='#6266E5', border_down_color='#222262',
wick_up_color='#6266E5', wick_down_color='#222262')
chart.volume_config(up_color='#777777', down_color='#cccccc')
chart.watermark('NVDA 1 min', color='#dddddd')
chart.crosshair(mode='normal', vert_color='#222222', vert_style='dotted',
horz_color='#222222', horz_style='dotted')
chart.legend(visible=True, font_size=14, color='#222222')
chart.grid(vert_enabled=False, horz_enabled=False)
The lightweight-charts
library also supports Jupyter and Streamlit. Simply follow the code below depending on your use case and replace accordingly:
# Jupyter
from lightweight_charts import JupyterChart
from lightweight_charts import JupyterChart
chart = JupyterChart()
# ... set data to the chart
chart.load()
# Streamlit
from lightweight_charts.widgets import StreamlitChart
chart = StreamlitChart()
# Set data to the chart
chart.load()
By combining Databento’s market data with the lightweight-charts-python
library, you can create highly customizable and interactive TradingView-style visualizations with minimal effort. This guide covered foundational concepts, and future articles will explore advanced topics such as live data streaming and visualization of order book features.
Credits
The following article was written by Idris Dosunmu, Databento user and Lead Product Manager at Mintara Digital adapted for Medium. Mintara Digital is a digital product consultancy for fintechs, hedge funds, and family offices. The original post can be found here.
On behalf of Databento, we’d like to thank Idris for generously sharing his knowledge via this tutorial. You can find Idris on LinkedIn here.
For suggestions or requests for specific visualization techniques, please reach out to Idris via idris@mintaragroup.com.