-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathqt.py
410 lines (336 loc) · 14.3 KB
/
qt.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 3 20:06:35 2017
@author: lizard
"""
import pyqtgraph as pg
from pyqtgraph import QtCore, QtGui
import pickle
from chan import Chan
import numpy as np
import math
import time
#from feedData import feedData
#from feedData import nowTime
import threading
from pandas import DataFrame
import matplotlib.pyplot as plt
# Create a subclass of GraphicsObject.
# The only required methods are paint() and boundingRect()
# (see QGraphicsItem documentation)
class CandlestickItem(pg.GraphicsObject):
def __init__(self):
pg.GraphicsObject.__init__(self)
self.flagHasData = False
def set_data(self, data):
self.data = data # data must have fields: time, open, close, min, max
self.flagHasData = True
self.generatePicture()
self.informViewBoundsChanged()
def generatePicture(self):
# pre-computing a QPicture object allows paint() to run much more quickly,
# rather than re-drawing the shapes every time.
self.picture = QtGui.QPicture()
p = QtGui.QPainter(self.picture)
p.setPen(pg.mkPen('w'))
w = (self.data[1][0] - self.data[0][0]) / 3.
for (t, open, close, min, max) in self.data:
p.drawLine(QtCore.QPointF(t, min), QtCore.QPointF(t, max))
if open > close:
p.setBrush(pg.mkBrush('g'))
else:
p.setBrush(pg.mkBrush('r'))
p.drawRect(QtCore.QRectF(t - w, open, w * 2, close - open))
p.end()
def paint(self, p, *args):
if self.flagHasData:
p.drawPicture(0, 0, self.picture)
def boundingRect(self):
# boundingRect _must_ indicate the entire area that will be drawn on
# or else we will get artifacts and possibly crashing.
# (in this case, QPicture does all the work of computing the bouning rect for us)
return QtCore.QRectF(self.picture.boundingRect())
class BisItem(pg.GraphicsObject):
def __init__(self):
pg.GraphicsObject.__init__(self)
self.flagHasData = False
def set_data(self, data):
self.data = data # data must have fields: time, open, close, min, max
self.flagHasData = True
self.generatePicture()
self.informViewBoundsChanged()
def generatePicture(self):
# pre-computing a QPicture object allows paint() to run much more quickly,
# rather than re-drawing the shapes every time.
self.picture = QtGui.QPicture()
p = QtGui.QPainter(self.picture)
for bi in self.data:
if bi.biType == 'up':
p.setPen(pg.mkPen('r'))
p.drawLine(QtCore.QPointF(chan.chanBars[bi.barIndex1].closeIndex, chan.lowBar[chan.chanBars[bi.barIndex1].closeIndex]), QtCore.QPointF(
chan.chanBars[bi.barIndex2].closeIndex, chan.highBar[chan.chanBars[bi.barIndex2].closeIndex]))
else:
p.setPen(pg.mkPen('g'))
p.drawLine(QtCore.QPointF(chan.chanBars[bi.barIndex1].closeIndex, chan.highBar[chan.chanBars[bi.barIndex1].closeIndex]), QtCore.QPointF(
chan.chanBars[bi.barIndex2].closeIndex, chan.lowBar[chan.chanBars[bi.barIndex2].closeIndex]))
p.end()
def paint(self, p, *args):
if self.flagHasData:
p.drawPicture(0, 0, self.picture)
def boundingRect(self):
# boundingRect _must_ indicate the entire area that will be drawn on
# or else we will get artifacts and possibly crashing.
# (in this case, QPicture does all the work of computing the bouning rect for us)
return QtCore.QRectF(self.picture.boundingRect())
class LinesItem(pg.GraphicsObject):
def __init__(self):
pg.GraphicsObject.__init__(self)
self.flagHasData = False
def set_data(self, data):
self.data = data # data must have fields: time, open, close, min, max
self.flagHasData = True
self.generatePicture()
self.informViewBoundsChanged()
def generatePicture(self):
# pre-computing a QPicture object allows paint() to run much more quickly,
# rather than re-drawing the shapes every time.
self.picture = QtGui.QPicture()
p = QtGui.QPainter(self.picture)
for line in self.data:
if line.lineType == 'up':
p.setPen(pg.mkPen('r'))
p.drawLine(QtCore.QPointF(chan.chanBars[line.barIndex1].closeIndex, chan.lowBar[chan.chanBars[line.barIndex1].closeIndex]), QtCore.QPointF(
chan.chanBars[line.barIndex2].closeIndex, chan.highBar[chan.chanBars[line.barIndex2].closeIndex]))
else:
p.setPen(pg.mkPen('g'))
p.drawLine(QtCore.QPointF(chan.chanBars[line.barIndex1].closeIndex, chan.highBar[chan.chanBars[line.barIndex1].closeIndex]), QtCore.QPointF(
chan.chanBars[line.barIndex2].closeIndex, chan.lowBar[chan.chanBars[line.barIndex2].closeIndex]))
p.end()
def paint(self, p, *args):
if self.flagHasData:
p.drawPicture(0, 0, self.picture)
def boundingRect(self):
# boundingRect _must_ indicate the entire area that will be drawn on
# or else we will get artifacts and possibly crashing.
# (in this case, QPicture does all the work of computing the bouning rect for us)
return QtCore.QRectF(self.picture.boundingRect())
class ZhongshusItem(pg.GraphicsObject):
def __init__(self):
pg.GraphicsObject.__init__(self)
self.flagHasData = False
def set_data(self, data):
self.data = data # data must have fields: time, open, close, min, max
self.flagHasData = True
self.generatePicture()
self.informViewBoundsChanged()
def generatePicture(self):
# pre-computing a QPicture object allows paint() to run much more quickly,
# rather than re-drawing the shapes every time.
self.picture = QtGui.QPicture()
p = QtGui.QPainter(self.picture)
p.setPen(pg.mkPen('w'))
p.setBrush(pg.mkBrush(None))
for zhongshu in self.data:
p.drawRect(QtCore.QRectF(chan.chanBars[zhongshu.barIndex1].closeIndex,
zhongshu.low, chan.chanBars[
zhongshu.barIndex2].closeIndex - chan.chanBars[zhongshu.barIndex1].closeIndex,
zhongshu.high - zhongshu.low))
p.end()
def paint(self, p, *args):
if self.flagHasData:
p.drawPicture(0, 0, self.picture)
def boundingRect(self):
# boundingRect _must_ indicate the entire area that will be drawn on
# or else we will get artifacts and possibly crashing.
# (in this case, QPicture does all the work of computing the bouning rect for us)
return QtCore.QRectF(self.picture.boundingRect())
class DateAxis(pg.AxisItem):
def __init__(self, dates, *args, **kwargs):
pg.AxisItem.__init__(self, *args, **kwargs)
self.x_values = list(range(len(dates)))
# self.x_strings = []
# for i in dates:
# self.x_strings.append(i.strftime('%Y%m%d'))
self.x_strings = dates
def tickStrings(self, values, scale, spacing):
strings = []
if(len(values)==0):
return strings
rng = max(values)-min(values)
for v in values:
vs = v* scale
if vs in self.x_values:
if rng >= 100:
vstr = self.x_strings[np.abs(self.x_values-vs).argmin()].strftime('%Y%m%d')
else:
vstr = self.x_strings[np.abs(self.x_values-vs).argmin()].strftime('%Y%m%d,%H:%M')
else:
vstr = ""
strings.append(vstr)
return strings
global data
import sys
sys.path.append('./')
sys.path.append('../')
import pandas as pd
class ValuesParser:
def __init__(self,dataframe):
self.Data = [data['open'].tolist(),data['high'].tolist(),data['low'].tolist(),data['close'].tolist(),data['volume'].tolist()]
self.Times= data.index.values.tolist()
data = pd.DataFrame.from_csv('rb1705_year.csv')[10000:11500]
data = ValuesParser(data)
global dataToNow
#dataToNow = w.wst(code, "last", data.Times[-1], nowTime, "")
dataToNow = []
quotes = []
for i in range(len(data.Times)):
quotes.append([i, data.Data[0][i], data.Data[3][i],
data.Data[2][i], data.Data[1][i]])
global chan
#chan = Chan(data.Data[0], data.Data[1], data.Data[2],data.Data[3], data.Data[4], data.Times)
chan = Chan([],[],[],[],[],[])
print(len(data.Times))
start = 400
for tick in range(0, start):
chan.append(data.Data[0][tick], data.Data[1][tick], data.Data[2][tick], data.Data[3][tick], data.Data[4][tick], data.Times[tick])
for tick in range(start, len(data.Times)):
chan.append(data.Data[0][tick], data.Data[1][tick], data.Data[2][tick], data.Data[3][tick], data.Data[4][tick], data.Times[tick])
chan.barsMerge()
chan.findFenxing()
chan.findBi()
chan.findLines()
chan.findZhongshus()
chan.calculate_ta()
chan.findBiZhongshus()
chan.macdSeparate()
chan.findTrendLines()
chan.decisionBi()
#chan.plotBuySell()
chan.plotBeichi()
plt.show()
print(len(chan.dingbeichi))
# app = QtGui.QApplication([])
# win = pg.GraphicsWindow()
# win.setWindowTitle('行情+缠论')
# label = pg.LabelItem(justify = "center")
# win.addItem(label)
# axis = DateAxis(data.Times,orientation='bottom')
# p1 = win.addPlot(row=1, col=0,axisItems = {'bottom':axis})
# p2 = win.addPlot(row=2, col=0,axisItems = {'bottom':axis})
# p2.setXLink(p1)
# p2.plot(x = list(range(len(data.Times))),y = chan.diff,pen = 'w')
# p2.plot(x = list(range(len(data.Times))),y = chan.dea,pen = 'y')
# hLine = pg.InfiniteLine(angle=0, movable=False)
# hLine.setPos(0)
# p2.addItem(hLine, ignoreBounds=True)
# macdPositive = []
# macdNegetive = []
# for i in chan.macd:
# if i>=0:
# macdPositive.append(i)
# macdNegetive.append(0)
# else:
# macdPositive.append(0)
# macdNegetive.append(i)
# curve0 = p2.plot(x = list(range(len(data.Times))),y = np.zeros(len(data.Times)))
# curve1 = p2.plot(x = list(range(len(data.Times))),y = macdPositive, pen = 'w')
# curve2 = p2.plot(x = list(range(len(data.Times))),y = macdNegetive, pen = 'w')
# itemFill1 = pg.FillBetweenItem(curve0,curve1,pg.mkBrush('r'))
# itemFill2 = pg.FillBetweenItem(curve0,curve2,pg.mkBrush('g'))
# p2.addItem(itemFill1)
# p2.addItem(itemFill2)
# #win.addItem(label)
# #text = pg.TextItem('test',anchor=(0,1))
# # p1.addItem(text)
# itemK = CandlestickItem()
# itemK.set_data(quotes)
# itemBi = BisItem()
# itemBi.set_data(chan.bis)
# itemLine = LinesItem()
# itemLine.set_data(chan.lines)
# itemZhongshu = ZhongshusItem()
# #itemZhongshu.set_data(chan.zhongshus)
# itemZhongshu.set_data(chan.biZhongshus)
# p1.plot()
# p1.addItem(itemK)
# p1.addItem(itemBi)
# p1.addItem(itemLine)
# p1.addItem(itemZhongshu)
# p1.showGrid(x=True,y=True)
# #p1.setWindowTitle('pyqtgraph example: customGraphicsItem')
# # cross hair
# vLine = pg.InfiniteLine(angle=90, movable=False)
# hLine = pg.InfiniteLine(angle=0, movable=False)
# p1.addItem(vLine, ignoreBounds=True)
# p1.addItem(hLine, ignoreBounds=True)
# vb1 = p1.vb
# vb2 = p2.vb
# def mouseMoved(evt):
# pos = evt[0] # using signal proxy turns original arguments into a tuple
# if p1.sceneBoundingRect().contains(pos):
# mousePoint = vb1.mapSceneToView(pos)
# index = int(mousePoint.x())
# if index > 0 and index < len(quotes):
# label.setText("<span style='font-size: 12pt'>date=%s, <span style='color: red'>open=%0.01f</span>, <span style='color: green'>close=%0.01f\n, high = %0.01f, low = %0.01f</span>" %
# (data.Times[index].strftime('%Y%m%d,%H:%M'), quotes[index][1], quotes[index][2], quotes[index][3], quotes[index][4]))
# vLine.setPos(mousePoint.x())
# hLine.setPos(mousePoint.y())
# setYRange()
# def setYRange():
# r = vb1.viewRange()
# xmin = math.floor(r[0][0])
# xmax = math.ceil(r[0][1])
# #fix index <0 bug
# xmax = max(0,xmax-xmin)
# xmin = max(0,xmin)
# xmin = min(xmin,len(data.Times)-1)
# xmax = min(xmax,len(data.Times)-1)
# highBound1 = max(data.Data[1][xmin:xmax])
# lowBound1 = min(data.Data[2][xmin:xmax])
# p1.setRange(yRange=(lowBound1,highBound1))
# highBound2 = max(chan.diff[xmin:xmax])
# lowBound2 = min(chan.diff[xmin:xmax])
# p2.setRange(yRange=(lowBound2,highBound2))
# proxy = pg.SignalProxy(p1.scene().sigMouseMoved, rateLimit=60, slot=mouseMoved)
# aaa = 0
# def update():
# global itemK,itemBi,itemLine,itemZhongshu
# dataToNowDf = DataFrame(index=dataToNow.Times,data = dataToNow.Data[0],columns=['price'])
# dataToNowDf = dataToNowDf.between_time('9:30','11:30').append(dataToNowDf.between_time('13:00','15:00'))
# a = dataToNowDf.resample('30T',how = {'price':'ohlc'},label='right').dropna()
# for i in a.iterrows():
# data.Times.append(i[0].to_datetime())
# data.Data[0].append(i[1]['price']['open'])
# data.Data[1].append(i[1]['price']['high'])
# data.Data[2].append(i[1]['price']['low'])
# data.Data[3].append(i[1]['price']['close'])
# data.Data[4].append(0)
# quotes = []
# for i in range(len(data.Times)):
# quotes.append([i, data.Data[0][i], data.Data[3][i],
# data.Data[2][i], data.Data[1][i]])
# chan = Chan(data.Data[0], data.Data[1], data.Data[2],
# data.Data[3], data.Data[4], data.Times)
# chan.barsMerge()
# chan.findFenxing()
# chan.findBi()
# chan.findLines()
# chan.findZhongshus()
# chan.calculate_ta()
# a += 1
# itemK.set_data(quotes)
# # itemBi.set_data(chan.bis)
# # itemLine.set_data(chan.lines)
# # itemZhongshu.set_data(chan.zhongshus)
# app.processEvents() ## force complete redraw for every plot
# #timer = QtCore.QTimer()
# #timer.timeout.connect(update)
# #timer.start(100000)
# Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
# threadFeedData = threading.Thread(target=feedData, name='threadFeedData')
# threadFeedData.start()
# threadFeedData.join()
QtGui.QApplication.instance().exec_()