Time
Overview¶
Time related data in roboquant uses the Python datetime object with
the timezone set to UTC.
For example event.time is always in timezone UTC, even if the event originates
from an exchange in a different timezone. This makes it fast and robust when
comparing dates.
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) == 100Timeline¶
Timeline implements a list of datetime objects. Like the other time variables,
the individual entries are Python datetime objects using the UTC timezone.
Timeline entries should be added in ascending order. This makes it suitable
to act as the index of a TimeSeries.
from datetime import datetime, timezone
import roboquant as rq
timeline = rq.Timeline()
timeline.append(datetime(2024, 1, 1, tzinfo=timezone.utc))
timeline.append(datetime(2024, 1, 2, tzinfo=timezone.utc))
print(len(timeline))
print(timeline[0])2
2024-01-01 00:00:00+00:00
Typically you don’t create timelines manually, but rather they are returned as the result of a method call.
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.017044
2020-01-03 05:00:00+00:00 97.235359
2020-01-06 05:00:00+00:00 97.061638
2020-01-07 05:00:00+00:00 97.126778
2020-01-08 05:00:00+00:00 97.937431
... ...
2026-09-24 04:00:00+00:00 227.059998
2026-09-25 04:00:00+00:00 225.509995
2026-09-28 04:00:00+00:00 220.669998
2026-09-29 04:00:00+00:00 219.990005
2026-09-30 04:00:00+00:00 219.929993
[1695 rows x 1 columns]
Asset correlations:
INTC TSLA MSFT IBM JPM
INTC 1.000000 0.144592 -0.033631 0.058141 0.255665
TSLA 0.144592 1.000000 0.714278 0.701025 0.746224
MSFT -0.033631 0.714278 1.000000 0.880512 0.879392
IBM 0.058141 0.701025 0.880512 1.000000 0.942873
JPM 0.255665 0.746224 0.879392 0.942873 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-09-24 04:00:00+00:00 2.319510e+06
2026-09-25 04:00:00+00:00 2.359148e+06
2026-09-28 04:00:00+00:00 2.318860e+06
2026-09-29 04:00:00+00:00 2.292761e+06
2026-09-30 04:00:00+00:00 2.286357e+06
[1695 rows x 1 columns]