/
main.py
384 lines (313 loc) · 13.3 KB
/
main.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
__author__ = 'sihanyou'
# -*- coding: utf-8 -*-
#
# import unittest
import os.path
import sys
import csv
import datetime
import GlobalValue
from PyQt4 import QtGui, QtCore
from PyQt4.Qt import pyqtSignal
from Actif import Actif
import ImportYahooData, SimpleModelling
import logging
import plot
import pickle
import Aggregation
# class TestStringMethods(unittest.TestCase):
# def unitTest(self):
# self.assertEqual(GlobalValue.ptf[0].nom, 'AAPL')
def debugOutput(message):
print("log: " + message)
logging.debug(('{}\t' + message).format(datetime.datetime.now()))
class Worker(QtCore.QObject):
processPercent = pyqtSignal(int)
simpleModelSucceed = pyqtSignal(bool)
def __init__(self):
super().__init__()
def preProcess(self, isOffLine, isDebugMode):
print("preProcess")
self.preProcessing(isOffLine, isDebugMode)
currentPercent = 10
self.processPercent.emit(currentPercent)
self.simpleModelling(currentPercent)
self.processPercent.emit(100)
def preProcessing(self, noInternet, debugMode):
if noInternet:
debugOutput('Picking data from local resource...')
if debugMode:
readHistData('Historical Data Short.csv')
else:
readHistData('Historical Data.csv')
else:
debugOutput('Picking data from Yahoo Finance...')
if ImportYahooData.importData() == False:
debugOutput('A serieuse warning as above: please check your file!')
else:
debugOutput('data imported successfully from Yahoo!')
def simpleModelling(self, currentPercent):
debugOutput("Preparing for the simple modelling..")
try:
SimpleModelling.main(self.processPercent, currentPercent)
except ValueError:
debugOutput("Calculation is wrong somewhere")
self.simpleModelSucceed.emit(False)
return
debugOutput("Modeling is successful!")
print(GlobalValue.modelParams)
self.simpleModelSucceed.emit(True)
########## MAIN DIALOG FORM: Main UI ##############
class ModelChoiceDialog(QtGui.QDialog):
def __init__(self, portfolio, parent):
super(ModelChoiceDialog, self).__init__(parent)
self.actifCount = len(portfolio)
self.armaCheck = list()
self.garchCheck = list()
self.svCheck = list()
self.vBox = QtGui.QVBoxLayout()
for i in range(self.actifCount):
self.armaCheck.append(QtGui.QRadioButton('ARMA', self))
self.garchCheck.append(QtGui.QRadioButton('GARCH', self))
self.svCheck.append(QtGui.QRadioButton('SV', self))
userChoiceGroupBox = QtGui.QGroupBox('Please select one model for the stock ' + portfolio[i].stockCode, self)
radioVBox = QtGui.QVBoxLayout()
radioVBox.addWidget(self.armaCheck[i])
radioVBox.addWidget(self.garchCheck[i])
radioVBox.addWidget(self.svCheck[i])
userChoiceGroupBox.setLayout(radioVBox)
self.vBox.addWidget(userChoiceGroupBox)
self.setLayout(self.vBox)
def getResult(self):
result = list()
for i in range(self.actifCount):
if self.armaCheck[i].isChecked():
result.append(0)
elif self.garchCheck[i].isChecked():
result.append(1)
elif self.svCheck[i].isChecked():
result.append(2)
else:
result.append(-1)
return result
class MainDialog(QtGui.QWidget):
preProcessRequest = pyqtSignal(bool, bool)
def __init__(self):
super(MainDialog, self).__init__()
self.initUI()
def setWorker(self, worker):
self.worker = worker
self.preProcessRequest.connect(self.worker.preProcess)
worker.processPercent.connect(self.progressBarValueChange)
worker.simpleModelSucceed.connect(self.simpleModellingFinishReceiver)
def initUI(self):
self.portfolioText = QtGui.QLabel("Please choose Portfolio Data File Path:", self)
self.portfolioText.setFont(QtGui.QFont('Times', 20))
self.processText = QtGui.QLabel("Pre-processing data...", self)
self.processText.setEnabled(False)
self.pathEdit = QtGui.QLineEdit(self)
self.importBtn = QtGui.QPushButton('Import Portfolio Data', self)
self.importBtn.clicked.connect(self.selectFile)
self.noInternetBtn = QtGui.QCheckBox('No Internet', self)
self.noInternetBtn.setEnabled(False)
self.debugTestBtn = QtGui.QCheckBox('Debug Mode(fewer data, faster processing)', self)
self.debugTestBtn.setEnabled(False)
self.beginProBtn = QtGui.QPushButton('Begin Pre-processing', self)
self.beginProBtn.clicked.connect(self.preProc)
self.progBar = QtGui.QProgressBar(self)
self.modelChoiceBtn = QtGui.QPushButton('Choose Models for stocks', self)
self.modelChoiceBtn.clicked.connect(self.chooseModel)
self.modelInfoLabel = QtGui.QLabel(self)
self.updateModelLabel()
self.modelGroupeBox = QtGui.QGroupBox('Chosen Model:', self)
modelLayout = QtGui.QVBoxLayout()
modelLayout.addWidget(self.modelInfoLabel)
self.modelGroupeBox.setLayout(modelLayout)
self.beginSimulationBtn = QtGui.QPushButton('Begin simulation', self)
self.beginSimulationBtn.clicked.connect(self.beginSimulation)
mainVBox = QtGui.QVBoxLayout()
hbox1 = QtGui.QHBoxLayout()
hbox2 = QtGui.QHBoxLayout()
mainVBox.addWidget(self.portfolioText)
hbox1.addWidget(self.pathEdit)
hbox1.addWidget(self.importBtn)
mainVBox.addLayout(hbox1)
mainVBox.addWidget(self.processText)
hbox2.addWidget(self.progBar)
hbox2.addWidget(self.beginProBtn)
mainVBox.addLayout(hbox2)
mainVBox.addWidget(self.noInternetBtn)
mainVBox.addWidget(self.debugTestBtn)
mainVBox.addWidget(self.modelChoiceBtn)
mainVBox.addWidget(self.modelGroupeBox)
mainVBox.addWidget(self.beginSimulationBtn)
self.setLayout(mainVBox)
self.setGeometry(300, 100, 500, 300)
self.setWindowTitle('Portfolio Risk Management Tool')
self.show()
self.enableProcessBlock(False)
self.enableSimulationBlock(False)
def updateModelLabel(self):
s = ''
if len(GlobalValue.ptf) == 0:
s = 'No stock name found.'
elif len(GlobalValue.modelChoice) == 0:
for i in range(len(GlobalValue.ptf)):
actifName = GlobalValue.ptf[i].stockCode
s = s + actifName + ': no model chosen\n'
else:
assert len(GlobalValue.modelChoice) == len(GlobalValue.ptf)
for i in range(len(GlobalValue.ptf)):
actifName = GlobalValue.ptf[i].stockCode
s = s + actifName + ': '
if GlobalValue.modelChoice[i] == 0:
s = s + 'arma\n'
elif GlobalValue.modelChoice[i] == 1:
s = s + 'garch\n'
elif GlobalValue.modelChoice[i] == 2:
s = s + 'sv\n'
else:
s = s + 'no model chosen\n'
self.modelInfoLabel.setText(s)
def preProc(self):
self.progBar.setValue(3)
noInternet = self.noInternetBtn.isChecked()
debugMode = self.debugTestBtn.isChecked()
self.preProcessRequest.emit(noInternet, debugMode)
def selectFile(self):
self.filename = QtGui.QFileDialog.getOpenFileName()
ok = readFile(self.filename)
if ok:
self.pathEdit.setText(self.filename)
self.enableProcessBlock(True)
else:
QtGui.QMessageBox.warning(self, "Error", "File format is invalid!")
def chooseModel(self):
modelDialog = ModelChoiceDialog(GlobalValue.ptf, self)
modelDialog.setGeometry(300, 100, 500, 300)
modelDialog.setWindowTitle('Choose model')
modelDialog.exec_()
GlobalValue.modelChoice = modelDialog.getResult()
self.updateModelLabel()
def beginSimulation(self):
self.applyModel()
Aggregation.agregation()
def simpleModellingFinishReceiver(self, succeed):
if succeed:
self.enableSimulationBlock(True)
pickle.dump(GlobalValue.modelParams, open("globalValue_modelParams.dat", "wb"))
pickle.dump(GlobalValue.yahooData, open("globalValue_yahooData.dat", "wb"))
else:
self.enableSimulationBlock(False)
QtGui.QMessageBox.warning(self, "Error", "Simple Modelling failed!")
for i in range(len(GlobalValue.yahooData)):
plot.plot_simulation(GlobalValue.modelParams[i], GlobalValue.yahooData[i],i)
def enableProcessBlock(self, enable):
self.progBar.setEnabled(enable)
self.processText.setEnabled(enable)
self.noInternetBtn.setEnabled(enable)
self.debugTestBtn.setEnabled(enable)
self.processText.setFont(QtGui.QFont('Times', 20))
self.progressBarValueChange(0)
self.beginProBtn.setEnabled(True)
def enableSimulationBlock(self, enable):
self.beginSimulationBtn.setEnabled(enable)
def progressBarValueChange(self, value):
self.progBar.setValue(value)
def applyModel(self):
# TODO: change assert to if to give a warning dialog
assert len(GlobalValue.ptf) == len(GlobalValue.modelParams)
assert len(GlobalValue.ptf) == len(GlobalValue.modelChoice)
assert len(GlobalValue.ptf) == len(GlobalValue.yahooData)
assert len(GlobalValue.ptf) > 0
for i in range(len(GlobalValue.ptf)):
assert GlobalValue.modelChoice[i] >= 0
assert GlobalValue.modelChoice[i] <= 2
GlobalValue.simulations = []
for i in range(len(GlobalValue.ptf)):
if GlobalValue.modelChoice[i] == 0:
returns = SimpleModelling.returns(GlobalValue.yahooData[i])
GlobalValue.simulations.append(plot.choice_ARMA(GlobalValue.modelParams[i]['arma'], returns))
elif GlobalValue.modelChoice[i] == 1:
GlobalValue.simulations.append(plot.Plot_GARCH(GlobalValue.modelParams[i]['garch']))
elif GlobalValue.modelChoice[i] == 2:
GlobalValue.simulations.append(plot.Plot_SV(GlobalValue.modelParams[i]['sv']))
else:
assert False
print(GlobalValue.simulations[0])
########## FROM HERE WE BEGIN TO IMPORT DATA ##############
########## If online, we read only Portfolio Structure ##############
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
def readFile(filename):
''' stock all actifs infomations in portfolio.'''
try:
with open(filename, 'r') as csvfile:
GlobalValue.ptf = []
pf = csv.reader(csvfile)
for row in pf:
if len(row) != 2 or not is_number(row[1]):
return False
stockCode = row[0]
quantity = row[1]
GlobalValue.ptf.append(Actif(stockCode, quantity))
csvfile.close()
debugOutput("File imported successfully!")
# print(GlobalValue.ptf[0].stockCode)
return True
except FileNotFoundError:
debugOutput("Please choose the file or close the program!")
sys.exit(app.exec_())
######## If offline, we have to read the historical data values stocked with the name 'Historical Data.csv' ##########
def readHistData(filename):
# stock all actifs infomations in portfolio.
try:
with open(filename, 'r') as csvfile:
GlobalValue.yahooData = []
pf = csv.reader(csvfile)
pf = list(pf)
temp = []
for actif in GlobalValue.ptf:
i = 0
for column in pf[0]:
if column == actif.stockCode:
for row in pf:
try:
temp.append(float(row[i]))
except ValueError:
print(row[i])
break
i = i + 1
GlobalValue.yahooData.append(temp)
temp = []
csvfile.close()
debugOutput("Historical Data File imported successfully!")
# print(GlobalValue.ptf[0].nom)
except FileNotFoundError:
debugOutput("Please choose the file or close the program!")
sys.exit(app.exec_())
########## FROM HERE WE BEGIN THE CALCULATION / MODELLING ##############
########## Pre-Processing will prepare the data we need ##############
if __name__ == '__main__':
LOG_FILENAME = 'debug.log'
lognum = 1
if os.path.isfile('iterate.dat'):
os.remove('iterate.dat')
while os.path.isfile(LOG_FILENAME):
LOG_FILENAME = 'debug' + '(' + str(lognum) + ').log'
lognum += 1
logging.basicConfig(filename=LOG_FILENAME, level=logging.DEBUG)
logging.debug('****************** Log File ***********************')
GlobalValue.init()
app = QtGui.QApplication(sys.argv)
worker = Worker()
workerThread = QtCore.QThread()
worker.moveToThread(workerThread)
md = MainDialog()
md.setWorker(worker)
workerThread.start()
sys.exit(app.exec_())