otp.state.tick_set_unordered#
- tick_set_unordered(insertion_policy, key_fields, default_value=None, max_distinct_keys=- 1, scope='query')[source]#
Defines unordered tick set.
- Parameters
insertion_policy ('oldest' or 'latest') – ‘oldest’ specifies not to overwrite ticks with the same keys. ‘latest’ makes the last inserted tick overwrite the one with the same keys (if existing).
key_fields (str, list of str) –
max_distinct_keys (int) – maximum expected size of the set or -1, if the number of ticks is not known.
default_value (
eval query
) – Eval query to initialize unordered tick set from.scope (str,) – Scope for the state variable. Possible values are: query, branch, cross_symbol, all_inputs, all_outputs
- Return type
Examples
>>> def fsq(): ... return otp.Ticks(B=[1, 1, 2, 2, 3, 3]) >>> data = otp.Tick(A=1) >>> data.state_vars['SET'] = otp.state.tick_set_unordered('oldest', 'B', otp.eval(fsq)) >>> data = data.state_vars['SET'].dump() >>> data.to_df()[['B']] B 0 1 1 2 2 3
- class TickSetUnordered(*args, max_distinct_keys=- 1, **kwargs)[source]#
Bases:
onetick.py.core._internal._state_objects.TickSet
Represents an unordered tick set. This class should only be created with
onetick.py.state.tick_set_unordered()
function and should be added to theonetick.py.Source.state_vars()
dictionary of theonetick.py.Source
and can be accessed only via this dictionary.Examples
>>> data = otp.Tick(A=1) >>> data.state_vars['SET'] = otp.state.tick_set_unordered('oldest', 'A') >>> data = data.state_vars['SET'].dump()
See also
- clear()#
Clear tick set. Can be used in per-tick script and on Source directly.
- inplace: bool
If
True
current source will be modified else modified copy will be returned. Makes sense only if used not in per-tick script.
- Returns
if
inplace
is False and method is not used in per-tick scriptthen returns
Source
copy.
Examples
Can be used in per-tick script:
>>> def fun(tick): ... tick.state_vars['SET'].clear() >>> data = otp.Tick(A=1) >>> data.state_vars['SET'] = otp.state.tick_set('oldest', 'A') >>> data = data.script(fun)
Can be used in source columns operations:
>>> data = otp.Tick(A=1) >>> data.state_vars['SET'] = otp.state.tick_set('oldest', 'A', otp.eval(otp.Tick(A=1))) >>> data = data.state_vars['SET'].clear()
>>> data.state_vars['SET'].dump().to_df() Empty DataFrame Columns: [] Index: []
- dump(when_to_dump='every_tick', **kwargs)#
Propagates all ticks from a given tick sequence upon the arrival of input tick. Timestamps of all propagated ticks are equal to the input tick’s TIMESTAMP.
- Parameters
propagate_input_ticks (bool) – Propagate input ticks or not.
when_to_dump (str) –
first_tick - Propagates once before input ticks. There must be at least one input tick.
before_tick - Propagates once before input ticks. Content will be propagated even if there are no input ticks.
every_tick - Propagates before each input tick.
delimiter ('tick', 'flag or None) –
This parameter specifies the policy for adding the delimiter field. The name of the additional field is “DELIMITER” +
added_field_name_suffix
. Possible options are:None - No additional field is added to propagated ticks.
’tick’ - An extra tick is created after the last tick. Also, an additional column is added to output ticks. The extra tick has values of all fields set to the defaults (0,NaN,””), except the delimiter field, which is set to string “D”. All other ticks have this field’s value set to empty string.
’flag’ - The delimiter field is appended to each output tick. The field’s value is empty for all ticks except the last tick of the tick sequence, which is string “D”.
added_field_name_suffix (str or None) – The suffix to add to the name of the additional field.
inplace (bool) – If
True
current source will be modified else modified copy will be returned
- Return type
if
inplace
is False then returnsSource
copy.
Examples
>>> def another_query(): ... return otp.Ticks(B=[1, 2, 3]) >>> data = otp.Tick(A=1) >>> data.state_vars['SET'] = otp.state.tick_set('oldest', 'B', otp.eval(another_query)) >>> data = data.state_vars['SET'].dump() >>> data.to_df()[['B']] B 0 1 1 2 2 3
- erase(*key_values, **named_keys)#
Erase tick with these keys from tick set.
- Parameters
- Returns
Operation
that evaluates to boolean.(1 if tick was erased, and 0 if tick was not in tick set).
- Return type
Examples
Can be used in per-tick script:
>>> def fun(tick): ... tick['B'] = tick.state_vars['SET'].erase(1) ... tick.state_vars['SET'].erase(A=1) >>> data = otp.Tick(A=1) >>> data.state_vars['SET'] = otp.state.tick_set('oldest', 'A') >>> data = data.script(fun)
Can be used in source columns operations:
>>> data = otp.Tick(A=1) >>> data.state_vars['SET'] = otp.state.tick_set('oldest', 'B', otp.eval(otp.Ticks(B=[1, 2, 3]))) >>> data['C1'] = data.state_vars['SET'].erase(1) >>> data['C2'] = data.state_vars['SET'].erase(1)
>>> data.to_df() Time A C1 C2 0 2003-12-01 1 1.0 0.0
>>> data.state_vars['SET'].dump().to_df() Time B 0 2003-12-01 2 1 2003-12-01 3
Can be used with
execute()
method to do erasing without returning result asOperation
:>>> data = otp.Tick(A=1) >>> data.state_vars['SET'] = otp.state.tick_set('oldest', 'B', otp.eval(otp.Tick(B=2))) >>> data = data.execute(data.state_vars['SET'].erase(B=2))
- erase_by_named_keys(**named_keys)#
Alias for erase with restricted set of parameters
See also
- Return type
- find(field_name, default_value=None, *key_values, throw=False, **named_keys)#
Find tick in tick set and get the value of
field_name
from it.- Parameters
field_name (str) – Name of tick’s field to return the value from
default_value – Value that will be returned if tick is not found in tick set. Must be set unless
throw
is True.key_values (list) – List of tick set’s keys values that will be used to find tick.
named_keys (dict) – Dict of tick set’s keys named and values that will be used to find tick.
throw (bool) – If
True
, if tick is not found, instead of returningdefault_value
, OneTick’s exception will be raised.
- Return type
Examples
Can be used in per-tick script:
>>> def fun(tick): ... tick['B'] = tick.state_vars['SET'].find('B', 0, 1) ... tick['B'] = tick.state_vars['SET'].find('B', 0, A=1) ... tick['B'] = tick.state_vars['SET'].find('B', 1, throw=True) >>> data = otp.Tick(A=1, B=2) >>> data.state_vars['SET'] = otp.state.tick_set('oldest', 'A') >>> data = data.script(fun)
Can be used in source columns operations:
>>> data = otp.Tick(A=1, B=2) >>> data.state_vars['SET'] = otp.state.tick_set('oldest', 'A', otp.eval(otp.Tick(A=1, B=4))) >>> data['B'] = data.state_vars['SET'].find('B', 1, throw=True) >>> data['B'] = data.state_vars['SET'].find('B', 0, 1) >>> data['DEFAULT_B'] = data.state_vars['SET'].find('B', 0, A=2)
>>> data.to_df() Time A B DEFAULT_B 0 2003-12-01 1 4.0 0
- find_by_named_keys(field_name, default_value=None, **named_keys)#
Alias for find with restricted set of parameters
See also
- Return type
- find_or_throw(field_name, *key_values)#
Alias for find with restricted set of parameters
See also
- Return type
- get_size()#
Get size of the tick set. Can be used in per-tick script or in Source operations directly.
- Return type
Operation
that evaluates to float value.
Examples
Can be used in per-tick script:
>>> def fun(tick): ... tick['B'] = tick.state_vars['SET'].get_size() >>> data = otp.Tick(A=1) >>> data.state_vars['SET'] = otp.state.tick_set('oldest', 'A') >>> data = data.script(fun)
Can be used in source columns operations:
>>> data = otp.Tick(A=1) >>> data.state_vars['SET'] = otp.state.tick_set('oldest', 'A') >>> data['B'] = data.state_vars['SET'].get_size()
>>> data.to_df() Time A B 0 2003-12-01 1 0
- insert(tick_object=None)#
Insert tick into tick set.
- Parameters
tick_object – Can be set only in per-tick script. If not set the current tick is inserted into tick set.
- Returns
Operation
that evaluates to boolean.(1 if tick was inserted, and 0 if tick was already presented in tick set).
- Return type
Examples
Can be used in per-tick script:
>>> def fun(tick): ... tick.state_vars['SET'].insert(tick) ... tick.state_vars['SET'].insert() >>> data = otp.Tick(A=1) >>> data.state_vars['SET'] = otp.state.tick_set('oldest', 'A') >>> data = data.script(fun)
Can be used in source columns operations:
>>> data = otp.Tick(A=1) >>> data.state_vars['SET'] = otp.state.tick_set('oldest', 'A') >>> data['B'] = data.state_vars['SET'].insert() >>> data['C'] = data.state_vars['SET'].insert()
>>> data.to_df() Time A B C 0 2003-12-01 1 1.0 0.0
>>> data.state_vars['SET'].dump().to_df() Time A 0 2003-12-01 1
Can be used with
execute()
method to do insertion without returning result asOperation
:>>> data = otp.Tick(A=1) >>> data.state_vars['SET'] = otp.state.tick_set('oldest', 'A') >>> data = data.execute(data.state_vars['SET'].insert())
- property key_fields#
- present(*key_values)#
Check if tick with these key values is present in tick set.
- Return type
Operation
that evaluates to boolean (float value 1 or 0).
Examples
Can be used in per-tick script:
>>> def fun(tick): ... tick['B'] = tick.state_vars['SET'].present(1) >>> data = otp.Tick(A=1) >>> data.state_vars['SET'] = otp.state.tick_set('oldest', 'A') >>> data = data.script(fun)
Can be used in source columns operations:
>>> data = otp.Tick(A=1) >>> data.state_vars['SET'] = otp.state.tick_set('oldest', 'A', otp.eval(otp.Tick(A=1))) >>> data['B'] = data.state_vars['SET'].present(1) >>> data['C'] = data.state_vars['SET'].present(2)
>>> data.to_df() Time A B C 0 2003-12-01 1 1 0
- size()#
See also
- Return type
- update(where=1, value_fields=None, erase_condition=0)#
Insert into or delete ticks from tick set. Can be used only on Source directly.
- Parameters
where (
Operation
) – Selection of input ticks that will be inserted into tick set. By default, all input ticks are selected.value_fields (list of str) – List of value fields to be inserted into tick sets. If param is empty, all fields of input tick are inserted. Note that this applies only to non-key fields (key-fields are always included). If new fields are added to tick set, they will have default values according to their type. If some fields are in tick set schema but not added in this method, they will have default values.
erase_condition (
Operation
) – Selection of input ticks that will be erased from tick set. If it is set thenwhere
parameter is not taken into account.inplace (bool) – If
True
current source will be modified else modified copy will be returned
- Return type
if
inplace
is False then returnsSource
copy.
Examples
>>> data = otp.Ticks(A=[1, 2, 3], B=[4, 5, 6], C=[7, 8, 9]) >>> data.state_vars['SET'] = otp.state.tick_set('oldest', 'A') >>> data = data.state_vars['SET'].update(value_fields=['B']) >>> data = data.state_vars['SET'].update(data['A'] == 2) >>> data = data.state_vars['SET'].update(erase_condition=data['A'] == 2)
>>> data.first().state_vars['SET'].dump(when_to_dump='first_tick').to_df() Time A B 0 2003-12-01 1 4 1 2003-12-01 3 6