pyqtgraph是python平台上一种功能强大的2d/3d绘图库,相对于matplotlib库,由于其在内部实现方式上,使用了高速计算的numpy信号处理库以及qt的graphicsview框架,因此它在大数据量的处理及快速显示方面有着天然的优势,非常适合于需要快速绘图更新、视频或实时交互性的操作场合,在数学、科学和工程领域都有着广泛的应用。
k线图介绍
对于股票交易者来讲,k线图是弄清股票一段时间走势的一种最基本的图形工具,k线分为阳线和阴线,阳线和阴线都包含了开盘价、收盘价、最高价和最低价,一般k线如下图所示:
当收盘价大于开盘价时,称为阳线,在图形上一般用红色表示,反之,当收盘价低于开盘价时,称为阴线,在图形上一般用绿色表示。由于其形状颇似一根根蜡烛,k线图有时也叫做蜡烛图。
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
|
# -*- coding: utf-8 -*-
# form implementation generated from reading ui file 'qwidget_plot.ui'
#
# created by: pyqt4 ui code generator 4.11.4
#
# warning! all changes made in this file will be lost!
import sys
reload (sys)
sys.setdefaultencoding( 'utf-8' )
from pyqt4 import qtcore, qtgui
import datetime
import pyqtgraph as pg
import tushare as ts
try :
_fromutf8 = qtcore.qstring.fromutf8
except attributeerror:
def _fromutf8(s):
return s
try :
_encoding = qtgui.qapplication.unicodeutf8
def _translate(context, text, disambig):
return qtgui.qapplication.translate(context, text, disambig, _encoding)
except attributeerror:
def _translate(context, text, disambig):
return qtgui.qapplication.translate(context, text, disambig)
class ui_mainwindow( object ):
def setupui( self , mainwindow):
mainwindow.setobjectname(_fromutf8( "mainwindow" ))
mainwindow.resize( 800 , 600 )
self .centralwidget = qtgui.qwidget(mainwindow)
self .centralwidget.setobjectname(_fromutf8( "centralwidget" ))
self .verticallayout_2 = qtgui.qvboxlayout( self .centralwidget)
self .verticallayout_2.setobjectname(_fromutf8( "verticallayout" ))
self .verticallayout_2.setobjectname(_fromutf8( "verticallayout_2" ))
self .verticallayout_2.setcontentsmargins( 0 , 0 , 0 , 0 )
mainwindow.setcentralwidget( self .centralwidget)
self .menubar = qtgui.qmenubar(mainwindow)
self .menubar.setgeometry(qtcore.qrect( 0 , 0 , 800 , 31 ))
self .menubar.setobjectname(_fromutf8( "menubar" ))
mainwindow.setmenubar( self .menubar)
self .drawchart = drawchart(ktype = 'd' )
self .verticallayout_2.addwidget( self .drawchart.pyqtgraphdrawchart())
self .retranslateui(mainwindow)
qtcore.qmetaobject.connectslotsbyname(mainwindow)
def retranslateui( self , mainwindow):
mainwindow.setwindowtitle(_translate( "mainwindow" , "mainwindow" , none))
class drawchart():
def __init__( self , code = 'sz50' , start = str (datetime.date.today() - datetime.timedelta(days = 200 )), end = str (datetime.date.today() + datetime.timedelta(days = 1 )), ktype = 'd' ):
self .code = code
self .start = start
self .end = end
self .ktype = ktype
self .data_list, self .t = self .getdata()
def pyqtgraphdrawchart( self ):
try :
self .item = candlestickitem( self .data_list)
self .xdict = { 0 : str ( self .hist_data.index[ 0 ]).replace( '-' , '/' ), int (( self .t + 1 ) / 2 ) - 1 : str ( self .hist_data.index[ int (( self .t + 1 ) / 2 )]).replace( '-' , '/' ), self .t - 1 : str ( self .hist_data.index[ - 1 ]).replace( '-' , '/' )}
self .stringaxis = pg.axisitem(orientation = 'bottom' )
self .stringaxis.setticks([ self .xdict.items()])
self .plt = pg.plotwidget(axisitems = { 'bottom' : self .stringaxis}, enablemenu = false)
self .plt.additem( self .item)
# self.plt.showgrid(x=true, y=true)
return self .plt
except :
return pg.plotwidget()
def getdata( self ):
self .start = str (datetime.date.today() - datetime.timedelta(days = 150 ))
self .end = str (datetime.date.today() + datetime.timedelta(days = 1 ))
self .hist_data = ts.get_hist_data( self .code, self .start, self .end, self .ktype).sort_index()[ - 300 : - 1 ]
data_list = []
t = 0
for dates, row in self .hist_data.iterrows():
open , high, close, low, volume, price_change, p_change, ma5, ma10, ma20 = row[: 10 ]
datas = (t, open , close, low, high, volume, price_change, p_change, ma5, ma10, ma20)
data_list.append(datas)
t + = 1
return data_list, t
class candlestickitem(pg.graphicsobject):
def __init__( self , data):
pg.graphicsobject.__init__( self )
self .data = data
self .generatepicture()
def generatepicture( self ):
self .picture = qtgui.qpicture()
p = qtgui.qpainter( self .picture)
p.setpen(pg.mkpen( 'w' ))
w = ( self .data[ 1 ][ 0 ] - self .data[ 0 ][ 0 ]) / 3.
prema5 = 0
prema10 = 0
prema20 = 0
for (t, open , close, min , max , volume, price_change, p_change, ma5, ma10, ma20) in self .data:
if open > close:
p.setpen(pg.mkpen( 'g' ))
p.setbrush(pg.mkbrush( 'g' ))
else :
p.setpen(pg.mkpen( 'r' ))
p.setbrush(pg.mkbrush( 'r' ))
p.drawline(qtcore.qpointf(t, min ), qtcore.qpointf(t, max ))
p.drawrect(qtcore.qrectf(t - w, open , w * 2 , close - open ))
if prema5 ! = 0 :
p.setpen(pg.mkpen( 'w' ))
p.setbrush(pg.mkbrush( 'w' ))
p.drawline(qtcore.qpointf(t - 1 , prema5), qtcore.qpointf(t, ma5))
prema5 = ma5
if prema10 ! = 0 :
p.setpen(pg.mkpen( 'c' ))
p.setbrush(pg.mkbrush( 'c' ))
p.drawline(qtcore.qpointf(t - 1 , prema10), qtcore.qpointf(t, ma10))
prema10 = ma10
if prema20 ! = 0 :
p.setpen(pg.mkpen( 'm' ))
p.setbrush(pg.mkbrush( 'm' ))
p.drawline(qtcore.qpointf(t - 1 , prema20), qtcore.qpointf(t, ma20))
prema20 = ma20
p.end()
def paint( self , p, * args):
p.drawpicture( 0 , 0 , self .picture)
def boundingrect( self ):
return qtcore.qrectf( self .picture.boundingrect())
if __name__ = = "__main__" :
import sys
app = qtgui.qapplication(sys.argv)
mainwindow = qtgui.qmainwindow()
ui = ui_mainwindow()
ui.setupui(mainwindow)
mainwindow.show()
sys.exit(app.exec_())
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/otter1010/article/details/83719709