Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Time & TimeSeries

Time in roboquant uses the Python datetime object with timezone set to UTC.

For example event.time is always in timezone UTC, even if the event originates from an exchange in another timezone.

Timeframe

A timeframe represents a period in time with a certain start- and end-time. Like other time variables in roboquant, these are Python datetime objects using the UTC timezone.

The start-time of a timeframe is always inclusive, but the end-time can be either inclusive or exclusive.

import roboquant as rq

tf = rq.Timeframe.fromisoformat("2020-01-01", "2024-01-01", inclusive = True)
print(tf)

tf = rq.Timeframe.fromisoformat(
  "2021-01-01T00:12:00+00:00", 
  "2021-10-01T00:13:00+00:00",
  False)
print(tf)
[2020-01-01 00:00:00 ― 2024-01-01 00:00:00]
[2021-01-01 00:12:00 ― 2021-10-01 00:13:00>

You can split timeframes as well as sample from a timeframe, useful in certain types of back test.

tfs = tf.split(5)
assert len(tfs) == 5

tfs = tf.sample(100, "60 days")
assert len(tfs) == 100

Timeline

Timeline is not its own type but just defined as list[datatime].

TimeSeries

TimeSeries implements a multi-variate timeseries. It extends Pandas DataFrame with the index always being a timeline and the columns are always float values.

import pandas as pd
import roboquant as rq

feed = rq.feeds.YahooFeed("IBM", start_date="2020-01-01")
df = feed.to_timeseries(rq.Stock("IBM"))
print("IBM Stock prices", df, sep="\n")

feed = rq.feeds.YahooFeed("IBM", "JPM", "MSFT", "TSLA", "INTC", start_date="2020-01-01")
data = feed.to_timeseries(*feed.assets())
print("Asset correlations:\n", data.corr())

strategy = rq.strategies.EMACrossover()
journal = rq.journals.MetricsJournal.pnl()
account = rq.run(feed, strategy, journal=journal)
equity = journal.get_metrics("pnl/equity")
print("Equity", equity, sep="\n")
Output
IBM Stock prices
                                  IBM
2020-01-02 05:00:00+00:00   98.017075
2020-01-03 05:00:00+00:00   97.235336
2020-01-06 05:00:00+00:00   97.061653
2020-01-07 05:00:00+00:00   97.126778
2020-01-08 05:00:00+00:00   97.937431
...                               ...
2026-08-07 04:00:00+00:00  235.589996
2026-08-10 04:00:00+00:00  236.309998
2026-08-11 04:00:00+00:00  238.419998
2026-08-12 04:00:00+00:00  235.979996
2026-08-13 04:00:00+00:00  237.139999

[1662 rows x 1 columns]
Asset correlations:
           MSFT       JPM       IBM      INTC      TSLA
MSFT  1.000000  0.873256  0.878497 -0.146185  0.707655
JPM   0.873256  1.000000  0.952175  0.149466  0.746711
IBM   0.878497  0.952175  1.000000 -0.008003  0.694219
INTC -0.146185  0.149466 -0.008003  1.000000  0.095438
TSLA  0.707655  0.746711  0.694219  0.095438  1.000000
Equity
                             pnl/equity
2020-01-02 05:00:00+00:00  1.000000e+06
2020-01-03 05:00:00+00:00  1.000000e+06
2020-01-06 05:00:00+00:00  1.000000e+06
2020-01-07 05:00:00+00:00  1.000000e+06
2020-01-08 05:00:00+00:00  1.000000e+06
...                                 ...
2026-08-07 04:00:00+00:00  2.228705e+06
2026-08-10 04:00:00+00:00  2.235396e+06
2026-08-11 04:00:00+00:00  2.238686e+06
2026-08-12 04:00:00+00:00  2.237811e+06
2026-08-13 04:00:00+00:00  2.235310e+06

[1662 rows x 1 columns]