-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathdatabaseController.py
More file actions
131 lines (117 loc) · 4.85 KB
/
databaseController.py
File metadata and controls
131 lines (117 loc) · 4.85 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
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
import psycopg2
import config
import logging
import sys
import traceback
def quote(msg):
return "'" + str(msg) + "'"
def parse_data(cursor):
desc = cursor.description
column_names = [col[0] for col in desc]
data = [dict(zip(column_names, row))
for row in cursor.fetchall()]
return data
class DatabaseController:
def __init__(self,
user=config.user,
password=config.password,
host="127.0.0.1",
port="5432",
database=config.database,
reset=False):
self.user = user
self._password = password
self._host = host
self._port = port
self.database = database
self.connection = None
self.connect()
if reset:
self.reset_db()
else:
self.setup_db()
def connect(self):
self.connection = psycopg2.connect(
user=self.user,
password=self._password,
host=self._host,
port=self._port,
database=self.database)
def execute(self, query):
cursor = self.connection.cursor()
try:
cursor.execute(query)
self.connection.commit()
except Exception as e:
msg = "Error executing query: " + query
logging.error(msg)
print(msg)
type_, value_, traceback_ = sys.exc_info()
logging.error('Type: ' + str(type_))
logging.error('Value: ' + str(value_))
logging.error('Traceback: ' + str(traceback.format_exc()))
self.connection.rollback()
return cursor
def insert_json(self, table, json_data):
data_str = str(json_data).replace("'", '"')
query = "INSERT INTO " + table + "(Data) VALUES ('" + data_str + "');"
self.execute(query)
msg = "Executed query: " + query
logging.info(msg)
print(msg)
def insert_snapshot(self, data, option=None):
if not data['state'] == 'open':
raise Exception("Orderbook is not open")
try:
if option is None:
query = '''INSERT INTO OrderbookSnapshots
(Symbol, Timestamp, Bids, Asks, BidVol, AskVol, Delta, Gamma, Vega, Theta)
VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s);''' % \
(quote(data['instrument']), quote(data['tstamp']), quote(str(data['bids']).replace("'", '"')), quote(str(data['asks']).replace("'", '"')),
quote(data['bidIv']), quote(data['askIv']), quote(data['delta']), quote(data['gamma']),
quote(data['vega']), quote(data['theta']))
else:
vol = (data['askIv'] + data['bidIv']) / 2
expiry = quote(option.expiry.strftime('%Y-%m-%d %H:%M:%S'))
query = '''INSERT INTO OrderbookSnapshots
(Symbol, Timestamp, Bids, Asks, Vol, BidVol, AskVol, Strike, Expiry, Delta, Gamma, Vega, Theta)
VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);''' % \
(quote(data['instrument']), quote(data['tstamp']), quote(str(data['bids']).replace("'", '"')),
quote(str(data['asks']).replace("'", '"')), quote(str(vol)),
quote(data['bidIv']), quote(data['askIv']), quote(option.strike), expiry,
quote(data['delta']), quote(data['gamma']),
quote(data['vega']), quote(data['theta']))
self.execute(query)
except Exception as e:
msg = "Error inserting orderbook snapshot: " + str(e) + ", data: " + str(data)
print(msg)
logging.error(msg)
def get_last_snapshot(self, symbol):
query = '''
SELECT Symbol, Strike, Expiry, Delta, Vol
FROM OrderbookSnapshots
WHERE Symbol='%s'
ORDER BY ID DESC LIMIT 1
''' % symbol
return parse_data(self.execute(query))
def setup_db(self):
query = '''CREATE TABLE IF NOT EXISTS OrderbookSnapshots(
Id serial PRIMARY KEY,
Symbol varchar(50) NOT NULL,
Timestamp bigint NOT NULL,
Bids jsonb NOT NULL,
Asks jsonb NOT NULL,
Vol double precision,
BidVol double precision,
AskVol double precision,
Strike int,
Expiry timestamp,
Delta double precision,
Gamma double precision,
Vega double precision,
Theta double precision);'''
self.execute(query)
def reset_db(self):
query = "DROP TABLE IF EXISTS OrderbookSnapshots;"
self.execute(query)
self.setup_db()