Added edge filter for static analysis

This commit is contained in:
Taha Afzal committed 2021-01-04 10:06:51 -05:00
1 parent 635d4196f1
commit 9a0bc05b8b
1 file changed
+94 -284
@@ -2,36 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 168,
"metadata": {},
"outputs": [],
"source": [
"# adds a new col called opp_p_id which is simply\n",
"# a numerical representation of [opp_id,pid]\n",
"def add_ids(poly_vals):\n",
" oid_map = {-1:{-1:0}}\n",
" oid_map.pop(-1)\n",
" curr_id=0\n",
" def get_new_id():\n",
" nonlocal curr_id\n",
" curr_id = curr_id+1\n",
" return curr_id\n",
" def get_opp_p_id(r):\n",
" oid = r['opp_id']\n",
" pid = r['pid']\n",
" if oid_map.get(oid)==None:\n",
" oid_map[oid]={-1:0}\n",
" pid_map = oid_map[oid]\n",
" if pid_map.get(pid)==None:\n",
" pid_map[pid]=get_new_id()\n",
" return oid_map[oid][pid]\n",
" poly_vals['opp_p_id'] = poly_vals.apply (lambda row: get_opp_p_id(row), axis=1)\n",
" return poly_vals"
]
},
{
"cell_type": "code",
"execution_count": 169,
"execution_count": 482,
"metadata": {},
"outputs": [],
"source": [
@@ -42,7 +13,63 @@
},
{
"cell_type": "code",
"execution_count": 170,
"execution_count": 483,
"metadata": {},
"outputs": [],
"source": [
"# returns the poly vals db table filtered for the products\n",
"def get_poly_vals_db(full_db_name, products):\n",
" where_txt = ''\n",
" i=0\n",
" for p in products:\n",
" p_filter = 'product=' + \"'\" + p + \"'\"\n",
" prefix_or = '' if (i==0) else ' or ' \n",
" where_txt = where_txt + prefix_or + p_filter\n",
" i=i+1\n",
" where_txt = '(' + where_txt + ')'\n",
" query = 'select * from polygon_value_events where ' + where_txt\n",
" print(query)\n",
" return pd.read_sql_query(query, sqlite3.connect(full_db_name))"
]
},
{
"cell_type": "code",
"execution_count": 484,
"metadata": {},
"outputs": [],
"source": [
"# adds a new col called opp_p_id which is simply\n",
"# a unique id for [opp_id,pid]\n",
"def add_opp_p_ids(poly_vals):\n",
" key_map = {'':0}\n",
" def get_opp_p_id(r):\n",
" key = str(r['opp_id']) + '_' + str(r['pid'])\n",
" if key_map.get(key)==None:\n",
" key_map[key]=len(key_map) + 1\n",
" return key_map[key]\n",
" poly_vals['opp_p_id'] = poly_vals.apply (lambda row: get_opp_p_id(row), axis=1)\n",
" return poly_vals"
]
},
{
"cell_type": "code",
"execution_count": 485,
"metadata": {},
"outputs": [],
"source": [
"# adds a new column called ticks (its hard coded to convert\n",
"# merged_value to ticks)\n",
"def add_ticks(poly_vals):\n",
" val_to_tick_map = {'GC':10, 'SI':5, 'HG':12.5}\n",
" def get_ticks(r):\n",
" return r['merged_value']/val_to_tick_map[r['product']]\n",
" poly_vals['ticks'] = poly_vals.apply (lambda row: get_ticks(row), axis=1)\n",
" return poly_vals"
]
},
{
"cell_type": "code",
"execution_count": 486,
"metadata": {},
"outputs": [],
"source": [
@@ -67,33 +94,32 @@
},
{
"cell_type": "code",
"execution_count": 171,
"execution_count": 487,
"metadata": {},
"outputs": [],
"source": [
"# returns the poly vals view filtered for the product\n",
"def get_poly_vals_db(full_db_name, products):\n",
" where_txt = ''\n",
" i=0\n",
" for p in products:\n",
" p_filter = 'product=' + \"'\" + p + \"'\"\n",
" prefix_or = '' if (i==0) else ' or ' \n",
" where_txt = where_txt + prefix_or + p_filter\n",
" i=i+1\n",
" where_txt = '(' + where_txt + ')'\n",
" query = 'select * from polygon_value_events where ' + where_txt\n",
" print(query)\n",
" return pd.read_sql_query(query, sqlite3.connect(full_db_name))"
"# filters out rows that dont meet the edge criteria specified\n",
"# in edge_map\n",
"def get_edge_filtered_rows(poly_vals, edge_map):\n",
" def apply_edge_filter(r):\n",
" return r['ticks'] >= edge_map[r['product']]\n",
" poly_vals['passes_filter'] = poly_vals.apply (lambda r: apply_edge_filter(r), axis=1)\n",
" poly_vals = poly_vals[poly_vals['passes_filter']==True]\n",
" del poly_vals['passes_filter']\n",
" return poly_vals"
]
},
{
"cell_type": "code",
"execution_count": 172,
"execution_count": 488,
"metadata": {},
"outputs": [],
"source": [
"def enrich_poly_vals(poly_vals_db):\n",
" poly_vals = add_ids(poly_vals_db)\n",
"def enrich_poly_vals(poly_vals_db, edge_map): \n",
" # ticks for the edge\n",
" poly_vals = add_ticks(poly_vals_db)\n",
" # filter for ticks\n",
" poly_vals = get_edge_filtered_rows(poly_vals, edge_map)\n",
" # add opp_start opp_first_s_time and last_first_s_time\n",
" poly_vals = add_opp_start_times(poly_vals)\n",
" poly_vals['proc_time'] = poly_vals['last_s_time']-poly_vals['t_time']\n",
@@ -101,35 +127,38 @@
" poly_vals['send_time_delta'] = poly_vals['last_s_time']-poly_vals['first_s_time']\n",
" poly_vals['t_time_from_opp_first_send'] = poly_vals['t_time']-poly_vals['opp_first_s_time']\n",
" poly_vals['t_time_from_opp_last_send'] = poly_vals['t_time']-poly_vals['opp_last_s_time']\n",
" # hide some cols\n",
" rv=poly_vals[['product' 'vid', 'pid', 'symbol', 'opp_id', 'opp_p_id', 'eid', 't_time', 'h_t_time',\n",
" # add opp_p_id\n",
" poly_vals = add_opp_p_ids(poly_vals)\n",
" # hide some cols \n",
" rv=poly_vals[['product', 'vid', 'pid', 'symbol', 'opp_id', 'opp_p_id', 'eid', 't_time', 'h_t_time',\n",
" 'n_packets', 'send_time_delta', 'proc_time',\n",
" 't_time_from_opp_first_send', 't_time_from_opp_last_send',\n",
" 'side', 'direct_value', 'direct_qty', 'merged_value', 'merged_qty',\n",
" 'side', 'direct_value', 'direct_qty', 'merged_value', 'merged_qty', 'ticks',\n",
" 'poly_bid_bk', 'poly_ask_bk',\n",
" 'contract', 'change', 'ok', 'order_side', 'price','qty',\n",
" 'book_1_sid', 'book_1_eid', 'book_1_side','book_1_bid_bk', 'book_1_ask_bk', \n",
" 'book_2_sid', 'book_2_eid','book_2_side', 'book_2_bid_bk', 'book_2_ask_bk',\n",
" 'book_3_sid', 'book_3_eid', 'book_3_side', 'book_3_bid_bk', 'book_3_ask_bk',\n",
" 'book_4_sid', 'book_4_eid', 'book_4_side', 'book_4_bid_bk', 'book_4_ask_bk']]\n",
" # sort by opp_id, pid, eid (to have consecutive rows for one polygon)\n",
" rv = rv.sort_values(['opp_id', 'pid', 'eid'], ascending=[True, True, True])\n",
" # sort by opp_id, pid, eid (to have consecutive rows for one polygon) \n",
" # rv = rv.sort_values(['opp_id', 'pid', 'eid'], ascending=[True, True, True])\n",
" rv = rv.sort_values(['opp_p_id', 'eid'], ascending=[True, True])\n",
" return rv"
]
},
{
"cell_type": "code",
"execution_count": 178,
"execution_count": 489,
"metadata": {},
"outputs": [],
"source": [
"def get_enriched_poly_vals(full_db_name, products):\n",
"def get_enriched_poly_vals(full_db_name, edge_map):\n",
" products = edge_map.keys()\n",
" poly_vals_db = get_poly_vals_db(full_db_name, products)\n",
" poly_vals = enrich_poly_vals(poly_vals_db)\n",
" poly_vals = enrich_poly_vals(poly_vals_db, edge_map) \n",
" appended_products = ''\n",
" for p in products:\n",
" appended_products = appended_products + '_' + p\n",
" print(appended_products)\n",
" file_name = full_db_name + '.sniper_opps' + appended_products + '.csv'\n",
" poly_vals.to_csv(file_name)\n",
" return poly_vals"
@@ -137,250 +166,31 @@
},
{
"cell_type": "code",
"execution_count": 179,
"execution_count": 491,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"select * from polygon_value_events where (product='GC' or product='HG')\n",
"_GC_HG\n"
"select * from polygon_value_events where (product='GC' or product='_HG' or product='_SI')\n"
]
}
],
"source": [
"g_r=get_enriched_poly_vals('/home/taha/sparkdata/simulations/comex/2020-12-01_360_24h.sqlite',['GC','HG'])"
]
},
{
"cell_type": "code",
"execution_count": 166,
"metadata": {},
"outputs": [
},
{
"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>vid</th>\n",
" <th>pid</th>\n",
" <th>symbol</th>\n",
" <th>opp_id</th>\n",
" <th>opp_p_id</th>\n",
" <th>eid</th>\n",
" <th>t_time</th>\n",
" <th>h_t_time</th>\n",
" <th>n_packets</th>\n",
" <th>send_time_delta</th>\n",
" <th>...</th>\n",
" <th>book_3_sid</th>\n",
" <th>book_3_eid</th>\n",
" <th>book_3_side</th>\n",
" <th>book_3_bid_bk</th>\n",
" <th>book_3_ask_bk</th>\n",
" <th>book_4_sid</th>\n",
" <th>book_4_eid</th>\n",
" <th>book_4_side</th>\n",
" <th>book_4_bid_bk</th>\n",
" <th>book_4_ask_bk</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>4596</td>\n",
" <td>51</td>\n",
" <td>+GCZ0 -GCG1 -(GCF1-GCG1) -(GCZ0-GCF1)</td>\n",
" <td>10</td>\n",
" <td>1</td>\n",
" <td>2802734</td>\n",
" <td>1606905177529835605</td>\n",
" <td>10:32:57.529835605</td>\n",
" <td>6</td>\n",
" <td>84857</td>\n",
" <td>...</td>\n",
" <td>21661</td>\n",
" <td>2802723</td>\n",
" <td>SIDE_BUY</td>\n",
" <td>-22/2 -23/5 -24/6 -25/9 -26/10</td>\n",
" <td>-20/5 -19/2 -18/8 -17/9 -16/14</td>\n",
" <td>27845</td>\n",
" <td>2802734</td>\n",
" <td>SIDE_BUY</td>\n",
" <td>-25/6 -26/57 -27/60 -28/47 -29/81</td>\n",
" <td>-24/28 -23/86 -22/68 -21/42 -20/56</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>4600</td>\n",
" <td>51</td>\n",
" <td>+GCZ0 -GCG1 -(GCF1-GCG1) -(GCZ0-GCF1)</td>\n",
" <td>10</td>\n",
" <td>1</td>\n",
" <td>2802735</td>\n",
" <td>1606905177530224689</td>\n",
" <td>10:32:57.530224689</td>\n",
" <td>2</td>\n",
" <td>8267</td>\n",
" <td>...</td>\n",
" <td>21661</td>\n",
" <td>2802723</td>\n",
" <td>SIDE_BUY</td>\n",
" <td>-22/2 -23/5 -24/6 -25/9 -26/10</td>\n",
" <td>-20/5 -19/2 -18/8 -17/9 -16/14</td>\n",
" <td>27845</td>\n",
" <td>2802734</td>\n",
" <td>SIDE_BUY</td>\n",
" <td>-25/6 -26/57 -27/60 -28/47 -29/81</td>\n",
" <td>-24/28 -23/86 -22/68 -21/42 -20/56</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>4603</td>\n",
" <td>51</td>\n",
" <td>+GCZ0 -GCG1 -(GCF1-GCG1) -(GCZ0-GCF1)</td>\n",
" <td>10</td>\n",
" <td>1</td>\n",
" <td>2802736</td>\n",
" <td>1606905177530225269</td>\n",
" <td>10:32:57.530225269</td>\n",
" <td>3</td>\n",
" <td>14216</td>\n",
" <td>...</td>\n",
" <td>21661</td>\n",
" <td>2802723</td>\n",
" <td>SIDE_BUY</td>\n",
" <td>-22/2 -23/5 -24/6 -25/9 -26/10</td>\n",
" <td>-20/5 -19/2 -18/8 -17/9 -16/14</td>\n",
" <td>27845</td>\n",
" <td>2802734</td>\n",
" <td>SIDE_BUY</td>\n",
" <td>-25/6 -26/57 -27/60 -28/47 -29/81</td>\n",
" <td>-24/28 -23/86 -22/68 -21/42 -20/56</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>4606</td>\n",
" <td>51</td>\n",
" <td>+GCZ0 -GCG1 -(GCF1-GCG1) -(GCZ0-GCF1)</td>\n",
" <td>10</td>\n",
" <td>1</td>\n",
" <td>2802737</td>\n",
" <td>1606905177530227117</td>\n",
" <td>10:32:57.530227117</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>...</td>\n",
" <td>21661</td>\n",
" <td>2802723</td>\n",
" <td>SIDE_BUY</td>\n",
" <td>-22/2 -23/5 -24/6 -25/9 -26/10</td>\n",
" <td>-20/5 -19/2 -18/8 -17/9 -16/14</td>\n",
" <td>27845</td>\n",
" <td>2802734</td>\n",
" <td>SIDE_BUY</td>\n",
" <td>-25/6 -26/57 -27/60 -28/47 -29/81</td>\n",
" <td>-24/28 -23/86 -22/68 -21/42 -20/56</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>4609</td>\n",
" <td>51</td>\n",
" <td>+GCZ0 -GCG1 -(GCF1-GCG1) -(GCZ0-GCF1)</td>\n",
" <td>10</td>\n",
" <td>1</td>\n",
" <td>2802738</td>\n",
" <td>1606905177530227549</td>\n",
" <td>10:32:57.530227549</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>...</td>\n",
" <td>21661</td>\n",
" <td>2802723</td>\n",
" <td>SIDE_BUY</td>\n",
" <td>-22/2 -23/5 -24/6 -25/9 -26/10</td>\n",
" <td>-20/5 -19/2 -18/8 -17/9 -16/14</td>\n",
" <td>27845</td>\n",
" <td>2802734</td>\n",
" <td>SIDE_BUY</td>\n",
" <td>-25/6 -26/57 -27/60 -28/47 -29/81</td>\n",
" <td>-24/28 -23/86 -22/68 -21/42 -20/56</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5 rows × 46 columns</p>\n",
"</div>"
],
"text/plain": [
" vid pid symbol opp_id opp_p_id \\\n",
"0 4596 51 +GCZ0 -GCG1 -(GCF1-GCG1) -(GCZ0-GCF1) 10 1 \n",
"4 4600 51 +GCZ0 -GCG1 -(GCF1-GCG1) -(GCZ0-GCF1) 10 1 \n",
"7 4603 51 +GCZ0 -GCG1 -(GCF1-GCG1) -(GCZ0-GCF1) 10 1 \n",
"10 4606 51 +GCZ0 -GCG1 -(GCF1-GCG1) -(GCZ0-GCF1) 10 1 \n",
"13 4609 51 +GCZ0 -GCG1 -(GCF1-GCG1) -(GCZ0-GCF1) 10 1 \n",
"\n",
" eid t_time h_t_time n_packets \\\n",
"0 2802734 1606905177529835605 10:32:57.529835605 6 \n",
"4 2802735 1606905177530224689 10:32:57.530224689 2 \n",
"7 2802736 1606905177530225269 10:32:57.530225269 3 \n",
"10 2802737 1606905177530227117 10:32:57.530227117 1 \n",
"13 2802738 1606905177530227549 10:32:57.530227549 1 \n",
"\n",
" send_time_delta ... book_3_sid book_3_eid book_3_side \\\n",
"0 84857 ... 21661 2802723 SIDE_BUY \n",
"4 8267 ... 21661 2802723 SIDE_BUY \n",
"7 14216 ... 21661 2802723 SIDE_BUY \n",
"10 0 ... 21661 2802723 SIDE_BUY \n",
"13 0 ... 21661 2802723 SIDE_BUY \n",
"\n",
" book_3_bid_bk book_3_ask_bk \\\n",
"0 -22/2 -23/5 -24/6 -25/9 -26/10 -20/5 -19/2 -18/8 -17/9 -16/14 \n",
"4 -22/2 -23/5 -24/6 -25/9 -26/10 -20/5 -19/2 -18/8 -17/9 -16/14 \n",
"7 -22/2 -23/5 -24/6 -25/9 -26/10 -20/5 -19/2 -18/8 -17/9 -16/14 \n",
"10 -22/2 -23/5 -24/6 -25/9 -26/10 -20/5 -19/2 -18/8 -17/9 -16/14 \n",
"13 -22/2 -23/5 -24/6 -25/9 -26/10 -20/5 -19/2 -18/8 -17/9 -16/14 \n",
"\n",
" book_4_sid book_4_eid book_4_side book_4_bid_bk \\\n",
"0 27845 2802734 SIDE_BUY -25/6 -26/57 -27/60 -28/47 -29/81 \n",
"4 27845 2802734 SIDE_BUY -25/6 -26/57 -27/60 -28/47 -29/81 \n",
"7 27845 2802734 SIDE_BUY -25/6 -26/57 -27/60 -28/47 -29/81 \n",
"10 27845 2802734 SIDE_BUY -25/6 -26/57 -27/60 -28/47 -29/81 \n",
"13 27845 2802734 SIDE_BUY -25/6 -26/57 -27/60 -28/47 -29/81 \n",
"\n",
" book_4_ask_bk \n",
"0 -24/28 -23/86 -22/68 -21/42 -20/56 \n",
"4 -24/28 -23/86 -22/68 -21/42 -20/56 \n",
"7 -24/28 -23/86 -22/68 -21/42 -20/56 \n",
"10 -24/28 -23/86 -22/68 -21/42 -20/56 \n",
"13 -24/28 -23/86 -22/68 -21/42 -20/56 \n",
"\n",
"[5 rows x 46 columns]"
"(93409, 48)"
]
},
"execution_count": 166,
"execution_count": 491,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g_r.head()"
"g_r=get_enriched_poly_vals('/home/taha/sparkdata/simulations/comex/2020-11-26_360_24h.sqlite',\n",
" {'GC':5, '_HG':2, '_SI':5})\n",
"g_r.shape"
]
},
{