Rendering query graphs#

Rendering onetick-py query is one of the helpful debug techniques. You can use either otp.Source.render_otq for onetick-py query or render_otq function for rendering otq files.

Here’s an example for simple onetick-py query:

from IPython.display import Image
import onetick.py as otp

data = otp.DataSource(db='US_COMP_SAMPLE', tick_type='TRD', symbols='AAPL')
data1, data2 = data[(data['PRICE'] > 50)]
data = otp.merge([data1, data2])
Image(data.render_otq(output_format='png'))
../../_images/563fb606793539420b3f46354b75a37e8fe18c1bc8718a81543717c2a798f4a6.png

Rendering function supports graphs with nested queries:

def agg_fun(source):
    return source.agg({
        'volume': otp.agg.sum('SIZE'),
        'vwap': otp.agg.vwap('PRICE', 'SIZE')
    })

data = otp.DataSource(db='US_COMP_SAMPLE', tick_type='TRD', symbols='AAPL')
data = data[['PRICE', 'SIZE']]
data = otp.agg.generic(agg_fun, bucket_interval=otp.Minute(1)).apply(data)
Image(data.render_otq(output_format='png'))
../../_images/895ea590b4986d654b8edd37313a981c4e692ed63e46280c246e2865119f198c.png

Query with multiple input sources:

start_time = otp.dt(2024, 2, 1, 9, 30)
end_time = otp.dt(2024, 2, 1, 9, 30, 1)

trd = otp.DataSource('US_COMP_SAMPLE', tick_type='TRD')
trd = trd[['PRICE', 'SIZE']]

qte = otp.DataSource('US_COMP_SAMPLE', tick_type='NBBO')
qte = qte[['BID_PRICE', 'ASK_PRICE']]

enh_trd = otp.join_by_time([trd, qte])
Image(enh_trd.render_otq(output_format='png', symbols=['AAPL'], start=start_time, end=end_time))
../../_images/152a0327ebaea0945e77d9478c48b310d1091a04696e952b1c91197c6c10e06e.png

Query with Per Tick Script:

def fun(tick):
    tick['VOLUME'] = tick['PRICE'] * tick['SIZE']
    if tick['SIZE'] >= 1000:
        tick['REQ_SIZE'] = 1
    else:
        tick['REQ_SIZE'] = 0

data = otp.DataSource(db='US_COMP_SAMPLE', tick_type='TRD', symbols='AAPL')
data = data.script(fun)
Image(data.render_otq(output_format='png'))
../../_images/db536a29a2a4c61ef916465c31287b9830308b35421c241be2e11345d5af3483.png