update of vbt doc

This commit is contained in:
David Brazda
2024-04-25 06:24:51 +02:00
parent c3d22e439f
commit 6b2a4bb066
4 changed files with 24529 additions and 30 deletions

View File

@ -2,9 +2,130 @@
"cells": [
{
"cell_type": "code",
"execution_count": 197,
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Activating profile profile1\n",
"</pre>\n"
],
"text/plain": [
"Activating profile profile1\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Profile profile1 loaded successfully.\n",
"</pre>\n"
],
"text/plain": [
"Profile profile1 loaded successfully.\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Current values:\n",
"<span style=\"font-weight: bold\">{</span>\n",
" <span style=\"color: #008000; text-decoration-color: #008000\">'AGG_EXCLUDED_TRADES'</span>: <span style=\"font-weight: bold\">[</span><span style=\"color: #008000; text-decoration-color: #008000\">'C'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'O'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'4'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'B'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'7'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'V'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'P'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'W'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'U'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'Z'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'F'</span><span style=\"font-weight: bold\">]</span>,\n",
" <span style=\"color: #008000; text-decoration-color: #008000\">'BT_DELAYS'</span>: <span style=\"font-weight: bold\">{</span>\n",
" <span style=\"color: #008000; text-decoration-color: #008000\">'trigger_to_strat'</span>: <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0.02</span>,\n",
" <span style=\"color: #008000; text-decoration-color: #008000\">'strat_to_sub'</span>: <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0.023</span>,\n",
" <span style=\"color: #008000; text-decoration-color: #008000\">'sub_to_fill'</span>: <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0.008</span>,\n",
" <span style=\"color: #008000; text-decoration-color: #008000\">'fill_to_not'</span>: <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0.023</span>,\n",
" <span style=\"color: #008000; text-decoration-color: #008000\">'limit_order_offset'</span>: <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0</span>\n",
" <span style=\"font-weight: bold\">}</span>,\n",
" <span style=\"color: #008000; text-decoration-color: #008000\">'BT_FILL_CONDITION_BUY_LIMIT'</span>: <span style=\"font-weight: bold\">&lt;</span><span style=\"color: #ff00ff; text-decoration-color: #ff00ff; font-weight: bold\">FillCondition.SLOW:</span><span style=\"color: #000000; text-decoration-color: #000000\"> </span><span style=\"color: #008000; text-decoration-color: #008000\">'slow'</span><span style=\"color: #000000; text-decoration-color: #000000\">&gt;,</span>\n",
"<span style=\"color: #000000; text-decoration-color: #000000\"> </span><span style=\"color: #008000; text-decoration-color: #008000\">'BT_FILL_CONDITION_SELL_LIMIT'</span><span style=\"color: #000000; text-decoration-color: #000000\">: &lt;FillCondition.SLOW: </span><span style=\"color: #008000; text-decoration-color: #008000\">'slow'</span><span style=\"font-weight: bold\">&gt;</span>,\n",
" <span style=\"color: #008000; text-decoration-color: #008000\">'BT_FILL_CONS_TRADES_REQUIRED'</span>: <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">2</span>,\n",
" <span style=\"color: #008000; text-decoration-color: #008000\">'BT_FILL_LOG_SURROUNDING_TRADES'</span>: <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">10</span>,\n",
" <span style=\"color: #008000; text-decoration-color: #008000\">'BT_FILL_PRICE_MARKET_ORDER_PREMIUM'</span>: <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0.005</span>,\n",
" <span style=\"color: #008000; text-decoration-color: #008000\">'COUNT_API_REQUESTS'</span>: <span style=\"color: #ff0000; text-decoration-color: #ff0000; font-style: italic\">False</span>,\n",
" <span style=\"color: #008000; text-decoration-color: #008000\">'GROUP_TRADES_WITH_TIMESTAMP_LESS_THAN'</span>: <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0.003</span>,\n",
" <span style=\"color: #008000; text-decoration-color: #008000\">'ILOG_SAVE_LEVEL_FROM'</span>: <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>,\n",
" <span style=\"color: #008000; text-decoration-color: #008000\">'LIVE_DATA_FEED'</span>: <span style=\"color: #008000; text-decoration-color: #008000\">'sip'</span>,\n",
" <span style=\"color: #008000; text-decoration-color: #008000\">'NORMALIZED_TICK_BASE_PRICE'</span>: <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">30.0</span>,\n",
" <span style=\"color: #008000; text-decoration-color: #008000\">'OFFLINE_MODE'</span>: <span style=\"color: #ff0000; text-decoration-color: #ff0000; font-style: italic\">False</span>,\n",
" <span style=\"color: #008000; text-decoration-color: #008000\">'PROD_SERVER_HOSTNAMES'</span>: <span style=\"font-weight: bold\">[</span><span style=\"color: #008000; text-decoration-color: #008000\">'tradingeastcoast'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'David-MacBook-Pro.local'</span><span style=\"font-weight: bold\">]</span>,\n",
" <span style=\"color: #008000; text-decoration-color: #008000\">'QUIET_MODE'</span>: <span style=\"color: #00ff00; text-decoration-color: #00ff00; font-style: italic\">True</span>,\n",
" <span style=\"color: #008000; text-decoration-color: #008000\">'TEST_SERVER_HOSTNAMES'</span>: <span style=\"font-weight: bold\">[</span><span style=\"color: #008000; text-decoration-color: #008000\">'tradingtest'</span><span style=\"font-weight: bold\">]</span>\n",
"<span style=\"font-weight: bold\">}</span>\n",
"</pre>\n"
],
"text/plain": [
"Current values:\n",
"\u001b[1m{\u001b[0m\n",
" \u001b[32m'AGG_EXCLUDED_TRADES'\u001b[0m: \u001b[1m[\u001b[0m\u001b[32m'C'\u001b[0m, \u001b[32m'O'\u001b[0m, \u001b[32m'4'\u001b[0m, \u001b[32m'B'\u001b[0m, \u001b[32m'7'\u001b[0m, \u001b[32m'V'\u001b[0m, \u001b[32m'P'\u001b[0m, \u001b[32m'W'\u001b[0m, \u001b[32m'U'\u001b[0m, \u001b[32m'Z'\u001b[0m, \u001b[32m'F'\u001b[0m\u001b[1m]\u001b[0m,\n",
" \u001b[32m'BT_DELAYS'\u001b[0m: \u001b[1m{\u001b[0m\n",
" \u001b[32m'trigger_to_strat'\u001b[0m: \u001b[1;36m0.02\u001b[0m,\n",
" \u001b[32m'strat_to_sub'\u001b[0m: \u001b[1;36m0.023\u001b[0m,\n",
" \u001b[32m'sub_to_fill'\u001b[0m: \u001b[1;36m0.008\u001b[0m,\n",
" \u001b[32m'fill_to_not'\u001b[0m: \u001b[1;36m0.023\u001b[0m,\n",
" \u001b[32m'limit_order_offset'\u001b[0m: \u001b[1;36m0\u001b[0m\n",
" \u001b[1m}\u001b[0m,\n",
" \u001b[32m'BT_FILL_CONDITION_BUY_LIMIT'\u001b[0m: \u001b[1m<\u001b[0m\u001b[1;95mFillCondition.SLOW:\u001b[0m\u001b[39m \u001b[0m\u001b[32m'slow'\u001b[0m\u001b[39m>,\u001b[0m\n",
"\u001b[39m \u001b[0m\u001b[32m'BT_FILL_CONDITION_SELL_LIMIT'\u001b[0m\u001b[39m: <FillCondition.SLOW: \u001b[0m\u001b[32m'slow'\u001b[0m\u001b[1m>\u001b[0m,\n",
" \u001b[32m'BT_FILL_CONS_TRADES_REQUIRED'\u001b[0m: \u001b[1;36m2\u001b[0m,\n",
" \u001b[32m'BT_FILL_LOG_SURROUNDING_TRADES'\u001b[0m: \u001b[1;36m10\u001b[0m,\n",
" \u001b[32m'BT_FILL_PRICE_MARKET_ORDER_PREMIUM'\u001b[0m: \u001b[1;36m0.005\u001b[0m,\n",
" \u001b[32m'COUNT_API_REQUESTS'\u001b[0m: \u001b[3;91mFalse\u001b[0m,\n",
" \u001b[32m'GROUP_TRADES_WITH_TIMESTAMP_LESS_THAN'\u001b[0m: \u001b[1;36m0.003\u001b[0m,\n",
" \u001b[32m'ILOG_SAVE_LEVEL_FROM'\u001b[0m: \u001b[1;36m1\u001b[0m,\n",
" \u001b[32m'LIVE_DATA_FEED'\u001b[0m: \u001b[32m'sip'\u001b[0m,\n",
" \u001b[32m'NORMALIZED_TICK_BASE_PRICE'\u001b[0m: \u001b[1;36m30.0\u001b[0m,\n",
" \u001b[32m'OFFLINE_MODE'\u001b[0m: \u001b[3;91mFalse\u001b[0m,\n",
" \u001b[32m'PROD_SERVER_HOSTNAMES'\u001b[0m: \u001b[1m[\u001b[0m\u001b[32m'tradingeastcoast'\u001b[0m, \u001b[32m'David-MacBook-Pro.local'\u001b[0m\u001b[1m]\u001b[0m,\n",
" \u001b[32m'QUIET_MODE'\u001b[0m: \u001b[3;92mTrue\u001b[0m,\n",
" \u001b[32m'TEST_SERVER_HOSTNAMES'\u001b[0m: \u001b[1m[\u001b[0m\u001b[32m'tradingtest'\u001b[0m\u001b[1m]\u001b[0m\n",
"\u001b[1m}\u001b[0m\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">config_handler.<span style=\"color: #808000; text-decoration-color: #808000\">active_profile</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'profile1'</span>\n",
"</pre>\n"
],
"text/plain": [
"config_handler.\u001b[33mactive_profile\u001b[0m=\u001b[32m'profile1'\u001b[0m\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">config handler initialized\n",
"</pre>\n"
],
"text/plain": [
"config handler initialized\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Realtime Websocket connection will use FEED: sip and credential of ACCOUNT1\n"
]
}
],
"source": [
"from v2realbot.tools.loadbatch import load_batch\n",
"from v2realbot.utils.utils import zoneNY\n",
@ -17,13 +138,12 @@
"vbt.settings.plotting.auto_rangebreaks = True\n",
"# Set the option to display with pagination\n",
"pd.set_option('display.notebook_repr_html', True)\n",
"pd.set_option('display.max_rows', 10) # Number of rows per page\n",
"\n"
"pd.set_option('display.max_rows', 10) # Number of rows per page"
]
},
{
"cell_type": "code",
"execution_count": 198,
"execution_count": 2,
"metadata": {},
"outputs": [
{
@ -301,7 +421,7 @@
"[2934 rows x 8 columns]"
]
},
"execution_count": 198,
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
@ -329,7 +449,7 @@
},
{
"cell_type": "code",
"execution_count": 199,
"execution_count": 209,
"metadata": {},
"outputs": [
{
@ -403,7 +523,7 @@
"name": "Rsi14",
"showlegend": true,
"type": "scatter",
"uid": "fbd37d04-c17e-4596-b1f8-2a601550bdac",
"uid": "efdece63-8464-407b-87a7-6ee41d71a834",
"x": [
"2024-03-12T09:30:00-04:00",
"2024-03-12T09:30:23-04:00",
@ -17655,7 +17775,7 @@
36.08
],
"type": "candlestick",
"uid": "b5f3d47a-000c-4c38-9609-c97c97b37434",
"uid": "62660d6c-b186-4034-ab2e-6701e4999efe",
"x": [
"2024-03-12T09:30:00-04:00",
"2024-03-12T09:30:23-04:00",
@ -21594,7 +21714,7 @@
"name": "Volume",
"opacity": 0.5,
"type": "bar",
"uid": "42c50c74-2e22-4820-891f-5a2ca88235c7",
"uid": "42b5b762-4a9c-41e7-9507-ec15130ca6fe",
"x": [
"2024-03-12T09:30:00-04:00",
"2024-03-12T09:30:23-04:00",
@ -35645,7 +35765,7 @@
},
{
"cell_type": "code",
"execution_count": 200,
"execution_count": 210,
"metadata": {},
"outputs": [
{
@ -35693,7 +35813,7 @@
"name": "real",
"showlegend": true,
"type": "scatter",
"uid": "dcdb9795-f954-4341-b6b6-73ab53286ca2",
"uid": "8d844b56-f6ee-460a-9595-0834d2f0acca",
"x": [
"2024-03-12T09:30:00-04:00",
"2024-03-12T09:30:23-04:00",
@ -45082,7 +45202,7 @@
"name": "real",
"showlegend": true,
"type": "scatter",
"uid": "e7db6821-cf5a-4b38-a395-b0821499ceef",
"uid": "0c2b8ac8-2956-4bc8-8308-27f67bcdf881",
"x": [
"2024-03-12T09:30:00-04:00",
"2024-03-12T09:30:23-04:00",
@ -54479,7 +54599,7 @@
},
{
"cell_type": "code",
"execution_count": 201,
"execution_count": 211,
"metadata": {},
"outputs": [
{
@ -54500,7 +54620,7 @@
"Name: Vwap, Length: 1961, dtype: bool"
]
},
"execution_count": 201,
"execution_count": 211,
"metadata": {},
"output_type": "execute_result"
}
@ -54512,7 +54632,7 @@
},
{
"cell_type": "code",
"execution_count": 202,
"execution_count": 212,
"metadata": {},
"outputs": [
{
@ -54533,7 +54653,7 @@
"Name: Vwap, Length: 1961, dtype: bool"
]
},
"execution_count": 202,
"execution_count": 212,
"metadata": {},
"output_type": "execute_result"
}
@ -54545,7 +54665,7 @@
},
{
"cell_type": "code",
"execution_count": 203,
"execution_count": 213,
"metadata": {},
"outputs": [
{
@ -62442,7 +62562,7 @@
36.08
],
"type": "candlestick",
"uid": "dca7bc1e-cf1a-4a22-af08-8eb952085b58",
"uid": "d4ca0ab8-1e54-4e68-8d6b-e2c18d2b243f",
"x": [
"2024-03-12T09:30:00-04:00",
"2024-03-12T09:30:23-04:00",
@ -66381,7 +66501,7 @@
"name": "Volume",
"opacity": 0.5,
"type": "bar",
"uid": "137ad3ae-64fe-4aca-b8d4-efb400fae336",
"uid": "1e37740d-9ee2-449d-a328-90a429c85b3a",
"x": [
"2024-03-12T09:30:00-04:00",
"2024-03-12T09:30:23-04:00",
@ -70319,7 +70439,7 @@
"name": "Vwap",
"showlegend": true,
"type": "scatter",
"uid": "cff29158-ffc3-42e0-9347-c2e3e53ca751",
"uid": "e102314a-487c-4d58-b8e5-db2a3720990a",
"x": [
"2024-03-12T09:30:00-04:00",
"2024-03-12T09:30:23-04:00",
@ -74263,7 +74383,7 @@
"name": "Entries",
"showlegend": true,
"type": "scatter",
"uid": "86e1b024-74be-4ed6-a808-35883f9f4dca",
"uid": "16de330c-817d-45e1-8fd2-b8b7addb8a44",
"x": [
"2024-03-12T11:27:11-04:00",
"2024-03-12T11:46:26-04:00",
@ -74355,7 +74475,7 @@
"name": "Exits",
"showlegend": true,
"type": "scatter",
"uid": "14a67283-6a19-4daa-a4cf-cf6aaa1e2467",
"uid": "ad7b9ceb-b1de-4c9d-ad44-514eb8abc73a",
"x": [
"2024-03-12T09:56:09-04:00",
"2024-03-12T09:57:42-04:00",
@ -92413,7 +92533,7 @@
36.08
],
"type": "candlestick",
"uid": "10208612-fba0-4af1-ae0e-6c3b630e7402",
"uid": "95062a7f-bddf-4ba8-89df-690d1fb7bf78",
"x": [
"2024-03-12T09:30:00-04:00",
"2024-03-12T09:30:23-04:00",
@ -96352,7 +96472,7 @@
"name": "Volume",
"opacity": 0.5,
"type": "bar",
"uid": "b993f341-efa1-4cf3-9490-b47bc606dd9d",
"uid": "f75fd578-357e-4279-8a5c-71b710573a80",
"x": [
"2024-03-12T09:30:00-04:00",
"2024-03-12T09:30:23-04:00",
@ -100290,7 +100410,7 @@
"name": "Vwap",
"showlegend": true,
"type": "scatter",
"uid": "72372cb6-d111-4d69-a5f1-c935e5e7559e",
"uid": "ac249064-e86c-4fd6-b577-a8a0e4553584",
"x": [
"2024-03-12T09:30:00-04:00",
"2024-03-12T09:30:23-04:00",
@ -104234,7 +104354,7 @@
"name": "Entries",
"showlegend": true,
"type": "scatter",
"uid": "af3474cd-1efe-49c0-bacb-9b54f5a92093",
"uid": "d89b4fe1-b7e8-4291-a5a2-35303bf3ee0a",
"x": [
"2024-03-12T11:27:11-04:00",
"2024-03-12T11:46:26-04:00",
@ -104282,7 +104402,7 @@
"name": "Exits",
"showlegend": true,
"type": "scatter",
"uid": "831cde06-a44c-45ba-a1a8-dbba410bf210",
"uid": "21a89431-dd37-4e59-a581-aa704917a3c1",
"x": [
"2024-03-12T11:36:40-04:00",
"2024-03-12T12:55:25-04:00",
@ -114433,7 +114553,7 @@
},
{
"cell_type": "code",
"execution_count": 204,
"execution_count": 214,
"metadata": {},
"outputs": [
{
@ -114442,7 +114562,7 @@
"12"
]
},
"execution_count": 204,
"execution_count": 214,
"metadata": {},
"output_type": "execute_result"
}

23637
research/test.ipynb Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,620 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import pyarrow\n",
"import numpy as np\n",
"from numba import jit\n",
"import v2realbot.utils.config_handler as cfh"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Další info k pokračování je zde https://blog.quantinsti.com/tick-tick-ohlc-data-pandas-tutorial/"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'pandas.core.frame.DataFrame'>\n",
"DatetimeIndex: 190261 entries, 2024-04-22 13:30:00.267711+00:00 to 2024-04-22 19:59:59.987614+00:00\n",
"Data columns (total 6 columns):\n",
" # Column Non-Null Count Dtype \n",
"--- ------ -------------- ----- \n",
" 0 exchange 190261 non-null object \n",
" 1 price 190261 non-null float64\n",
" 2 size 190261 non-null float64\n",
" 3 id 190261 non-null int64 \n",
" 4 conditions 190261 non-null object \n",
" 5 tape 190261 non-null object \n",
"dtypes: float64(2), int64(1), object(3)\n",
"memory usage: 10.2+ MB\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>exchange</th>\n",
" <th>price</th>\n",
" <th>size</th>\n",
" <th>id</th>\n",
" <th>conditions</th>\n",
" <th>tape</th>\n",
" </tr>\n",
" <tr>\n",
" <th>timestamp</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2024-04-22 13:30:00.267711+00:00</th>\n",
" <td>K</td>\n",
" <td>36.890</td>\n",
" <td>5.0</td>\n",
" <td>52983525037630</td>\n",
" <td>[ , F, I]</td>\n",
" <td>A</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-04-22 13:30:00.300501+00:00</th>\n",
" <td>D</td>\n",
" <td>37.005</td>\n",
" <td>1.0</td>\n",
" <td>71675241117014</td>\n",
" <td>[ , I]</td>\n",
" <td>A</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-04-22 13:30:00.305439+00:00</th>\n",
" <td>D</td>\n",
" <td>37.005</td>\n",
" <td>1.0</td>\n",
" <td>71675241117496</td>\n",
" <td>[ , I]</td>\n",
" <td>A</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-04-22 13:30:00.314520+00:00</th>\n",
" <td>D</td>\n",
" <td>37.005</td>\n",
" <td>1.0</td>\n",
" <td>71675241118034</td>\n",
" <td>[ , I]</td>\n",
" <td>A</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-04-22 13:30:00.335201+00:00</th>\n",
" <td>D</td>\n",
" <td>37.005</td>\n",
" <td>1.0</td>\n",
" <td>71675241121369</td>\n",
" <td>[ , I]</td>\n",
" <td>A</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-04-22 19:59:59.902614+00:00</th>\n",
" <td>V</td>\n",
" <td>37.750</td>\n",
" <td>1100.0</td>\n",
" <td>56480705310575</td>\n",
" <td>[ ]</td>\n",
" <td>A</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-04-22 19:59:59.977134+00:00</th>\n",
" <td>N</td>\n",
" <td>37.745</td>\n",
" <td>300.0</td>\n",
" <td>52983559963478</td>\n",
" <td>[ ]</td>\n",
" <td>A</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-04-22 19:59:59.977137+00:00</th>\n",
" <td>N</td>\n",
" <td>37.740</td>\n",
" <td>7300.0</td>\n",
" <td>52983559963696</td>\n",
" <td>[ ]</td>\n",
" <td>A</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-04-22 19:59:59.978626+00:00</th>\n",
" <td>V</td>\n",
" <td>37.750</td>\n",
" <td>16.0</td>\n",
" <td>56480706886228</td>\n",
" <td>[ , I]</td>\n",
" <td>A</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-04-22 19:59:59.987614+00:00</th>\n",
" <td>N</td>\n",
" <td>37.745</td>\n",
" <td>30.0</td>\n",
" <td>52983559963958</td>\n",
" <td>[ , I]</td>\n",
" <td>A</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>190261 rows × 6 columns</p>\n",
"</div>"
],
"text/plain": [
" exchange price size id \\\n",
"timestamp \n",
"2024-04-22 13:30:00.267711+00:00 K 36.890 5.0 52983525037630 \n",
"2024-04-22 13:30:00.300501+00:00 D 37.005 1.0 71675241117014 \n",
"2024-04-22 13:30:00.305439+00:00 D 37.005 1.0 71675241117496 \n",
"2024-04-22 13:30:00.314520+00:00 D 37.005 1.0 71675241118034 \n",
"2024-04-22 13:30:00.335201+00:00 D 37.005 1.0 71675241121369 \n",
"... ... ... ... ... \n",
"2024-04-22 19:59:59.902614+00:00 V 37.750 1100.0 56480705310575 \n",
"2024-04-22 19:59:59.977134+00:00 N 37.745 300.0 52983559963478 \n",
"2024-04-22 19:59:59.977137+00:00 N 37.740 7300.0 52983559963696 \n",
"2024-04-22 19:59:59.978626+00:00 V 37.750 16.0 56480706886228 \n",
"2024-04-22 19:59:59.987614+00:00 N 37.745 30.0 52983559963958 \n",
"\n",
" conditions tape \n",
"timestamp \n",
"2024-04-22 13:30:00.267711+00:00 [ , F, I] A \n",
"2024-04-22 13:30:00.300501+00:00 [ , I] A \n",
"2024-04-22 13:30:00.305439+00:00 [ , I] A \n",
"2024-04-22 13:30:00.314520+00:00 [ , I] A \n",
"2024-04-22 13:30:00.335201+00:00 [ , I] A \n",
"... ... ... \n",
"2024-04-22 19:59:59.902614+00:00 [ ] A \n",
"2024-04-22 19:59:59.977134+00:00 [ ] A \n",
"2024-04-22 19:59:59.977137+00:00 [ ] A \n",
"2024-04-22 19:59:59.978626+00:00 [ , I] A \n",
"2024-04-22 19:59:59.987614+00:00 [ , I] A \n",
"\n",
"[190261 rows x 6 columns]"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tdf=pd.read_parquet('trades_bac.parquet',engine='pyarrow')\n",
"#print(df)\n",
"df = tdf.loc['BAC']\n",
"df.info()\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [],
"source": [
"@jit(nopython=True)\n",
"def ohlcv_bars(ticks, start_time, end_time, resolution):\n",
" \"\"\"\n",
" Generate OHLCV bars from tick data, skipping intervals without trading activity.\n",
" \n",
" Parameters:\n",
" - ticks: numpy array with columns [timestamp, price, size]\n",
" - start_time: the start timestamp for bars (Unix timestamp)\n",
" - end_time: the end timestamp for bars (Unix timestamp)\n",
" - resolution: time resolution in seconds\n",
" \n",
" Returns:\n",
" - OHLCV bars as a numpy array\n",
" \"\"\"\n",
" num_bars = (end_time - start_time) // resolution + 1\n",
" bar_list = []\n",
"\n",
" for i in range(num_bars):\n",
" bar_start_time = start_time + i * resolution\n",
" bar_end_time = bar_start_time + resolution\n",
" bar_ticks = ticks[(ticks[:, 0] >= bar_start_time) & (ticks[:, 0] < bar_end_time)]\n",
" \n",
" if bar_ticks.shape[0] == 0:\n",
" continue # Skip this bar as there are no ticks\n",
"\n",
" # Calculate OHLCV values\n",
" open_price = bar_ticks[0, 1] # open\n",
" high_price = np.max(bar_ticks[:, 1]) # high\n",
" low_price = np.min(bar_ticks[:, 1]) # low\n",
" close_price = bar_ticks[-1, 1] # close\n",
" volume = np.sum(bar_ticks[:, 2]) # volume\n",
" bar_time = bar_start_time # timestamp for the bar\n",
"\n",
" bar_list.append([open_price, high_price, low_price, close_price, volume, bar_time])\n",
"\n",
" # Convert list to numpy array\n",
" if bar_list:\n",
" ohlcv = np.array(bar_list)\n",
" else:\n",
" ohlcv = np.empty((0, 6)) # return an empty array if no bars were created\n",
"\n",
" return ohlcv\n"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'pandas.core.frame.DataFrame'>\n",
"DatetimeIndex: 190261 entries, 2024-04-22 13:30:00.267711+00:00 to 2024-04-22 19:59:59.987614+00:00\n",
"Data columns (total 6 columns):\n",
" # Column Non-Null Count Dtype \n",
"--- ------ -------------- ----- \n",
" 0 exchange 190261 non-null object \n",
" 1 price 190261 non-null float64\n",
" 2 size 190261 non-null float64\n",
" 3 id 190261 non-null int64 \n",
" 4 conditions 190261 non-null object \n",
" 5 tape 190261 non-null object \n",
"dtypes: float64(2), int64(1), object(3)\n",
"memory usage: 10.2+ MB\n"
]
}
],
"source": [
"df.info()"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['C', 'O', '4', 'B', '7', 'V', 'P', 'W', 'U', 'Z', 'F']\n",
"<class 'pandas.core.frame.DataFrame'>\n",
"DatetimeIndex: 143751 entries, 2024-04-22 13:30:00.300501+00:00 to 2024-04-22 19:59:59.987614+00:00\n",
"Data columns (total 6 columns):\n",
" # Column Non-Null Count Dtype \n",
"--- ------ -------------- ----- \n",
" 0 exchange 143751 non-null object \n",
" 1 price 143751 non-null float64\n",
" 2 size 143751 non-null float64\n",
" 3 id 143751 non-null int64 \n",
" 4 conditions 143751 non-null object \n",
" 5 tape 143751 non-null object \n",
"dtypes: float64(2), int64(1), object(3)\n",
"memory usage: 7.7+ MB\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>exchange</th>\n",
" <th>price</th>\n",
" <th>size</th>\n",
" <th>id</th>\n",
" <th>conditions</th>\n",
" <th>tape</th>\n",
" </tr>\n",
" <tr>\n",
" <th>timestamp</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2024-04-22 13:30:00.300501+00:00</th>\n",
" <td>D</td>\n",
" <td>37.005</td>\n",
" <td>1.0</td>\n",
" <td>71675241117014</td>\n",
" <td>[ , I]</td>\n",
" <td>A</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-04-22 13:30:00.305439+00:00</th>\n",
" <td>D</td>\n",
" <td>37.005</td>\n",
" <td>1.0</td>\n",
" <td>71675241117496</td>\n",
" <td>[ , I]</td>\n",
" <td>A</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-04-22 13:30:00.314520+00:00</th>\n",
" <td>D</td>\n",
" <td>37.005</td>\n",
" <td>1.0</td>\n",
" <td>71675241118034</td>\n",
" <td>[ , I]</td>\n",
" <td>A</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-04-22 13:30:00.335201+00:00</th>\n",
" <td>D</td>\n",
" <td>37.005</td>\n",
" <td>1.0</td>\n",
" <td>71675241121369</td>\n",
" <td>[ , I]</td>\n",
" <td>A</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-04-22 13:30:00.346219+00:00</th>\n",
" <td>D</td>\n",
" <td>37.005</td>\n",
" <td>1.0</td>\n",
" <td>71675241122389</td>\n",
" <td>[ , I]</td>\n",
" <td>A</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-04-22 19:59:59.902614+00:00</th>\n",
" <td>V</td>\n",
" <td>37.750</td>\n",
" <td>1100.0</td>\n",
" <td>56480705310575</td>\n",
" <td>[ ]</td>\n",
" <td>A</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-04-22 19:59:59.977134+00:00</th>\n",
" <td>N</td>\n",
" <td>37.745</td>\n",
" <td>300.0</td>\n",
" <td>52983559963478</td>\n",
" <td>[ ]</td>\n",
" <td>A</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-04-22 19:59:59.977137+00:00</th>\n",
" <td>N</td>\n",
" <td>37.740</td>\n",
" <td>7300.0</td>\n",
" <td>52983559963696</td>\n",
" <td>[ ]</td>\n",
" <td>A</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-04-22 19:59:59.978626+00:00</th>\n",
" <td>V</td>\n",
" <td>37.750</td>\n",
" <td>16.0</td>\n",
" <td>56480706886228</td>\n",
" <td>[ , I]</td>\n",
" <td>A</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-04-22 19:59:59.987614+00:00</th>\n",
" <td>N</td>\n",
" <td>37.745</td>\n",
" <td>30.0</td>\n",
" <td>52983559963958</td>\n",
" <td>[ , I]</td>\n",
" <td>A</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>143751 rows × 6 columns</p>\n",
"</div>"
],
"text/plain": [
" exchange price size id \\\n",
"timestamp \n",
"2024-04-22 13:30:00.300501+00:00 D 37.005 1.0 71675241117014 \n",
"2024-04-22 13:30:00.305439+00:00 D 37.005 1.0 71675241117496 \n",
"2024-04-22 13:30:00.314520+00:00 D 37.005 1.0 71675241118034 \n",
"2024-04-22 13:30:00.335201+00:00 D 37.005 1.0 71675241121369 \n",
"2024-04-22 13:30:00.346219+00:00 D 37.005 1.0 71675241122389 \n",
"... ... ... ... ... \n",
"2024-04-22 19:59:59.902614+00:00 V 37.750 1100.0 56480705310575 \n",
"2024-04-22 19:59:59.977134+00:00 N 37.745 300.0 52983559963478 \n",
"2024-04-22 19:59:59.977137+00:00 N 37.740 7300.0 52983559963696 \n",
"2024-04-22 19:59:59.978626+00:00 V 37.750 16.0 56480706886228 \n",
"2024-04-22 19:59:59.987614+00:00 N 37.745 30.0 52983559963958 \n",
"\n",
" conditions tape \n",
"timestamp \n",
"2024-04-22 13:30:00.300501+00:00 [ , I] A \n",
"2024-04-22 13:30:00.305439+00:00 [ , I] A \n",
"2024-04-22 13:30:00.314520+00:00 [ , I] A \n",
"2024-04-22 13:30:00.335201+00:00 [ , I] A \n",
"2024-04-22 13:30:00.346219+00:00 [ , I] A \n",
"... ... ... \n",
"2024-04-22 19:59:59.902614+00:00 [ ] A \n",
"2024-04-22 19:59:59.977134+00:00 [ ] A \n",
"2024-04-22 19:59:59.977137+00:00 [ ] A \n",
"2024-04-22 19:59:59.978626+00:00 [ , I] A \n",
"2024-04-22 19:59:59.987614+00:00 [ , I] A \n",
"\n",
"[143751 rows x 6 columns]"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"excludes = cfh.config_handler.get_val('AGG_EXCLUDED_TRADES')\n",
"print(excludes)\n",
"#excludes = [\"F\", \"I\"]\n",
"# FILTER EXCLUDED TRADES\n",
"# Filter rows to exclude those where 'conditions' contains 'F' or 'I'\n",
"# This simplifies the logic by directly using ~ (bitwise not operator) with np.isin\n",
"df = df[~df['conditions'].apply(lambda x: np.isin(x, excludes).any())]"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/var/folders/8p/dwqnp65s0s77jdbm4_6z4vp80000gn/T/ipykernel_52602/3341929382.py:2: DeprecationWarning: parsing timezone aware datetimes is deprecated; this will raise an error in the future\n",
" structured_array = np.array(list(zip(df.index, df['price'], df['size'])),\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('2024-04-22T13:30:00.300501000', 37.005, 1.0e+00)\n",
" ('2024-04-22T13:30:00.305439000', 37.005, 1.0e+00)\n",
" ('2024-04-22T13:30:00.314520000', 37.005, 1.0e+00) ...\n",
" ('2024-04-22T19:59:59.977137000', 37.74 , 7.3e+03)\n",
" ('2024-04-22T19:59:59.978626000', 37.75 , 1.6e+01)\n",
" ('2024-04-22T19:59:59.987614000', 37.745, 3.0e+01)]\n"
]
},
{
"data": {
"text/plain": [
"array([('2024-04-22T13:30:00.300501000', 37.005, 1.0e+00),\n",
" ('2024-04-22T13:30:00.305439000', 37.005, 1.0e+00),\n",
" ('2024-04-22T13:30:00.314520000', 37.005, 1.0e+00), ...,\n",
" ('2024-04-22T19:59:59.977137000', 37.74 , 7.3e+03),\n",
" ('2024-04-22T19:59:59.978626000', 37.75 , 1.6e+01),\n",
" ('2024-04-22T19:59:59.987614000', 37.745, 3.0e+01)],\n",
" dtype=[('timestamp', '<M8[ns]'), ('price', '<f8'), ('size', '<f8')])"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Creating a structured array with the timestamp as the first element\n",
"structured_array = np.array(list(zip(df.index, df['price'], df['size'])),\n",
" dtype=[('timestamp', 'datetime64[ns]'), ('price', 'float'), ('size', 'float')])\n",
"\n",
"print(structured_array)\n",
"structured_array\n",
"\n",
"# ticks = df[['index', 'price', 'size']].to_numpy()\n",
"# # ticks[:, 0] = pd.to_datetime(ticks[:, 0]).astype('int64') // 1_000_000_000 # \n",
"# ticks"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"resolution_seconds = 1 # 1 second resolution\n",
"ohlcv_data = ohlcv_bars(structured_array, resolution_seconds)\n",
"\n",
"# Converting the result back to DataFrame for better usability\n",
"ohlcv_df = pd.DataFrame(ohlcv_data, columns=['Open', 'High', 'Low', 'Close', 'Volume', 'Time'])\n",
"ohlcv_df['Time'] = pd.to_datetime(ohlcv_df['Time'], unit='s') # Convert timestamps back to datetime\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"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.10.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@ -0,0 +1,122 @@
import pandas as pd
import numpy as np
from numba import jit
from alpaca.data.historical import StockHistoricalDataClient
from v2realbot.config import ACCOUNT1_PAPER_API_KEY, ACCOUNT1_PAPER_SECRET_KEY, DATA_DIR
from alpaca.data.requests import StockTradesRequest
import time
from datetime import datetime
from v2realbot.utils.utils import parse_alpaca_timestamp, ltp, zoneNY, send_to_telegram, fetch_calendar_data
import pyarrow
""""
WIP - for later use
"""""
def fetch_stock_trades(symbol, start, end, max_retries=5, backoff_factor=1):
"""
Attempts to fetch stock trades with exponential backoff. Raises an exception if all retries fail.
:param symbol: The stock symbol to fetch trades for.
:param start: The start time for the trade data.
:param end: The end time for the trade data.
:param max_retries: Maximum number of retries.
:param backoff_factor: Factor to determine the next sleep time.
:return: TradesResponse object.
:raises: ConnectionError if all retries fail.
"""
client = StockHistoricalDataClient(ACCOUNT1_PAPER_API_KEY, ACCOUNT1_PAPER_SECRET_KEY)
stockTradeRequest = StockTradesRequest(symbol_or_symbols=symbol, start=start, end=end)
last_exception = None
for attempt in range(max_retries):
try:
tradesResponse = client.get_stock_trades(stockTradeRequest)
print("Remote Fetch DAY DATA Complete", start, end)
return tradesResponse
except Exception as e:
print(f"Attempt {attempt + 1} failed: {e}")
last_exception = e
time.sleep(backoff_factor * (2 ** attempt))
print("All attempts to fetch data failed.")
raise ConnectionError(f"Failed to fetch stock trades after {max_retries} retries. Last exception: {str(last_exception)} and {format_exc()}")
@jit(nopython=True)
def ohlcv_bars(ticks, start_time, end_time, resolution):
"""
Generate OHLCV bars from tick data, skipping intervals without trading activity.
Parameters:
- ticks: numpy array with columns [timestamp, price, size]
- start_time: the start timestamp for bars (Unix timestamp)
- end_time: the end timestamp for bars (Unix timestamp)
- resolution: time resolution in seconds
Returns:
- OHLCV bars as a numpy array
"""
num_bars = (end_time - start_time) // resolution + 1
bar_list = []
for i in range(num_bars):
bar_start_time = start_time + i * resolution
bar_end_time = bar_start_time + resolution
bar_ticks = ticks[(ticks[:, 0] >= bar_start_time) & (ticks[:, 0] < bar_end_time)]
if bar_ticks.shape[0] == 0:
continue # Skip this bar as there are no ticks
# Calculate OHLCV values
open_price = bar_ticks[0, 1] # open
high_price = np.max(bar_ticks[:, 1]) # high
low_price = np.min(bar_ticks[:, 1]) # low
close_price = bar_ticks[-1, 1] # close
volume = np.sum(bar_ticks[:, 2]) # volume
bar_time = bar_start_time # timestamp for the bar
bar_list.append([open_price, high_price, low_price, close_price, volume, bar_time])
# Convert list to numpy array
if bar_list:
ohlcv = np.array(bar_list)
else:
ohlcv = np.empty((0, 6)) # return an empty array if no bars were created
return ohlcv
# Example usage
if __name__ == '__main__':
# symbol = ["BAC"]
# #datetime in zoneNY
# day_start = datetime(2024, 4, 22, 9, 30, 0)
# day_stop = datetime(2024, 4, 22, 16, 00, 0)
# day_start = zoneNY.localize(day_start)
# day_stop = zoneNY.localize(day_stop)
# tradesResponse = fetch_stock_trades(symbol, day_start, day_stop)
# df = tradesResponse.df
# df.to_parquet('trades_bac.parquet', engine='pyarrow')
df=pd.read_parquet('trades_bac.parquet',engine='pyarrow')
print(df)
#df = pd.read_csv('tick_data.csv') # DF with tick data
# Assuming 'df' is your DataFrame with columns 'time', 'price', 'size', 'condition'
exclude_conditions = ['ConditionA', 'ConditionB'] # Conditions to exclude
df_filtered = df[~df['condition'].isin(exclude_conditions)]
# Define your start and end times based on your trading session, ensure these are Unix timestamps
start_time = pd.to_datetime('2023-01-01 09:30:00').timestamp()
end_time = pd.to_datetime('2023-01-01 16:00:00').timestamp()
ticks = df[['time', 'price', 'size']].to_numpy()
ticks[:, 0] = pd.to_datetime(ticks[:, 0]).astype('int64') // 1_000_000_000 # Convert to Unix timestamp
resolution_seconds = 1 # 1 second resolution
ohlcv_data = ohlcv_bars(ticks, start_time, end_time, resolution_seconds)
# Converting the result back to DataFrame for better usability
ohlcv_df = pd.DataFrame(ohlcv_data, columns=['Open', 'High', 'Low', 'Close', 'Volume', 'Time'])
ohlcv_df['Time'] = pd.to_datetime(ohlcv_df['Time'], unit='s') # Convert timestamps back to datetime