Calculate VWAP and RSI
Overview
In this example we will use the Historical client to calculate two technical indicators: the volume weighted average price (VWAP) and relative strength indicator (RSI). To do this we will request the open, high, low, and close prices in one minute intervals along with aggregated volume data to calculate these indicators at one minute intervals.
OHLCV-1m schema
We'll demonstrate this example using the OHLCV-1m schema. The OHLCV family of schemas contain the opening, high, low and closing prices as well as the aggregated volume of trades within a time interval. We'll use an interval of one minute, a typical charting frequency, which is specified by the suffix -1m.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
import databento as db
# First, create a historical client
client = db.Historical("$YOUR_API_KEY")
# Next, we will request the OHLCV-1m data
data = client.timeseries.get_range(
dataset="GLBX.MDP3",
start="2022-09-20",
symbols="CLV2",
stype_in="raw_symbol",
schema="ohlcv-1m",
)
# Then, convert the DBNStore to a DataFrame
ohlcv_data = data.to_df()
# Now, we will calculate the volume weighted average price (VWAP)
ohlcv_data["pvt"] = ohlcv_data[["open", "high", "close"]].mean(axis=1) * ohlcv_data["volume"]
ohlcv_data["vwap"] = ohlcv_data["pvt"].cumsum() / ohlcv_data["volume"].cumsum()
# Finally, we will calculate the relative strength indicator (RSI)
ohlcv_data["up"] = (ohlcv_data["close"] - ohlcv_data["open"]).clip(lower=0)
ohlcv_data["down"] = (ohlcv_data["open"] - ohlcv_data["close"]).clip(lower=0)
# We will use a simple moving average for this example
ohlcv_data["rs"] = ohlcv_data["up"].rolling(5).mean() / ohlcv_data["down"].rolling(5).mean()
ohlcv_data["rsi"] = 100 - (100 / (1 + ohlcv_data["rs"]))
# We now have a DataFrame containing our technical indicators
print(ohlcv_data[["symbol", "open", "close", "vwap", "rsi"]]) Result
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
symbol open close vwap rsi ts_event 2022-09-20 00:00:00+00:00 CLV2 85.85 85.85 85.850000 NaN 2022-09-20 00:08:00+00:00 CLV2 86.05 86.01 85.938889 NaN 2022-09-20 00:10:00+00:00 CLV2 86.03 86.03 85.950278 NaN 2022-09-20 00:11:00+00:00 CLV2 86.03 85.98 85.964516 NaN 2022-09-20 00:12:00+00:00 CLV2 86.01 86.01 85.965938 0.000000 ... ... ... ... ... ... 2022-09-20 18:25:00+00:00 CLV2 84.27 84.40 84.532655 53.488372 2022-09-20 18:26:00+00:00 CLV2 84.41 84.32 84.529908 51.111111 2022-09-20 18:27:00+00:00 CLV2 84.32 84.33 84.526147 64.864865 2022-09-20 18:28:00+00:00 CLV2 84.33 84.40 84.523789 77.500000 2022-09-20 18:29:00+00:00 CLV2 84.30 84.19 84.523212 51.219512 [176 rows x 5 columns]