-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmain.py
executable file
·124 lines (93 loc) · 3.58 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
import time
import sys
reload(sys) # Reload does the trick!
sys.setdefaultencoding('UTF8')
#from Qt import QtWidgets, QtCore
from PyQtX import QtCore, QtWidgets
import iqoption as iq
import martingale
import pandasmanager
import QMatplotlib
import neuralnetwork as nn
#import model
class QtIQOption(QtWidgets.QWidget, QtCore.QObject):
def __init__(self, parent=None):
super(QtIQOption, self).__init__(parent)
# Initialize Core
self.martingale = martingale.Martingale()
self.neural = nn.IqNeuralNetwork()
self.iqStream = iq.IQOption()
self.dataframeManager = pandasmanager.PandasManager()
# Initialize UI
self.setWindowTitle('IqOption ML Trader')
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
self.layout = QtWidgets.QGridLayout()
self.log = QtWidgets.QTextEdit()
self.graph = QMatplotlib.QMatplotlib()
# Layout
self.layout.addWidget(self.log)
self.layout.addWidget(self.graph)
self.setLayout(self.layout)
# TEST
#self.execButton.clicked.connect(self.execFunction)
# Startup
self.bootstrapCounter = 0
#self.updateLog()
# Prepare Timer
currentTime = QtCore.QTime.currentTime()
nextMinute = QtCore.QTime(currentTime.hour(), currentTime.minute()+1)
waitSignal = currentTime.secsTo(nextMinute)
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.callback)
self.timer.start((waitSignal)*1000)
def callback(self):
print('Callback ')
self.timer.start(60*1000)
self.updateLog()
def updateLog(self):
# Boostrap
candleData = self.iqStream.getCandles()
if candleData:
self.dataframeManager.appendIQCandleRow(candleData)
print self.dataframeManager.df.shape
print self.dataframeManager.df
if self.bootstrapCounter > 30:
result = self.iqStream.getResult()
print 'TRADE RESULT: ', result
self.martingale.calc(result)# ToDo
#result = self.invest(self.martingale.getCurrentInvest())
else:
self.log.append('Whait ' + str(30-self.bootstrapCounter) + ' minutes to Start!')
self.bootstrapCounter = self.bootstrapCounter + 1
# update chart
self.graph.clear()
self.graph.addDataframe(self.dataframeManager.readLastNCluster(30))
self.graph.plot()
def invest(self, amount=1):
data = self.iqStream.getDataFrame()
while data.empty:
time.sleep(1)
data = self.iqStream.getDataFrame()
print 'wait'
if not data.empty:
old, last, current = self.iqStream.getCandles()
# add last candle
price_predict = self.neural.predict()## to do
# get last investment status
result = self.iqStream.getResult()
self.martingale.calc(result)
investAmount = self.martingale.getCurrentInvest()
lastClose = last[2]
print 'FORECAST PRICE: ', price_predict
if price_predict >= lastClose:
self.iqStream.openPosition(amount=investAmount, direction="call")
print 'BUY'
else:
self.iqStream.openPosition(amount=investAmount, direction="put")
print 'SELL'
return price_predict
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainApp = QtIQOption()
mainApp.show()
sys.exit(app.exec_())