Beispiel #1
0
class TradeItem(pg.GraphicsObject):
    def __init__(self, data):
        pg.GraphicsObject.__init__(self)
        self.data = data  # data里面必须有以下字段: 时间, 开盘价, 收盘价, 交易量
        self.generatePicture()  # 绑定按钮点击信号

    def generatePicture(self):
        self.picture = QPicture()  # 实例化一个绘图设备
        p = QPainter(self.picture)  # 在picture上实例化QPainter用于绘图
        w = (self.data[1][0] - self.data[0][0]) / 3
        for (t, open, close, trade) in self.data:
            p.setPen(pg.mkPen('w'))  # 设置画笔颜色
            p.drawLine(QPointF(t, 0), QPointF(t, trade / 10000))  # 绘制线条
            if open > close:  # 开盘价大于收盘价
                p.setBrush(pg.mkBrush('g'))  # 设置画刷颜色为绿
            else:
                p.setBrush(pg.mkBrush('r'))  # 设置画刷颜色为红
            p.drawRect(QRectF(t - w, 0, w * 2, trade / 10000))  # 绘制箱子
        p.end()

    def paint(self, p, *args):
        p.drawPicture(0, 0, self.picture)

    def boundingRect(self):
        return QRectF(self.picture.boundingRect())
Beispiel #2
0
class CandlestickItem(pg.GraphicsObject):
    def __init__(self, data):
        pg.GraphicsObject.__init__(self)
        self.data = data  ## data must have fields: time, open, close, min, max
        self.generatePicture()

    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 = QPicture()
        p = QPainter(self.picture)
        p.setPen(pg.mkPen('w'))
        w = 1 / 3.
        for (open, max, min, close, vol, ma10, ma20, ma30,
             t) in self.data.values:
            p.drawLine(QPointF(t, min), QPointF(t, max))
            if open > close:
                p.setBrush(pg.mkBrush('g'))
            else:
                p.setBrush(pg.mkBrush('r'))
            p.drawRect(QRectF(t - w, open, w * 2, close - open))
        p.end()

    def paint(self, p, *args):
        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 QRectF(self.picture.boundingRect())
Beispiel #3
0
class VolItem(pg.GraphicsObject):
    """ 自定义Item """
    def __init__(self, data: pd.DataFrame):
        super(VolItem, self).__init__()
        self._data = data
        self._pic = QPicture()
        self.generatePicture()

    def generatePicture(self):
        """ 绘图 """
        p = QPainter(self._pic)
        p.setPen(pg.mkPen({'color': "CCCCCC"}))
        # 两个坐标点的1/3, 两个点的距离为横坐标的差
        # 如果使用时间来,会出现间隔不连续,因为股票数据时间本身是不连续的
        w = 1 / 3
        for row in self._data.itertuples():
            _t = getattr(row, 'Index')
            vol = getattr(row, 'vol') / 10000
            o_price = getattr(row, 'open')
            c_price = getattr(row, 'close')

            if o_price > c_price:
                p.setBrush(pg.mkBrush('g'))
            else:
                p.setBrush(pg.mkBrush('r'))

            p.drawRect(QRectF(_t - w, 0, w * 2, vol))

        p.end()

    def paint(self, p, *args):
        p.drawPicture(0, 0, self._pic)

    def boundingRect(self):
        return QRectF(self._pic.boundingRect())
Beispiel #4
0
class CandlestickItem(pg.GraphicsObject):
    def __init__(self, data):
        pg.GraphicsObject.__init__(self)
        self.data = data  ## data must have fields: time, open, close, min, max
        self.generatePicture()

    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 = QPicture()
        painter = QPainter(self.picture)
        painter.setPen(pg.mkPen('w'))
        width = (self.data[1][0] - self.data[0][0]) / 3000.
        for (timestamp, open, high, low, close, _volume) in self.data:
            timestamp /= 1000
            #print(t, open, high, low, close)
            painter.drawLine(QPointF(timestamp, low), QPointF(timestamp, high))
            if open > close:
                painter.setBrush(pg.mkBrush('r'))
            else:
                painter.setBrush(pg.mkBrush('g'))
            painter.drawRect(
                QRectF(timestamp - width, open, width * 2, close - open))
        painter.end()

    def paint(self, p, *args):
        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 QRectF(self.picture.boundingRect())
Beispiel #5
0
 def sizeHint(self):
     if not self.sizeHint_:
         pic = QPicture()
         painter = QPainter(pic)
         getModuleAttrDict(Client.gameObject.module)['paintGame'](painter)
         painter.end()
         self.sizeHint_ = pic.boundingRect().size()
     return self.sizeHint_
Beispiel #6
0
class CandlestickItem(pg.GraphicsObject):
    '''
    K线图,自定义CandlestickItem类,这个类继承了pyqtgraph.GraphicsObject
    '''
    def __init__(self, data):
        '''
        一些初始设置
        '''
        pg.GraphicsObject.__init__(self)
        self.data = data
        # 变量data表示我们需要带入的数据
        
        self.generatePicture()

    def generatePicture(self):
        '''
        每个小的K线
        '''
        self.picture = QPicture()
        p = QPainter(self.picture)
        p.setPen(pg.mkPen('w'))# 白色
        w = (self.data[1][0] - self.data[0][0]) / 3.# 每个K线的宽度
        for (t, date, close, open, high, low, price_change) in self.data:
            # t:每个汇率数据的序号
            # date:日期
            # close:收盘价
            # open:开盘价
            # high:最高价
            # low:最低价
            # price_change:价格变化
            if open > close:
                p.setPen(pg.mkPen('g'))
                p.setBrush(pg.mkBrush('g'))
            else:
                p.setPen(pg.mkPen('r'))
                p.setBrush(pg.mkBrush('r'))
            # 如果收盘价高于开盘价,我们就用红色;如果收盘价低于开盘价,我们就用绿色
            if low != high:
                p.drawLine(QPointF(t, low), QPointF(t, high))
                # 当最高价和最低价不相同时,我们就画一条线,两点坐标是从最低价到最高价。
            p.drawRect(QRectF(t - w, open, w * 2, close - open))
            # 画出具体的收盘价、开盘价矩形图。这里注意下:要是close - open,矩形的方向是不一样的
        p.end()

    def paint(self, p, *args):
        '''
        绘图
        '''
        p.drawPicture(0, 0, self.picture)

    def boundingRect(self):
        '''
        所有绘画必须限制在图元的边界矩形内
        '''
        return QRectF(self.picture.boundingRect())
Beispiel #7
0
class CandlestickItem(pg.GraphicsObject):
    df: pd.DataFrame
    y_min: float
    y_max: float

    def __init__(self):
        super().__init__()
        self.picture = QPicture()

    def set_data(self, df: pd.DataFrame):
        self.df = df
        self.y_min = self.df['low'].min()
        self.y_max = self.df['high'].max()
        print(f'<CandlestickItem> received {self.df.shape[0]} rows, value min = {self.y_min}, max = {self.y_max}')
        self.generate()

    def generate(self):
        painter = QPainter(self.picture)
        painter.setPen(pg.mkPen('w'))
        w = 1.0 / 3.0
        for i in range(self.df.shape[0]):
            if self.df.at[i, 'close'] > self.df.at[i, 'open']:
                painter.setPen(pg.mkPen('g'))
                painter.setBrush(pg.mkBrush('g'))
            elif self.df.at[i, 'close'] < self.df.at[i, 'open']:
                painter.setPen(pg.mkPen('r'))
                painter.setBrush(pg.mkBrush('r'))
            else:
                painter.setPen(pg.mkPen('w'))
                painter.setBrush(pg.mkBrush('w'))
            painter.drawLine(
                QPointF(i, self.df.at[i, 'low']), QPointF(i, self.df.at[i, 'high'])
            )
            painter.drawRect(
                QRectF(i - w, self.df.at[i, 'open'], w * 2, self.df.at[i, 'close'] - self.df.at[i, 'open'])
            )
        painter.end()

    def paint(self, p, *args):
        p.drawPicture(0, 0, self.picture)

    def boundingRect(self):
        return QRectF(self.picture.boundingRect())
Beispiel #8
0
 def paintEvent(self, QPaintEvent):
     pic = QPicture()
     painter = QPainter(pic)
     getModuleAttrDict(Client.gameObject.module)['paintGame'](painter)
     painter.end()
     painter.begin(self)
     bRect = pic.boundingRect()
     self.sizeHint_ = bRect.size()
     painter.setWindow(bRect)
     painter.setViewTransformEnabled(True)
     width = self.width()
     height = self.height()
     if width * bRect.height() < height * bRect.width():
         pheight = (width * bRect.height()) / bRect.width()
         painter.setViewport(0, (height - pheight) / 2, width, pheight)
     else:
         pwidth = (height * bRect.width()) / bRect.height()
         painter.setViewport((width - pwidth) / 2, 0, pwidth, height)
     self.invTrafo = painter.combinedTransform().inverted()[0]
     pic.play(painter)
Beispiel #9
0
class CandlestickItem(pg.GraphicsObject):
    def __init__(self, data):
        pg.GraphicsObject.__init__(self)
        self.data = data  # data里面必须有以下字段: 时间, 开盘价, 收盘价, 最低价, 最高价, ma5
        self.generatePicture()  # 绑定按钮点击信号

    def generatePicture(self):
        prema5 = 0
        prema10 = 0
        prema20 = 0
        self.picture = QPicture()  # 实例化一个绘图设备
        p = QPainter(self.picture)  # 在picture上实例化QPainter用于绘图
        w = (self.data[1][0] - self.data[0][0]) / 3
        for (t, open, close, min, max, ma5, ma10, ma20) in self.data:
            p.setPen(pg.mkPen('w'))  # 设置画笔颜色
            # print(t, open, close, min, max)
            p.drawLine(QPointF(t, min), QPointF(t, max))  # 绘制线条
            if open > close:  # 开盘价大于收盘价
                p.setBrush(pg.mkBrush('g'))  # 设置画刷颜色为绿
            else:
                p.setBrush(pg.mkBrush('r'))  # 设置画刷颜色为红
            p.drawRect(QRectF(t - w, open, w * 2, close - open))  # 绘制箱子
            if prema5 != 0:
                p.setPen(pg.mkPen('r'))
                p.drawLine(QPointF(t - 1, prema5), QPointF(t, ma5))
            prema5 = ma5
            if prema10 != 0:
                p.setPen(pg.mkPen('c'))
                p.drawLine(QPointF(t - 1, prema10), QPointF(t, ma10))
            prema10 = ma10
            if prema20 != 0:
                p.setPen(pg.mkPen('m'))
                p.drawLine(QPointF(t - 1, prema20), QPointF(t, ma20))
            prema20 = ma20
        p.end()

    def paint(self, p, *args):
        p.drawPicture(0, 0, self.picture)

    def boundingRect(self):
        return QRectF(self.picture.boundingRect())
Beispiel #10
0
class CandlestickItem(pg.GraphicsObject):
    """ 自定义Item """
    def __init__(self, data: pd.DataFrame):
        super(CandlestickItem, self).__init__()
        self._data = data
        self._pic = QPicture()
        self.generatePicture()

    def generatePicture(self):
        """ 绘图 """
        p = QPainter(self._pic)
        p.setPen(pg.mkPen('w'))
        # 两个坐标点的1/3, 两个点的距离为横坐标的差
        # 如果使用时间来,会出现间隔不连续,因为股票数据时间本身是不连续的
        w = 1 / 3
        for row in self._data.itertuples():
            _t = getattr(row, 'Index')
            h_price = getattr(row, 'high')
            l_price = getattr(row, 'low')
            o_price = getattr(row, 'open')
            c_price = getattr(row, 'close')
            p.drawLine(QPointF(_t, l_price), QPointF(_t, h_price))

            if o_price > c_price:
                p.setBrush(pg.mkBrush('g'))
            else:
                p.setBrush(pg.mkBrush('r'))

            p.drawRect(QRectF(_t - w, o_price, w * 2, c_price - o_price))

        p.end()

    def paint(self, p, *args):
        p.drawPicture(0, 0, self._pic)

    def boundingRect(self):
        return QRectF(self._pic.boundingRect())
Beispiel #11
0
class BarGraphItem(pg.GraphicsObject):
    """BarGraphItem"""
    def __init__(self, x=None, y=None, *, width=1.0, pen=None, brush=None):
        """Initialization."""
        super().__init__()

        self._picture = None

        self._x = None
        self._y = None

        if width > 1.0 or width <= 0:
            width = 1.0
        self._width = width

        if pen is None and brush is None:
            self._pen = FColor.mkPen(None)
            self._brush = FColor.mkBrush('b')
        else:
            self._pen = FColor.mkPen(None) if pen is None else pen
            self._brush = FColor.mkBrush(None) if brush is None else brush

        self.setData(x, y)

    def setData(self, x, y):
        """PlotItem interface."""
        self._x = [] if x is None else x
        self._y = [] if y is None else y

        if len(self._x) != len(self._y):
            raise ValueError("'x' and 'y' data have different lengths!")

        self._picture = None
        self.update()
        self.informViewBoundsChanged()

    def drawPicture(self):
        self._picture = QPicture()
        p = QPainter(self._picture)
        p.setPen(self._pen)
        p.setBrush(self._brush)

        # Now it works for bar plot with equalized gaps
        # TODO: extend it
        if len(self._x) > 1:
            width = self._width * (self._x[1] - self._x[0])
        else:
            width = self._width

        for x, y in zip(self._x, self._y):
            p.drawRect(QRectF(x - width/2, 0, width, y))

        p.end()
        self.prepareGeometryChange()

    def paint(self, painter, *args):
        """Override."""
        if self._picture is None:
            self.drawPicture()
        self._picture.play(painter)

    def boundingRect(self):
        """Override."""
        if self._picture is None:
            self.drawPicture()
        return QRectF(self._picture.boundingRect())
Beispiel #12
0
class CandlestickItem(pg.GraphicsObject):
    # 州的先生zmister.com
    def __init__(self, data, indicators_state):
        pg.GraphicsObject.__init__(self)
        # data里面必须有以下字段: 时间, 开盘价, 收盘价, 最低价, 最高价
        self.data = data
        self.indicators_state = indicators_state
        self.generatePicture()

    def generatePicture(self):
        # 实例化一个绘图设备
        self.picture = QPicture()

        # 在picture上实例化QPainter用于绘图
        self.p = QPainter(self.picture)

        # 设置画笔颜色

        self.w = (self.data[1][0] - self.data[0][0]) / 3

        pre_ama = 0
        pre_ema12 = 0
        pre_ema20 = 0

        for (t, open, high, low, close, ama, ema12, ema20) in self.data:
            # 绘制线条
            self.p.setPen(pg.mkPen('w'))
            self.p.drawLine(QPointF(t, low), QPointF(t, high))
            # 开盘价大于收盘价
            if close >= open:
                # 设置画刷颜色为绿
                self.p.setPen(pg.mkPen('g'))
                self.p.setBrush(pg.mkBrush('g'))
            else:
                # 设置画刷颜色为红
                self.p.setPen(pg.mkPen('r'))
                self.p.setBrush(pg.mkBrush('r'))
            # 绘制箱子
            self.p.drawRect(QRectF(t - self.w, open, self.w * 2, close - open))

            # 根据是否被选中画均线
            if pre_ama != 0:
                if self.indicators_state[0]:
                    self.p.setPen(pg.mkPen('w'))
                    self.p.setBrush(pg.mkBrush('w'))
                    self.p.drawLine(QPointF(t - 1, pre_ama), QPointF(t, ama))
            pre_ama = ama

            if pre_ema12 != 0:
                if self.indicators_state[1]:
                    self.p.setPen(pg.mkPen('c'))
                    self.p.setBrush(pg.mkBrush('c'))
                    self.p.drawLine(QPointF(t - 1, pre_ema12),
                                    QPointF(t, ema12))
            pre_ema12 = ema12

            if pre_ema20 != 0:
                if self.indicators_state[2]:
                    self.p.setPen(pg.mkPen('m'))
                    self.p.setBrush(pg.mkBrush('m'))
                    self.p.drawLine(QPointF(t - 1, pre_ema20),
                                    QPointF(t, ema20))
            pre_ema20 = ema20

        self.p.end()
        self.last_t = t

    def paint(self, p, *args):
        p.drawPicture(0, 0, self.picture)

    def boundingRect(self):
        return QRectF(self.picture.boundingRect())