onetick-py 1.162.2__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 +266 -0
  4. locator_parser/common.py +365 -0
  5. locator_parser/io.py +41 -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 +280 -0
  12. onetick/lib/__init__.py +4 -0
  13. onetick/lib/instance.py +138 -0
  14. onetick/py/__init__.py +290 -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 +645 -0
  19. onetick/py/aggregations/_docs.py +912 -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 +427 -0
  26. onetick/py/aggregations/other.py +1014 -0
  27. onetick/py/backports.py +26 -0
  28. onetick/py/cache.py +373 -0
  29. onetick/py/callback/__init__.py +5 -0
  30. onetick/py/callback/callback.py +275 -0
  31. onetick/py/callback/callbacks.py +131 -0
  32. onetick/py/compatibility.py +752 -0
  33. onetick/py/configuration.py +736 -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 +2307 -0
  45. onetick/py/core/_internal/_state_vars.py +87 -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 +810 -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 +270 -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 +1001 -0
  58. onetick/py/core/_source/source_methods/joins.py +1393 -0
  59. onetick/py/core/_source/source_methods/merges.py +566 -0
  60. onetick/py/core/_source/source_methods/misc.py +1325 -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 +702 -0
  68. onetick/py/core/_source/symbol.py +202 -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 +215 -0
  75. onetick/py/core/column_operations/_methods/methods.py +294 -0
  76. onetick/py/core/column_operations/_methods/op_types.py +150 -0
  77. onetick/py/core/column_operations/accessors/__init__.py +0 -0
  78. onetick/py/core/column_operations/accessors/_accessor.py +30 -0
  79. onetick/py/core/column_operations/accessors/decimal_accessor.py +92 -0
  80. onetick/py/core/column_operations/accessors/dt_accessor.py +464 -0
  81. onetick/py/core/column_operations/accessors/float_accessor.py +160 -0
  82. onetick/py/core/column_operations/accessors/str_accessor.py +1374 -0
  83. onetick/py/core/column_operations/base.py +1061 -0
  84. onetick/py/core/cut_builder.py +149 -0
  85. onetick/py/core/db_constants.py +20 -0
  86. onetick/py/core/eval_query.py +244 -0
  87. onetick/py/core/lambda_object.py +442 -0
  88. onetick/py/core/multi_output_source.py +193 -0
  89. onetick/py/core/per_tick_script.py +2253 -0
  90. onetick/py/core/query_inspector.py +465 -0
  91. onetick/py/core/source.py +1663 -0
  92. onetick/py/db/__init__.py +2 -0
  93. onetick/py/db/_inspection.py +1042 -0
  94. onetick/py/db/db.py +1423 -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 +2354 -0
  100. onetick/py/license.py +188 -0
  101. onetick/py/log.py +88 -0
  102. onetick/py/math.py +947 -0
  103. onetick/py/misc.py +437 -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 +211 -0
  108. onetick/py/pyomd_mock.py +47 -0
  109. onetick/py/run.py +841 -0
  110. onetick/py/servers.py +173 -0
  111. onetick/py/session.py +1342 -0
  112. onetick/py/sources/__init__.py +19 -0
  113. onetick/py/sources/cache.py +167 -0
  114. onetick/py/sources/common.py +126 -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 +1049 -0
  119. onetick/py/sources/empty.py +94 -0
  120. onetick/py/sources/odbc.py +337 -0
  121. onetick/py/sources/order_book.py +238 -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 +357 -0
  129. onetick/py/sources/ticks.py +825 -0
  130. onetick/py/sql.py +70 -0
  131. onetick/py/state.py +256 -0
  132. onetick/py/types.py +2056 -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 +499 -0
  141. onetick/py/utils/query.py +49 -0
  142. onetick/py/utils/render.py +1139 -0
  143. onetick/py/utils/script.py +244 -0
  144. onetick/py/utils/temp.py +471 -0
  145. onetick/py/utils/types.py +118 -0
  146. onetick/py/utils/tz.py +82 -0
  147. onetick_py-1.162.2.dist-info/METADATA +148 -0
  148. onetick_py-1.162.2.dist-info/RECORD +152 -0
  149. onetick_py-1.162.2.dist-info/WHEEL +5 -0
  150. onetick_py-1.162.2.dist-info/entry_points.txt +2 -0
  151. onetick_py-1.162.2.dist-info/licenses/LICENSE +21 -0
  152. onetick_py-1.162.2.dist-info/top_level.txt +2 -0
@@ -0,0 +1,912 @@
1
+ from typing import List, Dict, Union, Optional, Callable, TYPE_CHECKING
2
+ from onetick.py.backports import Literal
3
+
4
+ from functools import wraps
5
+ from inspect import Signature, Parameter
6
+ from onetick.py.core.column import Column
7
+ from onetick.py.core.column_operations.base import Operation, OnetickParameter
8
+ from onetick.py.core._source._symbol_param import _SymbolParamColumn
9
+ from onetick.py.types import OTPBaseTimeOffset
10
+
11
+ if TYPE_CHECKING:
12
+ from onetick.py.core.source import Source # hack for annotations
13
+
14
+ from onetick.py.docs.docstring_parser import Docstring
15
+ from onetick.py.docs.utils import param_doc
16
+ from onetick.py.aggregations.high_low import HighTick, LowTick
17
+
18
+
19
+ _column_doc = param_doc(
20
+ name='column',
21
+ str_annotation='str or Column or Operation',
22
+ desc='''
23
+ String with the name of the column to be aggregated or :py:class:`~onetick.py.Column` object.
24
+ :py:class:`~onetick.py.Operation` object can also be used -- in this case
25
+ the results of this operation for each tick are aggregated
26
+ (see example in :ref:`common aggregation examples <aggregations_funcs>`).
27
+ ''',
28
+ annotation=Union[str, Column, Operation]
29
+ )
30
+
31
+ _running_doc = param_doc(
32
+ name='running',
33
+ desc='''
34
+ Aggregation will be calculated as sliding window.
35
+ ``running`` and ``bucket_interval`` parameters determines when new buckets are created.
36
+
37
+ * ``running`` = True
38
+
39
+ aggregation will be calculated in a sliding window.
40
+
41
+ * ``bucket_interval`` = N (N > 0)
42
+
43
+ Window size will be N. Output tick will be generated when tick "enter" window (**arrival event**) and
44
+ when "exit" window (**exit event**)
45
+
46
+ * ``bucket_interval`` = 0
47
+
48
+ Left boundary of window will be bound to start time. For each tick aggregation will be calculated in
49
+ [start_time; tick_t].
50
+
51
+ * ``running`` = False
52
+
53
+ buckets partition the [query start time, query end time) interval into non-overlapping intervals
54
+ of size ``bucket_interval`` (with the last interval possibly of a smaller size).
55
+ If ``bucket_interval`` is set to **0** a single bucket for the entire interval is created.
56
+
57
+ Note that in non-running mode OneTick unconditionally divides the whole time interval
58
+ into specified number of buckets.
59
+ It means that you will always get this specified number of ticks in the result,
60
+ even if you have less ticks in the input data.
61
+
62
+ Default: False
63
+ ''',
64
+ default=False,
65
+ annotation=bool
66
+ )
67
+
68
+ _all_fields_doc = param_doc(
69
+ name='all_fields',
70
+ desc="""
71
+ * ``all_fields`` = True
72
+
73
+ output ticks include all fields from the input ticks
74
+
75
+ * ``running`` = True
76
+
77
+ an output tick is created only when a tick enters the sliding window
78
+
79
+ * ``running`` = False
80
+
81
+ fields of first tick in bucket will be used
82
+
83
+ * ``all_fields`` = False and ``running`` = True
84
+
85
+ output ticks are created when a tick enters or leaves the sliding window.
86
+
87
+ * ``all_fields`` = "when_ticks_exit_window" and ``running`` = True
88
+
89
+ output ticks are generated only for exit events, but all attributes from the exiting tick are copied over
90
+ to the output tick and the aggregation is added as another attribute.
91
+ """,
92
+ default=False,
93
+ annotation=Union[bool, str]
94
+ )
95
+ _all_fields_with_policy_doc = param_doc(
96
+ name='all_fields',
97
+ desc="""
98
+ - If ``all_fields`` False - output tick will have only aggregation fields.
99
+
100
+ - If ``all_fields`` False and ``running`` True - output ticks are created when a tick enters or leaves the
101
+ sliding window.
102
+
103
+ - If ``all_fields`` True - an output tick is generated only for arrival events,
104
+ but all attributes from the input tick causing an arrival event are copied over to the output tick
105
+ and the aggregation is added as another attribute.
106
+
107
+ - If ``all_fields`` set to "first", "last", "high", or "low" - explicitly set tick selection policy for all
108
+ fields values. For "high" and "low" "PRICE" field will be selected as an input.
109
+ Otherwise, you will get the runtime error.
110
+ If ``all_fields`` is set to one of these values, ``running`` can't be `True`.
111
+
112
+ - If ``all_fields`` is aggregation ``HighTick`` or ``LowTick`` - set tick selection policy for all fields values to
113
+ "high" or "low" accordingly. But instead of "PRICE" the field selected as input will be set as aggregation's
114
+ first parameter.
115
+ """,
116
+ default=False,
117
+ annotation=Union[Literal[True, False, "first", "last", "high", "low"], HighTick, LowTick]
118
+ )
119
+ _bucket_interval_doc = param_doc(
120
+ name='bucket_interval',
121
+ desc="""
122
+ Determines the length of each bucket (units depends on ``bucket_units``).
123
+
124
+ If :py:class:`~onetick.py.Operation` of bool type is passed, acts as ``bucket_end_condition``.
125
+
126
+ Bucket interval can also be set as a *float* value
127
+ if ``bucket_units`` is set to *seconds*.
128
+ Note that values less than 0.001 (1 millisecond) are not supported.
129
+
130
+ Bucket interval can be set via some of the :ref:`datetime offset objects <datetime_offsets>`:
131
+ :py:func:`otp.Milli <onetick.py.Milli>`, :py:func:`otp.Second <onetick.py.Second>`,
132
+ :py:func:`otp.Minute <onetick.py.Minute>`, :py:func:`otp.Hour <onetick.py.Hour>`,
133
+ :py:func:`otp.Day <onetick.py.Day>`, :py:func:`otp.Month <onetick.py.Month>`.
134
+ In this case you could omit setting ``bucket_units`` parameter.
135
+
136
+ Bucket interval can also be set with integer :py:class:`~onetick.py.core.column_operations.base.OnetickParameter`
137
+ or :py:meth:`symbol parameter <onetick.py.core._source.symbol.SymbolType.__getitem__>`.
138
+ """,
139
+ default=0,
140
+ annotation=Union[int, float, Operation, OnetickParameter, _SymbolParamColumn, OTPBaseTimeOffset],
141
+ str_annotation=('int or float or :py:class:`~onetick.py.Operation`'
142
+ ' or :py:class:`~onetick.py.core.column_operations.base.OnetickParameter`'
143
+ ' or :py:meth:`symbol parameter <onetick.py.core._source.symbol.SymbolType.__getitem__>`'
144
+ ' or :ref:`datetime offset object <datetime_offsets>`'),
145
+ )
146
+ _bucket_time_doc = param_doc(
147
+ name='bucket_time',
148
+ desc="""
149
+ Control output timestamp.
150
+
151
+ * **start**
152
+
153
+ the timestamp assigned to the bucket is the start time of the bucket.
154
+
155
+ * **end**
156
+
157
+ the timestamp assigned to the bucket is the end time of the bucket.
158
+ """,
159
+ default="end",
160
+ annotation=Literal["start", "end"]
161
+ )
162
+ _bucket_units_doc_kwargs = dict(
163
+ name='bucket_units',
164
+ desc="""
165
+ Set bucket interval units.
166
+
167
+ By default, if ``bucket_units`` and ``bucket_end_condition`` not specified, set to **seconds**.
168
+ If ``bucket_end_condition`` specified, then ``bucket_units`` set to **flexible**.
169
+
170
+ If set to **flexible** then ``bucket_end_condition`` must be set.
171
+
172
+ Note that **seconds** bucket unit doesn't take into account daylight-saving time of the timezone,
173
+ so you may not get expected results when using, for example, 24 * 60 * 60 seconds as bucket interval.
174
+ In such case use **days** bucket unit instead.
175
+ See example in :py:func:`onetick.py.agg.sum`.
176
+ """,
177
+ default=None,
178
+ annotation=Optional[Literal["seconds", "ticks", "days", "months", "flexible"]]
179
+ )
180
+ _bucket_units_doc = param_doc(**_bucket_units_doc_kwargs)
181
+ _bucket_units_ob_doc = param_doc(**{
182
+ **_bucket_units_doc_kwargs,
183
+ # OB eps do not support 'ticks' bucket units
184
+ 'annotation': Optional[Literal["seconds", "days", "months", "flexible"]]
185
+ })
186
+
187
+ _bucket_end_condition_doc = param_doc(
188
+ name='bucket_end_condition',
189
+ str_annotation='condition',
190
+ desc='''
191
+ An expression that is evaluated on every tick. If it evaluates to "True", then a new bucket is created.
192
+ This parameter is only used if ``bucket_units`` is set to "flexible".
193
+
194
+ Also can be set via ``bucket_interval`` parameter by passing :py:class:`~onetick.py.Operation` object.
195
+ ''',
196
+ annotation=Optional[Operation],
197
+ default=None
198
+ )
199
+ _end_condition_per_group_doc = param_doc(
200
+ name='end_condition_per_group',
201
+ desc='''
202
+ Controls application of ``bucket_end_condition`` in groups.
203
+
204
+ * ``end_condition_per_group`` = True
205
+
206
+ ``bucket_end_condition`` is applied only to the group defined by ``group_by``
207
+
208
+ * ``end_condition_per_group`` = False
209
+
210
+ ``bucket_end_condition`` applied across all groups
211
+
212
+ This parameter is only used if ``bucket_units`` is set to "flexible".
213
+
214
+ When set to True, applies to all bucketing conditions. Useful, for example, if you need to specify ``group_by``,
215
+ and you want to group items first, and create buckets after that.
216
+ ''',
217
+ annotation=bool,
218
+ default=False
219
+ )
220
+ _boundary_tick_bucket_doc = param_doc(
221
+ name='boundary_tick_bucket',
222
+ desc='''
223
+ Controls boundary tick ownership.
224
+
225
+ * **previous**
226
+
227
+ A tick on which ``bucket_end_condition`` evaluates to "true" belongs to the bucket being closed.
228
+
229
+ * **new**
230
+
231
+ tick belongs to the new bucket.
232
+
233
+ This parameter is only used if ``bucket_units`` is set to "flexible"
234
+ ''',
235
+ annotation=Literal["new", "previous"],
236
+ default="new"
237
+ )
238
+ _group_by_doc = param_doc(
239
+ name='group_by',
240
+ str_annotation='list, str or expression',
241
+ desc='''
242
+ When specified, each bucket is broken further into additional sub-buckets based on specified field values.
243
+ If :py:class:`~onetick.py.Operation` is used then GROUP_{i} column is added. Where i is index in group_by list.
244
+ For example, if Operation is the only element in ``group_by`` list then GROUP_0 field will be added.
245
+ ''',
246
+ annotation=Optional[Union[List, str, Operation]],
247
+ default=None
248
+ )
249
+ _groups_to_display_doc = param_doc(
250
+ name='groups_to_display',
251
+ desc='''
252
+ Specifies for which sub-buckets (groups) ticks should be shown for each bucket interval.
253
+ By default **all** groups are shown at the end of each bucket interval.
254
+ If this parameter is set to **event_in_last_bucket**, only the groups that received at least one tick
255
+ within a given bucket interval are shown.
256
+ ''',
257
+ annotation=Literal["all", "previous"],
258
+ default="all",
259
+ )
260
+ _biased_doc = param_doc(
261
+ name='biased',
262
+ annotation=bool,
263
+ desc='''
264
+ Switches between biased and unbiased standard deviation calculation.
265
+ ''',
266
+ default=True
267
+ )
268
+ _n_doc = param_doc(
269
+ name='n',
270
+ default=1,
271
+ desc='''
272
+ Number of ticks to output''',
273
+ annotation=int
274
+ )
275
+ _keep_timestamp_doc = param_doc(
276
+ name='keep_timestamp',
277
+ desc="""
278
+ If True, timestamps of the output ticks are the same as timestamps of the original ticks.
279
+ Otherwise, timestamps of the output ticks are determined by bucket_time, and original timestamps
280
+ are put in the TICK_TIME field.
281
+ """,
282
+ annotation=bool,
283
+ default=True
284
+ )
285
+ _time_series_type_common = dict(
286
+ name='time_series_type',
287
+ desc='''
288
+ Controls initial value for each bucket
289
+
290
+ * **event_ts**
291
+
292
+ only ticks from current bucket used for calculations
293
+
294
+ * **state_ts**
295
+
296
+ * if there is a tick in bucket with timestamp = bucket start
297
+
298
+ only ticks in bucket used for calculation max value
299
+ * else
300
+
301
+ latest tick from previous bucket included in current bucket
302
+ ''',
303
+ annotation=Literal["event_ts", "state_ts"],
304
+ )
305
+ _time_series_type_doc = param_doc(
306
+ default="event_ts",
307
+ **_time_series_type_common,
308
+ )
309
+ _time_series_type_w_doc = param_doc(
310
+ default="state_ts",
311
+ **_time_series_type_common,
312
+ )
313
+ _selection_doc = param_doc(
314
+ name='selection',
315
+ desc='''
316
+ Controls the selection of the respective beginning or trailing part of ticks.
317
+ ''',
318
+ annotation=Literal["first", "last"],
319
+ default="first"
320
+ )
321
+ _side_doc = param_doc(
322
+ name='side',
323
+ desc="""
324
+ Specifies whether the function is to be applied to sell orders (ASK), buy orders (BID), or both (empty).
325
+ """,
326
+ annotation=Literal['ASK', 'BID'],
327
+ default=None,
328
+ )
329
+ _max_levels_doc = param_doc(
330
+ name='max_levels',
331
+ desc="""
332
+ Number of order book levels (between 1 and 100_000) that need to be computed.
333
+ If empty, all levels will be computed.
334
+ """,
335
+ annotation=int,
336
+ default=None,
337
+ )
338
+ _min_levels_doc = param_doc(
339
+ name='min_levels',
340
+ desc="""
341
+ Minimum number of order book levels, if so many levels are available in the book,
342
+ that need to be included into the computation when either ``max_depth_shares`` or/and ``max_depth_for_price``
343
+ are specified
344
+ """,
345
+ annotation=int,
346
+ default=None,
347
+ )
348
+ _max_depth_shares_doc = param_doc(
349
+ name='max_depth_shares',
350
+ desc="""
351
+ The total number of shares (i.e., the combined SIZE across top several levels of the book)
352
+ that determines the number of order book levels that need to be part of the order book computation.
353
+ If that number of levels exceeds `max_levels`, only `max_levels` levels of the book will be computed.
354
+ The shares in excess of `max_depth_shares`, from the last included level, are not taken into account.
355
+ """,
356
+ annotation=int,
357
+ default=None,
358
+ )
359
+ _max_depth_for_price_doc = param_doc(
360
+ name='max_depth_for_price',
361
+ desc="""
362
+ The multiplier, product of which with the price at the top level of the book determines maximum price distance
363
+ from the top of the book for the levels that are to be included into the book.
364
+ In other words, only bids at <top_price>*(1-`max_depth_for_price`) and above
365
+ and only asks of <top_price>*(1+`max_depth_for_price`) and less will be returned.
366
+ If the number of the levels that are to be included into the book, according to this criteria,
367
+ exceeds `max_levels`, only `max_levels` levels of the book will be returned.
368
+ """,
369
+ annotation=float,
370
+ default=None,
371
+ )
372
+ _min_levels_doc = param_doc(
373
+ name='min_levels',
374
+ desc="""
375
+ Minimum number of order book levels, if so many levels are available in the book, that need to be included
376
+ into the computation when either `max_depth_shares` or/and `max_depth_for_price` are specified.
377
+ """,
378
+ annotation=int,
379
+ default=None,
380
+ )
381
+ _max_initialization_days_doc = param_doc(
382
+ name='max_initialization_days',
383
+ desc="""
384
+ This parameter specifies how many days back book event processors should go in order to find
385
+ the latest full state of the book.
386
+ The query will not go back resulting number of days if it finds initial book state earlier.
387
+ When book event processors are used after VIRTUAL_OB EP, this parameter should be set to 0.
388
+ When set, this parameter takes precedence over the configuration parameter BOOKS.MAX_INITIALIZATION_DAYS.
389
+ """,
390
+ annotation=int,
391
+ default=1,
392
+ )
393
+ _book_uncross_method_doc = param_doc(
394
+ name='book_uncross_method',
395
+ desc="""
396
+ When set to "REMOVE_OLDER_CROSSED_LEVELS", all ask levels that have price lower or equal to
397
+ the price of a new bid tick get removed from the book, and all bid levels that have price higher or equal
398
+ to the price of a new ask tick get removed from the book.
399
+ """,
400
+ annotation=Literal['REMOVE_OLDER_CROSSED_LEVELS'],
401
+ default=None,
402
+ )
403
+ _dq_events_that_clear_book_doc = param_doc(
404
+ name='dq_events_that_clear_book',
405
+ desc="""
406
+ A list of names of data quality events arrival of which should clear the order book.
407
+ """,
408
+ annotation=List[str],
409
+ default=None,
410
+ )
411
+ _best_ask_price_field_doc = param_doc(
412
+ name='best_ask_price_field',
413
+ desc="""
414
+ If specified, this parameter represents the name of the field value of which represents the lowest ask price
415
+ starting from which the book ask size is to be computed.
416
+ This value would also be used as the top price, relative to which ``max_depth_for_price`` would be computed.
417
+ """,
418
+ annotation=Union[str, Column],
419
+ default=None,
420
+ )
421
+ _best_bid_price_field_doc = param_doc(
422
+ name='best_bid_price_field',
423
+ desc="""
424
+ If specified, this parameter represents the name of the field value of which represents the highest bid price
425
+ starting from which the book bid size is to be computed.
426
+ This value would also be used as the top price, relative to which ``max_depth_for_price`` would be computed.
427
+ """,
428
+ annotation=Union[str, Column],
429
+ default=None,
430
+ )
431
+ _bucket_interval_ob_num_levels_doc = param_doc(
432
+ name='bucket_interval',
433
+ desc="""
434
+ Determines the length of each bucket in seconds.
435
+
436
+ Bucket interval can be set via :ref:`datetime offset objects <datetime_offsets>`
437
+ like :py:func:`otp.Second <onetick.py.Second>`, :py:func:`otp.Minute <onetick.py.Minute>`,
438
+ :py:func:`otp.Hour <onetick.py.Hour>`, :py:func:`otp.Day <onetick.py.Day>`.
439
+ In this case it will be converted to seconds.
440
+ """,
441
+ default=0,
442
+ annotation=Union[int, OTPBaseTimeOffset],
443
+ str_annotation='int or :ref:`datetime offset object <datetime_offsets>`',
444
+ )
445
+ _identify_source_doc = param_doc(
446
+ name='identify_source',
447
+ desc="""
448
+ When this parameter is set to "true" and the input stream is fed through the VIRTUAL_OB event processor
449
+ (with the QUOTE_SOURCE_FIELDS parameter specified) and `group_by` is not set to be "SOURCE"
450
+ it will separate a tick with the same price from different sources into multiple ticks.
451
+ The parameter can also be used when merging ticks from multiple feeds.
452
+ Each feed going into the merge would need an ADD_FIELD EP source value set for the VALUE parameter,
453
+ where the value would be different for each leg.
454
+ """,
455
+ annotation=bool,
456
+ default=False,
457
+ )
458
+ _show_full_detail_doc = param_doc(
459
+ name='show_full_detail',
460
+ desc="""
461
+ When set to "true" and if the state key of the input ticks consists of some fields besides PRICE,
462
+ output ticks will contain all fields from the input ticks for each price level.
463
+ When set to "false" only PRICE, UPDATE_TIME, SIZE, LEVEL, and BUY_SELL_FLAG fields will be populated.
464
+ Note: setting this flag to "true" has no effect on a time series that does not have a state key.
465
+ """,
466
+ annotation=bool,
467
+ default=False,
468
+ )
469
+ _show_only_changes_doc = param_doc(
470
+ name='show_only_changes',
471
+ desc="""
472
+ When set to true, the output stream carries only changes to the book. The representation is as follows:
473
+ * Changed and added levels are represented by themselves.
474
+ * Deleted levels are shown with a size and level of zero.
475
+
476
+ As with other modes, correct detection of update boundaries may require setting the `book_delimiters` option.
477
+ """,
478
+ annotation=bool,
479
+ default=False,
480
+ )
481
+ _book_delimiters_doc = param_doc(
482
+ name='book_delimiters',
483
+ desc="""
484
+ When set to "D" an extra tick is created after each book.
485
+ Also, an additional column, called DELIMITER, is added to output ticks.
486
+ The extra tick has values of all fields set to the defaults (0,NaN,""),
487
+ except the delimiter field, which is set to "D."
488
+ All other ticks have the DELIMITER set to zero (0).
489
+ """,
490
+ annotation=Literal['D'],
491
+ default=None,
492
+ )
493
+ _state_key_max_inactivity_sec_doc = param_doc(
494
+ name='state_key_max_inactivity_sec',
495
+ desc="""
496
+ If set, specifies in how many seconds after it was added
497
+ a given state key should be automatically removed from the book.
498
+ """,
499
+ annotation=int,
500
+ default=None,
501
+ )
502
+ _size_max_fractional_digits_doc = param_doc(
503
+ name='size_max_fractional_digits',
504
+ desc="""
505
+ Specifies maximum number of digits after dot in SIZE, if SIZE can be fractional.
506
+ """,
507
+ annotation=int,
508
+ default=0,
509
+ )
510
+ _query_fun_doc = param_doc(
511
+ name='query_fun',
512
+ desc="""
513
+ Function that takes :class:`~onetick.py.Source` as a parameter,
514
+ applies some aggregation logic to it
515
+ and returns :class:`~onetick.py.Source` as a result.
516
+ Note that currently only methods that support dynamic symbol change
517
+ could be used in the provided function.
518
+ For example, :meth:`~onetick.py.Source.rename` can't be used.
519
+ If you try to use such methods here, you will get an error during runtime.
520
+ """,
521
+ annotation=Callable,
522
+ )
523
+ _bucket_delimiter_doc = param_doc(
524
+ name='bucket_delimiter',
525
+ desc="""
526
+ When set to ``True`` an extra tick is created after each bucket.
527
+ Also, an additional column, called DELIMITER, is added to output ticks.
528
+ The extra tick has values of all fields set to the defaults (0,NaN,""),
529
+ except the delimiter field, which is set to "D"
530
+ All other ticks have the DELIMITER set to string zero "0".
531
+ """,
532
+ annotation=bool,
533
+ default=False,
534
+ )
535
+ _large_ints_doc = param_doc(
536
+ name='large_ints',
537
+ desc="""
538
+ This parameter should be set
539
+ if the input field of this aggregation may contain integer values that consist of 15 digits or more.
540
+
541
+ Such large integer values cannot be represented by the double type without precision errors
542
+ and thus require special handling.
543
+
544
+ If set to **True** , the input field is expected to be a 64-bit integer number.
545
+ The output field will also have 64-bit integer type.
546
+ When no tick belongs to a given time bucket, the output value is set to a minimum of 64-bit integer.
547
+
548
+ When this parameter set to :py:class:`onetick.py.adaptive`, the aggregation behaves the same way
549
+ as when this parameter is set to True when the input field is a 64-bit integer type,
550
+ and the same way as when this parameter is set to **False** when the input field is not a 64-bit integer type.
551
+ """,
552
+ annotation=bool,
553
+ str_annotation="bool or :py:class:`onetick.py.adaptive`",
554
+ default=False,
555
+ )
556
+ _null_int_val_doc = param_doc(
557
+ name='null_int_val',
558
+ desc="""
559
+ The value of this parameter is considered to be the equivalent of ``NaN``
560
+ when ``large_ints`` is set to ``True`` or
561
+ when ``large_ints`` is set to :py:class:`onetick.py.adaptive` and the input field is a 64-bit integer type.
562
+ """,
563
+ annotation=int,
564
+ default=0,
565
+ )
566
+ _skip_tick_if_doc = param_doc(
567
+ name='skip_tick_if',
568
+ desc="""
569
+ If value of the input field is equal to the value in this parameter,
570
+ this tick is ignored in the aggregation computation.
571
+
572
+ This parameter is currently only supported for numeric fields.
573
+ """,
574
+ annotation=Optional[int],
575
+ default=None,
576
+ )
577
+ _default_tick_doc = param_doc(
578
+ name='default_tick',
579
+ desc="""
580
+ Mapping of input stream field names to some values.
581
+ When set, exactly one tick with specified default values will be created for each empty bucket.
582
+ If default value is specified as ``None``, then fields are initialized to the corresponding "zero" values:
583
+ 0 for integer types, NaN for doubles, empty string for string types, etc.
584
+ """,
585
+ annotation=Optional[dict],
586
+ default=None,
587
+ )
588
+ _decay_doc = param_doc(
589
+ name='decay',
590
+ desc="""
591
+ Weight decay. If **decay_value_type** is set to ``lambda``,
592
+ **decay** provides the value of the **Lambda** variable in the aforementioned formula.
593
+ Otherwise, if **decay_value_type** is set to ``half_life_index``, **decay** specifies the necessary number
594
+ of consecutive ticks, the first one of which would have twice less the weight of the last one.
595
+ The **Lambda** value is then calculated using this number.
596
+ """,
597
+ annotation=float,
598
+ )
599
+ _decay_value_type_common = dict(
600
+ name='decay_value_type',
601
+ desc="""
602
+ The decay value can specified either directly or indirectly, controlled respectively by
603
+ **lambda** and **half_life_index** values of this parameter.
604
+ """,
605
+ annotation=Literal['lambda', 'half_life_index'],
606
+ )
607
+ _decay_value_type_doc = param_doc(
608
+ default='lambda',
609
+ **_decay_value_type_common,
610
+ )
611
+ _decay_value_type_hl_doc = param_doc(
612
+ default='half_life_index',
613
+ **_decay_value_type_common,
614
+ )
615
+ _degree_doc = param_doc(
616
+ name='degree',
617
+ annotation=int,
618
+ desc='''
619
+ The order (degree) of the standardized moment to compute, denoted as k in the description above.
620
+ ''',
621
+ default=3,
622
+ )
623
+ _weight_field_name_doc = param_doc(
624
+ name='weight_field_name',
625
+ str_annotation='Optional, str or Column',
626
+ annotation=Optional[Union[str, Column]],
627
+ desc='''
628
+ The name of the field that contains the current value of weight for a member of the portfolio
629
+ that contributed the tick.
630
+
631
+ You can also specify weight through the value of symbol parameter ``WEIGHT``.
632
+
633
+ If ``weight_field_name`` is specified, all ticks should have the field pointed by this parameter and the value
634
+ of this field is used as the weight.
635
+
636
+ If weights are not specified in any of these ways, and you are running a single-stage query,
637
+ the weights take the default value ``1``.
638
+ ''',
639
+ default='',
640
+ )
641
+ _weight_multiplier_field_name_doc = param_doc(
642
+ name='weight_multiplier_field_name',
643
+ desc="""
644
+ Name of the field, value from which is used for multiplying portfolio value result.
645
+ """,
646
+ annotation=str,
647
+ default='',
648
+ )
649
+ _portfolio_side_doc = param_doc(
650
+ name='side',
651
+ desc="""
652
+ When set to **long**, the price of the portfolio is computed only for the input time series with ``weight > 0``.
653
+
654
+ When set to **short**, the price of the portfolio is computed only for the input time series with ``weight < 0``.
655
+
656
+ When set to **both**, the price of the portfolio is computed for all input time series.
657
+ """,
658
+ annotation=Literal['long', 'short', 'both'],
659
+ default='both',
660
+ )
661
+ _weight_type_doc = param_doc(
662
+ name='weight_type',
663
+ desc="""
664
+ When set to ``absolute``, the portfolio price is computed as the sum of ``input_field_value*weight``
665
+ across all members of the portfolio.
666
+
667
+ When set to ``relative``, the portfolio price is computed as the sum of
668
+ ``input_field_value*weight/sum_of_all_weights`` across all members of the portfolio.
669
+ """,
670
+ annotation=Literal['absolute', 'relative'],
671
+ default='absolute',
672
+ )
673
+ _portfolios_query_doc = param_doc(
674
+ name='portfolios_query',
675
+ desc="""
676
+ A mandatory parameter that the specifies server-side `.otq` file that is expected to return
677
+ mandatory columns ``PORTFOLIO_NAME`` and ``SYMBOL_NAME``,
678
+ as well as an optional columns ``WEIGHT``, ``FX_SYMBOL_NAME`` and ``FX_MULTIPLY``.
679
+
680
+ For a local OneTick server you can pass `otp.Source` objects.
681
+ """,
682
+ str_annotation='str, :class:`Source`',
683
+ )
684
+ _portfolios_query_params_doc = param_doc(
685
+ name='portfolios_query_params',
686
+ desc="""
687
+ An optional parameter that specifies parameters of the query specified in `portfolios_query`.
688
+ """,
689
+ annotation=Union[str, Dict[str, str]],
690
+ default='',
691
+ )
692
+ _portfolio_value_field_name_doc = param_doc(
693
+ name='portfolio_value_field_name',
694
+ desc="""
695
+ List of the names (string with comma-separated list or ``list`` of strings/``Columns``) of the output fields
696
+ which contain computed values of the portfolio.
697
+
698
+ The number of the field names must match the number of the field names listed in the `columns` parameter.
699
+ """,
700
+ annotation=Union[str, List[Union[str, Column]]],
701
+ default='VALUE',
702
+ )
703
+ _columns_portfolio_doc = param_doc(
704
+ name='columns',
705
+ str_annotation='str or list of Column or str',
706
+ desc='''
707
+ A list of the names of the input fields for which portfolio value is computed.
708
+
709
+ Could be set as a comma-separated list of the names or ``list`` of name strings/``Columns`` objects.
710
+ ''',
711
+ annotation=Union[str, List[Union[str, Column]]],
712
+ default='PRICE',
713
+ )
714
+ _symbols_doc = param_doc(
715
+ name='symbols',
716
+ desc="""
717
+ Symbol(s) from which data should be taken.
718
+ """,
719
+ str_annotation=('str, list of str, :class:`Source`, :class:`query`, :py:func:`eval query <onetick.py.eval>`, '
720
+ ':py:class:`onetick.query.GraphQuery`.'),
721
+ default=None,
722
+ )
723
+ _interest_rate_doc = param_doc(
724
+ name='interest_rate',
725
+ desc='''
726
+ The risk-free interest rate.
727
+
728
+ Could be set via a tick field, by specifying name of that field as string or
729
+ passing :py:class:`~onetick.py.Column` object.
730
+ ''',
731
+ annotation=Optional[Union[int, float, str, Column]],
732
+ default=None,
733
+ )
734
+ _price_field_doc = param_doc(
735
+ name='price_field',
736
+ desc='''
737
+ The name of the field carrying the price value.
738
+ ''',
739
+ annotation=Union[str, Column],
740
+ default='PRICE',
741
+ )
742
+ _option_price_field_doc = param_doc(
743
+ name='option_price_field',
744
+ desc='''
745
+ The name of the field carrying the option price value.
746
+ ''',
747
+ annotation=Union[str, Column],
748
+ default='OPTION_PRICE',
749
+ )
750
+ _method_doc = param_doc(
751
+ name='method',
752
+ desc='''
753
+ Allowed values are ``newton``, ``newton_with_fallback`` and ``bisections``.
754
+
755
+ Choose between ``newton`` and ``bisections`` for finding successively better approximations
756
+ to the implied volatility value.
757
+
758
+ Choose ``newton_with_fallback`` to automatically fall back to ``bisections`` method
759
+ when ``newton`` fails to converge.
760
+ ''',
761
+ annotation=str,
762
+ default='newton',
763
+ )
764
+ _precision_doc = param_doc(
765
+ name='precision',
766
+ desc='''
767
+ Precision of the implied volatility value.
768
+ ''',
769
+ annotation=float,
770
+ default='1.0e-5',
771
+ )
772
+ _value_for_non_converge_doc = param_doc(
773
+ name='value_for_non_converge',
774
+ desc='''
775
+ Allowed values are ``nan_val`` and ``closest_found_val``, where ``closest_found_val`` stands
776
+ for the volatility value for which the difference between calculated option price and input option price is minimal.
777
+
778
+ Choose between ``nan_val`` and ``closest_found_val`` as implied volatility value,
779
+ when the root-finding method does not converge within the specified precision.
780
+ ''',
781
+ annotation=str,
782
+ default='nan_val',
783
+ )
784
+ _option_type_field_doc = param_doc(
785
+ name='option_type_field',
786
+ desc='''
787
+ Specifies name of the field, which carries the option type (either **CALL** or **PUT**).
788
+ ''',
789
+ annotation=Union[str, Column],
790
+ default='',
791
+ )
792
+ _strike_price_field_doc = param_doc(
793
+ name='strike_price_field',
794
+ desc='''
795
+ Specifies name of the field, which carries the strike price of the option.
796
+ ''',
797
+ annotation=Union[str, Column],
798
+ default='',
799
+ )
800
+ _days_in_year_doc = param_doc(
801
+ name='days_in_year',
802
+ desc='''
803
+ Specifies number of days in a year (say, 365 or 252 (business days, etc.).
804
+ Used with ``days_till_expiration`` parameter to compute the fractional years till expiration.
805
+ ''',
806
+ annotation=int,
807
+ default=365,
808
+ )
809
+ _days_till_expiration_field_doc = param_doc(
810
+ name='days_till_expiration_field',
811
+ desc='''
812
+ Specifies name of the field, which carries number of days till expiration of the option.
813
+ ''',
814
+ annotation=Union[str, Column],
815
+ default='',
816
+ )
817
+ _expiration_date_field_doc = param_doc(
818
+ name='expiration_date_field',
819
+ desc='''
820
+ Specifies name of the field, which carries the expiration date of the option, in **YYYYMMDD** format.
821
+ ''',
822
+ annotation=Union[str, Column],
823
+ default='',
824
+ )
825
+
826
+
827
+ class DocMetaclass(type):
828
+ def __new__(mcs, name, bases, attrs, parameters: Optional[list] = None):
829
+ cls = super().__new__(mcs, name, bases, attrs)
830
+ doc = None
831
+ if '__init__' in attrs and cls.__init__.__doc__: # type: ignore
832
+ doc = cls.__init__.__doc__ # type: ignore
833
+ elif '__doc__' in attrs:
834
+ doc = attrs['__doc__']
835
+ if doc and parameters:
836
+ doc = Docstring(doc)
837
+ for param in parameters:
838
+ doc['Parameters'] = param
839
+ cls.__init__.__doc__ = doc.build() # type: ignore
840
+
841
+ return cls
842
+
843
+
844
+ def copy_method(obj, mimic=True, drop_examples=False):
845
+
846
+ """
847
+ Decorator to copy aggregation function as method
848
+
849
+ We assume that this decorator will be used only with aggregations.
850
+
851
+ Updates (same way as for dict) `obj` docstring with decorated function docstring
852
+ Updates decorated function signature: [self] + `obj` signature
853
+ if mimic is True - won't execute decorated function, will execute `obj` and apply self instead
854
+ if mimic is False - will execute decorated function as is
855
+
856
+ Parameters
857
+ ----------
858
+ obj:
859
+ donor aggregation function
860
+ mimic: bool, default=True
861
+ flag to execute decorated function or not
862
+ drop_examples: bool
863
+ Can be used to drop examples from ``obj`` docstring.
864
+ """
865
+
866
+ doc = obj.__doc__ or ''
867
+ params = [Parameter(name='self',
868
+ kind=Parameter.POSITIONAL_OR_KEYWORD)] + list(Signature.from_callable(obj).parameters.values())
869
+ doc = Docstring(doc)
870
+ if drop_examples and 'Examples' in doc.docstring:
871
+ doc.docstring.pop('Examples')
872
+
873
+ def _decorator(fun):
874
+ @wraps(fun)
875
+ def _inner(self, *args, **kwargs):
876
+ if mimic:
877
+ agg = obj(*args, **kwargs)
878
+ return agg.apply(self)
879
+ else:
880
+ return fun(self, *args, **kwargs)
881
+ fun_doc = fun.__doc__ or ''
882
+ fun_doc = Docstring(fun_doc)
883
+ doc.update(fun_doc)
884
+ _inner.__signature__ = Signature(parameters=params, return_annotation='Source')
885
+ _inner.__doc__ = doc.build()
886
+ return _inner
887
+ return _decorator
888
+
889
+
890
+ def copy_signature(obj, add_self=False, drop_parameters=[], return_annotation='Source'):
891
+ """
892
+ Decorator that copies signature of the callable ``obj`` to the decorated function.
893
+ """
894
+ drop_parameters = drop_parameters or []
895
+ obj_parameters = list(
896
+ param for param_name, param in Signature.from_callable(obj).parameters.items()
897
+ if param_name not in drop_parameters
898
+ )
899
+ params = []
900
+ if add_self:
901
+ params.append(
902
+ Parameter(name='self', kind=Parameter.POSITIONAL_ONLY)
903
+ )
904
+ params.extend(obj_parameters)
905
+
906
+ def _decorator(fun):
907
+ @wraps(fun)
908
+ def _inner(*args, **kwargs):
909
+ return fun(*args, **kwargs)
910
+ _inner.__signature__ = Signature(parameters=params, return_annotation=return_annotation)
911
+ return _inner
912
+ return _decorator