-
Notifications
You must be signed in to change notification settings - Fork 3
/
AggregatedBookAnalytics.py
executable file
·133 lines (122 loc) · 6.13 KB
/
AggregatedBookAnalytics.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
#!/usr/bin/python3
# -*- coding = utf-8 -*-
import json, time, csv, argparse
from AggregatedBook import AggregatedBookType
from prettytable import PrettyTable
class AggregatedBookAnalytics:
def __init__(self, book = None):
self.timestamp = int(time.time())
self.symbol = None
self.type = "AggregatedBookAnalytics"
self.spread = 0
self.book_depth = 0
self.book_imbalance = 0
self.best_bid_price = 0
self.best_ask_price = 0
self.pressure_ask = 0
self.pressure_bid = 0
self.weighted_bid_price = 0
self.weighted_ask_price = 0
self.weighted_mid_price = 0
self.weighted_price = 0
self.middle_price = 0
if book is not None:
self.symbol = book.symbol.upper()
self.calc(book)
def calc(self, book):
self.book_depth = len(book.bid)
volume_bid = 0
volume_ask = 0
total_qty_bid = 0
total_qty_ask = 0
for i in range(self.book_depth):
bid = book.bid[i]
ask = book.ask[i]
volume_bid = volume_bid + bid["Q"] * bid["P"]
volume_ask = volume_ask + ask["Q"] * ask["P"]
total_qty_bid = total_qty_bid + bid["Q"]
total_qty_ask = total_qty_ask + ask["Q"]
self.best_bid_price = book.bid[0]["P"]
self.best_ask_price = book.ask[0]["P"]
#higher liquidity => more shares are offered with less price gaps
self.spread = self.best_ask_price - self.best_bid_price
# Simple average of the best bid and ask
self.middle_price = (self.best_ask_price + self.best_bid_price)/2
volume_total = volume_ask + volume_bid
self.pressure_ask = volume_ask/volume_total
self.pressure_bid = volume_bid/volume_total
# Imbalance is a ratio of limit order volumes between the bid and ask side
self.book_imbalance = (volume_bid - volume_ask)/volume_total
# Weighted price to combine the price aspect and the quantity aspect of the book
self.weighted_bid_price = volume_bid/total_qty_bid
self.weighted_ask_price = volume_ask/total_qty_ask
# Weighted average mid-quote
self.weighted_mid_price = (book.bid[0]["P"]*book.bid[0]["Q"] + book.ask[0]["P"]*book.ask[0]["Q"])/(book.bid[0]["Q"] + book.ask[0]["Q"])
self.weighted_price = (volume_bid + volume_ask)/(total_qty_bid + total_qty_ask)
def print(self):
summary = PrettyTable()
summary.field_names = ["ANALYTIC", "RESULT"]
summary.add_row([ "Bid/Ask Spread $", '{0:.3f}'.format(self.spread)])
summary.add_row([ "Book depth", self.book_depth])
summary.add_row([ "Book Imbalance", '{0:.3f}'.format(self.book_imbalance)])
summary.add_row([ "===", '==='])
summary.add_row([ "Balance Ask %", '{0:.3f}'.format(self.pressure_ask)])
summary.add_row([ "Weighted Ask $", '{0:.3f}'.format(self.weighted_ask_price)])
summary.add_row([ "Best Ask $", self.best_ask_price])
summary.add_row([ "vvv", "vvv"])
summary.add_row([ "Weighted Price $", '{0:.3f}'.format(self.weighted_price)])
summary.add_row([ "Weighted Mid Price $", '{0:.3f}'.format(self.weighted_mid_price)])
summary.add_row([ "Middle Price $", '{0:.3f}'.format(self.middle_price)])
summary.add_row([ "^^^", "^^^"])
summary.add_row([ "Best Bid $", self.best_bid_price])
summary.add_row([ "Weighted Bid $", '{0:.3f}'.format(self.weighted_bid_price)])
summary.add_row([ "Balance Bid %", '{0:.3f}'.format(self.pressure_bid)])
print(summary)
#{'timestamp': 1615212000, 'spread': 0.06999999999999318,
# 'book_depth': 15, 'book_imbalance': -0.13826360774307303,
# 'best_bid_price': 94.53, 'best_ask_price': 94.6,
# 'pressure_ask': 0.5691318038715365, 'pressure_bid': 0.4308681961284635,
# 'weighted_bid_price': 94.44239436619718, 'weighted_ask_price': 94.72882352941177,
# 'weighted_mid_price': 94.54, 'weighted_price': 94.60519756838906,
# 'middle_price': 94.565}
def convert_json_to_csv(self, json_log):
out_file = json_log + ".csv"
with open(out_file, 'w', newline='') as file:
writer = csv.writer(file, delimiter=';')
writer.writerow(['timestamp','symbol','spread',
'book_imbalance','best_bid_p',
'best_ask_p','pressure_ask',
'pressure_bid','wt_bid_p',
'wt_ask_p','wt_mid_p',
'wt_p','mid_p'])
with open(json_log, "r") as values_file:
for line in values_file:
jl = json.loads(line.replace("'", '"'))
if ("type" in jl) and (jl["type"] == "AggregatedBookAnalytics"):
writer.writerow([jl['timestamp'],jl['symbol'].upper(),jl['spread'],
jl['book_imbalance'],jl['best_bid_price'],
jl['best_ask_price'],jl['pressure_ask'],
jl['pressure_bid'],jl['weighted_bid_price'],
jl['weighted_ask_price'],jl['weighted_mid_price'],
jl['weighted_price'],jl['middle_price']])
if __name__ == "__main__":
# Add the arguments
input_args_parser = argparse.ArgumentParser(description='File to be processed')
input_args_parser.add_argument('File',
nargs='?',
metavar='file',
type=str,
help='json log file')
args = input_args_parser.parse_args()
file = args.File
#file = 'example/aggregatedbook.json'
#with open(file, "r") as values_file:
# values_json = json.load(values_file)
#abt = AggregatedBookType(values_json)
#analytics = AggregatedBookAnalytics(abt)
#analytics.print()
#file = 'example/bpac11_bear_market_20210308_sample'
#file = 'bpac11_bear_market_20210308'
#file = 'example/wallet_example'
analytics = AggregatedBookAnalytics()
analytics.convert_json_to_csv(file)