Event
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'), ohlcv=[10.0, 11.0, 9.0, 10.0, 10000], frequency='')
Quote(asset=Stock(symbol='ABC', currency='USD'), data=[11.0, 1000, 10.0, 1000])
TradePrice(asset=Stock(symbol='ABC', currency='USD'), 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.12008666992188
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.96060180664062
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.69346618652344
2025-01-14 05:00:00+00:00 IBM 208.02786254882812
2025-01-15 05:00:00+00:00 IBM 210.2060546875
2025-01-16 05:00:00+00:00 IBM 212.71864318847656
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.29185485839844
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.42208862304688
2025-01-30 05:00:00+00:00 IBM 246.73870849609375
2025-01-31 05:00:00+00:00 IBM 244.283447265625
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.