From a2332ea54eee9ad61a64dbb1dd62f423f0a10d2d Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Sun, 23 May 2021 18:13:59 -0400 Subject: [PATCH] one row per opp in a usable state now --- ...opportunity_analysis_summary_working.ipynb | 255 +++--------------- 1 file changed, 37 insertions(+), 218 deletions(-) diff --git a/src/server/sparkd/cmds/spartan/analysis/cleanedup/sniper_opportunity_analysis_summary_working.ipynb b/src/server/sparkd/cmds/spartan/analysis/cleanedup/sniper_opportunity_analysis_summary_working.ipynb index 9f2cac708..3c26fca29 100644 --- a/src/server/sparkd/cmds/spartan/analysis/cleanedup/sniper_opportunity_analysis_summary_working.ipynb +++ b/src/server/sparkd/cmds/spartan/analysis/cleanedup/sniper_opportunity_analysis_summary_working.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 8, + "execution_count": 42, "metadata": { "scrolled": true }, @@ -10,12 +10,13 @@ "source": [ "import pandas as pd\n", "import sqlite3\n", - "import sniper_utils as su" + "import sniper_utils as su\n", + "from datetime import datetime" ] }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 43, "metadata": { "scrolled": true }, @@ -46,7 +47,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 44, "metadata": { "scrolled": true }, @@ -62,7 +63,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 45, "metadata": { "scrolled": true }, @@ -84,7 +85,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 46, "metadata": { "scrolled": true }, @@ -108,7 +109,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 47, "metadata": { "scrolled": true }, @@ -126,7 +127,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 62, "metadata": {}, "outputs": [], "source": [ @@ -137,6 +138,7 @@ " def __init__(self,leg_str):\n", " self.is_future = leg_str[1] != '('\n", " self.leg_qty = 1\n", + " self.leg_str = leg_str\n", " if(self.leg_str[1] == '-'):\n", " self.leg_qty = -1\n", " def n_contracts(self):\n", @@ -166,7 +168,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 49, "metadata": { "scrolled": true }, @@ -178,7 +180,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 50, "metadata": { "scrolled": true }, @@ -191,42 +193,14 @@ }, { "cell_type": "code", - "execution_count": 37, - "metadata": { - "scrolled": true - }, - "outputs": [], - "source": [ - "# gets the row within an opp,pid with the highest edge (edge*qty)\n", - "#def get_max_value_row (opp_p_rows_a):\n", - "# min_react_time = 30000\n", - "# opp_p_rows_a = opp_p_rows_a[opp_p_rows_a['status']=='IN']\n", - "# #opp_p_rows_a = opp_p_rows_a[opp_p_rows_a['t_opp_first_send'] >= min_react_time]\n", - "# opp_p_rows = opp_p_rows_a.copy()\n", - "# if (opp_p_rows.shape[0] == 0):\n", - "# return [0.0,0,0,False,'','','','']\n", - "# else:\n", - "# opp_p_rows['tot_merged_value'] = opp_p_rows['merged_value'] * opp_p_rows['merged_qty']\n", - "# m_idx = opp_p_rows['tot_merged_value'].argmax()\n", - "# m_max_row = opp_p_rows.iloc[m_idx]\n", - "# return [m_max_row['merged_value'], \n", - "# m_max_row['merged_qty'], \n", - "# m_max_row['ticks'],\n", - "# m_max_row['direct_value'] == m_max_row['merged_value'],\n", - "# get_leg_books(m_max_row),\n", - "# get_poly_book(m_max_row)]" - ] - }, - { - "cell_type": "code", - "execution_count": 28, + "execution_count": 51, "metadata": { "scrolled": true }, "outputs": [], "source": [ "# returns the row within an opp,pid with the highest edge (edge*qty)\n", - "def get_max_edge_row (opp_p_rows_a, latency_ns = 35000):\n", + "def get_max_edge_row (opp_p_rows_a, latency_ns = 55000):\n", " opp_p_rows = opp_p_rows_a\n", " opp_p_rows = opp_p_rows[opp_p_rows['status']=='IN']\n", " opp_p_rows = opp_p_rows[opp_p_rows['t_opp_first_send'] >= latency_ns]\n", @@ -240,7 +214,7 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 52, "metadata": { "scrolled": true }, @@ -270,7 +244,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 53, "metadata": { "scrolled": true }, @@ -303,7 +277,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 54, "metadata": { "scrolled": true }, @@ -334,7 +308,7 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 55, "metadata": { "scrolled": false }, @@ -379,12 +353,12 @@ }, { "cell_type": "code", - "execution_count": 67, + "execution_count": 56, "metadata": {}, "outputs": [], "source": [ "def add_date(opp_df):\n", - " ts = g_r_summ['start_t_time'][0]\n", + " ts = opp_df['start_t_time'][0]\n", " ts = int(((ts/1000)/1000)/1000)\n", " ts_str = datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d')\n", " opp_df.insert(0,'date',ts_str)" @@ -392,29 +366,7 @@ }, { "cell_type": "code", - "execution_count": 68, - "metadata": { - "scrolled": true - }, - "outputs": [ - { - "output_type": "stream", - "name": "stderr", - "text": [ - "/home/taha/.local/lib/python3.8/site-packages/IPython/core/interactiveshell.py:3165: DtypeWarning: Columns (46,47,48) have mixed types.Specify dtype option on import or set low_memory=False.\n has_raised = await self.run_ast_nodes(code_ast.body, cell_name,\n" - ] - } - ], - "source": [ - "g_file = '/home/taha/spartan_store/pcaps/360/2020-08-27_360_24h.poly_vals.csv'\n", - "g_r = pd.read_csv(g_file)\n", - "g_r_summ = summarize_opportunities(g_r)\n", - "g_r_summ = tabularize_opp_summ(g_r_summ)\n" - ] - }, - { - "cell_type": "code", - "execution_count": 72, + "execution_count": 57, "metadata": { "scrolled": true }, @@ -443,14 +395,15 @@ " this_summ = get_pv_summary(full_fl)\n", " all_summ_dfs.append(this_summ)\n", " appened_df = pd.concat(all_summ_dfs)\n", - " appened_df.to_csv(dir_name + output_file, index=False)\n", + " appened_sorted_df = appened_df.sort_values(by = ['start_t_time'], ascending=True)\n", + " appened_sorted_df.to_csv(dir_name + output_file, index=False)\n", " print('processed ' + str(len(all_summ_dfs)) + ' days')\n", " return appened_df" ] }, { "cell_type": "code", - "execution_count": 73, + "execution_count": 58, "metadata": { "scrolled": true }, @@ -459,54 +412,24 @@ "output_type": "stream", "name": "stdout", "text": [ - "Processing /home/taha/spartan_store/pcaps/360/2020-08-31_360_24h.poly_vals.csv\n", - "/home/taha/.local/lib/python3.8/site-packages/IPython/core/interactiveshell.py:3437: DtypeWarning: Columns (46,47,48) have mixed types.Specify dtype option on import or set low_memory=False.\n", + "Processing /home/taha/spartan_store/pcaps/360/temp/2020-08-30_360_24h.poly_vals.csv\n", + "Processing /home/taha/spartan_store/pcaps/360/temp/2020-08-27_360_24h.poly_vals.csv\n", + "/home/taha/.local/lib/python3.8/site-packages/IPython/core/interactiveshell.py:3427: DtypeWarning: Columns (46,47,48) have mixed types.Specify dtype option on import or set low_memory=False.\n", " exec(code_obj, self.user_global_ns, self.user_ns)\n", - "Processing /home/taha/spartan_store/pcaps/360/2020-08-30_360_24h.poly_vals.csv\n", - "Processing /home/taha/spartan_store/pcaps/360/2020-08-27_360_24h.poly_vals.csv\n", - "/home/taha/.local/lib/python3.8/site-packages/IPython/core/interactiveshell.py:3437: DtypeWarning: Columns (46,47,48) have mixed types.Specify dtype option on import or set low_memory=False.\n", + "Processing /home/taha/spartan_store/pcaps/360/temp/2020-08-31_360_24h.poly_vals.csv\n", + "/home/taha/.local/lib/python3.8/site-packages/IPython/core/interactiveshell.py:3427: DtypeWarning: Columns (46,47,48) have mixed types.Specify dtype option on import or set low_memory=False.\n", " exec(code_obj, self.user_global_ns, self.user_ns)\n", "processed 3 days\n" ] } ], "source": [ - "g_res = process_poly_vals('/home/taha/spartan_store/pcaps/360/','.poly_vals.csv','poly_vals_summ.csv')" + "g_res = process_poly_vals('/home/taha/spartan_store/pcaps/360/temp/','.poly_vals.csv','poly_vals_summ.csv')" ] }, { "cell_type": "code", - "execution_count": 22, - "metadata": {}, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "(31369, 23)" - ] - }, - "metadata": {}, - "execution_count": 22 - } - ], - "source": [ - "g_res.shape" - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": {}, - "outputs": [], - "source": [ - "g_res = g_res.sort_values(['start_t_time'])\n", - "g_res.to_csv('~/spartan_store/pcaps/310/poily_vals_summ.csv')" - ] - }, - { - "cell_type": "code", - "execution_count": 808, + "execution_count": 69, "metadata": {}, "outputs": [], "source": [ @@ -534,33 +457,21 @@ " summ_rows = summ_rows_arg.copy()\n", " summ_rows['pnl'] = summ_rows.apply (lambda r: compute_pnl_row(r), axis=1)\n", " summ_rows = summ_rows[summ_rows['pnl'] > 0]\n", - " summ_rows['key'] = summ_rows['file'] + '-' + summ_rows['opp_id'].astype(str) \n", + " summ_rows['key'] = summ_rows['date'] + '-' + summ_rows['opp_id'].astype(str) \n", " return summ_rows.groupby('key').apply(lambda x: agg(x))\n", " return summ_rows" ] }, { "cell_type": "code", - "execution_count": 809, + "execution_count": 72, "metadata": {}, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ - " fee lat lat_type pnl\n", - "0 0.60 50,000.00 fs_win 13,085.80\n", - "1 0.60 50,000.00 ls_win 12,338.10\n", - "2 0.60 100,000.00 fs_win 11,953.30\n", - "3 0.60 100,000.00 ls_win 11,537.40\n", - "4 0.60 150,000.00 fs_win 11,549.30\n", - "5 0.60 150,000.00 ls_win 11,346.80\n", - "6 1.50 50,000.00 fs_win 7,753.50\n", - "7 1.50 50,000.00 ls_win 7,199.00\n", - "8 1.50 100,000.00 fs_win 7,105.50\n", - "9 1.50 100,000.00 ls_win 6,825.00\n", - "10 1.50 150,000.00 fs_win 6,834.50\n", - "11 1.50 150,000.00 ls_win 6,755.00\n" + " fee lat lat_type pnl\n0 0.60 50,000.00 fs_win 1,907.30\n1 0.60 50,000.00 ls_win 1,465.50\n2 0.60 100,000.00 fs_win 1,456.50\n3 0.60 100,000.00 ls_win 1,324.70\n4 0.60 150,000.00 fs_win 1,322.50\n5 0.60 150,000.00 ls_win 1,233.20\n6 1.50 50,000.00 fs_win 1,174.50\n7 1.50 50,000.00 ls_win 924.50\n8 1.50 100,000.00 fs_win 957.50\n9 1.50 100,000.00 ls_win 865.00\n10 1.50 150,000.00 fs_win 869.50\n11 1.50 150,000.00 ls_win 819.50\n" ] } ], @@ -585,17 +496,7 @@ }, { "cell_type": "code", - "execution_count": 810, - "metadata": {}, - "outputs": [], - "source": [ - "g_res_direct = g_res[g_res['is_direct']==True]\n", - "compute_pnl(g_res_direct,1.5,100000,'ls_win').to_csv('/home/taha/sparkdata/simulations/comex/nov/output/pnl.csv')" - ] - }, - { - "cell_type": "code", - "execution_count": 782, + "execution_count": 71, "metadata": { "scrolled": true }, @@ -609,88 +510,6 @@ " print(df)" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "scrolled": true - }, - "outputs": [], - "source": [ - "#gc = g_res[g_res['prod']=='GC']\n", - "#gc_good = gc[gc['fs_win']>=50000]\n", - "#gc_bad = gc[gc['fs_win']<50000]\n", - "\n", - "#gc_bad = gc_bad[['opp_id', 'symbol','merged_qty', 'ticks', 'ls_win', 'is_direct']]\n", - "#gc_good = gc_good[['opp_id', 'symbol','merged_qty', 'ticks', 'ls_win', 'is_direct']]\n", - "\n", - "my_print(gc_bad)\n" - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [ - { - "output_type": "error", - "ename": "NameError", - "evalue": "name 'su' is not defined", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mpv_db_g\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msu\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_poly_vals_db\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'/home/taha/sparkdata/310/2021-04-28_310_24h.sqlite'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'ES'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mNameError\u001b[0m: name 'su' is not defined" - ] - } - ], - "source": [ - "pv_db_g = su.get_poly_vals_db('/home/taha/sparkdata/310/2021-04-28_310_24h.sqlite',['ES'])\n" - ] - }, - { - "cell_type": "code", - "execution_count": 43, - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "2020-08-27\n" - ] - } - ], - "source": [ - "from datetime import datetime\n", - "\n", - "#ts=int(598566184189943329)\n", - "ts=int(1598566184)\n", - "# if you encounter a \"year is out of range\" error the timestamp\n", - "# may be in milliseconds, try `ts /= 1000` in that case\n", - "print(datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d'))" - ] - }, - { - "cell_type": "code", - "execution_count": 60, - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "2020-08-27\n" - ] - } - ], - "source": [ - "ts = g_r_summ['start_t_time'][0]\n", - "ts = int(((ts/1000)/1000)/1000)\n", - "ts_str = datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d')\n", - "print(ts_str)" - ] - }, { "cell_type": "code", "execution_count": null, @@ -715,7 +534,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.6" + "version": "3.8.5" } }, "nbformat": 4,