From 89ef56f13142eef51cab88df00283dc3ceaf5fd0 Mon Sep 17 00:00:00 2001 From: Pedro Pinto Date: Mon, 12 Oct 2020 10:13:45 -0400 Subject: [PATCH] Naming polygons in the SQL DB. --- src/data_files/files/spartan/md.sql | 57 +++++++++++++------ .../cmds/spartan/md/mdp3/utils/mdp3_utils.cpp | 17 ++++-- 2 files changed, 51 insertions(+), 23 deletions(-) diff --git a/src/data_files/files/spartan/md.sql b/src/data_files/files/spartan/md.sql index 42514b5c9..723d677b2 100644 --- a/src/data_files/files/spartan/md.sql +++ b/src/data_files/files/spartan/md.sql @@ -108,7 +108,13 @@ CREATE TABLE spread_legs( ratio numeric /* leg ratio */ ); -/* Polygons, that is combinations of contracts that net out to 0 */ +/* Polygons, that is combinations of contracts that net out to 0 */ +drop table if exists polygons; +CREATE TABLE polygons( + pid numeric, /* polygon id */ + symbol text /* descriptor */ +); + drop table if exists polygon_legs; CREATE TABLE polygon_legs( pid numeric, /* polygon id */ @@ -119,6 +125,38 @@ CREATE TABLE polygon_legs( /* Usefull Views -------------------------------------------------------------------------------- */ +/* Stats on the time between events */ +drop view if exists symbol_event_deltas; +create view symbol_event_deltas as +with + paired_items as ( + select + t_time as t, + symbol, + lag(t_time) over(partition by symbol order by t_time) as p_t + from + event_items + where + t <>0 and status='ReadyToTrade') +select + symbol, + t-p_t as delta +from + paired_items; + +drop view if exists median_symbol_event_deltas; +create view median_symbol_event_deltas as +select + symbol, + median(delta)/1000 delta_us, + count(*) n_events +from + symbol_event_deltas +group by + symbol +order by + n_events; + /* a simpler view over event_items */ drop view if exists items; create view items as @@ -151,20 +189,5 @@ where order by spread_symbol; -/* Polygons and their legs */ -drop view if exists polygons; -create view polygons as -select - pid, - leg.symbol leg_symbol, - leg.sid lsid, - polygon_legs.ratio ratio, - polygon_legs.side side -from - securities leg, - polygon_legs -where - polygon_legs.lsid = leg.sid -order by - pid; + diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/utils/mdp3_utils.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/utils/mdp3_utils.cpp index 647b7ae29..0105b768f 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/utils/mdp3_utils.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/utils/mdp3_utils.cpp @@ -704,14 +704,18 @@ int to_csv(std::string const &file) { icebergs << i.first.value() << "," << i.second.estimated_quantity.value() << "," << i.second.filled << std::endl; - auto polys = std::ofstream(drop_extensions(file) + ".polygon_legs.csv"); - for (auto &[pid, p] : poly_finder.result) + auto polys = std::ofstream(drop_extensions(file) + ".polygons.csv"); + auto poly_legs = std::ofstream(drop_extensions(file) + ".polygon_legs.csv"); + auto imap = def_collector.to_instrument_map(); + for (auto &[pid, p] : poly_finder.result) { + polys << pid << "," << polygon_descriptor(p, imap) << std::endl; for (auto &part : p.parts) { - polys << pid << "," << part.sec_id << "," - << (part.ratio > 0 ? api::Side_Name(api::SIDE_BUY) - : api::Side_Name(api::SIDE_SELL)) - << "," << std::abs(part.ratio) << std::endl; + poly_legs << pid << "," << part.sec_id << "," + << (part.ratio > 0 ? api::Side_Name(api::SIDE_BUY) + : api::Side_Name(api::SIDE_SELL)) + << "," << std::abs(part.ratio) << std::endl; } + } return 0; } @@ -859,6 +863,7 @@ int to_sql(std::string const &pcap) { ".import {0}.books.csv books \n" ".import {0}.icebergs.csv icebergs \n" ".import {0}.spread_legs.csv spread_legs\n" + ".import {0}.polygons.csv polygons\n" ".import {0}.polygon_legs.csv polygon_legs\n", drop_extensions(pcap));