Variables and Data Structures#
import onetick.py as otp
You can use variables to keep track of state across ticks. The example below shows how you may keep track of P&L.
# set some defaults
from datetime import datetime
otp.config['default_start_time'] = datetime(2003, 12, 1, 0, 0, 0)
otp.config['default_end_time'] = datetime(2003, 12, 4, 0, 0, 0)
otp.config['default_db'] = 'DEMO_L1'
otp.config['default_symbol'] = 'AAPL'
ticks = otp.Ticks({"PRICE": [13.5, 13.6, 13.3, 14.0],
"QTY" : [ 200, 100, 150, 200],
"SIDE" : [ "B", "S", "B", "S"]})
ticks.state_vars["PROFIT"] = 0
ticks.state_vars["PROFIT"] += ticks.apply(lambda t:
t["PRICE"] * t["QTY"] if t["SIDE"] == "S" else
- t["PRICE"] * t["QTY"])
ticks["PROFIT"] = ticks.state_vars["PROFIT"]
otp.run(ticks)
Time | PRICE | QTY | SIDE | 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 |
Another use case is to store the value from the last tick during aggregation / grouping.
q = otp.Ticks(X=[-1, 3, -3, 4, 2], Y=[0, 1, 1, 0, 3])
q.state_vars["S"] = 0
q.state_vars["S"] = q["X"]
q = q.high("X", group_by=["Y"])
q["S"] = q.state_vars["S"]
otp.run(q)
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 | 3 | 2 |