Calculate TICK and TRIN indicators
Overview
In this example we will use the Live client to calculate two technical indicators: market tick (TICK) and the traders index (TRIN).
Trades schema
These indicators are updated on every execution for all symbols on the venue. To get this, we will use the trades schema, which provides records for every trade execution.
Example
We can pass in symbols="ALL_SYMBOLS" to request all symbols of a dataset at once.
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 32 33 34 35 36 37 38
import databento as db
import numpy as np
# First, create a live client and connect
live_client = db.Live(key="$YOUR_API_KEY")
# Next, we will subscribe to trades for all symbols
live_client.subscribe(
dataset="GLBX.MDP3",
schema="trades",
symbols="ALL_SYMBOLS",
)
# Then, we setup our initial values for TICK/TRIN
uptick = downtick = upvol = downvol = 0
# We will consume 20 records and then close for this example
trades = 0
for record in live_client:
if isinstance(record, db.TradeMsg):
side = record.side
if side == "A":
downtick += 1
downvol += record.size
elif side == "B":
uptick += 1
upvol += record.size
tick = uptick / downtick if downtick != 0 else np.nan
trin_base = upvol / downvol if downvol != 0 else np.nan
trin = tick / trin_base if trin_base != 0 else np.nan
print(f"{tick:.4f}", f"{trin:.4f}")
trades += 1
if trades >= 20:
break
elif isinstance(record, db.ErrorMsg):
print(f"Error: {record.err}") Result
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
1.0000 1.0000 0.5000 1.0000 0.3333 1.0000 0.2500 1.0000 0.2000 2.2000 0.1667 2.0000 0.1429 1.8571 0.1250 1.8750 0.1111 2.1111 0.2222 2.1111 0.3333 1.5833 0.4444 1.0556 0.4000 1.0000 0.5000 1.0000 0.6000 1.0909 0.5455 1.1405 0.5000 1.0909 0.4615 1.0909 0.4286 1.0909