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/266a6fe0837135143aabb134a3685c2345c96370c4382659b12e53448a0d9643.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/c6f2810dcbe2a643fc09fa1a3bf60b46a3af20ed1cfb2e78078c38367a9cc46e.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/aa868d2fabb14dd29c8bd48a5ea6483bf378bebfc86644359c99870ef04e3300.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/9c699e2701209d508413602927b61a9db731540a25800cc0678594592907ea54.png