OTQ testing#

onetick.py provides some helpers to test OTQ files.

You can use query from the OTQ file on your local filesystem using otp.query:

import onetick.py as otp
query = otp.query("my.otq::Query")

See query for details.

Adaptive tick type#

You can use adaptive tick type for your ticks to allow it inherit from the query input. You should provide an explicit symbol list in a case of adaptive tick type.

s = otp.Ticks({"PRICE": [1, 2, 3, 4]}, tick_type=adaptive)
query(IN=s)["OUT"].to_dataframe(symbols=["DEMO_L1::AAPL"])

Testing Existed OTQ with Test Provided Ticks#

OTP provides the way to test existed otq with data provided by tests. These tests can be then added to CI pipeline to ensure the code base changes didn’t broke anything.

Use the Bollinger Bands query you’ve created on week 1 day 3 of solution training or download this otq

path = "test_existed.otq::bollinger_bands"
query = otp.query(path,  # provide path to your otq here
                  INTERVAL_UNITS="SECONDS", INTERVAL=3)
data = otp.Ticks(dict(PRICE=[1.45, 1.55, 1.45, 1.30, 1.40],
                      offset=[0, 1000, 2000, 4000, 10_000]))
data = data.apply(query)
df = data.to_df()
print(df)

The result is:

                 Time  PRICE   AVERAGE   STDDEV  LOWER_BAND  UPPER_BAND
0 2003-12-01 00:00:00  1.45   1.450000  0.00000  1.450000    1.450000
1 2003-12-01 00:00:01  1.55   1.500000  0.05000  1.450000    1.550000
2 2003-12-01 00:00:02  1.45   1.483333  0.04714  1.436193    1.530474
3 2003-12-01 00:00:04  1.30   1.375000  0.07500  1.300000    1.450000
4 2003-12-01 00:00:10  1.40   1.400000  0.00000  1.400000    1.400000

The one can use existed otq query with query, where the first parameter is the path to the otq and other parameters are query arguments.

For Bollinger Bands calculation we need only one field - price, so we will create class Ticks with this field only. We can modify timestamp of the tick with magic field offset, it determines the number of milliseconds passed after the first tick, so last one will be 10 seconds late, and because the size of window is 3 seconds (INTERVAL argument of the query) it will be the only tick in the windows, as well as the first tick (by default we use only previous tick in aggregation and no tick from the future).