otp.Source.time_interval_shift#

Source.time_interval_shift(shift, inplace=False)#

Shifting time interval for a source.

The whole data flow is shifted all the way up to the source of the graph.

The start and end times of the query will be changed for all operations before this method, and will stay the same after this method.

WARNING: The ticks’ timestamps are changed automatically so they fit into original time range.

You will get different set of ticks from the database, but the timestamps of the ticks from that database will not be the same as in the database.

They need to be changed so they fit into the original query time range. See details in onetick.py.Source.modify_query_times().

Parameters
  • shift (int or datetime offset) –

    Offset to shift the whole time interval. Can be positive or negative. Positive value moves time interval into the future, negative – to the past. int values are interpreted as milliseconds.

    Timestamps of the ticks will be changed so they fit into the original query time range by subtracting shift from each timestamp.

  • inplace (bool) – The flag controls whether operation should be applied inplace or not. If inplace=True, then it returns nothing. Otherwise method returns a new modified object.

Return type

Source or None

Examples

–> Also see use-case using time_interval_shift() for calculating Markouts

>>> start = otp.dt(2022, 3, 2)
>>> end = otp.dt(2022, 3, 2) + otp.Milli(3)
>>> data = otp.DataSource('NYSE_TAQ', symbols='AAPL', tick_type='TRD')

Default data:

>>> otp.run(data, start=start, end=end)
                     Time  PRICE  SIZE
0 2022-03-02 00:00:00.000    1.0   100
1 2022-03-02 00:00:00.001    1.1   101
2 2022-03-02 00:00:00.002    1.2   102

Get window for a third tick:

>>> otp.run(data, start=start + otp.Milli(2), end=start + otp.Milli(3))
                     Time  PRICE  SIZE
0 2022-03-02 00:00:00.002    1.2   102

Shifting time window will result in different set of ticks, but the ticks will have their timestamps changed to fit into original time range. Let’s shift time 2 milliseconds back and thus get the first tick:

>>> t = data.time_interval_shift(shift=-otp.Milli(2))
>>> otp.run(t, start=start + otp.Milli(2), end=start + otp.Milli(3))
                     Time  PRICE  SIZE
0 2022-03-02 00:00:00.002    1.0   100

Here we are querying empty time interval, but shifting one second back to get ticks.

>>> t = data.time_interval_shift(shift=-otp.Second(1))
>>> otp.run(t, start=start + otp.Second(1), end=end + otp.Second(1))
                     Time  PRICE  SIZE
0 2022-03-02 00:00:01.000    1.0   100
1 2022-03-02 00:00:01.001    1.1   101
2 2022-03-02 00:00:01.002    1.2   102

Note that tick generators otp.Tick and otp.Ticks are not really affected by this method, they will have the same timestamps:

>>> t = otp.Tick(A=1)
>>> otp.run(t)
        Time  A
0 2003-12-01  1
>>> t = t.time_interval_shift(shift=otp.Second(1))
>>> otp.run(t)
        Time  A
0 2003-12-01  1