Added views to md.sql, documented better.
This commit is contained in:
1 parent
01ce2211f3
commit
e889d0835f
1 file changed
+46
-8
@@ -1,3 +1,6 @@
|
||||
/* BASE Tables -------------------------------------------------------------------------------- */
|
||||
|
||||
/* An exchange transaction/event */
|
||||
drop table if exists events;
|
||||
CREATE TABLE events(
|
||||
h_t_time TEXT, /* human readable t_time : 2020-08-24.14:49:29.146533601*/
|
||||
@@ -9,6 +12,7 @@ CREATE TABLE events(
|
||||
last_st numeric /* send time of the last packet*/
|
||||
);
|
||||
|
||||
/* The parts that describe an exchange transaction/event */
|
||||
drop table if exists event_items;
|
||||
CREATE TABLE event_items(
|
||||
eid numeric, /* id of the event (above) that this item belongs to (event_items:event is a n:1 relationship)*/
|
||||
@@ -32,6 +36,7 @@ CREATE TABLE event_items(
|
||||
agg TEXT /* for type=fill, indicates the cross/passive pairing (yes_buy,yes_sell for agg, no for passive)*/
|
||||
);
|
||||
|
||||
/* The (changed) books for an instrument after a new event/transaction */
|
||||
drop table if exists books;
|
||||
CREATE TABLE books(
|
||||
eid numeric,
|
||||
@@ -39,7 +44,6 @@ CREATE TABLE books(
|
||||
sid numeric,
|
||||
status TEXT,
|
||||
impl numeric,
|
||||
|
||||
/* regular books */
|
||||
bid_p_0 numeric, bid_q_0 numeric,
|
||||
bid_p_1 numeric, bid_q_1 numeric,
|
||||
@@ -61,14 +65,14 @@ CREATE TABLE books(
|
||||
ask_p_7 numeric, ask_q_7 numeric,
|
||||
ask_p_8 numeric, ask_q_8 numeric,
|
||||
ask_p_9 numeric, ask_q_9 numeric,
|
||||
|
||||
/* impl books */
|
||||
/* impliled books */
|
||||
ibid_p_0 numeric, ibid_q_0 numeric,
|
||||
ibid_p_1 numeric, ibid_q_1 numeric,
|
||||
iask_p_0 numeric, iask_q_0 numeric,
|
||||
iask_p_1 numeric, iask_q_1 numeric
|
||||
);
|
||||
|
||||
/* All the orders we were able to identify as icebergs */
|
||||
drop table if exists icebergs;
|
||||
CREATE TABLE icebergs(
|
||||
ok numeric,
|
||||
@@ -76,6 +80,7 @@ CREATE TABLE icebergs(
|
||||
filled numeri c
|
||||
);
|
||||
|
||||
/* All the securities*/
|
||||
drop table if exists securities;
|
||||
CREATE TABLE securities(
|
||||
sid numeric,
|
||||
@@ -91,14 +96,18 @@ CREATE TABLE securities(
|
||||
update_time numeric
|
||||
);
|
||||
|
||||
/* The leg constituents for spread securities */
|
||||
drop table if exists spread_legs;
|
||||
CREATE TABLE spread_legs(
|
||||
ssid numeric, /* spread security id */
|
||||
lsid numeric, /* leg security id*/
|
||||
side TEXT, /* leg side */
|
||||
ratio numeric /* leg ration */
|
||||
ratio numeric /* leg ratio */
|
||||
);
|
||||
|
||||
/* Usefull Views -------------------------------------------------------------------------------- */
|
||||
|
||||
/* a simpler view over event_items */
|
||||
drop view if exists items;
|
||||
create view items as
|
||||
select
|
||||
@@ -110,11 +119,25 @@ where
|
||||
event_items.sid = securities.sid and
|
||||
event_items.eid=events.eid;
|
||||
|
||||
/* Views usefull for determining which securities actually trade */
|
||||
|
||||
/* Per security fill count */
|
||||
drop view if exists fill_counts;
|
||||
create view fill_counts as
|
||||
select symbol,sid, n_fills
|
||||
from (select symbol,sid,count(*) n_fills
|
||||
from event_items
|
||||
where type='fill'
|
||||
group by symbol,sid) secs_with_fills
|
||||
|
||||
/* Spread securities and their legs */
|
||||
drop view if exists spreads_with_legs;
|
||||
create view spreads_with_legs as
|
||||
select
|
||||
spread.symbol spread,
|
||||
leg.symbol leg,
|
||||
spread.symbol spread_symbol,
|
||||
spread.sid ssid,
|
||||
leg.symbol leg_symbol,
|
||||
leg.sid lsid,
|
||||
spread_legs.ratio ratio,
|
||||
spread_legs.side side
|
||||
from
|
||||
@@ -125,16 +148,28 @@ where
|
||||
spread.sid = spread_legs.ssid and
|
||||
spread_legs.lsid = leg.sid;
|
||||
|
||||
/* Securities that traded at least 10 times */
|
||||
drop view if exists active_securities;
|
||||
create view active_securities as
|
||||
select sid from fill_counts where n_fills >10;
|
||||
|
||||
/* Spreads that traded */
|
||||
drop view if exists active_spreads;
|
||||
create view active_spreads as
|
||||
select sid from securities where securities.n_legs>0 and sid in (select * from active_securities);
|
||||
|
||||
/* Spread legs for spreads that traded */
|
||||
drop view if exists active_spread_legs;
|
||||
create view active_spread_legs as
|
||||
select distinct lsid as sid from spread_legs where ssid in (select * from active_spreads);
|
||||
|
||||
/* Securities that trade and legs of securities that trade */
|
||||
drop view if exists active_securities_closure;
|
||||
create view active_securities_closure as
|
||||
select distinct sid from
|
||||
(select sid from active_spread_legs union select sid from active_securities);
|
||||
|
||||
|
||||
/* Securities that trade paired with their legs */
|
||||
drop view if exists active_securities_closure_with_legs;
|
||||
create view active_securities_closure_with_legs as
|
||||
select
|
||||
@@ -149,6 +184,7 @@ from
|
||||
active_securities_closure left join spread_legs on
|
||||
active_securities_closure.sid = spread_legs.ssid;
|
||||
|
||||
/* A nicer view of securities that trade */
|
||||
drop view if exists pretty_active_securities ;
|
||||
create view pretty_active_securities as
|
||||
select
|
||||
@@ -170,7 +206,7 @@ from
|
||||
(select * from pretty_active_securities where side=-1) sell
|
||||
on buy.sid = sell.sid;
|
||||
|
||||
|
||||
/* Smash legs into the same row for 2 legged contracts */
|
||||
drop view if exists pretty_triplets;
|
||||
create view pretty_triplets as
|
||||
select
|
||||
@@ -185,6 +221,8 @@ from
|
||||
order by
|
||||
symbol;
|
||||
|
||||
/* Import derectives to bootstrap the DB from CSVs */
|
||||
|
||||
.mode csv
|
||||
.import event_items.csv event_items
|
||||
.import events.csv events
|
||||
|
||||
Reference in new issue
Block a user