-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDatabase.py
269 lines (206 loc) · 10.1 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
from pymongo import MongoClient
import numpy as np
import threading
import datetime
import copy
from datetime import date
from Security import Security
import pandas as pd
import Globals as gb
import json
from Predition import Prediction
class Database:
def __init__(self):
try:
client = MongoClient('localhost:27017')
self.db = client.stockstalker
print 'Database Connection Opened successfully.'
except:
print 'Failed to open connection to database'
return
self.stocks = self.db.FiveMinuteStocks
self.predictions = self.db.Predictions
return None
def insert_all(self, records):
for x in records:
self.fill_change(x)
def remove_all_records(self, stock):
self.stocks.remove({'symbol': stock.symbol})
if gb.do_prediction:
self.predictions.remove({'symbol': stock.symbol})
def get_last_record(self, symbol):
last = self.stocks.find({'symbol': symbol}).sort('date', -1).limit(1)
if last.count() > 0:
last = last[0]
last.pop('_id', None)
Security_record = Security()
Security_record.to_class(**last)
return Security_record
print "No Record found for : ", symbol
return
def get_lastn_records(self, symbol, n):
data = self.stocks.find({'symbol': symbol}).sort('date', -1).limit(n)
data2 = self.predictions.find({'symbol': symbol}).sort('cdate', -1).limit(2*n)
records = []
for last in data:
last.pop('_id', None)
records.append(last)
predictions = []
for last in data2:
last.pop('_id', None)
predictions.append(last)
# print "No Record found for : ", symbol
# print records
return records,predictions
def get_lastn_pred_records(self, symbol, n):
data2 = self.predictions.find({'symbol': symbol}).sort('cdate', -1).limit(2 * n)
predictions = []
for last in data2:
last.pop('_id', None)
predictions.append(last)
return predictions
def fill_change(self,security):
self.stocks.insert(security.__dict__)
last_records = self.stocks.find({'symbol':security.symbol}).sort('date',-1).limit(gb.lag+1)
changed = ''
if last_records.count(with_limit_and_skip=True) == gb.lag+1:
df = pd.DataFrame(list(last_records)[::-1])
id = df.iloc[-1]['_id']
del df['_id']
df['close'] = df['close'].astype(float)
df['m5_change'] = df['close'].pct_change()
df['m10_change'] = df['close'].pct_change(periods=2)
df['m15_change'] = df['close'].pct_change(periods=3)
df['m20_change'] = df['close'].pct_change(periods=4)
df['m60_change'] = df['close'].pct_change(periods=12)
df['rolling_mean'] = df['m5_change'].rolling(window=gb.rolling_mean).mean()
df = df.fillna(0)
df['m5_change'] = df['m5_change'].astype(str)
df['m10_change'] = df['m10_change'].astype(str)
df['m15_change'] = df['m15_change'].astype(str)
df['m20_change'] = df['m20_change'].astype(str)
df['m60_change'] = df['m60_change'].astype(str)
df['rolling_mean'] = df['rolling_mean'].astype(str)
mydict = df.T.to_dict()[len(df)-1]
self.stocks.update({'_id':id}, {'$set': mydict})
if gb.do_prediction:
security = Security()
security.to_class(**mydict)
new_prediction = Prediction(security)
new_prediction.update_prediction(self.predictions)
def insert_records(self, security_array):
for sa in security_array:
self.fill_change(sa)
def fill_all_changes(self,precords):
records = [ x.__dict__ for x in precords]
df = pd.DataFrame(records)
# print df
df['m5_change'] = df['close'].astype(float).pct_change()
df['m10_change'] = df['close'].astype(float).pct_change(periods=2)
df['m15_change'] = df['close'].astype(float).pct_change(periods=3)
df['m20_change'] = df['close'].astype(float).pct_change(periods=4)
df['m60_change'] = df['close'].astype(float).pct_change(periods=12)
df['rolling_mean'] = df['m5_change'].rolling(window=gb.rolling_mean).mean().astype(float)
df = df.fillna(0)
df['m5_change'] = df['m5_change'].astype(str)
df['m10_change'] = df['m10_change'].astype(str)
df['m15_change'] = df['m15_change'].astype(str)
df['m20_change'] = df['m20_change'].astype(str)
df['m60_change'] = df['m60_change'].astype(str)
df['rolling_mean'] = df['rolling_mean'].astype(str)
mydict = df.T.to_dict()
df_len = len(mydict)
mypredictions = [0]*df_len
if gb.do_prediction:
for i in range(0,df_len):
security = Security()
security.to_class(**mydict[i])
new_prediction = Prediction(security)
# if new_prediction.is_present(new_prediction,self.predictions):
# continue
if i >= gb.lag:
mypredictions[i-gb.lag].next_closing = float(new_prediction.current_closing)
if float(new_prediction.current_closing) >= float(mypredictions[i-gb.lag].current_closing):
mypredictions[i-gb.lag].actual_movement = 1
else:
mypredictions[i - gb.lag].actual_movement = 0
mypredictions[i] = new_prediction
if np.mod(i, 500) == 0:
print i
for d in range(0,df_len):
self.stocks.insert(mydict[d])
if gb.do_prediction:
print "Inserting Predictions"
self.predictions.insert(mypredictions[d].__dict__)
def update_security(self, current, updated):
self.stocks.update(current.__dict__, {'$set': updated.__dict__})
# print "Previous Record Updated ! "
def is_present(self, stock):
record = self.stocks.find({'symbol': stock.symbol, 'date': stock.date})
if record.count(with_limit_and_skip=True) > 0:
# print "Previously Found Similar Record, Updated !"
# self.stocks.update({'_id': record[0].pop('_id')}, {'$set': stock.__dict__})
# last_pred = Prediction(stock)
# last_pred.update_prediction(self.predictions)
print "Found Previously : ",stock.date
return True
return False
def proper_insert(self, stock):
missing = []
last_record = self.get_last_record(stock.symbol)
current_record = stock
if self.is_present(stock):
return True
print "Found New Record : ", stock.name
if last_record and current_record:
if current_record.date == last_record.date:
self.update_security(last_record, current_record)
if gb.days_between(last_record.date, current_record.date) < 1:
diff = gb.minutes_between(last_record.date, current_record.date)
# print diff
if diff > 5:
for i in range(5, diff, 5):
newdate = gb.add_minute(str(last_record.date), i)
new_record = copy.deepcopy(last_record)
new_record.date = newdate
new_record.volume = 0
# self.stocks.insert(copy.deepcopy(new_record.__dict__))
missing.append(new_record)
missing.append(current_record)
self.insert_records(missing)
# self.stocks.insert(copy.deepcopy(current_record.__dict__))
else:
print "Entering Initial Record for : ", stock.symbol
self.insert_records([stock])
def proper_insert_all(self, stocks):
missing = []
for s in range(0, len(stocks)):
if s < 1:
missing.append(stocks[s])
continue
stock = stocks[s]
last_record = stocks[s-1]
current_record = stock
# self.get_last_record(stock.symbol)
if self.is_present(stock):
continue
#
# print "Entering New Records"
if last_record and current_record:
if current_record.date == last_record.date:
self.update_security(last_record, current_record)
if gb.days_between(last_record.date, current_record.date) < 1:
diff = gb.minutes_between(last_record.date, current_record.date)
# print diff
if diff > 5:
for i in range(5, diff, 5):
newdate = gb.add_minute(str(last_record.date), i)
new_record = copy.deepcopy(last_record)
new_record.date = newdate
new_record.volume = 0
# self.stocks.insert(copy.deepcopy(new_record.__dict__))
missing.extend([new_record])
missing.extend([current_record])
# self.insert_records(missing)
print "Missing dates filled for :",stocks[0].symbol
self.fill_all_changes(missing)