otp.eval#
- eval(query, symbol=None, start=None, end=None, **kwargs)#
Creates an object with
querywith saved parameters that can be used later.It can be used to:
return a list of symbols for which the main query will be executed (multistage queries). Note that in this case
querymust return ticks with column SYMBOL_NAME.return some value dynamically to be used in other places in the main query. Note that in this case
querymust return only single tick.
Note that only constant expressions are allowed in query parameters, they must not depend on ticks.
- Parameters
query (
onetick.py.Source,onetick.py.queryor function) – source or query to evaluate. If function, then it must returnonetick.py.Sourceoronetick.py.query. Parameter with name symbol and parameters specified inkwargswill be propagated to this function. Parameter fromkwargsmust be specified in function signature, but parameter symbol may be omitted if it is not used.symbol (
_SymbolParamSource) – symbol parameter that will be used byqueryas a symbol. If the function is used as aquery, parameter symbol can be defined in function signature and used in source operations.start (meta field (
MetaFields) or symbol param (_SymbolParamColumn)) – start time with whichquerywill be executed. By default the start time for evaluated query is inherited from the main query.end (meta field (
MetaFields) or symbol param (_SymbolParamColumn)) – end time with whichquerywill be executed. By default the end time for evaluated query is inherited from the main query.kwargs (str, int, meta fields (
MetaFields) or symbol params (_SymbolParamColumn)) – orjoin_with_query()parameters parameters that will be passed toquery. If the function is used as aquery, parameters specified inkwargsmust be defined in function signature and can be used in source operations.
Examples
Use
otp.evalto be passed as symbols when running the query:>>> def fsq(): ... symbols = otp.Ticks(SYMBOL_NAME=['AAPL', 'AAP']) ... return symbols >>> main = otp.DataSource(db='NYSE_TAQ', tick_type='TRD', date=otp.dt(2022, 3, 1)) >>> main['SYMBOL_NAME'] = main.Symbol.name >>> main = otp.merge([main], symbols=otp.eval(fsq)) >>> otp.run(main) Time PRICE SIZE SYMBOL_NAME 0 2022-03-01 00:00:00.000 1.30 100 AAPL 1 2022-03-01 00:00:00.000 45.37 0 AAP 2 2022-03-01 00:00:00.001 1.40 10 AAPL 3 2022-03-01 00:00:00.001 45.41 0 AAP 4 2022-03-01 00:00:00.002 1.40 50 AAPL
Use
otp.evalas filter:>>> def get_filter(a, b): ... return otp.Tick(WHERE=f'X >= {a} and X < {b}', OTHER_FIELD='X') >>> data = otp.Ticks(X=[1, 2, 3]) >>> data, _ = data[otp.eval(get_filter, a=0, b=2)['WHERE']] >>> otp.run(data) Time X 0 2003-12-01 1
Use
otp.evalwith meta fields:>>> def filter_by_tt(tick_type): ... res = otp.Ticks({ ... 'TICK_TYPE': ['TRD', 'QTE'], ... 'WHERE': ['PRICE>=1.4', 'ASK_PRICE>=1.4'] ... }) ... res, _ = res[res['TICK_TYPE'] == tick_type] ... return res.drop(['TICK_TYPE']) >>> t = otp.DataSource('NYSE_TAQ::TRD') >>> t, _ = t[otp.eval(filter_by_tt, tick_type=t['_TICK_TYPE'])] >>> otp.run(t, start=otp.dt(2022, 3, 1), end=otp.dt(2022, 3, 2)) Time PRICE SIZE 0 2022-03-01 00:00:00.001 1.4 10 1 2022-03-01 00:00:00.002 1.4 50