Source Object#
Class _Source
represents Onetick execution
graph. It allows to read, modify, create and write result to database or output
result of the query.
Class _Source
is base class for
Ticks
which is used for ticks construction for tests
(this topic is covered in testing documentation)
and Custom
which is used for reading ticks from
database (it is covered in database documentation).
Source as a Query#
Ticks and Custom classes represent Onetick graph query. Operations are added to the graph via Source methods. Onetick-py wraps event processors and changes data schema accordingly as long as you use onetick-py’s methods.
Note
You can directly sink Onetick event processors to your Source by sink
method. But it is not recommended, because in such case otp can’t apply
schema changes.
Several Sources can be created independently in one session and merged or joined together.
ticks1 = otp.Ticks(dict(x=[1, 2], y=["a", "d"]))
ticks2 = otp.Custom(some_db, symbol=some_symbols, tick_type=some_tick_type)
ticks3 = otp.Empty()
data = otp.funcs.merge([ticks1, ticks2, ticks3])
Take a look at Empty
, it is source with no actual
data, it can be used in some special cases.
While Source construction otp makes no actual operations with data. To execute
the query you should call to_df()
.
You can also use run()
function. They will execute the
query and return it result (if any) as pandas DataFrame (by default) or as a
dictionary where keys are symbols and values are result as pandas DataFrame in
case of multi symbols queries.
Default Fields#
In order to simplify query execution and testing otp fills some values with
default values, such as start and and time and symbols. You can refer to them
in your queries as otp.config.default_start_time
, otp.config.default_end_time
and
otp.config.default_symbol
.
Source Schema#
Otp changes data schema according to data and specified operations, but in some
cases it can’t do so, e.g. you’ve applied complex external query via
apply()
method or some event processors
were added to the graph via sink()
method.
For schema accessing and modification
schema
property is used.
import onetick.py as otp
to_init = dict(PRICE=float)
to_update = dict(QTY=int)
data = otp.Empty(**to_init)
assert data.schema == to_init
data.schema.update(**to_update)
print(data.schema)
{'PRICE': <class 'float'>, 'QTY': <class 'int'>}
Warning
schema
property doesn’t modify data, it changes its python
representation only. Use table()
if
you want to change actual data schema.
Symbol Field#
Source can’t refer to symbol it was called with via
Symbol
field. Symbol itself
(_SYMBOL_NAME
Onetick pseudo field) can be accessed via name
property,
while other symbols are accessible via their names.
Note
All symbol parameters are passed to the query as string, you should convert them explicitly.
import onetick.py as otp
from pprint import pprint
symbols = otp.Ticks(SYMBOL_NAME=["A", "B"], param=[1, 2])
data = otp.Ticks(X=[1, 2])
data["S"] = 2 * data.Symbol.name.str.len() + data.Symbol.param.astype(int)
dfs = data.to_df(symbols=symbols)
pprint(dfs)
{'A': Time X S
0 2003-12-01 00:00:00.000 1 3
1 2003-12-01 00:00:00.001 2 3,
'B': Time X S
0 2003-12-01 00:00:00.000 1 4
1 2003-12-01 00:00:00.001 2 4}