onetick-py 1.177.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- locator_parser/__init__.py +0 -0
- locator_parser/acl.py +73 -0
- locator_parser/actions.py +262 -0
- locator_parser/common.py +368 -0
- locator_parser/io.py +43 -0
- locator_parser/locator.py +150 -0
- onetick/__init__.py +101 -0
- onetick/doc_utilities/__init__.py +3 -0
- onetick/doc_utilities/napoleon.py +40 -0
- onetick/doc_utilities/ot_doctest.py +140 -0
- onetick/doc_utilities/snippets.py +279 -0
- onetick/lib/__init__.py +4 -0
- onetick/lib/instance.py +141 -0
- onetick/py/__init__.py +293 -0
- onetick/py/_stack_info.py +89 -0
- onetick/py/_version.py +2 -0
- onetick/py/aggregations/__init__.py +11 -0
- onetick/py/aggregations/_base.py +648 -0
- onetick/py/aggregations/_docs.py +948 -0
- onetick/py/aggregations/compute.py +286 -0
- onetick/py/aggregations/functions.py +2216 -0
- onetick/py/aggregations/generic.py +104 -0
- onetick/py/aggregations/high_low.py +80 -0
- onetick/py/aggregations/num_distinct.py +83 -0
- onetick/py/aggregations/order_book.py +501 -0
- onetick/py/aggregations/other.py +1014 -0
- onetick/py/backports.py +26 -0
- onetick/py/cache.py +374 -0
- onetick/py/callback/__init__.py +5 -0
- onetick/py/callback/callback.py +276 -0
- onetick/py/callback/callbacks.py +131 -0
- onetick/py/compatibility.py +798 -0
- onetick/py/configuration.py +771 -0
- onetick/py/core/__init__.py +0 -0
- onetick/py/core/_csv_inspector.py +93 -0
- onetick/py/core/_internal/__init__.py +0 -0
- onetick/py/core/_internal/_manually_bound_value.py +6 -0
- onetick/py/core/_internal/_nodes_history.py +250 -0
- onetick/py/core/_internal/_op_utils/__init__.py +0 -0
- onetick/py/core/_internal/_op_utils/every_operand.py +9 -0
- onetick/py/core/_internal/_op_utils/is_const.py +10 -0
- onetick/py/core/_internal/_per_tick_scripts/tick_list_sort_template.script +121 -0
- onetick/py/core/_internal/_proxy_node.py +140 -0
- onetick/py/core/_internal/_state_objects.py +2312 -0
- onetick/py/core/_internal/_state_vars.py +93 -0
- onetick/py/core/_source/__init__.py +0 -0
- onetick/py/core/_source/_symbol_param.py +95 -0
- onetick/py/core/_source/schema.py +97 -0
- onetick/py/core/_source/source_methods/__init__.py +0 -0
- onetick/py/core/_source/source_methods/aggregations.py +809 -0
- onetick/py/core/_source/source_methods/applyers.py +296 -0
- onetick/py/core/_source/source_methods/columns.py +141 -0
- onetick/py/core/_source/source_methods/data_quality.py +301 -0
- onetick/py/core/_source/source_methods/debugs.py +272 -0
- onetick/py/core/_source/source_methods/drops.py +120 -0
- onetick/py/core/_source/source_methods/fields.py +619 -0
- onetick/py/core/_source/source_methods/filters.py +1002 -0
- onetick/py/core/_source/source_methods/joins.py +1413 -0
- onetick/py/core/_source/source_methods/merges.py +605 -0
- onetick/py/core/_source/source_methods/misc.py +1455 -0
- onetick/py/core/_source/source_methods/pandases.py +155 -0
- onetick/py/core/_source/source_methods/renames.py +356 -0
- onetick/py/core/_source/source_methods/sorts.py +183 -0
- onetick/py/core/_source/source_methods/switches.py +142 -0
- onetick/py/core/_source/source_methods/symbols.py +117 -0
- onetick/py/core/_source/source_methods/times.py +627 -0
- onetick/py/core/_source/source_methods/writes.py +986 -0
- onetick/py/core/_source/symbol.py +205 -0
- onetick/py/core/_source/tmp_otq.py +222 -0
- onetick/py/core/column.py +209 -0
- onetick/py/core/column_operations/__init__.py +0 -0
- onetick/py/core/column_operations/_methods/__init__.py +4 -0
- onetick/py/core/column_operations/_methods/_internal.py +28 -0
- onetick/py/core/column_operations/_methods/conversions.py +216 -0
- onetick/py/core/column_operations/_methods/methods.py +292 -0
- onetick/py/core/column_operations/_methods/op_types.py +160 -0
- onetick/py/core/column_operations/accessors/__init__.py +0 -0
- onetick/py/core/column_operations/accessors/_accessor.py +28 -0
- onetick/py/core/column_operations/accessors/decimal_accessor.py +104 -0
- onetick/py/core/column_operations/accessors/dt_accessor.py +537 -0
- onetick/py/core/column_operations/accessors/float_accessor.py +184 -0
- onetick/py/core/column_operations/accessors/str_accessor.py +1367 -0
- onetick/py/core/column_operations/base.py +1121 -0
- onetick/py/core/cut_builder.py +150 -0
- onetick/py/core/db_constants.py +20 -0
- onetick/py/core/eval_query.py +245 -0
- onetick/py/core/lambda_object.py +441 -0
- onetick/py/core/multi_output_source.py +232 -0
- onetick/py/core/per_tick_script.py +2256 -0
- onetick/py/core/query_inspector.py +464 -0
- onetick/py/core/source.py +1744 -0
- onetick/py/db/__init__.py +2 -0
- onetick/py/db/_inspection.py +1128 -0
- onetick/py/db/db.py +1327 -0
- onetick/py/db/utils.py +64 -0
- onetick/py/docs/__init__.py +0 -0
- onetick/py/docs/docstring_parser.py +112 -0
- onetick/py/docs/utils.py +81 -0
- onetick/py/functions.py +2398 -0
- onetick/py/license.py +190 -0
- onetick/py/log.py +88 -0
- onetick/py/math.py +935 -0
- onetick/py/misc.py +470 -0
- onetick/py/oqd/__init__.py +22 -0
- onetick/py/oqd/eps.py +1195 -0
- onetick/py/oqd/sources.py +325 -0
- onetick/py/otq.py +216 -0
- onetick/py/pyomd_mock.py +47 -0
- onetick/py/run.py +916 -0
- onetick/py/servers.py +173 -0
- onetick/py/session.py +1347 -0
- onetick/py/sources/__init__.py +19 -0
- onetick/py/sources/cache.py +167 -0
- onetick/py/sources/common.py +128 -0
- onetick/py/sources/csv.py +642 -0
- onetick/py/sources/custom.py +85 -0
- onetick/py/sources/data_file.py +305 -0
- onetick/py/sources/data_source.py +1045 -0
- onetick/py/sources/empty.py +94 -0
- onetick/py/sources/odbc.py +337 -0
- onetick/py/sources/order_book.py +271 -0
- onetick/py/sources/parquet.py +168 -0
- onetick/py/sources/pit.py +191 -0
- onetick/py/sources/query.py +495 -0
- onetick/py/sources/snapshots.py +419 -0
- onetick/py/sources/split_query_output_by_symbol.py +198 -0
- onetick/py/sources/symbology_mapping.py +123 -0
- onetick/py/sources/symbols.py +374 -0
- onetick/py/sources/ticks.py +825 -0
- onetick/py/sql.py +70 -0
- onetick/py/state.py +251 -0
- onetick/py/types.py +2131 -0
- onetick/py/utils/__init__.py +70 -0
- onetick/py/utils/acl.py +93 -0
- onetick/py/utils/config.py +186 -0
- onetick/py/utils/default.py +49 -0
- onetick/py/utils/file.py +38 -0
- onetick/py/utils/helpers.py +76 -0
- onetick/py/utils/locator.py +94 -0
- onetick/py/utils/perf.py +498 -0
- onetick/py/utils/query.py +49 -0
- onetick/py/utils/render.py +1374 -0
- onetick/py/utils/script.py +244 -0
- onetick/py/utils/temp.py +471 -0
- onetick/py/utils/types.py +120 -0
- onetick/py/utils/tz.py +84 -0
- onetick_py-1.177.0.dist-info/METADATA +137 -0
- onetick_py-1.177.0.dist-info/RECORD +152 -0
- onetick_py-1.177.0.dist-info/WHEEL +5 -0
- onetick_py-1.177.0.dist-info/entry_points.txt +2 -0
- onetick_py-1.177.0.dist-info/licenses/LICENSE +21 -0
- onetick_py-1.177.0.dist-info/top_level.txt +2 -0
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
from typing import Iterable, Callable
|
|
2
|
+
|
|
3
|
+
import onetick.py as otp
|
|
4
|
+
from onetick.py.docs.utils import docstring
|
|
5
|
+
|
|
6
|
+
from ..aggregations.order_book import (
|
|
7
|
+
OB_SNAPSHOT_DOC_PARAMS,
|
|
8
|
+
OB_SNAPSHOT_WIDE_DOC_PARAMS,
|
|
9
|
+
OB_SNAPSHOT_FLAT_DOC_PARAMS,
|
|
10
|
+
OB_SUMMARY_DOC_PARAMS,
|
|
11
|
+
OB_SIZE_DOC_PARAMS,
|
|
12
|
+
OB_VWAP_DOC_PARAMS,
|
|
13
|
+
OB_NUM_LEVELS_DOC_PARAMS,
|
|
14
|
+
)
|
|
15
|
+
from ..aggregations.functions import (
|
|
16
|
+
ob_snapshot, ob_snapshot_wide, ob_snapshot_flat, ob_summary, ob_size, ob_vwap, ob_num_levels,
|
|
17
|
+
)
|
|
18
|
+
from .. import utils
|
|
19
|
+
|
|
20
|
+
from .data_source import DataSource, DATA_SOURCE_DOC_PARAMS
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class _ObSource(DataSource):
|
|
24
|
+
OB_AGG_FUNC: Callable
|
|
25
|
+
OB_AGG_PARAMS: Iterable
|
|
26
|
+
_PROPERTIES = DataSource._PROPERTIES + ['_ob_agg']
|
|
27
|
+
|
|
28
|
+
def __init__(self, db=None, schema=None, **kwargs):
|
|
29
|
+
if self._try_default_constructor(schema=schema, **kwargs):
|
|
30
|
+
return
|
|
31
|
+
|
|
32
|
+
ob_agg_params = {
|
|
33
|
+
param.name: kwargs.pop(param.name, param.default)
|
|
34
|
+
for _, param in self.OB_AGG_PARAMS
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
symbol_param = kwargs.get('symbol')
|
|
38
|
+
symbols_param = kwargs.get('symbols')
|
|
39
|
+
|
|
40
|
+
if symbol_param and symbols_param:
|
|
41
|
+
raise ValueError(
|
|
42
|
+
'You have set the `symbol` and `symbols` parameters together, it is not allowed. '
|
|
43
|
+
'Please, clarify parameters'
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
symbols = symbol_param if symbol_param else symbols_param
|
|
47
|
+
tmp_otq = None
|
|
48
|
+
|
|
49
|
+
# Use bound symbols only in case, if db not passed
|
|
50
|
+
use_bound_symbols = not db and symbols and symbols is not utils.adaptive
|
|
51
|
+
if use_bound_symbols:
|
|
52
|
+
symbols, tmp_otq = self._cross_symbol_convert(symbols, kwargs.get('symbol_date'))
|
|
53
|
+
|
|
54
|
+
if symbols_param:
|
|
55
|
+
del kwargs['symbols']
|
|
56
|
+
|
|
57
|
+
kwargs['symbol'] = None
|
|
58
|
+
|
|
59
|
+
self._ob_agg = self.__class__.OB_AGG_FUNC(**ob_agg_params)
|
|
60
|
+
|
|
61
|
+
if kwargs.get('schema_policy') in [DataSource.POLICY_MANUAL, DataSource.POLICY_MANUAL_STRICT]:
|
|
62
|
+
self._ob_agg.disable_ob_input_columns_validation()
|
|
63
|
+
|
|
64
|
+
if use_bound_symbols:
|
|
65
|
+
self._ob_agg.set_bound_symbols(symbols)
|
|
66
|
+
|
|
67
|
+
super().__init__(db=db, schema=schema, **kwargs)
|
|
68
|
+
|
|
69
|
+
ob_agg_output_schema = self._ob_agg._get_output_schema(otp.Empty())
|
|
70
|
+
|
|
71
|
+
if getattr(self._ob_agg, 'show_full_detail', None):
|
|
72
|
+
self.schema.update(**ob_agg_output_schema)
|
|
73
|
+
else:
|
|
74
|
+
self.schema.set(**ob_agg_output_schema)
|
|
75
|
+
|
|
76
|
+
if tmp_otq:
|
|
77
|
+
self._tmp_otq.merge(tmp_otq)
|
|
78
|
+
|
|
79
|
+
def base_ep(self, *args, **kwargs):
|
|
80
|
+
src = super().base_ep(*args, **kwargs)
|
|
81
|
+
return self._ob_agg.apply(src)
|
|
82
|
+
|
|
83
|
+
def _base_ep_for_cross_symbol(self, *args, **kwargs):
|
|
84
|
+
src = super()._base_ep_for_cross_symbol(*args, **kwargs)
|
|
85
|
+
return self._ob_agg.apply(src)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
@docstring(parameters=OB_SNAPSHOT_DOC_PARAMS + DATA_SOURCE_DOC_PARAMS)
|
|
89
|
+
class ObSnapshot(_ObSource):
|
|
90
|
+
"""
|
|
91
|
+
Construct a source providing order book snapshot for a given ``db``.
|
|
92
|
+
This is just a shortcut for
|
|
93
|
+
:class:`~onetick.py.DataSource` + :func:`~onetick.py.agg.ob_snapshot`.
|
|
94
|
+
|
|
95
|
+
See also
|
|
96
|
+
--------
|
|
97
|
+
| :class:`onetick.py.DataSource`
|
|
98
|
+
| :meth:`onetick.py.Source.ob_snapshot`
|
|
99
|
+
| :func:`onetick.py.agg.ob_snapshot`
|
|
100
|
+
| **OB_SNAPSHOT** OneTick event processor
|
|
101
|
+
|
|
102
|
+
Examples
|
|
103
|
+
---------
|
|
104
|
+
|
|
105
|
+
>>> data = otp.ObSnapshot(db='SOME_DB', tick_type='PRL', symbols='AA', max_levels=1) # doctest: +SKIP
|
|
106
|
+
>>> otp.run(data) # doctest: +SKIP
|
|
107
|
+
Time PRICE UPDATE_TIME SIZE LEVEL BUY_SELL_FLAG
|
|
108
|
+
0 2003-12-04 2.0 2003-12-01 00:00:00.003 6 1 1
|
|
109
|
+
1 2003-12-04 5.0 2003-12-01 00:00:00.004 7 1 0
|
|
110
|
+
"""
|
|
111
|
+
OB_AGG_FUNC = ob_snapshot
|
|
112
|
+
OB_AGG_PARAMS = OB_SNAPSHOT_DOC_PARAMS
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
@docstring(parameters=OB_SNAPSHOT_WIDE_DOC_PARAMS + DATA_SOURCE_DOC_PARAMS)
|
|
116
|
+
class ObSnapshotWide(_ObSource):
|
|
117
|
+
"""
|
|
118
|
+
Construct a source providing order book wide snapshot for a given ``db``.
|
|
119
|
+
This is just a shortcut for
|
|
120
|
+
:class:`~onetick.py.DataSource` + :func:`~onetick.py.agg.ob_snapshot_wide`.
|
|
121
|
+
|
|
122
|
+
See also
|
|
123
|
+
--------
|
|
124
|
+
| :class:`onetick.py.DataSource`
|
|
125
|
+
| :meth:`onetick.py.Source.ob_snapshot_wide`
|
|
126
|
+
| :func:`onetick.py.agg.ob_snapshot_wide`
|
|
127
|
+
| **OB_SNAPSHOT_WIDE** OneTick event processor
|
|
128
|
+
|
|
129
|
+
Examples
|
|
130
|
+
---------
|
|
131
|
+
|
|
132
|
+
>>> data = otp.ObSnapshotWide(db='SOME_DB', tick_type='PRL', symbols='AA', max_levels=1) # doctest: +SKIP
|
|
133
|
+
>>> otp.run(data) # doctest: +SKIP
|
|
134
|
+
Time BID_PRICE BID_UPDATE_TIME BID_SIZE ASK_PRICE ASK_UPDATE_TIME ASK_SIZE LEVEL
|
|
135
|
+
0 2003-12-03 5.0 2003-12-01 00:00:00.004 7 2.0 2003-12-01 00:00:00.003 6 1
|
|
136
|
+
"""
|
|
137
|
+
OB_AGG_FUNC = ob_snapshot_wide
|
|
138
|
+
OB_AGG_PARAMS = OB_SNAPSHOT_WIDE_DOC_PARAMS
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
@docstring(parameters=OB_SNAPSHOT_FLAT_DOC_PARAMS + DATA_SOURCE_DOC_PARAMS)
|
|
142
|
+
class ObSnapshotFlat(_ObSource):
|
|
143
|
+
"""
|
|
144
|
+
Construct a source providing order book flat snapshot for a given ``db``.
|
|
145
|
+
This is just a shortcut for
|
|
146
|
+
:class:`~onetick.py.DataSource` + :func:`~onetick.py.agg.ob_snapshot_flat`.
|
|
147
|
+
|
|
148
|
+
See also
|
|
149
|
+
--------
|
|
150
|
+
| :class:`onetick.py.DataSource`
|
|
151
|
+
| :meth:`onetick.py.Source.ob_snapshot_flat`
|
|
152
|
+
| :func:`onetick.py.agg.ob_snapshot_flat`
|
|
153
|
+
| **OB_SNAPSHOT_FLAT** OneTick event processor
|
|
154
|
+
|
|
155
|
+
Examples
|
|
156
|
+
---------
|
|
157
|
+
|
|
158
|
+
>>> data = otp.ObSnapshotFlat(db='SOME_DB', tick_type='PRL', symbols='AA', max_levels=1) # doctest: +SKIP
|
|
159
|
+
>>> otp.run(data) # doctest: +SKIP
|
|
160
|
+
Time BID_PRICE1 BID_UPDATE_TIME1 BID_SIZE1 ASK_PRICE1 ASK_UPDATE_TIME1 ASK_SIZE1
|
|
161
|
+
0 2003-12-03 5.0 2003-12-01 00:00:00.004 7 2.0 2003-12-01 00:00:00.003 6
|
|
162
|
+
"""
|
|
163
|
+
OB_AGG_FUNC = ob_snapshot_flat
|
|
164
|
+
OB_AGG_PARAMS = OB_SNAPSHOT_FLAT_DOC_PARAMS
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
@docstring(parameters=OB_SUMMARY_DOC_PARAMS + DATA_SOURCE_DOC_PARAMS)
|
|
168
|
+
class ObSummary(_ObSource):
|
|
169
|
+
"""
|
|
170
|
+
Construct a source providing order book summary for a given ``db``.
|
|
171
|
+
This is just a shortcut for
|
|
172
|
+
:class:`~onetick.py.DataSource` + :func:`~onetick.py.agg.ob_summary`.
|
|
173
|
+
|
|
174
|
+
See also
|
|
175
|
+
--------
|
|
176
|
+
| :class:`onetick.py.DataSource`
|
|
177
|
+
| :meth:`onetick.py.Source.ob_summary`
|
|
178
|
+
| :func:`onetick.py.agg.ob_summary`
|
|
179
|
+
| **OB_SUMMARY** OneTick event processor
|
|
180
|
+
|
|
181
|
+
Examples
|
|
182
|
+
---------
|
|
183
|
+
|
|
184
|
+
>>> data = otp.ObSummary(db='SOME_DB', tick_type='PRL', symbols='AA', max_levels=1) # doctest: +SKIP
|
|
185
|
+
>>> otp.run(data) # doctest: +SKIP
|
|
186
|
+
Time BID_PRICE BID_SIZE BID_VWAP BEST_BID_PRICE WORST_BID_SIZE NUM_BID_LEVELS ASK_SIZE\
|
|
187
|
+
ASK_VWAP BEST_ASK_PRICE WORST_ASK_PRICE NUM_ASK_LEVELS
|
|
188
|
+
0 2003-12-04 NaN 7 5.0 5.0 NaN 1 6\
|
|
189
|
+
2.0 2.0 2.0 1
|
|
190
|
+
"""
|
|
191
|
+
OB_AGG_FUNC = ob_summary
|
|
192
|
+
OB_AGG_PARAMS = OB_SUMMARY_DOC_PARAMS
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
@docstring(parameters=OB_SIZE_DOC_PARAMS + DATA_SOURCE_DOC_PARAMS)
|
|
196
|
+
class ObSize(_ObSource):
|
|
197
|
+
"""
|
|
198
|
+
Construct a source providing number of order book levels for a given ``db``.
|
|
199
|
+
This is just a shortcut for
|
|
200
|
+
:class:`~onetick.py.DataSource` + :func:`~onetick.py.agg.ob_size`.
|
|
201
|
+
|
|
202
|
+
See also
|
|
203
|
+
--------
|
|
204
|
+
| :class:`onetick.py.DataSource`
|
|
205
|
+
| :meth:`onetick.py.Source.ob_size`
|
|
206
|
+
| :func:`onetick.py.agg.ob_size`
|
|
207
|
+
| **OB_SIZE** OneTick event processor
|
|
208
|
+
|
|
209
|
+
Examples
|
|
210
|
+
---------
|
|
211
|
+
|
|
212
|
+
>>> data = otp.ObSize(db='SOME_DB', tick_type='PRL', symbols='AA', max_levels=10) # doctest: +SKIP
|
|
213
|
+
>>> otp.run(data) # doctest: +SKIP
|
|
214
|
+
Time ASK_VALUE BID_VALUE
|
|
215
|
+
0 2003-12-01 84800 64500
|
|
216
|
+
"""
|
|
217
|
+
OB_AGG_FUNC = ob_size
|
|
218
|
+
OB_AGG_PARAMS = OB_SIZE_DOC_PARAMS
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
@docstring(parameters=OB_VWAP_DOC_PARAMS + DATA_SOURCE_DOC_PARAMS)
|
|
222
|
+
class ObVwap(_ObSource):
|
|
223
|
+
"""
|
|
224
|
+
Construct a source providing the size-weighted price
|
|
225
|
+
computed over a specified number of order book levels for a given ``db``.
|
|
226
|
+
This is just a shortcut for
|
|
227
|
+
:class:`~onetick.py.DataSource` + :func:`~onetick.py.agg.ob_vwap`.
|
|
228
|
+
|
|
229
|
+
See also
|
|
230
|
+
--------
|
|
231
|
+
| :class:`onetick.py.DataSource`
|
|
232
|
+
| :meth:`onetick.py.Source.ob_vwap`
|
|
233
|
+
| :func:`onetick.py.agg.ob_vwap`
|
|
234
|
+
| **OB_VWAP** OneTick event processor
|
|
235
|
+
|
|
236
|
+
Examples
|
|
237
|
+
---------
|
|
238
|
+
|
|
239
|
+
>>> data = otp.ObVwap(db='SOME_DB', tick_type='PRL', symbols='AA', max_levels=10) # doctest: +SKIP
|
|
240
|
+
>>> otp.run(data) # doctest: +SKIP
|
|
241
|
+
Time ASK_VALUE BID_VALUE
|
|
242
|
+
0 2003-12-01 23.313 23.20848
|
|
243
|
+
"""
|
|
244
|
+
OB_AGG_FUNC = ob_vwap
|
|
245
|
+
OB_AGG_PARAMS = OB_VWAP_DOC_PARAMS
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
@docstring(parameters=OB_NUM_LEVELS_DOC_PARAMS + DATA_SOURCE_DOC_PARAMS)
|
|
249
|
+
class ObNumLevels(_ObSource):
|
|
250
|
+
"""
|
|
251
|
+
Construct a source providing the number of levels in the order book for a given ``db``.
|
|
252
|
+
This is just a shortcut for
|
|
253
|
+
:class:`~onetick.py.DataSource` + :func:`~onetick.py.agg.ob_num_levels`.
|
|
254
|
+
|
|
255
|
+
See also
|
|
256
|
+
--------
|
|
257
|
+
| :class:`onetick.py.DataSource`
|
|
258
|
+
| :meth:`onetick.py.Source.ob_num_levels`
|
|
259
|
+
| :func:`onetick.py.agg.ob_num_levels`
|
|
260
|
+
| **OB_NUM_LEVELS** OneTick event processor
|
|
261
|
+
|
|
262
|
+
Examples
|
|
263
|
+
---------
|
|
264
|
+
|
|
265
|
+
>>> data = otp.ObNumLevels(db='SOME_DB', tick_type='PRL', symbols='AA') # doctest: +SKIP
|
|
266
|
+
>>> otp.run(data) # doctest: +SKIP
|
|
267
|
+
Time ASK_VALUE BID_VALUE
|
|
268
|
+
0 2003-12-01 248 67
|
|
269
|
+
"""
|
|
270
|
+
OB_AGG_FUNC = ob_num_levels
|
|
271
|
+
OB_AGG_PARAMS = OB_NUM_LEVELS_DOC_PARAMS
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import onetick.py as otp
|
|
2
|
+
from onetick.py.otq import otq
|
|
3
|
+
|
|
4
|
+
from onetick.py.core.source import Source
|
|
5
|
+
|
|
6
|
+
from .. import utils
|
|
7
|
+
|
|
8
|
+
from .common import update_node_tick_type
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class ReadParquet(Source):
|
|
12
|
+
def __init__(
|
|
13
|
+
self,
|
|
14
|
+
parquet_file_path=None,
|
|
15
|
+
where=None,
|
|
16
|
+
time_assignment="end",
|
|
17
|
+
discard_fields=None,
|
|
18
|
+
fields=None,
|
|
19
|
+
symbol_name_field=None,
|
|
20
|
+
symbol=utils.adaptive,
|
|
21
|
+
db=utils.adaptive_to_default,
|
|
22
|
+
tick_type=utils.adaptive,
|
|
23
|
+
start=utils.adaptive,
|
|
24
|
+
end=utils.adaptive,
|
|
25
|
+
schema=None,
|
|
26
|
+
**kwargs,
|
|
27
|
+
):
|
|
28
|
+
"""
|
|
29
|
+
Read ticks from Parquet file
|
|
30
|
+
|
|
31
|
+
Parameters
|
|
32
|
+
----------
|
|
33
|
+
parquet_file_path: str
|
|
34
|
+
Specifies the path or URL of the Parquet file to read.
|
|
35
|
+
where: str, None
|
|
36
|
+
Specifies a criterion for selecting the ticks to propagate.
|
|
37
|
+
time_assignment: str
|
|
38
|
+
Timestamps of the ticks created by `ReadParquet` are set to the start/end of the query or to the given
|
|
39
|
+
tick field depending on the `time_assignment` parameter.
|
|
40
|
+
Possible values are `start` and `end` (for `_START_TIME` and `_END_TIME`) or a field name.
|
|
41
|
+
Default: `end`
|
|
42
|
+
discard_fields: list, str, None
|
|
43
|
+
A list of fields (`list` or comma-separated string) to be discarded from the output ticks.
|
|
44
|
+
fields: list, str, None
|
|
45
|
+
A list of fields (`list` or comma-separated string) to be picked from the output ticks.
|
|
46
|
+
The opposite to `discard_fields`.
|
|
47
|
+
symbol_name_field: str, None
|
|
48
|
+
Field that is expected to contain the symbol name.
|
|
49
|
+
When this parameter is set and one or more symbols containing time series
|
|
50
|
+
(i.e. `[dbname]::[time series name]` and not just `[dbname]::`) are bound to the query or to this EP,
|
|
51
|
+
only rows belonging to those symbols will be propagated.
|
|
52
|
+
symbol: str, list of str, :class:`Source`, :class:`query`, :py:func:`eval query <onetick.py.eval>`
|
|
53
|
+
Symbol(s) from which data should be taken.
|
|
54
|
+
tick_type: str
|
|
55
|
+
Tick type.
|
|
56
|
+
Default: ANY.
|
|
57
|
+
start: :py:class:`otp.datetime <onetick.py.datetime>`
|
|
58
|
+
Start time for tick generation. By default the start time of the query will be used.
|
|
59
|
+
end: :py:class:`otp.datetime <onetick.py.datetime>`
|
|
60
|
+
End time for tick generation. By default the end time of the query will be used.
|
|
61
|
+
schema: dict
|
|
62
|
+
Dictionary of columns names with their types.
|
|
63
|
+
You should set schema manually, if you want to use fields in `onetick-py` query description
|
|
64
|
+
before its execution.
|
|
65
|
+
kwargs:
|
|
66
|
+
Deprecated. Use ``schema`` instead.
|
|
67
|
+
Dictionary of columns names with their types.
|
|
68
|
+
|
|
69
|
+
See also
|
|
70
|
+
--------
|
|
71
|
+
| **READ_FROM_PARQUET** OneTick event processor
|
|
72
|
+
| :py:meth:`onetick.py.Source.write_parquet`
|
|
73
|
+
|
|
74
|
+
Examples
|
|
75
|
+
--------
|
|
76
|
+
Simple Parquet file read:
|
|
77
|
+
|
|
78
|
+
>>> data = otp.ReadParquet("/path/to/parquet/file")
|
|
79
|
+
>>> otp.run(data) # doctest: +SKIP
|
|
80
|
+
|
|
81
|
+
Read Parquet file and filter fields:
|
|
82
|
+
|
|
83
|
+
>>> data = otp.ReadParquet("/path/to/parquet/file", fields=["some_field", "another_field"])
|
|
84
|
+
>>> otp.run(data) # doctest: +SKIP
|
|
85
|
+
|
|
86
|
+
Read Parquet file and filter rows:
|
|
87
|
+
|
|
88
|
+
>>> data = otp.ReadParquet("/path/to/parquet/file", where="PRICE > 20")
|
|
89
|
+
>>> otp.run(data) # doctest: +SKIP
|
|
90
|
+
"""
|
|
91
|
+
if self._try_default_constructor(schema=schema, **kwargs):
|
|
92
|
+
return
|
|
93
|
+
|
|
94
|
+
if parquet_file_path is None:
|
|
95
|
+
raise ValueError("Missing required parameter `parquet_file_path`")
|
|
96
|
+
|
|
97
|
+
if discard_fields and fields:
|
|
98
|
+
raise ValueError("`discard_fields` and `fields` cannot be specified at the same time")
|
|
99
|
+
|
|
100
|
+
if where is None:
|
|
101
|
+
where = ""
|
|
102
|
+
|
|
103
|
+
if discard_fields is None:
|
|
104
|
+
discard_fields = ""
|
|
105
|
+
elif isinstance(discard_fields, list):
|
|
106
|
+
discard_fields = ",".join(discard_fields)
|
|
107
|
+
|
|
108
|
+
if fields is None:
|
|
109
|
+
fields = ""
|
|
110
|
+
elif isinstance(fields, list):
|
|
111
|
+
fields = ",".join(fields)
|
|
112
|
+
|
|
113
|
+
if time_assignment in {"start", "end"}:
|
|
114
|
+
time_assignment = f"_{time_assignment}_time".upper()
|
|
115
|
+
|
|
116
|
+
super().__init__(
|
|
117
|
+
_symbols=symbol,
|
|
118
|
+
_start=start,
|
|
119
|
+
_end=end,
|
|
120
|
+
_base_ep_func=lambda: self.base_ep(
|
|
121
|
+
db=db,
|
|
122
|
+
tick_type=tick_type,
|
|
123
|
+
parquet_file_path=parquet_file_path,
|
|
124
|
+
where=where,
|
|
125
|
+
time_assignment=time_assignment,
|
|
126
|
+
discard_fields=discard_fields,
|
|
127
|
+
fields=fields,
|
|
128
|
+
symbol_name_field=symbol_name_field,
|
|
129
|
+
),
|
|
130
|
+
schema=schema,
|
|
131
|
+
**kwargs,
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
def base_ep(
|
|
135
|
+
self,
|
|
136
|
+
parquet_file_path,
|
|
137
|
+
where=None,
|
|
138
|
+
time_assignment="end",
|
|
139
|
+
discard_fields=None,
|
|
140
|
+
fields=None,
|
|
141
|
+
symbol_name_field=None,
|
|
142
|
+
db=utils.adaptive_to_default,
|
|
143
|
+
tick_type=utils.adaptive,
|
|
144
|
+
start=utils.adaptive,
|
|
145
|
+
end=utils.adaptive,
|
|
146
|
+
):
|
|
147
|
+
if not hasattr(otq, "ReadFromParquet"):
|
|
148
|
+
raise RuntimeError("Current version of OneTick don't support READ_FROM_PARQUET EP")
|
|
149
|
+
|
|
150
|
+
node_kwargs = {}
|
|
151
|
+
if symbol_name_field:
|
|
152
|
+
node_kwargs["symbol_name_field"] = symbol_name_field
|
|
153
|
+
|
|
154
|
+
src = Source(
|
|
155
|
+
otq.ReadFromParquet(
|
|
156
|
+
parquet_file_path=parquet_file_path,
|
|
157
|
+
time_assignment=time_assignment,
|
|
158
|
+
where=where,
|
|
159
|
+
discard_fields=discard_fields,
|
|
160
|
+
fields=fields,
|
|
161
|
+
**node_kwargs,
|
|
162
|
+
)
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
if db and tick_type:
|
|
166
|
+
update_node_tick_type(src, tick_type, db)
|
|
167
|
+
|
|
168
|
+
return src
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
from typing import List, Union
|
|
2
|
+
|
|
3
|
+
import onetick.py as otp
|
|
4
|
+
from onetick.py.backports import Literal
|
|
5
|
+
from onetick.py.core.source import Source
|
|
6
|
+
from onetick.py.otq import otq
|
|
7
|
+
|
|
8
|
+
from .. import types as ott
|
|
9
|
+
from .. import utils
|
|
10
|
+
from ..compatibility import is_supported_point_in_time
|
|
11
|
+
from .common import update_node_tick_type
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def PointInTime( # NOSONAR
|
|
15
|
+
source: 'Source',
|
|
16
|
+
times: List[Union[str, ott.datetime]],
|
|
17
|
+
offsets: List[int],
|
|
18
|
+
offset_type: Literal['time_msec', 'num_ticks'] = 'time_msec',
|
|
19
|
+
db=utils.adaptive_to_default,
|
|
20
|
+
tick_type=utils.adaptive,
|
|
21
|
+
symbol=utils.adaptive_to_default,
|
|
22
|
+
start=utils.adaptive,
|
|
23
|
+
end=utils.adaptive,
|
|
24
|
+
) -> 'Source':
|
|
25
|
+
"""
|
|
26
|
+
This function propagates ticks from ``source`` that are offset by
|
|
27
|
+
the specified number of milliseconds or by the specified number of ticks
|
|
28
|
+
relative to the timestamps specified in ``times``.
|
|
29
|
+
|
|
30
|
+
Output tick may be generated for each specified timestamp and offset pair.
|
|
31
|
+
|
|
32
|
+
If ``source`` doesn't have a tick with specified timestamp and offset, then output tick is not generated.
|
|
33
|
+
|
|
34
|
+
Fields **TICK_TIME** and **OFFSET** are also added to the output ticks,
|
|
35
|
+
specifying original timestamp of the tick and the offset that was specified for it.
|
|
36
|
+
|
|
37
|
+
Note
|
|
38
|
+
----
|
|
39
|
+
In order for this method to have reasonable performance,
|
|
40
|
+
the set of queried timestamps has to be relatively small.
|
|
41
|
+
|
|
42
|
+
In other words, the points in time, which the user is interested in,
|
|
43
|
+
have to be quite few in order usage of this method to be justified.
|
|
44
|
+
|
|
45
|
+
Parameters
|
|
46
|
+
----------
|
|
47
|
+
source:
|
|
48
|
+
The source from which the data will be queried.
|
|
49
|
+
times:
|
|
50
|
+
List of timestamps to get query ticks from.
|
|
51
|
+
offsets:
|
|
52
|
+
List of integers specifying offsets for each timestamp.
|
|
53
|
+
offset_type: 'time_msec' or 'num_ticks'
|
|
54
|
+
The type of offset: number of milliseconds or the number of ticks.
|
|
55
|
+
db: str
|
|
56
|
+
This parameter is used to set database part of a tick type of the OneTick graph node.
|
|
57
|
+
Should not be specified in most cases,
|
|
58
|
+
default value will be deduced depending on configuration and query structure.
|
|
59
|
+
tick_type: str
|
|
60
|
+
This parameter is used to set tick type part of a tick type of the OneTick graph node.
|
|
61
|
+
Should not be specified in most cases,
|
|
62
|
+
default value will be deduced depending on configuration and query structure.
|
|
63
|
+
symbol: str
|
|
64
|
+
This parameter is used to set bound symbol of the OneTick graph node.
|
|
65
|
+
Should not be specified in most cases,
|
|
66
|
+
default value will be deduced depending on configuration and query structure.
|
|
67
|
+
start:
|
|
68
|
+
Can be used to specify custom start time for this source.
|
|
69
|
+
By default start time of the query will be inherited when running the query.
|
|
70
|
+
end:
|
|
71
|
+
Can be used to specify custom start time for this source.
|
|
72
|
+
By default end time of the query will be inherited when running the query.
|
|
73
|
+
|
|
74
|
+
See also
|
|
75
|
+
--------
|
|
76
|
+
| **POINT_IN_TIME** OneTick event processor
|
|
77
|
+
| :meth:`onetick.py.Source.point_in_time`
|
|
78
|
+
| :func:`onetick.py.join_by_time`
|
|
79
|
+
|
|
80
|
+
Examples
|
|
81
|
+
--------
|
|
82
|
+
|
|
83
|
+
Quotes for testing:
|
|
84
|
+
|
|
85
|
+
.. testcode::
|
|
86
|
+
|
|
87
|
+
qte = otp.Ticks(ASK_PRICE=[20, 21, 22, 23, 24, 25], BID_PRICE=[20, 21, 22, 23, 24, 25])
|
|
88
|
+
print(otp.run(qte))
|
|
89
|
+
|
|
90
|
+
.. testoutput::
|
|
91
|
+
|
|
92
|
+
Time ASK_PRICE BID_PRICE
|
|
93
|
+
0 2003-12-01 00:00:00.000 20 20
|
|
94
|
+
1 2003-12-01 00:00:00.001 21 21
|
|
95
|
+
2 2003-12-01 00:00:00.002 22 22
|
|
96
|
+
3 2003-12-01 00:00:00.003 23 23
|
|
97
|
+
4 2003-12-01 00:00:00.004 24 24
|
|
98
|
+
5 2003-12-01 00:00:00.005 25 25
|
|
99
|
+
|
|
100
|
+
Getting quotes exactly at specified timestamps:
|
|
101
|
+
|
|
102
|
+
.. testcode::
|
|
103
|
+
:skipif: not is_supported_point_in_time()
|
|
104
|
+
|
|
105
|
+
data = otp.PointInTime(qte,
|
|
106
|
+
times=[otp.dt(2003, 12, 1, 0, 0, 0, 1000), '20031201000000.003'],
|
|
107
|
+
offsets=[0])
|
|
108
|
+
print(otp.run(data))
|
|
109
|
+
|
|
110
|
+
.. testoutput::
|
|
111
|
+
|
|
112
|
+
Time ASK_PRICE BID_PRICE TICK_TIME OFFSET
|
|
113
|
+
0 2003-12-01 00:00:00.001 21 21 2003-12-01 00:00:00.001 0
|
|
114
|
+
1 2003-12-01 00:00:00.003 23 23 2003-12-01 00:00:00.003 0
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
Offset may be positive or negative.
|
|
118
|
+
If several offsets are specified, several output ticks may be generated for a single timestamp:
|
|
119
|
+
|
|
120
|
+
.. testcode::
|
|
121
|
+
:skipif: not is_supported_point_in_time()
|
|
122
|
+
|
|
123
|
+
data = otp.PointInTime(qte,
|
|
124
|
+
times=[otp.dt(2003, 12, 1, 0, 0, 0, 3000)],
|
|
125
|
+
offsets=[0, 1])
|
|
126
|
+
print(otp.run(data))
|
|
127
|
+
|
|
128
|
+
.. testoutput::
|
|
129
|
+
|
|
130
|
+
Time ASK_PRICE BID_PRICE TICK_TIME OFFSET
|
|
131
|
+
0 2003-12-01 00:00:00.003 23 23 2003-12-01 00:00:00.003 0
|
|
132
|
+
1 2003-12-01 00:00:00.003 24 24 2003-12-01 00:00:00.004 1
|
|
133
|
+
|
|
134
|
+
By default the number of milliseconds is used as an offset.
|
|
135
|
+
You can also specify the number of ticks as an offset:
|
|
136
|
+
|
|
137
|
+
.. testcode::
|
|
138
|
+
:skipif: not is_supported_point_in_time()
|
|
139
|
+
|
|
140
|
+
data = otp.PointInTime(qte,
|
|
141
|
+
times=[otp.dt(2003, 12, 1, 0, 0, 0, 3000)],
|
|
142
|
+
offsets=[-1, 1],
|
|
143
|
+
offset_type='num_ticks')
|
|
144
|
+
print(otp.run(data))
|
|
145
|
+
|
|
146
|
+
.. testoutput::
|
|
147
|
+
|
|
148
|
+
Time ASK_PRICE BID_PRICE TICK_TIME OFFSET
|
|
149
|
+
0 2003-12-01 00:00:00.003 22 22 2003-12-01 00:00:00.002 -1
|
|
150
|
+
1 2003-12-01 00:00:00.003 24 24 2003-12-01 00:00:00.004 1
|
|
151
|
+
"""
|
|
152
|
+
if not is_supported_point_in_time():
|
|
153
|
+
raise RuntimeError('PointInTime event processor is not supported on this OneTick version')
|
|
154
|
+
|
|
155
|
+
res = otp.Source(_symbols=symbol, _start=start, _end=end)
|
|
156
|
+
|
|
157
|
+
times = [
|
|
158
|
+
t if isinstance(t, str) else ott._format_datetime(t, '%Y%m%d%H%M%S.%f', add_nano_suffix=True)
|
|
159
|
+
for t in times or []
|
|
160
|
+
]
|
|
161
|
+
|
|
162
|
+
if offset_type not in ('time_msec', 'num_ticks'):
|
|
163
|
+
raise ValueError(f"Wrong value for parameter 'offset_type': {offset_type}")
|
|
164
|
+
|
|
165
|
+
query_name = source._store_in_tmp_otq(
|
|
166
|
+
res._tmp_otq,
|
|
167
|
+
operation_suffix='point_in_time',
|
|
168
|
+
# set default symbol, even if it's not set by user, symbol's value doesn't matter in this case
|
|
169
|
+
symbols=otp.config.get('default_symbol', 'ANY')
|
|
170
|
+
)
|
|
171
|
+
otq_query = f'THIS::{query_name}'
|
|
172
|
+
|
|
173
|
+
pit_params = dict(
|
|
174
|
+
otq_query=otq_query,
|
|
175
|
+
offset_type=offset_type.upper(),
|
|
176
|
+
offsets=','.join(map(str, offsets)),
|
|
177
|
+
times=','.join(map(str, times)),
|
|
178
|
+
)
|
|
179
|
+
res.source(otq.PointInTime(**pit_params))
|
|
180
|
+
|
|
181
|
+
res.schema.set(**source.schema)
|
|
182
|
+
res.schema.update(
|
|
183
|
+
**{
|
|
184
|
+
'TICK_TIME': otp.nsectime,
|
|
185
|
+
'OFFSET': int,
|
|
186
|
+
}
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
update_node_tick_type(res, tick_type, db)
|
|
190
|
+
|
|
191
|
+
return res
|