Naming polygons in the SQL DB.

This commit is contained in:
Pedro Pinto committed 2020-10-12 10:13:45 -04:00
1 parent cb81b0e53e
commit 89ef56f131
2 files changed
+51 -23

No files matched your search

+40 -17
View File
@@ -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;
@@ -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));