{ "cells": [ { "cell_type": "markdown", "id": "57b7d32b-5db5-44bc-83b7-4b39de81c3e9", "metadata": { "tags": [] }, "source": [ "# Variables and Data Structures" ] }, { "cell_type": "code", "execution_count": 3, "id": "d2d2ee9d-6e6a-44c6-8183-b16b3a25b4ff", "metadata": {}, "outputs": [], "source": [ "import onetick.py as otp\n" ] }, { "cell_type": "markdown", "id": "5f17e599-7a9a-4355-b4d2-6788478d0451", "metadata": {}, "source": [ "We can use variables to keep track of a state across trd. The example below shows how we may keep track of P&L." ] }, { "cell_type": "code", "execution_count": 5, "id": "a93cd883-9d90-405a-826c-afb4466fc6a7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
TimePRICEQTYSIDEPROFIT
02003-12-01 00:00:00.00013.5200B-2700
12003-12-01 00:00:00.00113.6100S-1340
22003-12-01 00:00:00.00213.3150B-3335
32003-12-01 00:00:00.00314.0200S-535
\n", "
" ], "text/plain": [ " Time PRICE QTY SIDE PROFIT\n", "0 2003-12-01 00:00:00.000 13.5 200 B -2700\n", "1 2003-12-01 00:00:00.001 13.6 100 S -1340\n", "2 2003-12-01 00:00:00.002 13.3 150 B -3335\n", "3 2003-12-01 00:00:00.003 14.0 200 S -535" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "trd = otp.Ticks({'PRICE': [13.5, 13.6, 13.3, 14.0],\n", " 'SIZE': [200, 100, 150, 200],\n", " 'SIDE': ['B', 'S', 'B', 'S']})\n", "\n", "trd.state_vars['PROFIT'] = 0\n", "trd.state_vars['PROFIT'] += trd.apply(\n", " lambda t: t['PRICE'] * t['SIZE'] if t['SIDE'] == 'S' else -(t['PRICE'] * t['SIZE'])\n", ")\n", "\n", "trd['PROFIT'] = trd.state_vars['PROFIT']\n", "\n", "otp.run(trd)" ] }, { "cell_type": "markdown", "id": "67ade8dd-9aea-4944-b657-ce448e8ada76", "metadata": {}, "source": [ "Let's take a look at what the code above does: the variable 'PROFIT' keeps a running total. In other words, it aggregates state across trd.\n", "\n", "An alternative implementation might avoid using variables by keeping the running total in a separate column. Let's try implementing that." ] }, { "cell_type": "code", "execution_count": 12, "id": "e85c42c1-c38d-47aa-a08c-9fc085bbbc48", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
TimePROFIT
02003-12-01 00:00:00.000-2700.0
12003-12-01 00:00:00.001-1340.0
22003-12-01 00:00:00.002-3335.0
32003-12-01 00:00:00.003-535.0
\n", "
" ], "text/plain": [ " Time PROFIT\n", "0 2003-12-01 00:00:00.000 -2700.0\n", "1 2003-12-01 00:00:00.001 -1340.0\n", "2 2003-12-01 00:00:00.002 -3335.0\n", "3 2003-12-01 00:00:00.003 -535.0" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "trd = otp.Ticks({'PRICE': [13.5, 13.6, 13.3, 14.0],\n", " 'SIZE': [200, 100, 150, 200],\n", " 'SIDE': ['B', 'S', 'B', 'S']})\n", "trd['VALUE'] = trd.apply(\n", " lambda t: t['PRICE'] * t['SIZE'] if t['SIDE'] == 'S' else -(t['PRICE'] * t['SIZE'])\n", ")\n", "trd = trd.agg({'PROFIT': otp.agg.sum('VALUE')}, running=True)\n", "otp.run(trd)" ] }, { "cell_type": "markdown", "id": "e1d1ee63-7d33-4e89-b848-3ba69f5ca1d7", "metadata": {}, "source": [ "Another use case is to store the value from the last tick during aggregation / grouping." ] }, { "cell_type": "code", "execution_count": 13, "id": "2d3b08e8-c6b3-4bda-bab8-7ea390a683da", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
TimeXYS
02003-12-01 00:00:00.001312
12003-12-01 00:00:00.003402
22003-12-01 00:00:00.004232
\n", "
" ], "text/plain": [ " Time X Y S\n", "0 2003-12-01 00:00:00.001 3 1 2\n", "1 2003-12-01 00:00:00.003 4 0 2\n", "2 2003-12-01 00:00:00.004 2 3 2" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "q = otp.Ticks(X=[-1, 3, -3, 4, 2], Y=[0, 1, 1, 0, 3])\n", "q.state_vars['S'] = 0\n", "q.state_vars['S'] = q['X']\n", "q = q.high('X', group_by=['Y'])\n", "q['S'] = q.state_vars['S']\n", "otp.run(q)" ] }, { "cell_type": "code", "execution_count": null, "id": "d67dabf2-0d16-4f8e-a210-7880ed72075b", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.8" } }, "nbformat": 4, "nbformat_minor": 5 }