{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"id": "57b7d32b-5db5-44bc-83b7-4b39de81c3e9",
"metadata": {
"tags": []
},
"source": [
"# Variables and Data Structures"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "3d49431a-f6ec-4ab4-8481-cb70210d9e24",
"metadata": {},
"source": [
"## Variables (aka 'state variables')"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "5f17e599-7a9a-4355-b4d2-6788478d0451",
"metadata": {},
"source": [
"Variables can be used to keep track of state across ticks. The example below shows how we may keep track of P&L."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "a93cd883-9d90-405a-826c-afb4466fc6a7",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Time | \n",
" PRICE | \n",
" SIZE | \n",
" SIDE | \n",
" PROFIT | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 2003-12-01 00:00:00.000 | \n",
" 13.5 | \n",
" 200 | \n",
" B | \n",
" -2700 | \n",
"
\n",
" \n",
" 1 | \n",
" 2003-12-01 00:00:00.001 | \n",
" 13.6 | \n",
" 100 | \n",
" S | \n",
" -1340 | \n",
"
\n",
" \n",
" 2 | \n",
" 2003-12-01 00:00:00.002 | \n",
" 13.3 | \n",
" 150 | \n",
" B | \n",
" -3335 | \n",
"
\n",
" \n",
" 3 | \n",
" 2003-12-01 00:00:00.003 | \n",
" 14.0 | \n",
" 200 | \n",
" S | \n",
" -535 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Time PRICE SIZE 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": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import onetick.py as otp\n",
"s = otp.dt(2023, 5, 15, 9, 30)\n",
"e = otp.dt(2023, 5, 15, 9, 30, 1)\n",
"\n",
"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)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "67ade8dd-9aea-4944-b657-ce448e8ada76",
"metadata": {},
"source": [
"The variable 'PROFIT' keeps a running total. In other words, it aggregates state across trades.\n",
"\n",
"Note that the same can be accomplished without variables by keeping the running total in a separate column."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "e85c42c1-c38d-47aa-a08c-9fc085bbbc48",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Time | \n",
" PROFIT | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 2022-04-01 00:00:00.000 | \n",
" -2700.0 | \n",
"
\n",
" \n",
" 1 | \n",
" 2022-04-01 00:00:00.001 | \n",
" -1340.0 | \n",
"
\n",
" \n",
" 2 | \n",
" 2022-04-01 00:00:00.002 | \n",
" -3335.0 | \n",
"
\n",
" \n",
" 3 | \n",
" 2022-04-01 00:00:00.003 | \n",
" -535.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Time PROFIT\n",
"0 2022-04-01 00:00:00.000 -2700.0\n",
"1 2022-04-01 00:00:00.001 -1340.0\n",
"2 2022-04-01 00:00:00.002 -3335.0\n",
"3 2022-04-01 00:00:00.003 -535.0"
]
},
"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",
"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)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "e1d1ee63-7d33-4e89-b848-3ba69f5ca1d7",
"metadata": {},
"source": [
"Below is an example of using variables that cannot be as easily implemented with running totals: remembering the value of the last tick and referring to it after grouping/aggregation."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "2d3b08e8-c6b3-4bda-bab8-7ea390a683da",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Time | \n",
" X | \n",
" Y | \n",
" last_x | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 2022-04-01 00:00:00.001 | \n",
" 3 | \n",
" 1 | \n",
" 2 | \n",
"
\n",
" \n",
" 1 | \n",
" 2022-04-01 00:00:00.003 | \n",
" 4 | \n",
" 0 | \n",
" 2 | \n",
"
\n",
" \n",
" 2 | \n",
" 2022-04-01 00:00:00.004 | \n",
" 2 | \n",
" 3 | \n",
" 2 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Time X Y last_x\n",
"0 2022-04-01 00:00:00.001 3 1 2\n",
"1 2022-04-01 00:00:00.003 4 0 2\n",
"2 2022-04-01 00:00:00.004 2 3 2"
]
},
"execution_count": 6,
"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['last_x'] = 0\n",
"q.state_vars['last_x'] = q['X']\n",
"q = q.high('X', group_by=['Y'])\n",
"q['last_x'] = q.state_vars['last_x']\n",
"otp.run(q)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "58d44336-5591-4d58-8d10-b7c6a5dd4d0a",
"metadata": {},
"source": [
"# Dictionaries / Maps (aka `tick sets`)\n",
"## Looking up static data for every tick\n",
"A map can be created with keys taken from one or more columns and holding entire ticks as values.\n",
"\n",
"The example below uses exchange reference data to create a map keyed by exchange code."
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "bcb1eea8-62af-4fce-bde2-acd467f14f0a",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Time | \n",
" EXCHANGE | \n",
" NAME | \n",
" LOCATION | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 2022-04-01 00:00:00.000 | \n",
" A | \n",
" NYSE American (Amex) | \n",
" US | \n",
"
\n",
" \n",
" 1 | \n",
" 2022-04-01 00:00:00.001 | \n",
" B | \n",
" Nasdaq BX | \n",
" US | \n",
"
\n",
" \n",
" 2 | \n",
" 2022-04-01 00:00:00.002 | \n",
" C | \n",
" NYSE National (NSX) | \n",
" US | \n",
"
\n",
" \n",
" 3 | \n",
" 2022-04-01 00:00:00.003 | \n",
" D | \n",
" FINRA ADF + NYSE/Nasdaq TRFs | \n",
" US | \n",
"
\n",
" \n",
" 4 | \n",
" 2022-04-01 00:00:00.004 | \n",
" H | \n",
" MIAX Pearl | \n",
" US | \n",
"
\n",
" \n",
" 5 | \n",
" 2022-04-01 00:00:00.005 | \n",
" I | \n",
" International Securities Exchange | \n",
" US | \n",
"
\n",
" \n",
" 6 | \n",
" 2022-04-01 00:00:00.006 | \n",
" J | \n",
" Cboe EDGA | \n",
" US | \n",
"
\n",
" \n",
" 7 | \n",
" 2022-04-01 00:00:00.007 | \n",
" K | \n",
" Cboe EDGX | \n",
" US | \n",
"
\n",
" \n",
" 8 | \n",
" 2022-04-01 00:00:00.008 | \n",
" L | \n",
" Long-Term Stock Exchange (LTSE) | \n",
" US | \n",
"
\n",
" \n",
" 9 | \n",
" 2022-04-01 00:00:00.009 | \n",
" M | \n",
" NYSE Chicago | \n",
" US | \n",
"
\n",
" \n",
" 10 | \n",
" 2022-04-01 00:00:00.010 | \n",
" N | \n",
" New York Stock Exchange | \n",
" US | \n",
"
\n",
" \n",
" 11 | \n",
" 2022-04-01 00:00:00.011 | \n",
" P | \n",
" NYSE Arca | \n",
" US | \n",
"
\n",
" \n",
" 12 | \n",
" 2022-04-01 00:00:00.012 | \n",
" Q | \n",
" Nasdaq (Tape C securities) | \n",
" US | \n",
"
\n",
" \n",
" 13 | \n",
" 2022-04-01 00:00:00.013 | \n",
" S | \n",
" Consolidated Tape System (CTS) | \n",
" US | \n",
"
\n",
" \n",
" 14 | \n",
" 2022-04-01 00:00:00.014 | \n",
" T | \n",
" Nasdaq (Tape A,B securities) | \n",
" US | \n",
"
\n",
" \n",
" 15 | \n",
" 2022-04-01 00:00:00.015 | \n",
" U | \n",
" Members Exchange (MEMX) | \n",
" US | \n",
"
\n",
" \n",
" 16 | \n",
" 2022-04-01 00:00:00.016 | \n",
" V | \n",
" The Investors' Exchange (IEX) | \n",
" US | \n",
"
\n",
" \n",
" 17 | \n",
" 2022-04-01 00:00:00.017 | \n",
" W | \n",
" CBOE Stock Exchange (CBSX) | \n",
" US | \n",
"
\n",
" \n",
" 18 | \n",
" 2022-04-01 00:00:00.018 | \n",
" X | \n",
" Nasdaq PSX | \n",
" US | \n",
"
\n",
" \n",
" 19 | \n",
" 2022-04-01 00:00:00.019 | \n",
" Y | \n",
" Cboe BYX | \n",
" US | \n",
"
\n",
" \n",
" 20 | \n",
" 2022-04-01 00:00:00.020 | \n",
" Z | \n",
" Cboe BZX | \n",
" US | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Time EXCHANGE NAME LOCATION\n",
"0 2022-04-01 00:00:00.000 A NYSE American (Amex) US\n",
"1 2022-04-01 00:00:00.001 B Nasdaq BX US\n",
"2 2022-04-01 00:00:00.002 C NYSE National (NSX) US\n",
"3 2022-04-01 00:00:00.003 D FINRA ADF + NYSE/Nasdaq TRFs US\n",
"4 2022-04-01 00:00:00.004 H MIAX Pearl US\n",
"5 2022-04-01 00:00:00.005 I International Securities Exchange US\n",
"6 2022-04-01 00:00:00.006 J Cboe EDGA US\n",
"7 2022-04-01 00:00:00.007 K Cboe EDGX US\n",
"8 2022-04-01 00:00:00.008 L Long-Term Stock Exchange (LTSE) US\n",
"9 2022-04-01 00:00:00.009 M NYSE Chicago US\n",
"10 2022-04-01 00:00:00.010 N New York Stock Exchange US\n",
"11 2022-04-01 00:00:00.011 P NYSE Arca US\n",
"12 2022-04-01 00:00:00.012 Q Nasdaq (Tape C securities) US\n",
"13 2022-04-01 00:00:00.013 S Consolidated Tape System (CTS) US\n",
"14 2022-04-01 00:00:00.014 T Nasdaq (Tape A,B securities) US\n",
"15 2022-04-01 00:00:00.015 U Members Exchange (MEMX) US\n",
"16 2022-04-01 00:00:00.016 V The Investors' Exchange (IEX) US\n",
"17 2022-04-01 00:00:00.017 W CBOE Stock Exchange (CBSX) US\n",
"18 2022-04-01 00:00:00.018 X Nasdaq PSX US\n",
"19 2022-04-01 00:00:00.019 Y Cboe BYX US\n",
"20 2022-04-01 00:00:00.020 Z Cboe BZX US"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"exchanges = otp.Ticks(EXCHANGE=['A', 'B', 'C', 'D', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'],\n",
" NAME=['NYSE American (Amex)', 'Nasdaq BX', 'NYSE National (NSX)', 'FINRA ADF + NYSE/Nasdaq TRFs', 'MIAX Pearl', 'International Securities Exchange', 'Cboe EDGA', 'Cboe EDGX', 'Long-Term Stock Exchange (LTSE)', 'NYSE Chicago', 'New York Stock Exchange', 'NYSE Arca', 'Nasdaq (Tape C securities)', 'Consolidated Tape System (CTS)', 'Nasdaq (Tape A,B securities)', 'Members Exchange (MEMX)', \"The Investors' Exchange (IEX)\", 'CBOE Stock Exchange (CBSX)', 'Nasdaq PSX', 'Cboe BYX', 'Cboe BZX'])\n",
"exchanges['LOCATION'] = 'US'\n",
"otp.run(exchanges)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "eba643c4-04f8-4df7-a8fd-bfc95384f04b",
"metadata": {},
"source": [
"We will add exchange name to trades. First let's examine the trades."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "d0aa2e40-266d-4ea5-8fb0-226fb5f46ed0",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Time | \n",
" PRICE | \n",
" SIZE | \n",
" COND | \n",
" EXCHANGE | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 2023-05-15 09:30:00.000178688 | \n",
" 412.22 | \n",
" 100 | \n",
" T | \n",
" P | \n",
"
\n",
" \n",
" 1 | \n",
" 2023-05-15 09:30:00.000776704 | \n",
" 412.22 | \n",
" 247 | \n",
" | \n",
" Z | \n",
"
\n",
" \n",
" 2 | \n",
" 2023-05-15 09:30:00.003603456 | \n",
" 412.22 | \n",
" 100 | \n",
" T | \n",
" T | \n",
"
\n",
" \n",
" 3 | \n",
" 2023-05-15 09:30:00.006352128 | \n",
" 412.24 | \n",
" 1 | \n",
" I | \n",
" K | \n",
"
\n",
" \n",
" 4 | \n",
" 2023-05-15 09:30:00.007128064 | \n",
" 412.24 | \n",
" 3 | \n",
" I | \n",
" K | \n",
"
\n",
" \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
"
\n",
" \n",
" 310 | \n",
" 2023-05-15 09:30:00.934032640 | \n",
" 412.27 | \n",
" 160 | \n",
" T | \n",
" T | \n",
"
\n",
" \n",
" 311 | \n",
" 2023-05-15 09:30:00.975609344 | \n",
" 412.24 | \n",
" 2 | \n",
" I | \n",
" D | \n",
"
\n",
" \n",
" 312 | \n",
" 2023-05-15 09:30:00.980264448 | \n",
" 412.27 | \n",
" 1 | \n",
" I | \n",
" D | \n",
"
\n",
" \n",
" 313 | \n",
" 2023-05-15 09:30:00.985391616 | \n",
" 412.28 | \n",
" 100 | \n",
" | \n",
" T | \n",
"
\n",
" \n",
" 314 | \n",
" 2023-05-15 09:30:00.985394944 | \n",
" 412.28 | \n",
" 100 | \n",
" Q | \n",
" T | \n",
"
\n",
" \n",
"
\n",
"
315 rows × 5 columns
\n",
"
"
],
"text/plain": [
" Time PRICE SIZE COND EXCHANGE\n",
"0 2023-05-15 09:30:00.000178688 412.22 100 T P\n",
"1 2023-05-15 09:30:00.000776704 412.22 247 Z\n",
"2 2023-05-15 09:30:00.003603456 412.22 100 T T\n",
"3 2023-05-15 09:30:00.006352128 412.24 1 I K\n",
"4 2023-05-15 09:30:00.007128064 412.24 3 I K\n",
".. ... ... ... ... ...\n",
"310 2023-05-15 09:30:00.934032640 412.27 160 T T\n",
"311 2023-05-15 09:30:00.975609344 412.24 2 I D\n",
"312 2023-05-15 09:30:00.980264448 412.27 1 I D\n",
"313 2023-05-15 09:30:00.985391616 412.28 100 T\n",
"314 2023-05-15 09:30:00.985394944 412.28 100 Q T\n",
"\n",
"[315 rows x 5 columns]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"q = otp.DataSource('NYSE_TAQ', tick_type='TRD')\n",
"q = q[['PRICE', 'SIZE', 'COND', 'EXCHANGE']]\n",
"otp.run(q, start=s, end=e, symbols=['SPY'])"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "4b3d7ba1-c106-4dfa-9e2f-e8a69abfdd0a",
"metadata": {},
"source": [
"The value of the `EXCHANGE` field from trades will be used to look up the name of the corresponding exchange."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "ba306745-8c6a-414e-a3d2-3187a4906f9f",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Time | \n",
" PRICE | \n",
" SIZE | \n",
" COND | \n",
" EXCHANGE | \n",
" exchange_name | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 2023-05-15 09:30:00.000178688 | \n",
" 412.22 | \n",
" 100 | \n",
" T | \n",
" P | \n",
" NYSE Arca | \n",
"
\n",
" \n",
" 1 | \n",
" 2023-05-15 09:30:00.000776704 | \n",
" 412.22 | \n",
" 247 | \n",
" | \n",
" Z | \n",
" Cboe BZX | \n",
"
\n",
" \n",
" 2 | \n",
" 2023-05-15 09:30:00.003603456 | \n",
" 412.22 | \n",
" 100 | \n",
" T | \n",
" T | \n",
" Nasdaq (Tape A,B securities) | \n",
"
\n",
" \n",
" 3 | \n",
" 2023-05-15 09:30:00.006352128 | \n",
" 412.24 | \n",
" 1 | \n",
" I | \n",
" K | \n",
" Cboe EDGX | \n",
"
\n",
" \n",
" 4 | \n",
" 2023-05-15 09:30:00.007128064 | \n",
" 412.24 | \n",
" 3 | \n",
" I | \n",
" K | \n",
" Cboe EDGX | \n",
"
\n",
" \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
"
\n",
" \n",
" 310 | \n",
" 2023-05-15 09:30:00.934032640 | \n",
" 412.27 | \n",
" 160 | \n",
" T | \n",
" T | \n",
" Nasdaq (Tape A,B securities) | \n",
"
\n",
" \n",
" 311 | \n",
" 2023-05-15 09:30:00.975609344 | \n",
" 412.24 | \n",
" 2 | \n",
" I | \n",
" D | \n",
" FINRA ADF + NYSE/Nasdaq TRFs | \n",
"
\n",
" \n",
" 312 | \n",
" 2023-05-15 09:30:00.980264448 | \n",
" 412.27 | \n",
" 1 | \n",
" I | \n",
" D | \n",
" FINRA ADF + NYSE/Nasdaq TRFs | \n",
"
\n",
" \n",
" 313 | \n",
" 2023-05-15 09:30:00.985391616 | \n",
" 412.28 | \n",
" 100 | \n",
" | \n",
" T | \n",
" Nasdaq (Tape A,B securities) | \n",
"
\n",
" \n",
" 314 | \n",
" 2023-05-15 09:30:00.985394944 | \n",
" 412.28 | \n",
" 100 | \n",
" Q | \n",
" T | \n",
" Nasdaq (Tape A,B securities) | \n",
"
\n",
" \n",
"
\n",
"
315 rows × 6 columns
\n",
"
"
],
"text/plain": [
" Time PRICE SIZE COND EXCHANGE exchange_name\n",
"0 2023-05-15 09:30:00.000178688 412.22 100 T P NYSE Arca\n",
"1 2023-05-15 09:30:00.000776704 412.22 247 Z Cboe BZX\n",
"2 2023-05-15 09:30:00.003603456 412.22 100 T T Nasdaq (Tape A,B securities)\n",
"3 2023-05-15 09:30:00.006352128 412.24 1 I K Cboe EDGX\n",
"4 2023-05-15 09:30:00.007128064 412.24 3 I K Cboe EDGX\n",
".. ... ... ... ... ... ...\n",
"310 2023-05-15 09:30:00.934032640 412.27 160 T T Nasdaq (Tape A,B securities)\n",
"311 2023-05-15 09:30:00.975609344 412.24 2 I D FINRA ADF + NYSE/Nasdaq TRFs\n",
"312 2023-05-15 09:30:00.980264448 412.27 1 I D FINRA ADF + NYSE/Nasdaq TRFs\n",
"313 2023-05-15 09:30:00.985391616 412.28 100 T Nasdaq (Tape A,B securities)\n",
"314 2023-05-15 09:30:00.985394944 412.28 100 Q T Nasdaq (Tape A,B securities)\n",
"\n",
"[315 rows x 6 columns]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"q = otp.DataSource('NYSE_TAQ', tick_type='TRD')\n",
"q = q[['PRICE','SIZE','COND','EXCHANGE']]\n",
"exchanges = otp.Ticks(EXCHANGE=['A', 'B', 'C', 'D', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'],\n",
" NAME=['NYSE American (Amex)', 'Nasdaq BX', 'NYSE National (NSX)', 'FINRA ADF + NYSE/Nasdaq TRFs', 'MIAX Pearl', 'International Securities Exchange', 'Cboe EDGA', 'Cboe EDGX', 'Long-Term Stock Exchange (LTSE)', 'NYSE Chicago', 'New York Stock Exchange', 'NYSE Arca', 'Nasdaq (Tape C securities)', 'Consolidated Tape System (CTS)', 'Nasdaq (Tape A,B securities)', 'Members Exchange (MEMX)', \"The Investors' Exchange (IEX)\", 'CBOE Stock Exchange (CBSX)', 'Nasdaq PSX', 'Cboe BYX', 'Cboe BZX'])\n",
"\n",
"q.state_vars['exchanges'] = otp.state.tick_set('latest', 'EXCHANGE', otp.eval(exchanges))\n",
"q['exchange_name'] = q.state_vars['exchanges'].find('NAME', 'unknown')\n",
"\n",
"otp.run(q, start=s, end=e, symbols=['SPY'])"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "0a0d24b2-5453-4e6f-8467-e0c3c2da06ad",
"metadata": {},
"source": [
"## Checking if ticks are present in another time series\n",
"A tick set can be used to check if (a small number of) ticks are present in another time series with potentially different time stamps.\n",
"\n",
"We generate two \"special\" ticks."
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "49db461e-fa91-48d2-82e8-f0a57a7216bf",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Time | \n",
" PRICE | \n",
" SIZE | \n",
" INDEX | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 2003-12-01 00:00:00.000 | \n",
" 412.22 | \n",
" 247 | \n",
" 1 | \n",
"
\n",
" \n",
" 1 | \n",
" 2003-12-01 00:00:00.001 | \n",
" 400.00 | \n",
" 1000 | \n",
" 2 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Time PRICE SIZE INDEX\n",
"0 2003-12-01 00:00:00.000 412.22 247 1\n",
"1 2003-12-01 00:00:00.001 400.00 1000 2"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ticks = otp.Ticks(PRICE=[412.22, 400], SIZE=[247, 1000], INDEX=[1, 2])\n",
"otp.run(ticks)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "aedfb084-06be-4793-a132-fb07289f03ea",
"metadata": {},
"source": [
"We then check if any of the trades match these ticks by ``PRICE`` and ``SIZE``."
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "6b198189-b8cc-4fda-9610-092696e07a70",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Time | \n",
" PRICE | \n",
" SIZE | \n",
" COND | \n",
" EXCHANGE | \n",
" special | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 2023-05-15 09:30:00.000776704 | \n",
" 412.22 | \n",
" 247 | \n",
" | \n",
" Z | \n",
" 1 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Time PRICE SIZE COND EXCHANGE special\n",
"0 2023-05-15 09:30:00.000776704 412.22 247 Z 1"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"q = otp.DataSource('NYSE_TAQ', tick_type='TRD')\n",
"q = q[['PRICE', 'SIZE', 'COND', 'EXCHANGE']]\n",
"\n",
"q.state_vars['special_ticks'] = otp.state.tick_set('latest', ['PRICE', 'SIZE'], otp.eval(ticks))\n",
"q['special'] = q.state_vars['special_ticks'].find('INDEX', -1)\n",
"\n",
"q, _ = q[q['special'] != -1]\n",
"\n",
"otp.run(q, start=s, end=e, symbols=['SPY'])"
]
},
{
"cell_type": "markdown",
"id": "8e5d70df",
"metadata": {},
"source": [
"# Lists and Queues\n",
"Lists and double-ended queues (deques) can be used to keep track of collections ticks.\n",
"## Lists and Queues Use Cases\n",
"[Realized P&L (FIFO)](realizedpandl)"
]
},
{
"cell_type": "markdown",
"id": "d1bc2a06",
"metadata": {},
"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.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}