enrich_poly_vals

This commit is contained in:
Taha Afzal committed 2021-02-27 23:40:19 -06:00
1 parent 875c297e6b
commit ffd8eca92f
1 file changed
+72 -66
@@ -2,12 +2,12 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"import spartan\n",
"import sniper_utils as sa\n",
"import sniper_utils as su\n",
"import pandas as pd\n",
"import sqlite3\n",
"import sklearn as skl"
@@ -15,13 +15,15 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"def enrich_poly_vals(poly_vals_db): \n",
"# this function takes in the polygon_values table for the db and\n",
"# adds new columns\n",
"def enrich_poly_vals(poly_vals_db):\n",
" # add opp_start opp_first_s_time and last_first_s_time\n",
" poly_vals = sa.add_opp_start_times(poly_vals_db)\n",
" poly_vals = su.add_opp_start_times(poly_vals_db)\n",
" \n",
" # add the following cols:\n",
" # time delta between exchange seeing a transaction to sending it out\n",
@@ -45,9 +47,8 @@
" 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\n",
" poly_vals = add_opp_p_id_func(poly_vals)\n",
" \n",
" return poly_vals\n",
"\n",
" poly_vals = add_opp_p_id_func(poly_vals) \n",
" \n",
" # conversion to $'s and adding ticks\n",
" def add_ticks_func(poly_vals):\n",
@@ -68,12 +69,12 @@
" poly_vals['direct_value'] = poly_vals.apply (lambda row: get_cash_value(row, False), axis=1)\n",
" poly_vals['ticks'] = poly_vals.apply (lambda row: get_ticks(row), axis=1)\n",
" return poly_vals\n",
" poly_vals = add_ticks_func(poly_vals)\n",
" \n",
" return poly_vals\n",
" poly_vals = add_ticks_func(poly_vals) \n",
"\n",
" # hide some cols\n",
" rv=poly_vals[['status', 'product', 'vid', 'pid', 'symbol', 'opp_id', 'opp_p_id', 'eid', 't_time', 'h_t_time',\n",
" rv=poly_vals[[\n",
" 'status', '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_opp_first_send', 't_opp_last_send',\n",
" 'side', 'direct_value', 'direct_qty', 'merged_value', 'merged_qty', 'ticks',\n",
@@ -82,7 +83,8 @@
" '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",
" 'book_4_sid', 'book_4_eid', 'book_4_side', 'book_4_bid_bk', 'book_4_ask_bk'\n",
" ]]\n",
" \n",
" # sort by opp_id, pid, eid (to have consecutive rows for one polygon) \n",
" rv = rv.sort_values(['opp_p_id', 'eid'], ascending=[True, True])\n",
@@ -91,19 +93,72 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"name": "stdout",
"text": [
"select * from polygon_value_events where (product='GC' or product='SI' or product='HG')\n"
]
}
],
"source": [
"pv_db = sa.get_poly_vals_db('/home/taha/sparkdata/comex/feb/2021-02-16_360_24h.sqlite',['GC','SI','HG'])"
"pv_db = su.get_poly_vals_db('/home/taha/sparkdata/comex/feb/2021-02-24_360_24h.sqlite',\n",
"['GC','SI','HG'])"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(55250, 49)"
]
},
"metadata": {},
"execution_count": 24
}
],
"source": [
"rv = enrich_poly_vals(pv_db)\n",
"rv.shape"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"rv_si = rv[rv['product']=='SI']\n",
"rv_si = rv_si[rv_si['ticks']>=3]\n",
"rv_si.to_csv('/home/taha/sparkdata/comex/feb/out/2021-02-24.pv.csv')"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(1721, 49)"
]
},
"metadata": {},
"execution_count": 37
}
],
"source": [
"rv_si.shape"
]
},
{
@@ -127,55 +182,6 @@
" print(output_fl)\n",
" print('Done processing dbs in ' + dir_name)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"pv_enr = enrich_poly_vals(pv_db)\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"status IN\n",
"product GC\n",
"vid 33232\n",
"pid 3328\n",
"symbol +(GCM1-GCZ1) +GCZ1 -GCM1\n",
" ... \n",
"n_packets 1\n",
"send_time_delta 0\n",
"t_opp_first_send -151191\n",
"t_opp_last_send -178729\n",
"opp_p_id 748\n",
"Name: 33304, Length: 62, dtype: object"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pv_enr_gc = pv_enr[pv_enr['product']=='GC']\n",
"pv_enr_gc.shape\n",
"pv_enr_gc.iloc[20]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
@@ -194,9 +200,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
"version": "3.8.5-final"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
}