otp.DataSource#
- class DataSource[source]#
- Bases: - onetick.py.core.source.Source- Construct a source providing data from a given - db.- Parameters
- db (str, list of str, - otp.DB, default=None) – Name(s) of the database or the database object(s).
- symbol (str, list of str, - Source,- query,- eval query, default=- onetick.py.adaptive) – Symbol(s) from which data should be taken.
- tick_type (str, list of str, default= - onetick.py.adaptive) – Tick type of the data. If not specified, all ticks from db will be taken. If ticks can’t be found or there are many databases specified in db then default is “TRD”.
- start ( - datetime.datetime,- otp.datetime,- onetick.py.adaptive, default=- onetick.py.adaptive) – Start of the interval from which the data should be taken. Default is- onetick.py.adaptive, making the final query deduce the time limits from the rest of the graph.
- end ( - datetime.datetime,- otp.datetime,- onetick.py.adaptive, default=- onetick.py.adaptive) – End of the interval from which the data should be taken. Default is- onetick.py.adaptive, making the final query deduce the time limits from the rest of the graph.
- date ( - datetime.datetime,- otp.datetime, optional) – Allows to specify a whole day instead of passing explicitly- startand- endparameters. If it is set along with the- startand- endparameters then last two are ignored.
- schema_policy ('tolerant', 'tolerant_strict', 'fail', 'fail_strict', 'manual', 'manual_strict') – - Schema deduction policy: - ’manual’ The resulting schema is a combination of - desired_schemaand database schema. Compatibility with database schema will not be checked.
- ’manual_strict’ The resulting schema will be exactly - desired_schema. Compatibility with database schema will not be checked.
- ’tolerant’ The resulting schema is a combination of - desired_schemaand database schema. If the database schema can be deduced, it’s checked to be type-compatible with a- desired_schema, and ValueError is raised if checks are failed. Also, with this policy database is scanned 5 days back to find the schema. It is useful when database is misconfigured or in case of holidays.
- ’tolerant_strict’ The resulting schema will be - desired_schemaif it’s not empty. Otherwise, database schema is used. If the database schema can be deduced, it’s checked if it lacks fields from the- desired_schemaand it’s checked to be type-compatible with a- desired_schemaand ValueError is raised if checks are failed. Also, with this policy database is scanned 5 days back to find the schema. It is useful when database is misconfigured or in case of holidays.
- ’fail’ The same as ‘tolerant’, but if the database schema can’t be deduced, raises an Exception. 
- ’fail_strict’ The same as ‘tolerant_strict’, but if the database schema can’t be deduced, raises an Exception. 
 
- guess_schema (bool) – - Deprecated since version 1.3.16. - Use - schema_policyparameter instead.
- identify_input_ts (bool) – If set to False, the fields SYMBOL_NAME and TICK_TYPE are not appended to the output ticks. 
- back_to_first_tick (int, offset, - otp.expr,- Operation) – Determines how far back to go looking for the latest tick before- starttime. If one is found, it is inserted into the output time series with the timestamp set to- starttime. Note: it will be rounded to int, so otp.Millis(999) will be 0 seconds.
- keep_first_tick_timestamp (str) – If set, new field with this name will be added to source. This field contains original timestamp of the tick that was taken from before the start time of the query. For all other ticks value in this field will be equal to the value of Time field. This parameter is ignored if - back_to_first_tickis not set.
- presort (bool, default= - onetick.py.adaptive) – Add the presort EP in case of bound symbols. Applicable only when- symbolsis not None. By default, it is set to True if- symbolsare set and to False otherwise.
- concurrency (int) – Specifies number of CPU cores to utilize for the - presortBy default, the value from otp.config.default_concurrency is used.
- batch_size (int) – Specifies the query batch size for the - presort. By default, the value from otp.config.default_batch_size is used.
- max_back_ticks_to_prepend (int) – When the - back_to_first_tickinterval is specified, this parameter determines the maximum number of the most recent ticks before start_time that will be prepended to the output time series. Their timestamp will be changed to start_time.
- where_clause_for_back_ticks (onetick.py.core.column_operations.base.Raw) – A logical expression that is computed only for the ticks encountered when a query goes back from the start time, in search of the ticks to prepend. If it returns false, a tick is ignored. 
- desired_schema (type[str]) – List of <column name> -> <column type> pairs that the source is expected to have. If the type is irrelevant, provide None as the type in question. 
 
 - Examples - Symbol can be a collection - >>> data = otp.DataSource(db='SOME_DB', tick_type='TT', symbols=['S1', 'S2']) >>> otp.run(data) Time X 0 2003-12-01 00:00:00.000 1 1 2003-12-01 00:00:00.000 -3 2 2003-12-01 00:00:00.001 2 3 2003-12-01 00:00:00.001 -2 4 2003-12-01 00:00:00.002 3 5 2003-12-01 00:00:00.002 -1 - Source also can be passed as symbols, in such case magic named column SYMBOL_NAME will be transform to symbol and all other columns will be symbol parameters - >>> # OTdirective: snippet-name:fetch data.symbols as a source; >>> symbols = otp.Ticks(SYMBOL_NAME=['S1', 'S2']) >>> data = otp.DataSource(db='SOME_DB', symbols=symbols, tick_type='TT') >>> otp.run(data) Time X 0 2003-12-01 00:00:00.000 1 1 2003-12-01 00:00:00.000 -3 2 2003-12-01 00:00:00.001 2 3 2003-12-01 00:00:00.001 -2 4 2003-12-01 00:00:00.002 3 5 2003-12-01 00:00:00.002 -1 - Default schema policy is tolerant. - >>> data = otp.DataSource(db='NYSE_TAQ', tick_type='TRD', symbols='AAPL', PRICE=float, date=otp.dt(2022, 3, 1)) >>> data.schema {'PRICE': <class 'float'>, 'SIZE': <class 'int'>} - >>> data = otp.DataSource(db='NYSE_TAQ', tick_type='TRD', symbols='AAPL', PRICE=int, date=otp.dt(2022, 3, 1)) Traceback (most recent call last): ... ValueError: Database(-s) NYSE_TAQ::TRD schema field PRICE has type <class 'float'>, but <class 'int'> was requested - Schema policy manual uses exactly desired_schema: - >>> data = otp.DataSource(db='NYSE_TAQ', tick_type='TRD', symbols='AAPL', PRICE=float, date=otp.dt(2022, 3, 1), schema_policy='manual') >>> data.schema {'PRICE': <class 'float'>} - Schema policy ‘fail’ raise an exception if the schema cannot be deduced - >>> data = otp.DataSource(db='NYSE_TAQ', tick_type='TRD', symbols='AAPL', date=otp.dt(2021, 3, 1), schema_policy='fail') Traceback (most recent call last): ... Exception: No ticks found in database(-s) NYSE_TAQ::TRD - back_to_first_ticksets how far back to go looking for the latest tick before- starttime: >>> data = otp.DataSource(db=’NYSE_TAQ’, tick_type=’TRD’, symbols=’AAPL’, date=otp.dt(2022, 3, 2), back_to_first_tick=otp.Day(1)) >>> otp.run(data)- Time PRICE SIZE - 0 2022-03-02 00:00:00.000 1.4 50 1 2022-03-02 00:00:00.000 1.0 100 2 2022-03-02 00:00:00.001 1.1 101 3 2022-03-02 00:00:00.002 1.2 102 - keep_first_tick_timestampallows to show the original timestamp of the tick that was taken from before the start time of the query: >>> data = otp.DataSource(db=’NYSE_TAQ’, tick_type=’TRD’, symbols=’AAPL’, date=otp.dt(2022, 3, 2), back_to_first_tick=otp.Day(1), keep_first_tick_timestamp=’ORIGIN_TIMESTAMP’) >>> otp.run(data)- Time PRICE SIZE ORIGIN_TIMESTAMP - 0 2022-03-02 00:00:00.000 1.4 50 2022-03-01 00:00:00.002 1 2022-03-02 00:00:00.000 1.0 100 2022-03-02 00:00:00.000 2 2022-03-02 00:00:00.001 1.1 101 2022-03-02 00:00:00.001 3 2022-03-02 00:00:00.002 1.2 102 2022-03-02 00:00:00.002 - max_back_ticks_to_prependis used with- back_to_first_tickif more than 1 ticks before start time should be retrieved:- >>> data = otp.DataSource(db='NYSE_TAQ', tick_type='TRD', symbols='AAPL', date=otp.dt(2022, 3, 2), max_back_ticks_to_prepend=2, back_to_first_tick=otp.Day(1), keep_first_tick_timestamp='ORIGIN_TIMESTAMP') >>> otp.run(data) Time PRICE SIZE ORIGIN_TIMESTAMP 0 2022-03-02 00:00:00.000 1.4 10 2022-03-01 00:00:00.001 1 2022-03-02 00:00:00.000 1.4 50 2022-03-01 00:00:00.002 2 2022-03-02 00:00:00.000 1.0 100 2022-03-02 00:00:00.000 3 2022-03-02 00:00:00.001 1.1 101 2022-03-02 00:00:00.001 4 2022-03-02 00:00:00.002 1.2 102 2022-03-02 00:00:00.002 - where_clause_for_back_ticksis used to filter out ticks before the start time: >>> data = otp.DataSource(db=’NYSE_TAQ’, tick_type=’TRD’, symbols=’AAPL’, date=otp.dt(2022, 3, 2), where_clause_for_back_ticks=otp.raw(‘SIZE>=50’, dtype=bool), back_to_first_tick=otp.Day(1), max_back_ticks_to_prepend=2, keep_first_tick_timestamp=’ORIGIN_TIMESTAMP’) # doctest: +SKIP >>> otp.run(data) # doctest: +SKIP- Time PRICE SIZE ORIGIN_TIMESTAMP - 0 2022-03-02 00:00:00.000 1.3 100 2022-03-01 00:00:00.000 1 2022-03-02 00:00:00.000 1.4 50 2022-03-01 00:00:00.002 2 2022-03-02 00:00:00.000 1.0 100 2022-03-02 00:00:00.000 3 2022-03-02 00:00:00.001 1.1 101 2022-03-02 00:00:00.001 4 2022-03-02 00:00:00.002 1.2 102 2022-03-02 00:00:00.002