State Variables#

Definition#

A state variable is a variable of a particular type used to keep and share a value of that type (state) between several event processors within the query.

Usage#

State variables can be accessed and/or modified by the state_vars property of the Source object. The syntax for it is similar to accessing/modifying columns, the main difference between tick field and state variables is the scope, while fields are tick specific, state variables is the same for every tick.

Below you can see the small example of creating and modifying state variables.

ticks = otp.Ticks({"PRICE": [13.5, 13.6, 13.3],
                   "QTY"  : [ 100,  200,  150]})
ticks.state_vars["S"] = "A"
ticks.state_vars["N"] = 1
print(ticks.to_df())

This code will print the following data:

                     Time  PRICE  QTY
0 2003-12-01 00:00:00.000  13.5   100
1 2003-12-01 00:00:00.001  13.6   200
2 2003-12-01 00:00:00.002  13.3   150

Warning

State variables can be initialized by constant expression only, you can’t use tick fields for initializing them.

ticks = otp.Ticks({"PRICE": [13.5, 13.6, 13.3],
                   "QTY"  : [ 100,  200,  150]})
ticks.state_vars["S"] = ticks["PRICE"]
print(ticks.to_df())
Traceback (most recent call last):
    ...
AssertionError: OneTick supports state variables creation with constants only

Note

As you can see state variables are not printed after to_df method, but you can initialize tick field with state variables expression.

ticks = otp.Ticks({"PRICE": [13.5, 13.6, 13.3],
                   "QTY"  : [ 100,  200,  150]})
ticks.state_vars["TAX"] = 1.1  # 10%
ticks["N"] = ticks["PRICE"] * ticks["QTY"]
ticks["N"] *= ticks.state_vars["TAX"]
print(ticks.to_df())

This code will print the following data:

                     Time  PRICE  QTY       N
0 2003-12-01 00:00:00.000  13.5   100  1485.0
1 2003-12-01 00:00:00.001  13.6   200  2992.0
2 2003-12-01 00:00:00.002  13.3   150  2194.5

State variables support all operations provided for tick field.

data = otp.Ticks(dict(X=[-1.14, 3.89, -3.17]))
data.state_vars["S"] = 0
data.state_vars["S"] = (data["X"] + 1.0).round()
data["S"] = data.state_vars["S"]

df = data.to_df()
print(df)
                     Time     X  S
0 2003-12-01 00:00:00.000 -1.14  0
1 2003-12-01 00:00:00.001  3.89  5
2 2003-12-01 00:00:00.002 -3.17 -2

Examples#

Summarize Balance#

Data has the fields for price and quantity and flag for operation, B - buy, S - sell. The following code will find the profit of operations.

ticks = otp.Ticks({"PRICE": [13.5, 13.6, 13.3, 14.0],
                   "QTY"  : [ 200,  100,  150, 200],
                   "OP"   : [ "B",  "S",  "B", "S"]})
ticks.state_vars["PROFIT"] = 0
ticks.state_vars["PROFIT"] += ticks.apply(lambda row:
    row["PRICE"] * row["QTY"] if row["OP"] == "S" else
    - row["PRICE"] * row["QTY"])
ticks["PROFIT"] = ticks.state_vars["PROFIT"]
print(ticks.to_df())

This code will print the following data:

                     Time  PRICE  QTY OP  PROFIT
0 2003-12-01 00:00:00.000  13.5   200  B  -2700
1 2003-12-01 00:00:00.001  13.6   100  S  -1340
2 2003-12-01 00:00:00.002  13.3   150  B  -3335
3 2003-12-01 00:00:00.003  14.0   200  S  -535

Joining Fields#

The following example show how an user can join field of all ticks, like python join method.

data = otp.Ticks(dict(A=["A", "B", "C"], N=[0, 1, 2]))
data.state_vars["ALL"] = ""
data.state_vars["ALL"] = data.apply(lambda tick: tick["A"]
    if not tick.state_vars["ALL"]
    else tick.state_vars["ALL"] + ", " + tick["A"])
data["ALL"] = data.state_vars["ALL"]
df = data.to_df()
print(df)
                     Time  A  N      ALL
0 2003-12-01 00:00:00.000  A  0  A
1 2003-12-01 00:00:00.001  B  1  A, B
2 2003-12-01 00:00:00.002  C  2  A, B, C

Aggregation with Saving the Field of the Last Tick#

In the following example is shown how the field of last tick can be saved during aggregation.

data = otp.Ticks(dict(X=[-1, 3, -3, 4, 2], Y=[0, 1, 1, 0, 2]))
data.state_vars["S"] = 0
data.state_vars["S"] = data["X"]
data = data.high("X", group_by=["Y"])
data["S"] = data.state_vars["S"]

df = data.to_df()
print(df)
                     Time  X  Y  S
0 2003-12-01 00:00:00.001  3  1  2
1 2003-12-01 00:00:00.003  4  0  2
2 2003-12-01 00:00:00.004  2  2  2