{ "cells": [ { "attachments": {}, "cell_type": "markdown", "id": "ad42f7ef", "metadata": {}, "source": [ "# Data Retrieval" ] }, { "attachments": {}, "cell_type": "markdown", "id": "1ceb704d-c4cd-4cc3-965a-b8596bde08d0", "metadata": {}, "source": [ "OneTick is a time series database meaning that each record has a timestamp and timestamps of consecutive records are non-decreasing. Multiple time series are stored in OneTick. An individual time series is identified by the symbol (aka ticker, financial instrument, security), tick type (i.e., the type of data such as trades or quotes), and the name of the database where the time series is stored." ] }, { "cell_type": "code", "execution_count": 2, "id": "fa14e1fa-a1c7-458b-90f5-4741058ea8ac", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['ACTIV_ALPHA', 'ACTIV_CANADA', 'ACTIV_CSE', 'ACTIV_CSE2', 'ACTIV_CX2']" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import onetick.py as otp\n", "\n", "s = otp.dt(2023, 5, 15, 9, 30)\n", "e = otp.dt(2023, 5, 15, 9, 30, 1)\n", "\n", "# we can retrieve the list of databases available\n", "dbs = otp.databases()\n", "list(dbs)[:5]" ] }, { "cell_type": "code", "execution_count": 4, "id": "01eb7800-2d4f-43a9-8835-f1062e87bba2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[datetime.date(2023, 4, 7),\n", " datetime.date(2023, 4, 8),\n", " datetime.date(2023, 4, 9),\n", " datetime.date(2023, 4, 10),\n", " datetime.date(2023, 4, 11)]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# the list of dates with data for the db\n", "dbs['ACTIV_CANADA'].dates()[-5:]" ] }, { "cell_type": "code", "execution_count": 5, "id": "90557e70-7808-4df8-9366-92e88a7d1db2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.date(2023, 4, 11)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# or just the last day with data\n", "dbs['ACTIV_CANADA'].last_date" ] }, { "cell_type": "code", "execution_count": 6, "id": "39d6e0af-f219-4439-841e-5f39b4ecdbf1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['DAY', 'QTE', 'STAT', 'TRD']" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# and the list of tick types\n", "dbs['ACTIV_CANADA'].tick_types()" ] }, { "attachments": {}, "cell_type": "markdown", "id": "b5e390ca-8396-41ba-97c0-afa0a4427ef8", "metadata": {}, "source": [ "We can now retrieve symbols traded in a given time range. (In many financial markets, there are properties that remain constant throughout the trading day. Examples include the name of a financial instrument and the set of instruments traded)." ] }, { "cell_type": "code", "execution_count": 7, "id": "fe30bf21-201c-4ac8-949e-3dc13e8e2e48", "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
TimeSYMBOL_NAME
02023-04-11A.H
12023-04-11AAA.P
22023-04-11AAAJ.P
32023-04-11AAB
42023-04-11AAC.P
.........
57332023-04-11ZYZ.SB.A
57342023-04-11ZYZ.UN.U
57352023-04-11ZZE.H
57362023-04-11ZZZ
57372023-04-11ZZZD
\n", "

5738 rows × 2 columns

\n", "
" ], "text/plain": [ " Time SYMBOL_NAME\n", "0 2023-04-11 A.H\n", "1 2023-04-11 AAA.P\n", "2 2023-04-11 AAAJ.P\n", "3 2023-04-11 AAB\n", "4 2023-04-11 AAC.P\n", "... ... ...\n", "5733 2023-04-11 ZYZ.SB.A\n", "5734 2023-04-11 ZYZ.UN.U\n", "5735 2023-04-11 ZZE.H\n", "5736 2023-04-11 ZZZ\n", "5737 2023-04-11 ZZZD\n", "\n", "[5738 rows x 2 columns]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "symbols = otp.Symbols('ACTIV_CANADA')\n", "otp.run(symbols, start=otp.dt(2023, 4, 11), end=otp.dt(2023, 4, 12))" ] }, { "attachments": {}, "cell_type": "markdown", "id": "c19aa09b-4b03-484b-bcf2-c6350c7929ff", "metadata": {}, "source": [ "We used the `otp.run` method above, which executed a query that retrieved the list of symbols. \n", "\n", "The start and end timestamps were specified with `onetick-py`'s datetime class `otp.dt`." ] }, { "attachments": {}, "cell_type": "markdown", "id": "63125d3b-b49c-48d7-a173-f4a4816abd80", "metadata": {}, "source": [ "Now that we have database names, tick types, and symbols, we are ready to query a time series." ] }, { "cell_type": "code", "execution_count": 4, "id": "d7614b09-c381-406c-8c7b-3f5baaa4c05e", "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", " \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", " \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", " \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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
TimeEXCHANGECONDSTOP_STOCKSOURCETRFTTETICKERPRICEDELETED_TIMETICK_STATUSSIZECORRSEQ_NUMTRADE_IDPARTICIPANT_TIMETRF_TIMEOMDSEQ
02023-05-15 09:30:00.000178688PTNC0SPY412.221969-12-31 19:00:000100039195529835251671532023-05-15 09:30:00.0001500161969-12-31 19:00:00.0000000000
12023-05-15 09:30:00.000776704ZNC0SPY412.221969-12-31 19:00:000247039196529835250353552023-05-15 09:30:00.0004500001969-12-31 19:00:00.0000000001
22023-05-15 09:30:00.003603456TTNC0SPY412.221969-12-31 19:00:000100039212628791331579502023-05-15 09:30:00.0031651221969-12-31 19:00:00.0000000000
32023-05-15 09:30:00.006352128KINC0SPY412.241969-12-31 19:00:0001039227529835250983012023-05-15 09:30:00.0060910001969-12-31 19:00:00.0000000000
42023-05-15 09:30:00.007128064KINC0SPY412.241969-12-31 19:00:0003039231529835250983022023-05-15 09:30:00.0068730001969-12-31 19:00:00.0000000000
.........................................................
3102023-05-15 09:30:00.934032640TTNC0SPY412.271969-12-31 19:00:000160040517628791335455382023-05-15 09:30:00.9336780331969-12-31 19:00:00.0000000001
3112023-05-15 09:30:00.975609344DINCT0SPY412.241969-12-31 19:00:0002040543716752405957892023-05-15 09:30:00.6610000002023-05-15 09:30:00.9752415140
3122023-05-15 09:30:00.980264448DINCT0SPY412.271969-12-31 19:00:0001040545716752405962942023-05-15 09:30:00.5535010002023-05-15 09:30:00.9798957000
3132023-05-15 09:30:00.985391616TNC0SPY412.281969-12-31 19:00:000100040547628791335527172023-05-15 09:30:00.9849461041969-12-31 19:00:00.0000000000
3142023-05-15 09:30:00.985394944TQNC0SPY412.281969-12-31 19:00:000100040549628791335527192023-05-15 09:30:00.9849583121969-12-31 19:00:00.0000000001
\n", "

315 rows × 18 columns

\n", "
" ], "text/plain": [ " Time EXCHANGE COND STOP_STOCK SOURCE TRF TTE TICKER PRICE DELETED_TIME TICK_STATUS SIZE CORR SEQ_NUM TRADE_ID PARTICIPANT_TIME TRF_TIME OMDSEQ\n", "0 2023-05-15 09:30:00.000178688 P T N C 0 SPY 412.22 1969-12-31 19:00:00 0 100 0 39195 52983525167153 2023-05-15 09:30:00.000150016 1969-12-31 19:00:00.000000000 0\n", "1 2023-05-15 09:30:00.000776704 Z N C 0 SPY 412.22 1969-12-31 19:00:00 0 247 0 39196 52983525035355 2023-05-15 09:30:00.000450000 1969-12-31 19:00:00.000000000 1\n", "2 2023-05-15 09:30:00.003603456 T T N C 0 SPY 412.22 1969-12-31 19:00:00 0 100 0 39212 62879133157950 2023-05-15 09:30:00.003165122 1969-12-31 19:00:00.000000000 0\n", "3 2023-05-15 09:30:00.006352128 K I N C 0 SPY 412.24 1969-12-31 19:00:00 0 1 0 39227 52983525098301 2023-05-15 09:30:00.006091000 1969-12-31 19:00:00.000000000 0\n", "4 2023-05-15 09:30:00.007128064 K I N C 0 SPY 412.24 1969-12-31 19:00:00 0 3 0 39231 52983525098302 2023-05-15 09:30:00.006873000 1969-12-31 19:00:00.000000000 0\n", ".. ... ... ... ... ... .. .. ... ... ... ... ... ... ... ... ... ... ...\n", "310 2023-05-15 09:30:00.934032640 T T N C 0 SPY 412.27 1969-12-31 19:00:00 0 160 0 40517 62879133545538 2023-05-15 09:30:00.933678033 1969-12-31 19:00:00.000000000 1\n", "311 2023-05-15 09:30:00.975609344 D I N C T 0 SPY 412.24 1969-12-31 19:00:00 0 2 0 40543 71675240595789 2023-05-15 09:30:00.661000000 2023-05-15 09:30:00.975241514 0\n", "312 2023-05-15 09:30:00.980264448 D I N C T 0 SPY 412.27 1969-12-31 19:00:00 0 1 0 40545 71675240596294 2023-05-15 09:30:00.553501000 2023-05-15 09:30:00.979895700 0\n", "313 2023-05-15 09:30:00.985391616 T N C 0 SPY 412.28 1969-12-31 19:00:00 0 100 0 40547 62879133552717 2023-05-15 09:30:00.984946104 1969-12-31 19:00:00.000000000 0\n", "314 2023-05-15 09:30:00.985394944 T Q N C 0 SPY 412.28 1969-12-31 19:00:00 0 100 0 40549 62879133552719 2023-05-15 09:30:00.984958312 1969-12-31 19:00:00.000000000 1\n", "\n", "[315 rows x 18 columns]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "q = otp.DataSource('NYSE_TAQ', tick_type='TRD')\n", "otp.run(q, start=s, end=e, symbols=['SPY'])" ] }, { "attachments": {}, "cell_type": "markdown", "id": "d051b530-da39-4075-8bcc-7013c78aca86", "metadata": {}, "source": [ "Note that we specified the start and end of the time series to retrieve the corresponding interval." ] }, { "attachments": {}, "cell_type": "markdown", "id": "c4e0e559-56b7-44f4-958f-15f954c9090c", "metadata": {}, "source": [ "Let's just keep the columns we're interested in to make it more digestible." ] }, { "cell_type": "code", "execution_count": 5, "id": "279f2c52-18fe-41ea-a275-f4f61598dbad", "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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
TimePRICESIZECONDEXCHANGE
02023-05-15 09:30:00.000178688412.22100TP
12023-05-15 09:30:00.000776704412.22247Z
22023-05-15 09:30:00.003603456412.22100TT
32023-05-15 09:30:00.006352128412.241IK
42023-05-15 09:30:00.007128064412.243IK
..................
3102023-05-15 09:30:00.934032640412.27160TT
3112023-05-15 09:30:00.975609344412.242ID
3122023-05-15 09:30:00.980264448412.271ID
3132023-05-15 09:30:00.985391616412.28100T
3142023-05-15 09:30:00.985394944412.28100QT
\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": 5, "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": "45b2d6d5-8afa-4149-adeb-2d55393e8b03", "metadata": {}, "source": [ "We can retrieve multiple time series." ] }, { "cell_type": "code", "execution_count": 6, "id": "71da7662-31ce-417e-813d-3d52ea785853", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'SPY': 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],\n", " 'QQQ': Time PRICE SIZE COND EXCHANGE\n", " 0 2023-05-15 09:30:00.000234302 325.51 300 @ B\n", " 1 2023-05-15 09:30:00.001657060 325.54 2 @ I Z\n", " 2 2023-05-15 09:30:00.001661871 325.54 166 @ Z\n", " 3 2023-05-15 09:30:00.001667551 325.54 167 @ Z\n", " 4 2023-05-15 09:30:00.001671172 325.54 100 @ Z\n", " .. ... ... ... ... ...\n", " 378 2023-05-15 09:30:00.973742814 325.52 4 @ I P\n", " 379 2023-05-15 09:30:00.974814807 325.53 50 @ I Q\n", " 380 2023-05-15 09:30:00.975917138 325.51 7 @ I P\n", " 381 2023-05-15 09:30:00.984404972 325.52 65 @F I Z\n", " 382 2023-05-15 09:30:00.999619637 325.50 6 @ I Q\n", " \n", " [383 rows x 5 columns]}" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "q = otp.DataSource('NYSE_TAQ', tick_type='TRD')\n", "q = q[['PRICE', 'SIZE', 'COND', 'EXCHANGE']]\n", "mult = otp.run(q, start=s, end=e, symbols=['SPY', 'QQQ'])\n", "mult" ] }, { "attachments": {}, "cell_type": "markdown", "id": "99a8b4f5-e362-4c45-b4cb-6b304fc9de59", "metadata": {}, "source": [ "Each time series is returned as the value of a dict keyed by the corresponding symbol." ] }, { "cell_type": "code", "execution_count": 7, "id": "830d1abd-3134-4b68-9912-808ffa5472cf", "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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
TimePRICESIZECONDEXCHANGE
02023-05-15 09:30:00.000234302325.51300@B
12023-05-15 09:30:00.001657060325.542@ IZ
22023-05-15 09:30:00.001661871325.54166@Z
32023-05-15 09:30:00.001667551325.54167@Z
42023-05-15 09:30:00.001671172325.54100@Z
..................
3782023-05-15 09:30:00.973742814325.524@ IP
3792023-05-15 09:30:00.974814807325.5350@ IQ
3802023-05-15 09:30:00.975917138325.517@ IP
3812023-05-15 09:30:00.984404972325.5265@F IZ
3822023-05-15 09:30:00.999619637325.506@ IQ
\n", "

383 rows × 5 columns

\n", "
" ], "text/plain": [ " Time PRICE SIZE COND EXCHANGE\n", "0 2023-05-15 09:30:00.000234302 325.51 300 @ B\n", "1 2023-05-15 09:30:00.001657060 325.54 2 @ I Z\n", "2 2023-05-15 09:30:00.001661871 325.54 166 @ Z\n", "3 2023-05-15 09:30:00.001667551 325.54 167 @ Z\n", "4 2023-05-15 09:30:00.001671172 325.54 100 @ Z\n", ".. ... ... ... ... ...\n", "378 2023-05-15 09:30:00.973742814 325.52 4 @ I P\n", "379 2023-05-15 09:30:00.974814807 325.53 50 @ I Q\n", "380 2023-05-15 09:30:00.975917138 325.51 7 @ I P\n", "381 2023-05-15 09:30:00.984404972 325.52 65 @F I Z\n", "382 2023-05-15 09:30:00.999619637 325.50 6 @ I Q\n", "\n", "[383 rows x 5 columns]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mult['QQQ']" ] }, { "attachments": {}, "cell_type": "markdown", "id": "3c405829-86e9-4d04-849b-b9be60c279c8", "metadata": {}, "source": [ "We can also retrieve all of the symbols from the database or all of the symbols matching a pattern." ] }, { "cell_type": "code", "execution_count": 8, "id": "d89dc08b-0ead-4b3d-98ae-442ab9901978", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'AAA': Time PRICE SIZE COND EXCHANGE\n", " 0 2023-05-15 09:30:00.227737600 24.55 20 I P\n", " 1 2023-05-15 09:30:00.227740672 24.55 20 Q P,\n", " 'AAAU': Time PRICE SIZE COND EXCHANGE\n", " 0 2023-05-15 09:30:00.018481664 19.990 7985 O X Z\n", " 1 2023-05-15 09:30:00.033499392 19.990 50 4 I D\n", " 2 2023-05-15 09:30:00.034347520 19.990 61 4 I D\n", " 3 2023-05-15 09:30:00.034804480 19.990 250 4 D\n", " 4 2023-05-15 09:30:00.041420288 19.990 77 I K\n", " 5 2023-05-15 09:30:00.042842624 19.990 6000 T T\n", " 6 2023-05-15 09:30:00.184702208 19.990 1 I D\n", " 7 2023-05-15 09:30:00.219001600 19.995 2 I Z\n", " 8 2023-05-15 09:30:00.817365248 19.995 1 I D}" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "q = otp.DataSource('NYSE_TAQ', tick_type='TRD')\n", "q = q[['PRICE', 'SIZE', 'COND', 'EXCHANGE']]\n", "mult = otp.run(q, start=s, end=e, symbols=otp.Symbols('NYSE_TAQ', pattern='AAA%'))\n", "mult" ] }, { "attachments": {}, "cell_type": "markdown", "id": "409ef584-4ea7-4fcb-8caa-ba64706ba489", "metadata": {}, "source": [ "We can merge all of the time series by time." ] }, { "cell_type": "code", "execution_count": 9, "id": "29e588e6-bf9a-4d4a-9bdd-e40d7a2cf88b", "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", " \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", " \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", "
TimePRICESIZECONDEXCHANGESYMBOL_NAMETICK_TYPE
02023-05-15 09:30:00.000178688412.22100TPSPYTRD
12023-05-15 09:30:00.000234302325.51300@BQQQTRD
22023-05-15 09:30:00.000776704412.22247ZSPYTRD
32023-05-15 09:30:00.001657060325.542@ IZQQQTRD
42023-05-15 09:30:00.001661871325.54166@ZQQQTRD
........................
6932023-05-15 09:30:00.980264448412.271IDSPYTRD
6942023-05-15 09:30:00.984404972325.5265@F IZQQQTRD
6952023-05-15 09:30:00.985391616412.28100TSPYTRD
6962023-05-15 09:30:00.985394944412.28100QTSPYTRD
6972023-05-15 09:30:00.999619637325.506@ IQQQQTRD
\n", "

698 rows × 7 columns

\n", "
" ], "text/plain": [ " Time PRICE SIZE COND EXCHANGE SYMBOL_NAME TICK_TYPE\n", "0 2023-05-15 09:30:00.000178688 412.22 100 T P SPY TRD\n", "1 2023-05-15 09:30:00.000234302 325.51 300 @ B QQQ TRD\n", "2 2023-05-15 09:30:00.000776704 412.22 247 Z SPY TRD\n", "3 2023-05-15 09:30:00.001657060 325.54 2 @ I Z QQQ TRD\n", "4 2023-05-15 09:30:00.001661871 325.54 166 @ Z QQQ TRD\n", ".. ... ... ... ... ... ... ...\n", "693 2023-05-15 09:30:00.980264448 412.27 1 I D SPY TRD\n", "694 2023-05-15 09:30:00.984404972 325.52 65 @F I Z QQQ TRD\n", "695 2023-05-15 09:30:00.985391616 412.28 100 T SPY TRD\n", "696 2023-05-15 09:30:00.985394944 412.28 100 Q T SPY TRD\n", "697 2023-05-15 09:30:00.999619637 325.50 6 @ I Q QQQ TRD\n", "\n", "[698 rows x 7 columns]" ] }, "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", "q = otp.merge([q], symbols=['SPY', 'QQQ'], identify_input_ts=True)\n", "single = otp.run(q, start=s, end=e)\n", "single" ] }, { "attachments": {}, "cell_type": "markdown", "id": "e5810875-f2e7-401c-8c84-e0afe53370aa", "metadata": {}, "source": [ "The time range and symbols can be specified directly on the data source. This way we can have different times ranges for difference sources that we can later merge." ] }, { "cell_type": "code", "execution_count": 10, "id": "99cf6179-b095-437c-aaef-6ae2c09a668b", "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", " \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", " \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", " \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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
TimeEXCHANGECONDSTOP_STOCKSOURCETRFTTETICKERPRICEDELETED_TIMETICK_STATUSSIZECORRSEQ_NUMTRADE_IDPARTICIPANT_TIMETRF_TIMEOMDSEQ
02023-05-15 09:30:00.000178688PTNC0SPY412.221969-12-31 19:00:000100039195529835251671532023-05-15 09:30:00.0001500161969-12-31 19:00:00.0000000000
12023-05-15 09:30:00.000776704ZNC0SPY412.221969-12-31 19:00:000247039196529835250353552023-05-15 09:30:00.0004500001969-12-31 19:00:00.0000000001
22023-05-15 09:30:00.003603456TTNC0SPY412.221969-12-31 19:00:000100039212628791331579502023-05-15 09:30:00.0031651221969-12-31 19:00:00.0000000000
32023-05-15 09:30:00.006352128KINC0SPY412.241969-12-31 19:00:0001039227529835250983012023-05-15 09:30:00.0060910001969-12-31 19:00:00.0000000000
42023-05-15 09:30:00.007128064KINC0SPY412.241969-12-31 19:00:0003039231529835250983022023-05-15 09:30:00.0068730001969-12-31 19:00:00.0000000000
.........................................................
3102023-05-15 09:30:00.934032640TTNC0SPY412.271969-12-31 19:00:000160040517628791335455382023-05-15 09:30:00.9336780331969-12-31 19:00:00.0000000001
3112023-05-15 09:30:00.975609344DINCT0SPY412.241969-12-31 19:00:0002040543716752405957892023-05-15 09:30:00.6610000002023-05-15 09:30:00.9752415140
3122023-05-15 09:30:00.980264448DINCT0SPY412.271969-12-31 19:00:0001040545716752405962942023-05-15 09:30:00.5535010002023-05-15 09:30:00.9798957000
3132023-05-15 09:30:00.985391616TNC0SPY412.281969-12-31 19:00:000100040547628791335527172023-05-15 09:30:00.9849461041969-12-31 19:00:00.0000000000
3142023-05-15 09:30:00.985394944TQNC0SPY412.281969-12-31 19:00:000100040549628791335527192023-05-15 09:30:00.9849583121969-12-31 19:00:00.0000000001
\n", "

315 rows × 18 columns

\n", "
" ], "text/plain": [ " Time EXCHANGE COND STOP_STOCK SOURCE TRF TTE TICKER PRICE DELETED_TIME TICK_STATUS SIZE CORR SEQ_NUM TRADE_ID PARTICIPANT_TIME TRF_TIME OMDSEQ\n", "0 2023-05-15 09:30:00.000178688 P T N C 0 SPY 412.22 1969-12-31 19:00:00 0 100 0 39195 52983525167153 2023-05-15 09:30:00.000150016 1969-12-31 19:00:00.000000000 0\n", "1 2023-05-15 09:30:00.000776704 Z N C 0 SPY 412.22 1969-12-31 19:00:00 0 247 0 39196 52983525035355 2023-05-15 09:30:00.000450000 1969-12-31 19:00:00.000000000 1\n", "2 2023-05-15 09:30:00.003603456 T T N C 0 SPY 412.22 1969-12-31 19:00:00 0 100 0 39212 62879133157950 2023-05-15 09:30:00.003165122 1969-12-31 19:00:00.000000000 0\n", "3 2023-05-15 09:30:00.006352128 K I N C 0 SPY 412.24 1969-12-31 19:00:00 0 1 0 39227 52983525098301 2023-05-15 09:30:00.006091000 1969-12-31 19:00:00.000000000 0\n", "4 2023-05-15 09:30:00.007128064 K I N C 0 SPY 412.24 1969-12-31 19:00:00 0 3 0 39231 52983525098302 2023-05-15 09:30:00.006873000 1969-12-31 19:00:00.000000000 0\n", ".. ... ... ... ... ... .. .. ... ... ... ... ... ... ... ... ... ... ...\n", "310 2023-05-15 09:30:00.934032640 T T N C 0 SPY 412.27 1969-12-31 19:00:00 0 160 0 40517 62879133545538 2023-05-15 09:30:00.933678033 1969-12-31 19:00:00.000000000 1\n", "311 2023-05-15 09:30:00.975609344 D I N C T 0 SPY 412.24 1969-12-31 19:00:00 0 2 0 40543 71675240595789 2023-05-15 09:30:00.661000000 2023-05-15 09:30:00.975241514 0\n", "312 2023-05-15 09:30:00.980264448 D I N C T 0 SPY 412.27 1969-12-31 19:00:00 0 1 0 40545 71675240596294 2023-05-15 09:30:00.553501000 2023-05-15 09:30:00.979895700 0\n", "313 2023-05-15 09:30:00.985391616 T N C 0 SPY 412.28 1969-12-31 19:00:00 0 100 0 40547 62879133552717 2023-05-15 09:30:00.984946104 1969-12-31 19:00:00.000000000 0\n", "314 2023-05-15 09:30:00.985394944 T Q N C 0 SPY 412.28 1969-12-31 19:00:00 0 100 0 40549 62879133552719 2023-05-15 09:30:00.984958312 1969-12-31 19:00:00.000000000 1\n", "\n", "[315 rows x 18 columns]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "q = otp.DataSource('NYSE_TAQ', tick_type='TRD', start=s, end=e, symbols=['SPY'])\n", "otp.run(q)" ] }, { "attachments": {}, "cell_type": "markdown", "id": "c20e354f-3a32-41c6-84dc-68a91cd53686", "metadata": {}, "source": [ "For example, we can get the data from March 29 for SPY and from April 20 for QQQ." ] }, { "cell_type": "code", "execution_count": 15, "id": "180e6b06-06a5-40c5-8ba2-2ba766692ab5", "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", " \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", "
TimePRICESIZESYMBOL_NAME
02023-03-29 09:30:00.000877568399.920400SPY
12023-03-29 09:30:00.001151232399.9201000SPY
22023-03-29 09:30:00.001154304399.9201000SPY
32023-03-29 09:30:00.001921280399.930657SPY
42023-03-29 09:30:00.010831360399.925100SPY
...............
7492023-04-20 09:30:00.989278707315.64041QQQ
7502023-04-20 09:30:00.989278785315.64010QQQ
7512023-04-20 09:30:00.990793766315.620100QQQ
7522023-04-20 09:30:00.990868824315.610186QQQ
7532023-04-20 09:30:00.990872151315.6101314QQQ
\n", "

754 rows × 4 columns

\n", "
" ], "text/plain": [ " Time PRICE SIZE SYMBOL_NAME\n", "0 2023-03-29 09:30:00.000877568 399.920 400 SPY\n", "1 2023-03-29 09:30:00.001151232 399.920 1000 SPY\n", "2 2023-03-29 09:30:00.001154304 399.920 1000 SPY\n", "3 2023-03-29 09:30:00.001921280 399.930 657 SPY\n", "4 2023-03-29 09:30:00.010831360 399.925 100 SPY\n", ".. ... ... ... ...\n", "749 2023-04-20 09:30:00.989278707 315.640 41 QQQ\n", "750 2023-04-20 09:30:00.989278785 315.640 10 QQQ\n", "751 2023-04-20 09:30:00.990793766 315.620 100 QQQ\n", "752 2023-04-20 09:30:00.990868824 315.610 186 QQQ\n", "753 2023-04-20 09:30:00.990872151 315.610 1314 QQQ\n", "\n", "[754 rows x 4 columns]" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "spy = otp.DataSource('NYSE_TAQ', tick_type='TRD', start=otp.dt(2023, 3, 29, 9, 30), end=otp.dt(2023, 3, 29, 9, 30, 1), symbols=['SPY'])\n", "qqq = otp.DataSource('NYSE_TAQ', tick_type='TRD', start=otp.dt(2023, 4, 20, 9, 30), end=otp.dt(2023, 4, 20, 9, 30, 1), symbols=['QQQ'])\n", "merged = otp.merge([spy, qqq], identify_input_ts=True)\n", "merged = merged[['PRICE', 'SIZE', 'SYMBOL_NAME']]\n", "otp.run(merged)" ] }, { "attachments": {}, "cell_type": "markdown", "id": "826dedb9-515b-4278-a1f4-ed89b7f34acc", "metadata": {}, "source": [ "We can also specify multiple symbols in the data source in which case they will be merged by time." ] }, { "cell_type": "code", "execution_count": 17, "id": "9d78165c-3624-4850-a6a6-21dbb9983c64", "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", " \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", " \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", " \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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
TimeEXCHANGECONDSTOP_STOCKSOURCETRFTTETICKERPRICEDELETED_TIMETICK_STATUSSIZECORRSEQ_NUMTRADE_IDPARTICIPANT_TIMETRF_TIMEOMDSEQ
02023-05-15 09:30:00.000178688PTNC0SPY412.221969-12-31 19:00:000100039195529835251671532023-05-15 09:30:00.0001500161969-12-31 19:00:00.0000000000
12023-05-15 09:30:00.000234302B@N0QQQ325.511969-12-31 19:00:000300067371442023-05-15 09:30:00.0002112201969-12-31 19:00:00.0000000000
22023-05-15 09:30:00.000776704ZNC0SPY412.221969-12-31 19:00:000247039196529835250353552023-05-15 09:30:00.0004500001969-12-31 19:00:00.0000000001
32023-05-15 09:30:00.001657060Z@ IN0QQQ325.541969-12-31 19:00:00020673734602023-05-15 09:30:00.0014370001969-12-31 19:00:00.0000000000
42023-05-15 09:30:00.001661871Z@N0QQQ325.541969-12-31 19:00:0001660673744612023-05-15 09:30:00.0014370001969-12-31 19:00:00.0000000001
.........................................................
6932023-05-15 09:30:00.980264448DINCT0SPY412.271969-12-31 19:00:0001040545716752405962942023-05-15 09:30:00.5535010002023-05-15 09:30:00.9798957000
6942023-05-15 09:30:00.984404972Z@F IN1QQQ325.521969-12-31 19:00:000650731445072023-05-15 09:30:00.9842060001969-12-31 19:00:00.0000000000
6952023-05-15 09:30:00.985391616TNC0SPY412.281969-12-31 19:00:000100040547628791335527172023-05-15 09:30:00.9849461041969-12-31 19:00:00.0000000000
6962023-05-15 09:30:00.985394944TQNC0SPY412.281969-12-31 19:00:000100040549628791335527192023-05-15 09:30:00.9849583121969-12-31 19:00:00.0000000001
6972023-05-15 09:30:00.999619637Q@ IN0QQQ325.501969-12-31 19:00:000607317519872023-05-15 09:30:00.9996026991969-12-31 19:00:00.0000000000
\n", "

698 rows × 18 columns

\n", "
" ], "text/plain": [ " Time EXCHANGE COND STOP_STOCK SOURCE TRF TTE TICKER PRICE DELETED_TIME TICK_STATUS SIZE CORR SEQ_NUM TRADE_ID PARTICIPANT_TIME TRF_TIME OMDSEQ\n", "0 2023-05-15 09:30:00.000178688 P T N C 0 SPY 412.22 1969-12-31 19:00:00 0 100 0 39195 52983525167153 2023-05-15 09:30:00.000150016 1969-12-31 19:00:00.000000000 0\n", "1 2023-05-15 09:30:00.000234302 B @ N 0 QQQ 325.51 1969-12-31 19:00:00 0 300 0 67371 44 2023-05-15 09:30:00.000211220 1969-12-31 19:00:00.000000000 0\n", "2 2023-05-15 09:30:00.000776704 Z N C 0 SPY 412.22 1969-12-31 19:00:00 0 247 0 39196 52983525035355 2023-05-15 09:30:00.000450000 1969-12-31 19:00:00.000000000 1\n", "3 2023-05-15 09:30:00.001657060 Z @ I N 0 QQQ 325.54 1969-12-31 19:00:00 0 2 0 67373 460 2023-05-15 09:30:00.001437000 1969-12-31 19:00:00.000000000 0\n", "4 2023-05-15 09:30:00.001661871 Z @ N 0 QQQ 325.54 1969-12-31 19:00:00 0 166 0 67374 461 2023-05-15 09:30:00.001437000 1969-12-31 19:00:00.000000000 1\n", ".. ... ... ... ... ... .. .. ... ... ... ... ... ... ... ... ... ... ...\n", "693 2023-05-15 09:30:00.980264448 D I N C T 0 SPY 412.27 1969-12-31 19:00:00 0 1 0 40545 71675240596294 2023-05-15 09:30:00.553501000 2023-05-15 09:30:00.979895700 0\n", "694 2023-05-15 09:30:00.984404972 Z @F I N 1 QQQ 325.52 1969-12-31 19:00:00 0 65 0 73144 507 2023-05-15 09:30:00.984206000 1969-12-31 19:00:00.000000000 0\n", "695 2023-05-15 09:30:00.985391616 T N C 0 SPY 412.28 1969-12-31 19:00:00 0 100 0 40547 62879133552717 2023-05-15 09:30:00.984946104 1969-12-31 19:00:00.000000000 0\n", "696 2023-05-15 09:30:00.985394944 T Q N C 0 SPY 412.28 1969-12-31 19:00:00 0 100 0 40549 62879133552719 2023-05-15 09:30:00.984958312 1969-12-31 19:00:00.000000000 1\n", "697 2023-05-15 09:30:00.999619637 Q @ I N 0 QQQ 325.50 1969-12-31 19:00:00 0 6 0 73175 1987 2023-05-15 09:30:00.999602699 1969-12-31 19:00:00.000000000 0\n", "\n", "[698 rows x 18 columns]" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "q = otp.DataSource('NYSE_TAQ', tick_type='TRD', start=s, end=e, symbols=['SPY', 'QQQ'])\n", "otp.run(q)" ] }, { "attachments": {}, "cell_type": "markdown", "id": "7b7816bf", "metadata": {}, "source": [ "We can look up symbols in multiple databases." ] }, { "cell_type": "code", "execution_count": 7, "id": "36682b1a", "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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
TimePRICESIZESYMBOL_NAMETICK_TYPE
02023-05-15 09:30:00.0000000004143.004TDI_FUT::ES_r_tdiTRD
12023-05-15 09:30:00.000178688412.22100NYSE_TAQ::SPYTRD
22023-05-15 09:30:00.000776704412.22247NYSE_TAQ::SPYTRD
32023-05-15 09:30:00.0010000004143.004TDI_FUT::ES_r_tdiTRD
42023-05-15 09:30:00.0010000004143.251TDI_FUT::ES_r_tdiTRD
..................
5472023-05-15 09:30:00.9860000004143.759TDI_FUT::ES_r_tdiTRD
5482023-05-15 09:30:00.9860000004143.751TDI_FUT::ES_r_tdiTRD
5492023-05-15 09:30:00.9860000004143.752TDI_FUT::ES_r_tdiTRD
5502023-05-15 09:30:00.9860000004143.752TDI_FUT::ES_r_tdiTRD
5512023-05-15 09:30:00.9860000004143.752TDI_FUT::ES_r_tdiTRD
\n", "

552 rows × 5 columns

\n", "
" ], "text/plain": [ " Time PRICE SIZE SYMBOL_NAME TICK_TYPE\n", "0 2023-05-15 09:30:00.000000000 4143.00 4 TDI_FUT::ES_r_tdi TRD\n", "1 2023-05-15 09:30:00.000178688 412.22 100 NYSE_TAQ::SPY TRD\n", "2 2023-05-15 09:30:00.000776704 412.22 247 NYSE_TAQ::SPY TRD\n", "3 2023-05-15 09:30:00.001000000 4143.00 4 TDI_FUT::ES_r_tdi TRD\n", "4 2023-05-15 09:30:00.001000000 4143.25 1 TDI_FUT::ES_r_tdi TRD\n", ".. ... ... ... ... ...\n", "547 2023-05-15 09:30:00.986000000 4143.75 9 TDI_FUT::ES_r_tdi TRD\n", "548 2023-05-15 09:30:00.986000000 4143.75 1 TDI_FUT::ES_r_tdi TRD\n", "549 2023-05-15 09:30:00.986000000 4143.75 2 TDI_FUT::ES_r_tdi TRD\n", "550 2023-05-15 09:30:00.986000000 4143.75 2 TDI_FUT::ES_r_tdi TRD\n", "551 2023-05-15 09:30:00.986000000 4143.75 2 TDI_FUT::ES_r_tdi TRD\n", "\n", "[552 rows x 5 columns]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "q = otp.DataSource(tick_type='TRD')\n", "q = q.table(PRICE=float, SIZE=int)\n", "q = otp.merge(q, symbols=['NYSE_TAQ::SPY', 'TDI_FUT::ES_r_tdi'], identify_input_ts=True)\n", "otp.run(q, start=s, end=e, symbol_date=s)" ] }, { "attachments": {}, "cell_type": "markdown", "id": "8cf5d654", "metadata": {}, "source": [ "We can also look up the same symbols in different databases (even if they have different tick types)." ] }, { "cell_type": "code", "execution_count": 5, "id": "461969e1", "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", " \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", " \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", " \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", " \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", " \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", " \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", "
TimeBID_PRICEASK_PRICESYMBOL_NAMETICK_TYPE
02023-05-15 09:30:00.000174080412.21412.26SPYQTE
12023-05-15 09:30:00.000174080412.22412.25SPYNBBO
22023-05-15 09:30:00.000293590325.51325.55QQQQTE
32023-05-15 09:30:00.000293590325.51325.54QQQNBBO
42023-05-15 09:30:00.000524867325.51325.57QQQQTE
52023-05-15 09:30:00.000524962325.06325.57QQQQTE
62023-05-15 09:30:00.000524962325.51325.54QQQNBBO
72023-05-15 09:30:00.000580467325.51325.55QQQQTE
82023-05-15 09:30:00.000580467325.51325.54QQQNBBO
92023-05-15 09:30:00.000595200412.21412.24SPYQTE
102023-05-15 09:30:00.000595200412.22412.24SPYNBBO
112023-05-15 09:30:00.000610755325.51325.54QQQQTE
122023-05-15 09:30:00.000610755325.51325.54QQQNBBO
132023-05-15 09:30:00.000640528325.51325.54QQQQTE
142023-05-15 09:30:00.000640528325.51325.54QQQNBBO
152023-05-15 09:30:00.000715520412.21412.25SPYQTE
162023-05-15 09:30:00.000715520412.21412.24SPYNBBO
172023-05-15 09:30:00.000730138325.51325.54QQQQTE
182023-05-15 09:30:00.000730138325.51325.54QQQNBBO
192023-05-15 09:30:00.000752301325.51325.55QQQQTE
202023-05-15 09:30:00.000752301325.51325.54QQQNBBO
212023-05-15 09:30:00.000790784412.22412.25SPYQTE
222023-05-15 09:30:00.000790784412.22412.24SPYNBBO
232023-05-15 09:30:00.000845125325.51325.54QQQQTE
242023-05-15 09:30:00.000845125325.51325.54QQQNBBO
252023-05-15 09:30:00.000860928412.22412.25SPYQTE
262023-05-15 09:30:00.000860928412.22412.24SPYNBBO
272023-05-15 09:30:00.000924837325.06325.57QQQQTE
282023-05-15 09:30:00.000924837325.51325.54QQQNBBO
292023-05-15 09:30:00.000931072412.21412.24SPYQTE
302023-05-15 09:30:00.000931072412.21412.24SPYQTE
312023-05-15 09:30:00.000931072412.22412.24SPYNBBO
322023-05-15 09:30:00.000931072412.22412.24SPYNBBO
332023-05-15 09:30:00.000935936412.21412.26SPYQTE
342023-05-15 09:30:00.000966144412.21412.25SPYQTE
352023-05-15 09:30:00.000966144412.22412.25SPYNBBO
362023-05-15 09:30:00.000996096412.22412.24SPYQTE
372023-05-15 09:30:00.000996096412.22412.24SPYNBBO
\n", "
" ], "text/plain": [ " Time BID_PRICE ASK_PRICE SYMBOL_NAME TICK_TYPE\n", "0 2023-05-15 09:30:00.000174080 412.21 412.26 SPY QTE\n", "1 2023-05-15 09:30:00.000174080 412.22 412.25 SPY NBBO\n", "2 2023-05-15 09:30:00.000293590 325.51 325.55 QQQ QTE\n", "3 2023-05-15 09:30:00.000293590 325.51 325.54 QQQ NBBO\n", "4 2023-05-15 09:30:00.000524867 325.51 325.57 QQQ QTE\n", "5 2023-05-15 09:30:00.000524962 325.06 325.57 QQQ QTE\n", "6 2023-05-15 09:30:00.000524962 325.51 325.54 QQQ NBBO\n", "7 2023-05-15 09:30:00.000580467 325.51 325.55 QQQ QTE\n", "8 2023-05-15 09:30:00.000580467 325.51 325.54 QQQ NBBO\n", "9 2023-05-15 09:30:00.000595200 412.21 412.24 SPY QTE\n", "10 2023-05-15 09:30:00.000595200 412.22 412.24 SPY NBBO\n", "11 2023-05-15 09:30:00.000610755 325.51 325.54 QQQ QTE\n", "12 2023-05-15 09:30:00.000610755 325.51 325.54 QQQ NBBO\n", "13 2023-05-15 09:30:00.000640528 325.51 325.54 QQQ QTE\n", "14 2023-05-15 09:30:00.000640528 325.51 325.54 QQQ NBBO\n", "15 2023-05-15 09:30:00.000715520 412.21 412.25 SPY QTE\n", "16 2023-05-15 09:30:00.000715520 412.21 412.24 SPY NBBO\n", "17 2023-05-15 09:30:00.000730138 325.51 325.54 QQQ QTE\n", "18 2023-05-15 09:30:00.000730138 325.51 325.54 QQQ NBBO\n", "19 2023-05-15 09:30:00.000752301 325.51 325.55 QQQ QTE\n", "20 2023-05-15 09:30:00.000752301 325.51 325.54 QQQ NBBO\n", "21 2023-05-15 09:30:00.000790784 412.22 412.25 SPY QTE\n", "22 2023-05-15 09:30:00.000790784 412.22 412.24 SPY NBBO\n", "23 2023-05-15 09:30:00.000845125 325.51 325.54 QQQ QTE\n", "24 2023-05-15 09:30:00.000845125 325.51 325.54 QQQ NBBO\n", "25 2023-05-15 09:30:00.000860928 412.22 412.25 SPY QTE\n", "26 2023-05-15 09:30:00.000860928 412.22 412.24 SPY NBBO\n", "27 2023-05-15 09:30:00.000924837 325.06 325.57 QQQ QTE\n", "28 2023-05-15 09:30:00.000924837 325.51 325.54 QQQ NBBO\n", "29 2023-05-15 09:30:00.000931072 412.21 412.24 SPY QTE\n", "30 2023-05-15 09:30:00.000931072 412.21 412.24 SPY QTE\n", "31 2023-05-15 09:30:00.000931072 412.22 412.24 SPY NBBO\n", "32 2023-05-15 09:30:00.000931072 412.22 412.24 SPY NBBO\n", "33 2023-05-15 09:30:00.000935936 412.21 412.26 SPY QTE\n", "34 2023-05-15 09:30:00.000966144 412.21 412.25 SPY QTE\n", "35 2023-05-15 09:30:00.000966144 412.22 412.25 SPY NBBO\n", "36 2023-05-15 09:30:00.000996096 412.22 412.24 SPY QTE\n", "37 2023-05-15 09:30:00.000996096 412.22 412.24 SPY NBBO" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "qte = otp.DataSource('NYSE_TAQ', tick_type='QTE')\n", "qte = qte[['BID_PRICE', 'ASK_PRICE']]\n", "nbbo = otp.DataSource('TAQ_NBBO', tick_type='NBBO')\n", "nbbo = nbbo[['BID_PRICE', 'ASK_PRICE']]\n", "\n", "q = otp.merge([qte,nbbo], symbols=['SPY', 'QQQ'], identify_input_ts=True)\n", "otp.run(q, start=s, end=s+otp.Milli(1))" ] }, { "cell_type": "code", "execution_count": null, "id": "20489bd7", "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.6" } }, "nbformat": 4, "nbformat_minor": 5 }