Event
Overview¶
An Event is a snapshot of information occurring at a specific moment in time. It contains a list of items, and all items in a single event share the same timestamp. When an event is created, its timestamp is always converted to UTC.
An event is generated by a Feed and can contain any type of information.
Price Items¶
Since trading prices and volumes are such common type of data used in algo-trading, roboquant supports this with a number of special classes.
The base class is the PriceItem. Roboquant provides three built-in price
items, and it is easy to add your own:
Bar— Open, high, low, close prices and volume (a.k.a. candlesticks). The default price is the close price, and additional price types likeOPEN,HIGH, andLOWare available.Quote— Ask and bid prices and volumes. The default price is the mid-point price;ASKandBIDare also available.TradePrice— A single traded price with an optional volume.
from roboquant import Stock
from roboquant.common.event import Bar, Quote, TradePrice
asset = Stock("ABC")
item1 = Bar(asset, [10.0, 11.0, 9.0, 10.0, 10_000]) # OHLCV
item2 = Quote(asset, [11.0, 1_000, 10.0, 1_000]) # [ask-price, ask-volume, bid-price, bid-volume]
item3 = TradePrice(asset, 100.0, 10_000) # price, volume
print(item1, item2, item3, sep="\n")Output
Bar(asset=Stock(symbol='ABC', currency='USD', info=None), ohlcv=[10.0, 11.0, 9.0, 10.0, 10000], frequency='')
Quote(asset=Stock(symbol='ABC', currency='USD', info=None), data=[11.0, 1000, 10.0, 1000])
TradePrice(asset=Stock(symbol='ABC', currency='USD', info=None), trade_price=100.0, trade_volume=10000)
Accessing Prices¶
The price_items property returns a dictionary that maps each asset in the event to its price item.
This makes it fast and easy to access the prices of all assets in a single event.
import roboquant as rq
feed = rq.feeds.YahooFeed("IBM", start_date="2025-01-01", end_date="2025-02-01")
for event in feed.play():
for asset, item in event.price_items.items():
assert isinstance(item, Bar)
print(event.time, asset.symbol, item.price())Output
2025-01-02 05:00:00+00:00 IBM 210.1200714111328
2025-01-03 05:00:00+00:00 IBM 212.70907592773438
2025-01-06 05:00:00+00:00 IBM 212.72817993164062
2025-01-07 05:00:00+00:00 IBM 213.96058654785156
2025-01-08 05:00:00+00:00 IBM 213.21542358398438
2025-01-10 05:00:00+00:00 IBM 209.9385528564453
2025-01-13 05:00:00+00:00 IBM 207.6934814453125
2025-01-14 05:00:00+00:00 IBM 208.02784729003906
2025-01-15 05:00:00+00:00 IBM 210.2060546875
2025-01-16 05:00:00+00:00 IBM 212.7186279296875
2025-01-17 05:00:00+00:00 IBM 214.7535400390625
2025-01-21 05:00:00+00:00 IBM 214.2471923828125
2025-01-22 05:00:00+00:00 IBM 213.2918243408203
2025-01-23 05:00:00+00:00 IBM 215.94772338867188
2025-01-24 05:00:00+00:00 IBM 214.76309204101562
2025-01-27 05:00:00+00:00 IBM 214.1230010986328
2025-01-28 05:00:00+00:00 IBM 215.58468627929688
2025-01-29 05:00:00+00:00 IBM 218.4220733642578
2025-01-30 05:00:00+00:00 IBM 246.7386932373047
2025-01-31 05:00:00+00:00 IBM 244.28346252441406
There are also a few helper methods directly on the Event:
get_price(asset, price_type="DEFAULT")— the price of one asset, orNoneif not present.get_prices(price_type="DEFAULT")— a dictionary with the prices of all assets.get_volume(asset, volume_type="DEFAULT")— the volume of one asset, orNoneif not present.is_empty()— whether the event contains any items.