-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
46 lines (40 loc) · 1.36 KB
/
utils.py
File metadata and controls
46 lines (40 loc) · 1.36 KB
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
def cal_avg_price(orderbook, trade_size):
"""
:param orderbook:
:param trade_size:
:return: avg_price 평단가
"""
asks = orderbook['asks']
ask_amount = 0
long_avg_price = 0
trade_size = trade_size
for idx, (price, amount) in enumerate(asks):
ask_amount += amount
if ask_amount >= trade_size:
alloc_size = trade_size - (ask_amount - amount)
if idx == 0:
long_avg_price += price
else:
long_avg_price += (alloc_size / trade_size) * price
break
else:
long_avg_price += (amount / trade_size) * price
if idx == len(asks) - 1 and ask_amount <= trade_size:
long_avg_price = 1e12
bids = orderbook['bids']
bid_amount = 0
short_avg_price = 0
for idx, (price, amount) in enumerate(bids):
bid_amount += amount
if bid_amount >= trade_size:
alloc_size = trade_size - (bid_amount - amount)
if idx == 0:
short_avg_price += price
else:
short_avg_price += (alloc_size / trade_size) * price
break
else:
short_avg_price += (amount / trade_size) * price
if idx == len(bids) - 1 and bid_amount <= trade_size:
short_avg_price = -1e12
return long_avg_price, short_avg_price