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.
Files changed (152) hide show
  1. locator_parser/__init__.py +0 -0
  2. locator_parser/acl.py +73 -0
  3. locator_parser/actions.py +262 -0
  4. locator_parser/common.py +368 -0
  5. locator_parser/io.py +43 -0
  6. locator_parser/locator.py +150 -0
  7. onetick/__init__.py +101 -0
  8. onetick/doc_utilities/__init__.py +3 -0
  9. onetick/doc_utilities/napoleon.py +40 -0
  10. onetick/doc_utilities/ot_doctest.py +140 -0
  11. onetick/doc_utilities/snippets.py +279 -0
  12. onetick/lib/__init__.py +4 -0
  13. onetick/lib/instance.py +141 -0
  14. onetick/py/__init__.py +293 -0
  15. onetick/py/_stack_info.py +89 -0
  16. onetick/py/_version.py +2 -0
  17. onetick/py/aggregations/__init__.py +11 -0
  18. onetick/py/aggregations/_base.py +648 -0
  19. onetick/py/aggregations/_docs.py +948 -0
  20. onetick/py/aggregations/compute.py +286 -0
  21. onetick/py/aggregations/functions.py +2216 -0
  22. onetick/py/aggregations/generic.py +104 -0
  23. onetick/py/aggregations/high_low.py +80 -0
  24. onetick/py/aggregations/num_distinct.py +83 -0
  25. onetick/py/aggregations/order_book.py +501 -0
  26. onetick/py/aggregations/other.py +1014 -0
  27. onetick/py/backports.py +26 -0
  28. onetick/py/cache.py +374 -0
  29. onetick/py/callback/__init__.py +5 -0
  30. onetick/py/callback/callback.py +276 -0
  31. onetick/py/callback/callbacks.py +131 -0
  32. onetick/py/compatibility.py +798 -0
  33. onetick/py/configuration.py +771 -0
  34. onetick/py/core/__init__.py +0 -0
  35. onetick/py/core/_csv_inspector.py +93 -0
  36. onetick/py/core/_internal/__init__.py +0 -0
  37. onetick/py/core/_internal/_manually_bound_value.py +6 -0
  38. onetick/py/core/_internal/_nodes_history.py +250 -0
  39. onetick/py/core/_internal/_op_utils/__init__.py +0 -0
  40. onetick/py/core/_internal/_op_utils/every_operand.py +9 -0
  41. onetick/py/core/_internal/_op_utils/is_const.py +10 -0
  42. onetick/py/core/_internal/_per_tick_scripts/tick_list_sort_template.script +121 -0
  43. onetick/py/core/_internal/_proxy_node.py +140 -0
  44. onetick/py/core/_internal/_state_objects.py +2312 -0
  45. onetick/py/core/_internal/_state_vars.py +93 -0
  46. onetick/py/core/_source/__init__.py +0 -0
  47. onetick/py/core/_source/_symbol_param.py +95 -0
  48. onetick/py/core/_source/schema.py +97 -0
  49. onetick/py/core/_source/source_methods/__init__.py +0 -0
  50. onetick/py/core/_source/source_methods/aggregations.py +809 -0
  51. onetick/py/core/_source/source_methods/applyers.py +296 -0
  52. onetick/py/core/_source/source_methods/columns.py +141 -0
  53. onetick/py/core/_source/source_methods/data_quality.py +301 -0
  54. onetick/py/core/_source/source_methods/debugs.py +272 -0
  55. onetick/py/core/_source/source_methods/drops.py +120 -0
  56. onetick/py/core/_source/source_methods/fields.py +619 -0
  57. onetick/py/core/_source/source_methods/filters.py +1002 -0
  58. onetick/py/core/_source/source_methods/joins.py +1413 -0
  59. onetick/py/core/_source/source_methods/merges.py +605 -0
  60. onetick/py/core/_source/source_methods/misc.py +1455 -0
  61. onetick/py/core/_source/source_methods/pandases.py +155 -0
  62. onetick/py/core/_source/source_methods/renames.py +356 -0
  63. onetick/py/core/_source/source_methods/sorts.py +183 -0
  64. onetick/py/core/_source/source_methods/switches.py +142 -0
  65. onetick/py/core/_source/source_methods/symbols.py +117 -0
  66. onetick/py/core/_source/source_methods/times.py +627 -0
  67. onetick/py/core/_source/source_methods/writes.py +986 -0
  68. onetick/py/core/_source/symbol.py +205 -0
  69. onetick/py/core/_source/tmp_otq.py +222 -0
  70. onetick/py/core/column.py +209 -0
  71. onetick/py/core/column_operations/__init__.py +0 -0
  72. onetick/py/core/column_operations/_methods/__init__.py +4 -0
  73. onetick/py/core/column_operations/_methods/_internal.py +28 -0
  74. onetick/py/core/column_operations/_methods/conversions.py +216 -0
  75. onetick/py/core/column_operations/_methods/methods.py +292 -0
  76. onetick/py/core/column_operations/_methods/op_types.py +160 -0
  77. onetick/py/core/column_operations/accessors/__init__.py +0 -0
  78. onetick/py/core/column_operations/accessors/_accessor.py +28 -0
  79. onetick/py/core/column_operations/accessors/decimal_accessor.py +104 -0
  80. onetick/py/core/column_operations/accessors/dt_accessor.py +537 -0
  81. onetick/py/core/column_operations/accessors/float_accessor.py +184 -0
  82. onetick/py/core/column_operations/accessors/str_accessor.py +1367 -0
  83. onetick/py/core/column_operations/base.py +1121 -0
  84. onetick/py/core/cut_builder.py +150 -0
  85. onetick/py/core/db_constants.py +20 -0
  86. onetick/py/core/eval_query.py +245 -0
  87. onetick/py/core/lambda_object.py +441 -0
  88. onetick/py/core/multi_output_source.py +232 -0
  89. onetick/py/core/per_tick_script.py +2256 -0
  90. onetick/py/core/query_inspector.py +464 -0
  91. onetick/py/core/source.py +1744 -0
  92. onetick/py/db/__init__.py +2 -0
  93. onetick/py/db/_inspection.py +1128 -0
  94. onetick/py/db/db.py +1327 -0
  95. onetick/py/db/utils.py +64 -0
  96. onetick/py/docs/__init__.py +0 -0
  97. onetick/py/docs/docstring_parser.py +112 -0
  98. onetick/py/docs/utils.py +81 -0
  99. onetick/py/functions.py +2398 -0
  100. onetick/py/license.py +190 -0
  101. onetick/py/log.py +88 -0
  102. onetick/py/math.py +935 -0
  103. onetick/py/misc.py +470 -0
  104. onetick/py/oqd/__init__.py +22 -0
  105. onetick/py/oqd/eps.py +1195 -0
  106. onetick/py/oqd/sources.py +325 -0
  107. onetick/py/otq.py +216 -0
  108. onetick/py/pyomd_mock.py +47 -0
  109. onetick/py/run.py +916 -0
  110. onetick/py/servers.py +173 -0
  111. onetick/py/session.py +1347 -0
  112. onetick/py/sources/__init__.py +19 -0
  113. onetick/py/sources/cache.py +167 -0
  114. onetick/py/sources/common.py +128 -0
  115. onetick/py/sources/csv.py +642 -0
  116. onetick/py/sources/custom.py +85 -0
  117. onetick/py/sources/data_file.py +305 -0
  118. onetick/py/sources/data_source.py +1045 -0
  119. onetick/py/sources/empty.py +94 -0
  120. onetick/py/sources/odbc.py +337 -0
  121. onetick/py/sources/order_book.py +271 -0
  122. onetick/py/sources/parquet.py +168 -0
  123. onetick/py/sources/pit.py +191 -0
  124. onetick/py/sources/query.py +495 -0
  125. onetick/py/sources/snapshots.py +419 -0
  126. onetick/py/sources/split_query_output_by_symbol.py +198 -0
  127. onetick/py/sources/symbology_mapping.py +123 -0
  128. onetick/py/sources/symbols.py +374 -0
  129. onetick/py/sources/ticks.py +825 -0
  130. onetick/py/sql.py +70 -0
  131. onetick/py/state.py +251 -0
  132. onetick/py/types.py +2131 -0
  133. onetick/py/utils/__init__.py +70 -0
  134. onetick/py/utils/acl.py +93 -0
  135. onetick/py/utils/config.py +186 -0
  136. onetick/py/utils/default.py +49 -0
  137. onetick/py/utils/file.py +38 -0
  138. onetick/py/utils/helpers.py +76 -0
  139. onetick/py/utils/locator.py +94 -0
  140. onetick/py/utils/perf.py +498 -0
  141. onetick/py/utils/query.py +49 -0
  142. onetick/py/utils/render.py +1374 -0
  143. onetick/py/utils/script.py +244 -0
  144. onetick/py/utils/temp.py +471 -0
  145. onetick/py/utils/types.py +120 -0
  146. onetick/py/utils/tz.py +84 -0
  147. onetick_py-1.177.0.dist-info/METADATA +137 -0
  148. onetick_py-1.177.0.dist-info/RECORD +152 -0
  149. onetick_py-1.177.0.dist-info/WHEEL +5 -0
  150. onetick_py-1.177.0.dist-info/entry_points.txt +2 -0
  151. onetick_py-1.177.0.dist-info/licenses/LICENSE +21 -0
  152. onetick_py-1.177.0.dist-info/top_level.txt +2 -0
@@ -0,0 +1,419 @@
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 ReadSnapshot(Source):
12
+ def __init__(
13
+ self,
14
+ snapshot_name='VALUE',
15
+ snapshot_storage='memory',
16
+ allow_snapshot_absence=False,
17
+ symbol=utils.adaptive,
18
+ db=utils.adaptive_to_default,
19
+ tick_type=utils.adaptive,
20
+ start=utils.adaptive,
21
+ end=utils.adaptive,
22
+ schema=None,
23
+ **kwargs,
24
+ ):
25
+ """
26
+ Reads ticks for a specified symbol name and snapshot name from global memory storage or
27
+ from a memory mapped file.
28
+
29
+ These ticks should be written there by the :py:meth:`onetick.py.Source.save_snapshot` event processor.
30
+ Ticks with an empty symbol name (for example, those after merging the time series of different symbols)
31
+ are saved under **CEP_SNAPSHOT::** symbol name, so for reading such ticks a dummy database
32
+ with the name **CEP_SNAPSHOT** must be configured in the database locator file.
33
+
34
+ Parameters
35
+ ----------
36
+ snapshot_name: str
37
+ The name that was specified in :py:meth:`onetick.py.Source.save_snapshot` as a ``snapshot_name``
38
+ during saving.
39
+
40
+ Default: ``VALUE``
41
+ snapshot_storage: 'memory' or 'memory_mapped_file'
42
+ This parameter specifies the place of storage of the snapshot. Possible options are:
43
+
44
+ * `memory` - the snapshot is stored in the dynamic (heap) memory of the process
45
+ that ran (or is still running) the :py:meth:`onetick.py.Source.save_snapshot` for the snapshot.
46
+ * `memory_mapped_file` - the snapshot is stored in a memory mapped file.
47
+ For each symbol to get the location of the snapshot in the file system, ``ReadSnapshot`` looks at
48
+ the **SAVE_SNAPSHOT_DIR** parameter value in the locator section for the database of the symbol.
49
+
50
+ Default: `memory`
51
+ allow_snapshot_absence: bool
52
+ If specified, the EP does not display an error about missing snapshot
53
+ if the snapshot has not been saved or is still being saved.
54
+
55
+ Default: `False`
56
+ symbol: str, list of str, :class:`Source`, :class:`query`, :py:func:`eval query <onetick.py.eval>`
57
+ Symbol(s) from which data should be taken.
58
+ tick_type: str
59
+ Tick type.
60
+ Default: ANY.
61
+ start: :py:class:`otp.datetime <onetick.py.datetime>`
62
+ Start time for tick generation. By default the start time of the query will be used.
63
+ end: :py:class:`otp.datetime <onetick.py.datetime>`
64
+ End time for tick generation. By default the end time of the query will be used.
65
+ schema: dict
66
+ Dictionary of columns names with their types.
67
+
68
+ .. warning::
69
+ You should set schema manually, if you want to use fields in `onetick-py` query description
70
+ before its execution.
71
+
72
+ See also
73
+ --------
74
+ | **READ_SNAPSHOT** OneTick event processor
75
+ | :py:class:`onetick.py.ShowSnapshotList`
76
+ | :py:class:`onetick.py.FindSnapshotSymbols`
77
+ | :py:meth:`onetick.py.Source.save_snapshot`
78
+ | :py:meth:`onetick.py.Source.join_with_snapshot`
79
+
80
+ Examples
81
+ --------
82
+ Read snapshot from memory:
83
+
84
+ >>> src = otp.ReadSnapshot(snapshot_name='some_snapshot')
85
+ >>> otp.run(src) # doctest: +SKIP
86
+ Time PRICE SIZE TICK_TIME
87
+ 0 2003-12-01 100.2 500 2003-12-01 00:00:00.000
88
+ 1 2003-12-01 98.3 250 2003-12-01 00:00:00.001
89
+ 2 2003-12-01 102.5 400 2003-12-01 00:00:00.002
90
+
91
+ You can specify schema manually in order to reference snapshot fields while constructing query via `onetick-py`:
92
+
93
+ >>> src = otp.ReadSnapshot(
94
+ ... snapshot_name='some_snapshot', schema={'PRICE': float, 'SIZE': int},
95
+ ... )
96
+ >>> src['VOLUME'] = src['PRICE'] * src['SIZE']
97
+
98
+ Read snapshot for specified database and symbol name:
99
+
100
+ >>> src = otp.ReadSnapshot(snapshot_name='some_snapshot', db='DB', symbol='AAA')
101
+
102
+ Read snapshot from memory mapped file:
103
+
104
+ >>> src = otp.ReadSnapshot(
105
+ ... snapshot_name='some_snapshot', snapshot_storage='memory_mapped_file', db='DB',
106
+ ... )
107
+ """
108
+ if self._try_default_constructor(schema=schema):
109
+ return
110
+
111
+ if not hasattr(otq, "ReadSnapshot"):
112
+ raise RuntimeError("Current version of OneTick don't support READ_SNAPSHOT EP")
113
+
114
+ if schema is None:
115
+ schema = {}
116
+
117
+ schema.update({'TICK_TIME': otp.nsectime})
118
+
119
+ if snapshot_storage not in ['memory', 'memory_mapped_file']:
120
+ raise ValueError('`snapshot_storage` must be one of "memory", "memory_mapped_file"')
121
+
122
+ super().__init__(
123
+ _symbols=symbol,
124
+ _start=start,
125
+ _end=end,
126
+ _base_ep_func=lambda: self.base_ep(
127
+ db=db,
128
+ tick_type=tick_type,
129
+ snapshot_name=snapshot_name,
130
+ snapshot_storage=snapshot_storage,
131
+ allow_snapshot_absence=allow_snapshot_absence,
132
+ ),
133
+ schema=schema,
134
+ **kwargs,
135
+ )
136
+
137
+ def base_ep(
138
+ self,
139
+ snapshot_name='VALUE',
140
+ snapshot_storage='memory',
141
+ allow_snapshot_absence=False,
142
+ db=utils.adaptive_to_default,
143
+ tick_type=utils.adaptive,
144
+ start=utils.adaptive,
145
+ end=utils.adaptive,
146
+ ):
147
+ snapshot_storage = snapshot_storage.upper()
148
+
149
+ src = Source(
150
+ otq.ReadSnapshot(
151
+ snapshot_name=snapshot_name,
152
+ snapshot_storage=snapshot_storage,
153
+ allow_snapshot_absence=allow_snapshot_absence,
154
+ )
155
+ )
156
+
157
+ if db or tick_type:
158
+ update_node_tick_type(src, tick_type, db)
159
+
160
+ return src
161
+
162
+
163
+ class ShowSnapshotList(Source):
164
+ def __init__(self, snapshot_storage='all', **kwargs):
165
+ """
166
+ Outputs all snapshots names for global memory storage and/or for memory mapped files.
167
+ These snapshots should be written there by the :py:meth:`onetick.py.Source.save_snapshot`.
168
+ Output ticks have ``SNAPSHOT_NAME``, ``STORAGE_TYPE`` and ``DB_NAME`` fields.
169
+
170
+ Parameters
171
+ ----------
172
+ snapshot_storage: 'memory' or 'memory_mapped_file'
173
+ This parameter specifies the place of storage of the snapshot. Possible options are:
174
+
175
+ * `memory` - the snapshot is stored in the dynamic (heap) memory of the process
176
+ that ran (or is still running) the :py:meth:`onetick.py.Source.save_snapshot` for the snapshot.
177
+ * `memory_mapped_file` - the snapshot is stored in a memory mapped file.
178
+ For each symbol to get the location of the snapshot in the file system, ``ReadSnapshot`` looks at
179
+ the **SAVE_SNAPSHOT_DIR** parameter value in the locator section for the database of the symbol.
180
+ * `all` - shows both, `memory`, `memory_mapped_file` snapshots.
181
+
182
+ Default: `all`
183
+
184
+ See also
185
+ --------
186
+ | **SHOW_SNAPSHOT_LIST** OneTick event processor
187
+ | :py:class:`onetick.py.ReadSnapshot`
188
+ | :py:class:`onetick.py.FindSnapshotSymbols`
189
+ | :py:meth:`onetick.py.Source.save_snapshot`
190
+ | :py:meth:`onetick.py.Source.join_with_snapshot`
191
+
192
+ Examples
193
+ --------
194
+ List snapshots from all snapshot storage types:
195
+
196
+ >>> src = otp.ShowSnapshotList(snapshot_storage='all') # doctest: +SKIP
197
+ >>> otp.run(src) # doctest: +SKIP
198
+ Time SNAPSHOT_NAME STORAGE_TYPE DB_NAME
199
+ 0 2003-12-01 snapshot_1 MEMORY DEMO_L1
200
+ 1 2003-12-01 snapshot_2 MEMORY DEMO_L1
201
+ 2 2003-12-01 snapshot_3 MEMORY_MAPPED_FILE SNAPSHOT_DEMO
202
+
203
+ List snapshots from memory:
204
+
205
+ >>> src = otp.ShowSnapshotList(snapshot_storage='memory') # doctest: +SKIP
206
+ >>> otp.run(src) # doctest: +SKIP
207
+ Time SNAPSHOT_NAME STORAGE_TYPE DB_NAME
208
+ 0 2003-12-01 snapshot_1 MEMORY DEMO_L1
209
+ 1 2003-12-01 snapshot_2 MEMORY DEMO_L1
210
+
211
+ List snapshots from memory mapped files:
212
+
213
+ >>> src = otp.ShowSnapshotList(snapshot_storage='memory_mapped_file') # doctest: +SKIP
214
+ >>> otp.run(src) # doctest: +SKIP
215
+ Time SNAPSHOT_NAME STORAGE_TYPE DB_NAME
216
+ 0 2003-12-01 snapshot_3 MEMORY_MAPPED_FILE SNAPSHOT_DEMO
217
+ """
218
+ if 'schema' not in kwargs:
219
+ kwargs['schema'] = {'SNAPSHOT_NAME': str, 'STORAGE_TYPE': str, 'DB_NAME': str}
220
+
221
+ if self._try_default_constructor(**kwargs):
222
+ return
223
+
224
+ if not hasattr(otq, "ShowSnapshotList"):
225
+ raise RuntimeError("Current version of OneTick don't support SHOW_SNAPSHOT_LIST EP")
226
+
227
+ if snapshot_storage not in ['memory', 'memory_mapped_file', 'all']:
228
+ raise ValueError('`snapshot_storage` must be one of "memory", "memory_mapped_file", "all"')
229
+
230
+ super().__init__(
231
+ _symbols=utils.adaptive,
232
+ _base_ep_func=lambda: self.base_ep(snapshot_storage=snapshot_storage),
233
+ **kwargs,
234
+ )
235
+
236
+ def base_ep(self, snapshot_storage='all'):
237
+ snapshot_storage = snapshot_storage.upper()
238
+
239
+ src = Source(otq.ShowSnapshotList(snapshot_storage=snapshot_storage))
240
+ update_node_tick_type(src, otp.adaptive, otp.config.get('default_db', 'LOCAL'))
241
+
242
+ return src
243
+
244
+
245
+ class FindSnapshotSymbols(Source):
246
+ def __init__(
247
+ self,
248
+ snapshot_name='VALUE',
249
+ snapshot_storage='memory',
250
+ pattern='%',
251
+ symbology=None,
252
+ show_original_symbols=False,
253
+ discard_on_match=False,
254
+ db=None,
255
+ tick_type=None,
256
+ **kwargs,
257
+ ):
258
+ """
259
+ Takes snapshot's name and storage type and produces a union of all symbols in the snapshot.
260
+ It also optionally translates the symbols into a user-specified symbology and
261
+ propagates those that match a specified name pattern as a tick with a single field: **SYMBOL_NAME**.
262
+
263
+ If symbol name translation is performed, symbol names in native snapshot can optionally be propagated
264
+ as another tick field: ``ORIGINAL_SYMBOL_NAME``, in which case symbols with missing translations
265
+ are also propagated. Symbol name translation is performed as of the query symbol date and requires
266
+ the reference database to be configured.
267
+
268
+ The name of the database to query is extracted from the input symbol.
269
+ For example, if an input symbol is ``TAQ::``, the symbols from the **TAQ** database will be returned.
270
+
271
+ The ``pattern`` parameter can contain the special characters ``%`` (matches any number of characters) and
272
+ ``_`` (matches any character).
273
+ For example, the pattern ``I_M%`` returns any symbol beginning with I and has M as its third letter.
274
+
275
+ If the ``discard_on_match`` parameter is set to ``True``, the names that do not match the pattern
276
+ will be propagated.
277
+
278
+ The timestamps of the ticks created by ``FindSnapshotSymbols`` are set to the start time of the query.
279
+
280
+ Parameters
281
+ ----------
282
+ snapshot_name: str
283
+ The name that was specified in :py:meth:`onetick.py.Source.save_snapshot` as a ``snapshot_name``
284
+ during saving.
285
+
286
+ Default: `VALUE`
287
+ snapshot_storage: 'memory' or 'memory_mapped_file'
288
+ This parameter specifies the place of storage of the snapshot. Possible options are:
289
+
290
+ * `memory` - the snapshot is stored in the dynamic (heap) memory of the process
291
+ that ran (or is still running) the :py:meth:`onetick.py.Source.save_snapshot` for the snapshot.
292
+ * `memory_mapped_file` - the snapshot is stored in a memory mapped file.
293
+ For each symbol to get the location of the snapshot in the file system, ``ReadSnapshot`` looks at
294
+ the **SAVE_SNAPSHOT_DIR** parameter value in the locator section for the database of the symbol.
295
+
296
+ Default: `memory`
297
+ pattern: str
298
+ The pattern for symbol selection. It can contain special characters ``%`` (matches any number of characters)
299
+ and ``_`` (matches any character). To avoid this special interpretation of characters ``%`` and ``_``,
300
+ the ``\\`` character must be added in front of them. Note that if symbol name translation is required,
301
+ translated rather than original symbol names are checked to match the name pattern.
302
+
303
+ Default: `%`
304
+ symbology: Optional[str]
305
+ The destination symbology for a symbol name translation, the latter being performed,
306
+ if destination symbology is not empty and is different from that of the queried database.
307
+ show_original_symbols: bool
308
+ Switches original symbol name propagation as a tick field after symbol name translation is performed.
309
+ Note that if this parameter is set to true, database symbols with missing translations are also propagated.
310
+
311
+ Default: `False`
312
+ discard_on_match: bool
313
+ When set to true only ticks that did not match the filter are propagated,
314
+ otherwise ticks that satisfy the filter condition are propagated.
315
+
316
+ Default: `False`
317
+ db: str, optional
318
+ Database to use for snapshots search query.
319
+
320
+ It's required to fill either this parameter or explicitly specify
321
+ the ``symbols`` parameter of :py:func:`otp.run <onetick.py.run>`,
322
+ or have bound symbols on any node of your query
323
+ tick_type: str, optional
324
+ Tick type.
325
+
326
+ See also
327
+ --------
328
+ | **FIND_SNAPSHOT_SYMBOLS** OneTick event processor
329
+ | :py:class:`onetick.py.ReadSnapshot`
330
+ | :py:class:`onetick.py.ShowSnapshotList`
331
+ | :py:meth:`onetick.py.Source.save_snapshot`
332
+ | :py:meth:`onetick.py.Source.join_with_snapshot`
333
+
334
+ Examples
335
+ --------
336
+ Find symbols for snapshot `some_snapshot` in database `S1`:
337
+
338
+ >>> src = otp.FindSnapshotSymbols(snapshot_name='some_snapshot', db='S1')
339
+ >>> otp.run(src, symbols='S1::') # doctest: +SKIP
340
+ Time SYMBOL_NAME
341
+ 0 2003-12-01 S1::AAPL
342
+ 1 2003-12-01 S1::AAAA
343
+ 2 2003-12-01 S1::MSFT
344
+
345
+ Use ``pattern`` parameter to filter symbol names:
346
+
347
+ >>> src = otp.FindSnapshotSymbols(snapshot_name='some_snapshot', db='S1', pattern='A%')
348
+ >>> otp.run(src, symbols='S1::') # doctest: +SKIP
349
+ Time SYMBOL_NAME
350
+ 0 2003-12-01 S1::AAPL
351
+ 1 2003-12-01 S1::AAAA
352
+
353
+ Select symbol names not matched by pattern:
354
+
355
+ >>> src = otp.FindSnapshotSymbols(
356
+ ... snapshot_name='some_snapshot', db='S1', pattern='A%', discard_on_match=True,
357
+ ... )
358
+ >>> otp.run(src, symbols='S1::') # doctest: +SKIP
359
+ Time SYMBOL_NAME
360
+ 0 2003-12-01 S1::MSFT
361
+ """
362
+ if 'schema' not in kwargs:
363
+ kwargs['schema'] = {'SYMBOL_NAME': str}
364
+
365
+ if show_original_symbols:
366
+ kwargs['schema'].update({'ORIGINAL_SYMBOL_NAME': str})
367
+
368
+ if self._try_default_constructor(**kwargs):
369
+ return
370
+
371
+ if not hasattr(otq, "FindSnapshotSymbols"):
372
+ raise RuntimeError("Current version of OneTick don't support SHOW_SNAPSHOT_LIST EP")
373
+
374
+ if symbology is None:
375
+ symbology = ''
376
+
377
+ if snapshot_storage not in ['memory', 'memory_mapped_file']:
378
+ raise ValueError('`snapshot_storage` must be one of "memory", "memory_mapped_file"')
379
+
380
+ super().__init__(
381
+ _base_ep_func=lambda: self.base_ep(
382
+ snapshot_name=snapshot_name,
383
+ snapshot_storage=snapshot_storage,
384
+ pattern=pattern,
385
+ symbology=symbology,
386
+ show_original_symbols=show_original_symbols,
387
+ discard_on_match=discard_on_match,
388
+ db=db,
389
+ tick_type=tick_type,
390
+ ),
391
+ **kwargs,
392
+ )
393
+
394
+ def base_ep(
395
+ self,
396
+ snapshot_name='VALUE',
397
+ snapshot_storage='memory',
398
+ pattern='%',
399
+ symbology='',
400
+ show_original_symbols=False,
401
+ discard_on_match=False,
402
+ db=None,
403
+ tick_type=None,
404
+ ):
405
+ snapshot_storage = snapshot_storage.upper()
406
+
407
+ src = Source(
408
+ otq.FindSnapshotSymbols(
409
+ snapshot_name=snapshot_name,
410
+ snapshot_storage=snapshot_storage,
411
+ pattern=pattern,
412
+ symbology=symbology,
413
+ show_original_symbols=show_original_symbols,
414
+ discard_on_match=discard_on_match,
415
+ )
416
+ )
417
+ update_node_tick_type(src, tick_type, db)
418
+
419
+ return src
@@ -0,0 +1,198 @@
1
+ import os
2
+
3
+ from functools import partial
4
+
5
+ import onetick.py as otp
6
+ from onetick.py.otq import otq
7
+
8
+ from onetick.py.core.source import Source
9
+
10
+ from .. import utils
11
+
12
+ from .common import update_node_tick_type
13
+
14
+
15
+ class SplitQueryOutputBySymbol(Source):
16
+ def __init__(self,
17
+ query=None,
18
+ symbol_field=None,
19
+ single_invocation=False,
20
+ db=utils.adaptive_to_default,
21
+ tick_type=utils.adaptive,
22
+ start=utils.adaptive,
23
+ end=utils.adaptive,
24
+ symbols=utils.adaptive,
25
+ schema=None,
26
+ **kwargs):
27
+ """
28
+ A data source used to dispatch output ticks,
29
+ resulting after execution of the specified query,
30
+ according to the values of the specified field in those ticks.
31
+
32
+ Each replica of this EP, corresponding to a particular bound or unbound symbol, thus,
33
+ propagates resulting ticks of the specified query,
34
+ with values of the specified field in those ticks equal to that symbol.
35
+
36
+ Note, that database name part of symbols is not taken into account.
37
+
38
+ Parameters
39
+ ----------
40
+ query:
41
+ Specify query to execute.
42
+ symbol_field:
43
+ Specifies the field in the resulting ticks of the underlying query,
44
+ according to values of which those ticks are dispatched.
45
+ single_invocation:
46
+ By default, the underlying query is executed once
47
+ per symbol batch and per execution thread of the containing query.
48
+ If this parameter is set to True, the underlying query is executed once
49
+ regardless of the batch size and the number of CPU cores utilized.
50
+
51
+ The former option should be the preferred (hence the default) one, as it reduces memory overhead,
52
+ while the latter one might be chosen to speed-up the overall execution.
53
+
54
+ Note, that if the underlying query is a CEP query, than this option has no effect,
55
+ as there is a single batch and a single thread anyway.
56
+ db:
57
+ tick_type:
58
+ Tick type to set on the OneTick's graph node.
59
+ Can be used to specify database name with tick type or tick type only.
60
+
61
+ By default setting these parameters is not required, database is usually set
62
+ with parameter ``symbols`` or in :py:func:`otp.run <onetick.py.run>`.
63
+ start:
64
+ Custom start time of the source.
65
+ If set, will override the value specified in :py:func:`otp.run <onetick.py.run>`.
66
+ end:
67
+ Custom end time of the source.
68
+ If set, will override the value specified in :py:func:`otp.run <onetick.py.run>`.
69
+ symbols:
70
+ Symbol(s) from which data should be taken.
71
+ If set, will override the value specified in :py:func:`otp.run <onetick.py.run>`.
72
+
73
+ Examples
74
+ --------
75
+
76
+ Get only the ticks that have needed symbols specified in field ``TICKER``:
77
+
78
+ >>> data = otp.Ticks(X=[1, 2, 3, 4], TICKER=['A', 'B', 'A', 'C'])
79
+ >>> data = otp.SplitQueryOutputBySymbol(data, data['TICKER'])
80
+ >>> res = otp.run(data, symbols=['A', 'B'])
81
+ >>> res['A']
82
+ Time X TICKER
83
+ 0 2003-12-01 00:00:00.000 1 A
84
+ 1 2003-12-01 00:00:00.002 3 A
85
+ >>> res['B']
86
+ Time X TICKER
87
+ 0 2003-12-01 00:00:00.001 2 B
88
+ """
89
+
90
+ if self._try_default_constructor(schema=schema, **kwargs):
91
+ return
92
+
93
+ if isinstance(query, Source):
94
+ query = query.copy()
95
+ otq_query = query.to_otq()
96
+ q_start, q_end, _ = query._set_date_range_and_symbols()
97
+ if start is utils.adaptive and end is utils.adaptive:
98
+ start, end = q_start, q_end
99
+ else:
100
+ # TODO: support already existing queries
101
+ raise ValueError('Non supported type of the `query` is specified')
102
+
103
+ super().__init__(
104
+ _symbols=symbols,
105
+ _start=start,
106
+ _end=end,
107
+ _base_ep_func=partial(self.build, db, tick_type, symbol_field, otq_query, single_invocation),
108
+ schema=schema,
109
+ **kwargs,
110
+ )
111
+
112
+ def build(self, db, tick_type, symbol_field_name, otq_query, single_invocation):
113
+ src = Source(otq.SplitQueryOutputBySymbol(otq_query=otq_query,
114
+ symbol_field_name=str(symbol_field_name),
115
+ ensure_single_invocation=single_invocation))
116
+
117
+ update_node_tick_type(src, tick_type, db)
118
+
119
+ return src
120
+
121
+
122
+ def by_symbol(src: Source,
123
+ symbol_field,
124
+ single_invocation=False,
125
+ db=utils.adaptive_to_default,
126
+ tick_type=utils.adaptive,
127
+ start=utils.adaptive,
128
+ end=utils.adaptive,
129
+ ) -> Source:
130
+ """
131
+ Create a separate data series for each unique value of ``symbol_field`` in the output of ``src``.
132
+ ``src`` must specify enough parameters to be run (e.g., symbols, query range). A typical use case is to split a
133
+ single data series (e.g., from a CSV file) into separate data series by symbol. This method is a source.
134
+
135
+ Parameters
136
+ ----------
137
+ src: Source
138
+ a query which output is to be split by ``symbol_field``
139
+ symbol_field: str
140
+ the name of the field carrying symbol name in the ``src`` query
141
+ single_invocation: bool, optional
142
+ ``True`` means that the ``src`` query is run once and the result stored in memory speeding up the execution.
143
+ ``False`` means that the ``src`` query is run for every symbol of the query saving memory
144
+ but slowing down query execution.
145
+ Default: ``False``
146
+ db: str, optional
147
+ Database for running the query. Doesn't affect the ``src`` query. The default value
148
+ is ``otp.config['default_db']``.
149
+ tick_type: str, optional
150
+ Tick type for the query. Doesn't affect the ``src`` query.
151
+ start: otp.dt, optional
152
+ By default it is taken from the ``src`` start time
153
+ end: otp.dt, optional
154
+ By default it is taken from the ``src`` end time
155
+
156
+ See also
157
+ --------
158
+ **SPLIT_QUERY_OUTPUT_BY_SYMBOL** OneTick event processor
159
+
160
+ Examples
161
+ --------
162
+ >>> executions = otp.CSV( # doctest: +SKIP
163
+ ... otp.utils.file(os.path.join(cur_dir, 'data', 'example_events.csv')),
164
+ ... converters={"time_number": lambda c: c.apply(otp.nsectime)},
165
+ ... timestamp_name="time_number",
166
+ ... start=otp.dt(2022, 7, 1),
167
+ ... end=otp.dt(2022, 7, 2),
168
+ ... order_ticks=True
169
+ ... )[['stock', 'px']]
170
+ >>> csv = otp.by_symbol(executions, 'stock') # doctest: +SKIP
171
+ >>> trd = otp.DataSource( # doctest: +SKIP
172
+ ... db='US_COMP',
173
+ ... tick_type='TRD',
174
+ ... start=otp.dt(2022, 7, 1),
175
+ ... end=otp.dt(2022, 7, 2)
176
+ ... )[['PRICE', 'SIZE']]
177
+ >>> data = otp.join_by_time([csv, trd]) # doctest: +SKIP
178
+ >>> result = otp.run(data, symbols=executions.distinct(keys='stock')[['stock']], concurrency=8) # doctest: +SKIP
179
+ >>> result['THG'] # doctest: +SKIP
180
+ Time stock px PRICE SIZE
181
+ 0 2022-07-01 11:37:56.432947200 THG 148.02 146.48 1
182
+ >>> result['TFX'] # doctest: +SKIP
183
+ Time stock px PRICE SIZE
184
+ 0 2022-07-01 11:39:45.882808576 TFX 255.61 251.97 1
185
+ >>> result['BURL'] # doctest: +SKIP
186
+ Time stock px PRICE SIZE
187
+ 0 2022-07-01 11:42:35.125718016 BURL 137.53 135.41 2
188
+ """
189
+ result = SplitQueryOutputBySymbol(src,
190
+ symbol_field=symbol_field,
191
+ single_invocation=single_invocation,
192
+ db=db,
193
+ tick_type=tick_type,
194
+ start=start,
195
+ end=end)
196
+
197
+ result.schema.set(**src.schema)
198
+ return result