otp.state.tick_set#

tick_set(insertion_policy, key_fields, default_value=None, scope='query')[source]#

Defines a state 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) –

  • default_value (eval query) – Eval query to initialize tick set from.

  • scope (str) – Scope for the state variable. Possible values are: query, branch, cross_symbol, all_inputs, all_outputs

Return type

onetick.py.core._internal._state_objects.TickSet

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('oldest', 'B', otp.eval(fsq))
>>> data = data.state_vars['SET'].dump()
>>> data.to_df()[['B']]
   B
0  1
1  2
2  3
class TickSet(*args, insertion_policy, key_fields, **kwargs)[source]#

Represents a tick set. This class should only be created with onetick.py.state.tick_set() function and should be added to the onetick.py.Source.state_vars() dictionary of the onetick.py.Source and can be accessed only via this dictionary.

Examples

>>> data = otp.Tick(A=1)
>>> data.state_vars['SET'] = otp.state.tick_set('oldest', 'A')
>>> data = data.state_vars['SET'].dump()

See also

TickSequenceTick

property key_fields#
dump(when_to_dump='every_tick', **kwargs)[source]#

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 returns Source 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
update(where=1, value_fields=None, erase_condition=0)[source]#

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 then where 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 returns Source 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
find(field_name, default_value=None, *key_values, throw=False, **named_keys)[source]#

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 returning default_value, OneTick’s exception will be raised.

Return type

onetick.py.core.column_operations.base.Operation

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)[source]#

Alias for find with restricted set of parameters

See also

find

Return type

onetick.py.core.column_operations.base.Operation

find_or_throw(field_name, *key_values)[source]#

Alias for find with restricted set of parameters

See also

find

Return type

onetick.py.core.column_operations.base.Operation

erase(*key_values, **named_keys)[source]#

Erase tick with these keys from tick set.

Parameters
  • 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.

Returns

  • Operation that evaluates to boolean.

  • (1 if tick was erased, and 0 if tick was not in tick set).

Return type

Optional[onetick.py.core.column_operations.base.Operation]

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 as Operation:

>>> 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)[source]#

Alias for erase with restricted set of parameters

See also

erase

Return type

Optional[onetick.py.core.column_operations.base.Operation]

clear()[source]#

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 script

  • then 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: []
insert(tick_object=None)[source]#

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

Optional[onetick.py.core.column_operations.base.Operation]

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 as Operation:

>>> data = otp.Tick(A=1)
>>> data.state_vars['SET'] = otp.state.tick_set('oldest', 'A')
>>> data = data.execute(data.state_vars['SET'].insert())
get_size()[source]#

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
size()[source]#

See also

get_size

Return type

onetick.py.core.column_operations.base.Operation

present(*key_values)[source]#

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
property schema: dict#