fromonetick.py.core.column_operations.baseimport_Operationclass_MaxOperator(_Operation):def__init__(self,objs):fromonetick.py.typesimportget_type_by_objectssuper().__init__(dtype=get_type_by_objects(objs))def_str_max(l_val,r_val):iftype(r_val)islist:iflen(r_val)>1:r_val=_str_max(r_val[0],r_val[1:])else:r_val=r_val[0]# CASE should be uppercased because it can be used in per-tick scriptreturnf'CASE({l_val} > {r_val}, 1, {l_val}, {r_val})'self._repr=_str_max(objs[0],objs[1:])def__str__(self):returnself._repr
[docs]defmax(*objs):""" Returns maximum value from list of ``objs``. Parameters ---------- objs: int, float, :py:class:`~onetick.py.Operation`, :py:class:`~onetick.py.Column` Return ------ :py:class:`~onetick.py.Operation` Examples -------- >>> data = otp.Tick(A=1) >>> data['MAX'] = otp.math.max(5, data['A']) >>> otp.run(data) Time A MAX 0 2003-12-01 1 5 """return_MaxOperator(list(objs))
class_MinOperator(_Operation):def__init__(self,objs):fromonetick.py.typesimportget_type_by_objectssuper().__init__(dtype=get_type_by_objects(objs))def_str_min(l_val,r_val):iftype(r_val)islist:iflen(r_val)>1:r_val=_str_min(r_val[0],r_val[1:])else:r_val=r_val[0]# CASE should be uppercased because it can be used in per-tick scriptreturnf'CASE({l_val} < {r_val}, 1, {l_val}, {r_val})'self._repr=_str_min(objs[0],objs[1:])def__str__(self):returnself._repr
[docs]defmin(*objs):""" Returns minimum value from list of ``objs``. Parameters ---------- objs: int, float, :py:class:`~onetick.py.Operation`, :py:class:`~onetick.py.Column` Return ------ :py:class:`~onetick.py.Operation` Examples -------- >>> data = otp.Tick(A=1) >>> data['MIN'] = otp.math.min(-5, data['A']) >>> otp.run(data) Time A MIN 0 2003-12-01 1 -5 """return_MinOperator(list(objs))
class_RandomFunc(_Operation):""" It implements the `rand` built-in function. """def__init__(self,min_value:int,max_value:int,seed:int=None):super().__init__(dtype=int)def_repr(min_value,max_value,seed):result=f'rand({min_value}, {max_value}'ifseedisnotNone:result+=f',{seed})'else:result+=')'returnresultself._repr=_repr(min_value,max_value,seed)def__str__(self):returnself._repr
[docs]defrand(min_value:int,max_value:int,seed:int=None):""" Returns a pseudo-random value in the range between ``min_value`` and ``max_value``. If ``seed`` is not specified, the function produces different values each time a query is invoked. If ``seed`` is specified, for this seed the function produces the same sequence of values each time a query is invoked. Parameters ---------- min_value: int, :py:class:`~onetick.py.Operation`, :py:class:`~onetick.py.Column` max_value: int, :py:class:`~onetick.py.Operation`, :py:class:`~onetick.py.Column` seed: int, :py:class:`~onetick.py.Operation`, :py:class:`~onetick.py.Column` Return ------ :py:class:`~onetick.py.Operation` Examples -------- >>> data = otp.Tick(A=1) >>> data['RAND'] = otp.math.rand(1, 1000) """ifisinstance(min_value,int)andmin_value<0:raiseException("It is not possible to use negative values for the `min_value`")ifisinstance(min_value,int)andisinstance(max_value,int)andmin_value>=max_value:raiseException("The `max_value` parameter should be more than `min_value`")return_RandomFunc(min_value,max_value,seed)
class_Now(_Operation):def__init__(self):fromonetick.py.typesimportnsectimesuper().__init__(dtype=nsectime)def_repr():return'now()'self._repr=_repr()def__str__(self):returnself._repr# TODO: this is not math, let's move it somewhere else
[docs]defnow():""" Returns the current time expressed as the number of milliseconds since the UNIX epoch. Return ------ :py:class:`~onetick.py.Operation` Examples -------- >>> data = otp.Tick(A=1) >>> data['NOW'] = otp.now() """return_Now()
class_Ln(_Operation):""" Compute the natural logarithm. """def__init__(self,value):super().__init__(dtype=float)def_repr(value):returnf'LOG({value})'self._repr=_repr(value)def__str__(self):returnself._repr
[docs]defln(value):""" Compute the natural logarithm of the ``value``. Parameters ---------- value: int, float, :py:class:`~onetick.py.Operation`, :py:class:`~onetick.py.Column` Return ------ :py:class:`~onetick.py.Operation` Examples -------- >>> data = otp.Tick(A=1) >>> data['LN'] = otp.math.ln(2.718282) >>> otp.run(data) Time A LN 0 2003-12-01 1 1.0 See Also -------- onetick.py.math.exp """return_Ln(value)
class_Log10(_Operation):""" Compute the base-10 logarithm. """def__init__(self,value):super().__init__(dtype=float)def_repr(value):returnf'LOG10({value})'self._repr=_repr(value)def__str__(self):returnself._repr
[docs]deflog10(value):""" Compute the base-10 logarithm of the ``value``. Parameters ---------- value: int, float, :py:class:`~onetick.py.Operation`, :py:class:`~onetick.py.Column` Return ------ :py:class:`~onetick.py.Operation` Examples -------- >>> data = otp.Tick(A=1) >>> data['LOG10'] = otp.math.log10(100) >>> otp.run(data) Time A LOG10 0 2003-12-01 1 2.0 """return_Log10(value)
class_Exp(_Operation):""" Compute the natural exponent. """def__init__(self,value):super().__init__(dtype=float)def_repr(value):returnf'EXP({value})'self._repr=_repr(value)def__str__(self):returnself._repr
[docs]defexp(value):""" Compute the natural exponent of the ``value``. Parameters ---------- value: int, float, :py:class:`~onetick.py.Operation`, :py:class:`~onetick.py.Column` Return ------ :py:class:`~onetick.py.Operation` Examples -------- >>> data = otp.Tick(A=1) >>> data['E'] = otp.math.exp(1) >>> otp.run(data) Time A E 0 2003-12-01 1 2.718282 See Also -------- onetick.py.math.ln """return_Exp(value)
class_Sqrt(_Operation):""" Compute the square root. """def__init__(self,value):super().__init__(dtype=float)def_repr(value):returnf'SQRT({value})'self._repr=_repr(value)def__str__(self):returnself._repr
[docs]defsqrt(value):""" Compute the square root of the ``value``. Parameters ---------- value: int, float, :py:class:`~onetick.py.Operation`, :py:class:`~onetick.py.Column` Return ------ :py:class:`~onetick.py.Operation` Examples -------- >>> data = otp.Tick(A=1) >>> data['SQRT'] = otp.math.sqrt(4) >>> otp.run(data) Time A SQRT 0 2003-12-01 1 2.0 """return_Sqrt(value)
class_Sign(_Operation):""" Get the sign of value. """def__init__(self,value):super().__init__(dtype=int)def_repr(value):returnf'SIGN({value})'self._repr=_repr(value)def__str__(self):returnself._repr
[docs]defsign(value):""" Compute the sign of the ``value``. Parameters ---------- value: int, float, :py:class:`~onetick.py.Operation`, :py:class:`~onetick.py.Column` Return ------ :py:class:`~onetick.py.Operation` Examples -------- >>> data = otp.Tick(A=1) >>> data['SIGN_POS'] = otp.math.sign(123) >>> data['SIGN_ZERO'] = otp.math.sign(0) >>> data['SIGN_NEG'] = otp.math.sign(-123) >>> otp.run(data) Time A SIGN_POS SIGN_ZERO SIGN_NEG 0 2003-12-01 1 1 0 -1 """return_Sign(value)
class_Power(_Operation):""" Compute the ``base`` to the power of the ``exponent``. """def__init__(self,base,exponent):super().__init__(dtype=float)def_repr(base,exponent):returnf'POWER({base}, {exponent})'self._repr=_repr(base,exponent)def__str__(self):returnself._repr
[docs]defpow(base,exponent):""" Compute the ``base`` to the power of the ``exponent``. Parameters ---------- base: int, float, :py:class:`~onetick.py.Operation`, :py:class:`~onetick.py.Column` exponent: int, float, :py:class:`~onetick.py.Operation`, :py:class:`~onetick.py.Column` Return ------ :py:class:`~onetick.py.Operation` Examples -------- >>> data = otp.Tick(A=2) >>> data['RES'] = otp.math.pow(data['A'], 10) >>> otp.run(data) Time A RES 0 2003-12-01 2 1024.0 """return_Power(base,exponent)
[docs]defpi():""" Returns the value of Pi number. Return ------ :py:class:`~onetick.py.Operation` Examples -------- >>> data = otp.Tick(A=1) >>> data['PI'] = otp.math.pi() >>> otp.run(data) Time A PI 0 2003-12-01 1 3.141593 """return_Pi()
class_Sin(_Operation):""" Returns the value of trigonometric function `sin` for the given angle number expressed in radians. """def__init__(self,value):super().__init__(dtype=float)def_repr(value):returnf'SIN({value})'self._repr=_repr(value)def__str__(self):returnself._repr
[docs]defsin(value):""" Returns the value of trigonometric function `sin` for the given ``value`` number expressed in radians. Parameters ---------- value: int, float, :py:class:`~onetick.py.Operation`, :py:class:`~onetick.py.Column` Return ------ :py:class:`~onetick.py.Operation` Examples -------- >>> data = otp.Tick(A=1) >>> data['SIN'] = otp.math.sin(otp.math.pi() / 6) >>> otp.run(data) Time A SIN 0 2003-12-01 1 0.5 See Also -------- onetick.py.math.pi """return_Sin(value)
class_Cos(_Operation):""" Returns the value of trigonometric function `cos` for the given angle number expressed in radians. """def__init__(self,value):super().__init__(dtype=float)def_repr(value):returnf'COS({value})'self._repr=_repr(value)def__str__(self):returnself._repr
[docs]defcos(value):""" Returns the value of trigonometric function `cos` for the given ``value`` number expressed in radians. Parameters ---------- value: int, float, :py:class:`~onetick.py.Operation`, :py:class:`~onetick.py.Column` Return ------ :py:class:`~onetick.py.Operation` Examples -------- >>> data = otp.Tick(A=1) >>> data['COS'] = otp.math.cos(otp.math.pi() / 3) >>> otp.run(data) Time A COS 0 2003-12-01 1 0.5 See Also -------- onetick.py.math.pi """return_Cos(value)
class_Tan(_Operation):""" Returns the value of trigonometric function `tan` for the given angle number expressed in radians. """def__init__(self,value):super().__init__(dtype=float)def_repr(value):returnf'TAN({value})'self._repr=_repr(value)def__str__(self):returnself._repr
[docs]deftan(value):""" Returns the value of trigonometric function `tan` for the given ``value`` number expressed in radians. Parameters ---------- value: int, float, :py:class:`~onetick.py.Operation`, :py:class:`~onetick.py.Column` Return ------ :py:class:`~onetick.py.Operation` Examples -------- >>> data = otp.Tick(A=1) >>> data['TAN'] = otp.math.tan(otp.math.pi() / 4) >>> otp.run(data) Time A TAN 0 2003-12-01 1 1.0 See Also -------- onetick.py.math.pi """return_Tan(value)
class_Mod(_Operation):""" Implements the remainder from dividing ``value1`` by ``value2`` """def__init__(self,value1,value2):super().__init__(dtype=int)def_repr(value1,value2):returnf'MOD({value1}, {value2})'self._repr=_repr(value1,value2)def__str__(self):returnself._repr
[docs]defmod(value1,value2):""" Computes the remainder from dividing ``value1`` by ``value2`` Parameters ---------- value1: int, float, :py:class:`~onetick.py.Operation`, :py:class:`~onetick.py.Column` value2: int, float, :py:class:`~onetick.py.Operation`, :py:class:`~onetick.py.Column` Return ------ :py:class:`~onetick.py.Operation` Examples -------- >>> data = otp.Tick(A=100) >>> data['MOD'] = otp.math.mod(data['A'], 72) >>> otp.run(data) Time A MOD 0 2003-12-01 100 28 """return_Mod(value1,value2)
class_Floor(_Operation):""" Returns a long integer value representing the largest integer that is less than or equal to the `value`. """def__init__(self,value):super().__init__(dtype=int)def_repr(val):returnf'FLOOR({val})'self._repr=_repr(value)def__str__(self):returnself._repr
[docs]deffloor(value):""" Returns a long integer value representing the largest integer that is less than or equal to the `value`. Parameters ---------- value: int, float, :py:class:`~onetick.py.Operation`, :py:class:`~onetick.py.Column` Return ------ :py:class:`~onetick.py.Operation` Examples -------- >>> data = otp.Tick(A=1.2) >>> data['FLOOR'] = otp.math.floor(data['A']) >>> otp.run(data) Time A FLOOR 0 2003-12-01 1.2 1 """return_Floor(value)