-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
208 lines (195 loc) · 8.88 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import json
import sqlite3
from pathlib import Path
def main():
db = sqlite3.connect(Path(__file__).parent / 'app.db')
db.execute('drop table if exists orders')
db.execute("""
create table orders (
exchange text,
symbol text,
side text,
price real,
account text,
size real,
id text,
primary key (exchange, symbol, side, id)
) without rowid, strict;
""")
db.execute('drop table if exists quotes')
db.execute("""
create table quotes (
exchange text,
symbol text,
size real,
mid_price real,
weighted_average_buy_price real,
weighted_average_sell_price real,
timestamp text,
primary key (exchange, symbol, timestamp, size)
) without rowid, strict
""")
for exchange, symbol, is_snapshot, orders, timestamp, is_target in db.execute("""
with
instructions as (
select
exchange,
symbol,
content->>'type' = 'l3snapshot' as is_snapshot,
json_group_array(
json_object(
'side', case when value->>'side' = 'buy' then 'bids' when value->>'side' = 'sell' then 'asks' end,
'price', coalesce(value->>'price', 0),
'size', coalesce(value->>'size', 0),
'account', value->>'account',
'id', value->>'orderId'
)
) as orders,
content->>'timestamp' as timestamp,
coalesce(strftime('%Y-%m-%dT%H:%M:00.000Z', content->>'timestamp') != lag(strftime('%Y-%m-%dT%H:%M:00.000Z', content->>'timestamp')) over (partition by exchange, symbol order by content->>'timestamp'), true) as is_target
from messages, json_each(
case
when content->>'type' = 'l3snapshot' then
(
select
json_group_array(json(value)) as value
from (
select value from json_each(content->'asks')
union all
select value from json_each(content->'bids')
)
)
else json_array(json(content))
end
)
where exchange = 'Mango Markets'
and symbol = 'SOL/USDC'
and content->>'type' in ('l3snapshot', 'open', 'done')
group by exchange, symbol, content->>'timestamp'
order by exchange, symbol, content->>'timestamp'
)
select
exchange,
symbol,
is_snapshot,
(
with
scratch as (
select value from json_each(orders) order by value->>'price' desc
),
split as (
select
value->>'side' as side,
json_group_array(json_remove(value, '$.side')) as orders
from scratch group by side
)
select json_group_object(side, json(orders)) from split
) as orders,
timestamp,
is_target
from instructions;
"""):
if is_snapshot:
db.execute('delete from orders where exchange = ? and symbol = ?', [exchange, symbol])
for side in ['bids', 'asks']:
for order in json.loads(orders).get(side) or []:
if order['price'] == 0:
db.execute('delete from orders where exchange = ? and symbol = ? and id = ?', [exchange, symbol, order['id']])
else:
db.execute(
'insert or replace into orders values (?, ?, ?, ?, ?, ?, ?)',
[exchange, symbol, side, order['price'], order['account'], order['size'], order['id']]
)
else:
if not is_target:
continue
for size in [1000, 10000, 25000, 50000, 100000]:
db.execute("""
insert into quotes
with
orders as (
select
exchange,
symbol,
side,
price,
size,
price * size as volume,
sum(price * size) over (partition by exchange, symbol, side order by case when side = 'bids' then - price when side = 'asks' then price end) as cumulative_volume
from (
select
exchange,
symbol,
side,
price,
sum(size) as size
from main.orders
group by exchange, symbol, side, price
)
order by exchange, symbol, side, case when side = 'bids' then - price when side = 'asks' then price end
),
fills as (
select
exchange,
symbol,
side,
price,
fill,
sum(fill) over (
partition by side order by case when side = 'bids' then - price when side = 'asks' then price end
) as cumulative_fill
from (
select
exchange,
symbol,
side,
price,
case
when cumulative_volume < :size then volume
else coalesce(lag(remainder) over (partition by exchange, symbol, side), case when volume < :size then volume else :size end)
end as fill
from (select *, :size - cumulative_volume as remainder from orders)
)
where fill > 0
),
weighted_average_fill_prices as (
select
exchange,
symbol,
:size as size,
case when sum(case when side = 'asks' then fill end) = :size then sum(case when side = 'asks' then price * fill end) / :size end as weighted_average_buy_price,
case when sum(case when side = 'bids' then fill end) = :size then sum(case when side = 'bids' then price * fill end) / :size end as weighted_average_sell_price,
:timestamp as timestamp
from fills
group by exchange, symbol, timestamp, size
),
misc as (
select
exchange,
symbol,
(top_bid + top_ask) / 2 as mid_price
from (
select
exchange,
symbol,
max(price) filter ( where side = 'bids') as top_bid,
min(price) filter ( where side = 'asks') as top_ask
from orders
group by exchange, symbol
)
)
select
exchange,
symbol,
size,
mid_price,
weighted_average_buy_price,
weighted_average_sell_price,
timestamp
from weighted_average_fill_prices
inner join misc using (exchange, symbol);
""", {'size': size, 'timestamp': timestamp})
print(timestamp)
db.commit()
if __name__ == '__main__':
main()