Merge branch 'spartan_tas' of bitbucket.org:bluetradesys/spark into spartan_tas

This commit is contained in:
Pedro Pinto committed 2021-01-13 09:47:34 -05:00
commit ac5d961a70
1 file changed
+96 -30
@@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 694,
"execution_count": 157,
"metadata": {},
"outputs": [],
"source": [
@@ -13,7 +13,21 @@
},
{
"cell_type": "code",
"execution_count": 695,
"execution_count": 158,
"metadata": {},
"outputs": [],
"source": [
"#def get_enriched_poly_vals(inp_df, edge_map, full_file_name, summ_file_name):\n",
"# poly_vals = enrich_poly_vals(inp_df, edge_map)\n",
"# poly_vals_summ = tabularize_opp_summ(summarize_opportunities(poly_vals)) \n",
"# poly_vals.to_csv(full_file_name)\n",
"# poly_vals_summ.to_csv(summ_file_name)\n",
"# return [poly_vals,poly_vals_summ]"
]
},
{
"cell_type": "code",
"execution_count": 159,
"metadata": {},
"outputs": [],
"source": [
@@ -42,7 +56,7 @@
},
{
"cell_type": "code",
"execution_count": 696,
"execution_count": 160,
"metadata": {},
"outputs": [],
"source": [
@@ -59,12 +73,12 @@
" return [m_max_row['merged_value'], m_max_row['merged_qty'], m_max_row['ticks'],\n",
" m_max_row['direct_value'] == m_max_row['merged_value'],\n",
" m_max_row['poly_bid_bk'], m_max_row['poly_ask_bk'],\n",
" m_max_row['book_1_bid_bk'] + ';' + m_max_row['book_1_ask_bk'] + ' ' +\n",
" m_max_row['book_2_bid_bk'] + ';' + m_max_row['book_2_ask_bk'] + ' ' +\n",
" m_max_row['book_3_bid_bk'] + ';' + m_max_row['book_3_ask_bk'] + ' ' +\n",
" m_max_row['book_1_bid_bk'] + ';' + m_max_row['book_1_ask_bk'] + ' : ' +\n",
" m_max_row['book_2_bid_bk'] + ';' + m_max_row['book_2_ask_bk'] + ' : ' +\n",
" m_max_row['book_3_bid_bk'] + ';' + m_max_row['book_3_ask_bk'] + ' : ' +\n",
" m_max_row['book_4_bid_bk'] + ';' + m_max_row['book_4_ask_bk']]\n",
" def summarize_single_opp_p_pid(opp_p_rows):\n",
" opp_idx = 0\n",
" opp_idx = 1\n",
" # summary cols\n",
" poly = opp_p_rows['symbol'].iloc[opp_idx]\n",
" opp_id = opp_p_rows['opp_id'].iloc[opp_idx]\n",
@@ -86,7 +100,7 @@
},
{
"cell_type": "code",
"execution_count": 697,
"execution_count": 161,
"metadata": {},
"outputs": [],
"source": [
@@ -107,12 +121,12 @@
},
{
"cell_type": "code",
"execution_count": 698,
"execution_count": 162,
"metadata": {},
"outputs": [],
"source": [
"# adds a new col called opp_p_id which is simply\n",
"# a unique id for [opp_id,pid]\n",
"# a unique id for every [opp_id,pid]\n",
"def add_opp_p_ids(poly_vals):\n",
" key_map = {'':0}\n",
" def get_opp_p_id(r):\n",
@@ -126,23 +140,32 @@
},
{
"cell_type": "code",
"execution_count": 699,
"execution_count": 163,
"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",
" val_to_tick_map = {'GC':10, 'SI':5, 'HG':5}\n",
" tick_to_dollar_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",
" return r['merged_value']/tick_to_dollar_map[r['product']]\n",
" def get_cash_value(r, is_merged):\n",
" col_name = \"merged_value\" if is_merged else \"direct_value\"\n",
" norm_value = r[col_name]\n",
" if r['side'] == 'Buy':\n",
" norm_value = -1*norm_value\n",
" return (norm_value/val_to_tick_map[r['product']]) * tick_to_dollar_map[r['product']] \n",
" poly_vals['merged_value'] = poly_vals.apply (lambda row: get_cash_value(row, True), axis=1)\n",
" 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"
]
},
{
"cell_type": "code",
"execution_count": 700,
"execution_count": 164,
"metadata": {},
"outputs": [],
"source": [
@@ -151,9 +174,8 @@
"def add_opp_start_times(poly_vals):\n",
" # this is the groupby method that acts on a single opp\n",
" def analyze_single_opp(single_opp_pvs):\n",
" sz = len(single_opp_pvs['vid'])\n",
" opp_first_p_st = single_opp_pvs['first_s_time'].iloc[0]\n",
" opp_last_p_st = single_opp_pvs['last_s_time'].iloc[0] \n",
" opp_first_p_st = single_opp_pvs['first_s_time'].iloc[1]\n",
" opp_last_p_st = single_opp_pvs['last_s_time'].iloc[1]\n",
" return [opp_first_p_st,opp_last_p_st]\n",
" rv = poly_vals.groupby('opp_id').apply(lambda x: analyze_single_opp(x))\n",
" st_df = rv.to_frame().reset_index()\n",
@@ -167,7 +189,7 @@
},
{
"cell_type": "code",
"execution_count": 701,
"execution_count": 165,
"metadata": {},
"outputs": [],
"source": [
@@ -184,7 +206,7 @@
},
{
"cell_type": "code",
"execution_count": 702,
"execution_count": 166,
"metadata": {},
"outputs": [],
"source": [
@@ -203,7 +225,7 @@
" # 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",
" rv=poly_vals[['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",
@@ -221,14 +243,14 @@
},
{
"cell_type": "code",
"execution_count": 703,
"execution_count": 167,
"metadata": {},
"outputs": [],
"source": [
"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, edge_map) \n",
" poly_vals = enrich_poly_vals(poly_vals_db, edge_map)\n",
" poly_vals_summ = tabularize_opp_summ(summarize_opportunities(poly_vals))\n",
" appended_products = ''\n",
" for p in products:\n",
@@ -242,32 +264,76 @@
},
{
"cell_type": "code",
"execution_count": 717,
"execution_count": 168,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"select * from polygon_value_events where (product='GC' or product='HG' or product='SI')\n"
"select * from polygon_value_events where (product='xxxGC' or product='HG' or product='xxxSI')\n"
]
},
}
],
"source": [
"g_pvs=get_enriched_poly_vals('/home/taha/sparkdata/simulations/comex/2020-12-01_360_24h.sqlite',\n",
" {'xxxGC':1, 'HG':-1000000000000, 'xxxSI':3})\n",
"#g_r = pd.read_csv('/home/taha/sparkdata/simulations/comex/Output/2020-12-01-HG_new.csv')\n",
"#g_r = enrich_poly_vals(g_r,{'HG':-1000000000000})\n",
"#g_r.to_csv('/home/taha/sparkdata/simulations/comex/Output/2020-12-01-HG_new_out.csv')\n",
"#g_r.columns\n",
"#g_r_summ = summarize_opportunities(g_r)\n",
"#g_r_summ.to_csv('/home/taha/sparkdata/simulations/comex/Output/2020-12-01-HG_new_summ_out.csv')"
]
},
{
"cell_type": "code",
"execution_count": 110,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"select * from polygon_value_events where (product='HG')\n"
]
}
],
"source": [
"g_pvs=get_enriched_poly_vals('/home/taha/sparkdata/simulations/comex/2020-12-01_360_24h.sqlite',\n",
" {'HG':-100000000000000})"
]
},
{
"cell_type": "code",
"execution_count": 156,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(394, 15)"
"0 0.000000e+00\n",
"1 1.250000e+01\n",
"2 1.250000e+01\n",
"3 1.250000e+01\n",
"4 1.250000e+01\n",
" ... \n",
"62 -2.305842e+10\n",
"55 -2.305842e+10\n",
"56 1.250000e+01\n",
"59 1.250000e+01\n",
"61 -2.305842e+10\n",
"Name: merged_value, Length: 63, dtype: float64"
]
},
"execution_count": 717,
"execution_count": 156,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g_pvs=get_enriched_poly_vals('/home/taha/sparkdata/simulations/comex/2020-08-27_360_24h.sqlite',\n",
" {'GC':1, 'HG':1, 'SI':3})\n",
"g_pvs[0].shape\n",
"g_pvs[1].shape"
"g_pvs[0].merged_value"
]
},
{