forked from jobtrader/Arbitrage-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
152 lines (102 loc) · 3.58 KB
/
database.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
# coding: utf-8
# Import Files
# In[1]:
import sqlite3
from datetime import datetime
# Define database name
# In[2]:
dbname = 'Bid_Ask.db'
# Create database
# In[3]:
def create_db():
conn = sqlite3.connect(dbname)
conn.close()
# Create bid and ask table
# In[4]:
def create_table():
conn = sqlite3.connect(dbname)
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS bid
(ID INTEGER,
Exchange TEXT,
Symbol TEXT,
Bid REAL,
BidVolume REAL,
TimeStamp INTEGER,
TickTime REAL)''')
c.execute('''CREATE TABLE IF NOT EXISTS ask
(ID INTEGER,
Exchange TEXT,
Symbol TEXT,
Ask REAL,
AskVolume REAL,
TimeStamp INTEGER,
TickTime REAL)''')
c.close()
conn.close()
# Increased ID
# In[ ]:
def increase_id(c, exchange, symbol):
c.execute('''SELECT MAX(ID) FROM bid WHERE Symbol = ? AND Exchange = ?'''
, (symbol, exchange))
max_tick = c.fetchone()[0]
if max_tick == None:
max_tick = 1
else:
max_tick += 1
return max_tick
# Return True if tick change
# In[ ]:
def is_tick_change(c, exchange, symbol, orderbook):
is_bid = False
is_ask = False
c.execute('''SELECT MAX(ID) FROM bid WHERE Symbol = ? AND Exchange = ?;'''
, (symbol, exchange))
max_tick_bid = c.fetchone()[0]
c.execute('''SELECT MAX(ID) FROM ask WHERE Symbol = ? AND Exchange = ?;'''
, (symbol, exchange))
max_tick_ask = c.fetchone()[0]
c.execute('''SELECT Bid FROM bid WHERE Exchange = ? AND ID = ? AND Symbol = ?;'''
, (exchange, max_tick_bid, symbol))
previous_bid = c.fetchall()
c.execute('''SELECT Ask FROM ask WHERE Exchange = ? AND ID = ? AND Symbol = ?;'''
, (exchange, max_tick_ask, symbol))
previous_ask = c.fetchall()
if len(previous_bid) > 0 and len(previous_ask) > 0:
if previous_bid[0][0] != orderbook['bids'][0]:
is_bid = True
if previous_ask[0][0] != orderbook['asks'][0]:
is_ask = True
return is_bid, is_ask
else:
return True, True
# Insert price data into database
# In[ ]:
def insert_data(orderbook, exchange, symbol, time_tick_change):
conn = sqlite3.connect(dbname)
c = conn.cursor()
is_insert = True
increased_id = increase_id(c, exchange, symbol)
is_bid, is_ask = is_tick_change(c, exchange, symbol, orderbook)
if is_bid or is_ask:
time = 0
if (is_bid):
time = time_tick_change
c.execute('''INSERT INTO bid
(ID, Exchange, Symbol, Bid, BidVolume, TimeStamp, TickTime)
VALUES
(?, ?, ?, ?, ?, ?, ?)''',
(increased_id, exchange, symbol, orderbook['bids'][0][0], orderbook['bids'][0][1], orderbook['timestamp'], time))
time = 0
if(is_ask):
time = time_tick_change
c.execute('''INSERT INTO ask
(ID, Exchange, Symbol, Ask, AskVolume, TimeStamp, TickTime)
VALUES
(?, ?, ?, ?, ?, ?, ?)''',
(increased_id, exchange, symbol, orderbook['asks'][0][0], orderbook['asks'][0][1], orderbook['timestamp'], time))
conn.commit()
is_insert = True
c.close()
conn.close()
return is_insert