def plot_frame(self): width = self.width() - 8 height = self.height() - 8 y_trans = Translator(0, 1, 0, self.height()) x_trans = Translator(0, 1, 0, width) textItems = [] x = x_trans.t(1 - self.widthspace) + 10 y1 = y_trans.t(self.space) self.scene.addLine(0, y1, x, y1, QtGui.QPen(QtCore.Qt.DashLine)) self.top_text = TextPairWidget() self.top_text.setup_Widget(x) self.top_text.set_Widget(posY=y1) textItems += self.top_text.get_TextItems() y2 = y_trans.t(self.space + self.chartsize) self.scene.addLine(0, y2, x, y2, QtGui.QPen(QtCore.Qt.DashLine)) self.bot_text = TextPairWidget() self.bot_text.setup_Widget(x) self.bot_text.set_Widget(posY=y2) textItems += self.bot_text.get_TextItems() self.current_text = TextPairWidget() self.current_text.setup_Widget(x, maxY=[y1, y2]) self.current_text.set_Widget(posY=200) textItems += self.current_text.get_TextItems() self.indi_y_trans = Translator( 150, -150, self.height() * (self.chartsize + (self.space * 2)), self.height() - (self.height() * self.space)) y = self.indi_y_trans.t(100) inditop_text = QtWidgets.QGraphicsTextItem('100') inditop_text.setPos(x, y - 10) inditop_line = QtWidgets.QGraphicsLineItem(0, y, x, y) #self.scene.addItem(inditop_text) self.scene.addItem(inditop_line) y = self.indi_y_trans.t(-100) indibot_text = QtWidgets.QGraphicsTextItem('-100') indibot_text.setPos(x, y - 10) indibot_line = QtWidgets.QGraphicsLineItem(0, y, x, y) #self.scene.addItem(indibot_text) self.scene.addItem(indibot_line) for item in textItems: self.scene.addItem(item) self.scene.addLine(0, 0, width, 0) self.scene.addLine(0, height, width, height) self.scene.addLine(0, 0, 0, height) self.scene.addLine(width, 0, width, height)
class ChartPlotter(QtWidgets.QGraphicsView): def __init__(self, *args, **kwargs): super(QtWidgets.QGraphicsView, self).__init__(*args, **kwargs) self.candleItems = [] self.currentCandle = [] self.indicatorItems = [] self.currentIndicator = [] self.chartsize = 0.6 self.indicatorsize = 0.25 self.space = (1 - (self.chartsize + self.indicatorsize)) / 4 self.widthspace = 0.09 self.candleWidth = 0.65 self.chart_x_trans = None self.chart_y_trans = None self.indi_x_trans = None self.indi_y_trans = None self.green = QtGui.QColor(46, 204, 113) self.red = QtGui.QColor(231, 76, 60) self.lastprint = '' def init_plotter(self): self.set_newscene() self.plot_frame() def set_newscene(self): self.scene = QtWidgets.QGraphicsScene() self.setScene(self.scene) self.currentCandle.clear() self.candleItems.clear() self.currentIndicator.clear() self.indicatorItems.clear() def plot_frame(self): width = self.width() - 8 height = self.height() - 8 y_trans = Translator(0, 1, 0, self.height()) x_trans = Translator(0, 1, 0, width) textItems = [] x = x_trans.t(1 - self.widthspace) + 10 y1 = y_trans.t(self.space) self.scene.addLine(0, y1, x, y1, QtGui.QPen(QtCore.Qt.DashLine)) self.top_text = TextPairWidget() self.top_text.setup_Widget(x) self.top_text.set_Widget(posY=y1) textItems += self.top_text.get_TextItems() y2 = y_trans.t(self.space + self.chartsize) self.scene.addLine(0, y2, x, y2, QtGui.QPen(QtCore.Qt.DashLine)) self.bot_text = TextPairWidget() self.bot_text.setup_Widget(x) self.bot_text.set_Widget(posY=y2) textItems += self.bot_text.get_TextItems() self.current_text = TextPairWidget() self.current_text.setup_Widget(x, maxY=[y1, y2]) self.current_text.set_Widget(posY=200) textItems += self.current_text.get_TextItems() self.indi_y_trans = Translator( 150, -150, self.height() * (self.chartsize + (self.space * 2)), self.height() - (self.height() * self.space)) y = self.indi_y_trans.t(100) inditop_text = QtWidgets.QGraphicsTextItem('100') inditop_text.setPos(x, y - 10) inditop_line = QtWidgets.QGraphicsLineItem(0, y, x, y) #self.scene.addItem(inditop_text) self.scene.addItem(inditop_line) y = self.indi_y_trans.t(-100) indibot_text = QtWidgets.QGraphicsTextItem('-100') indibot_text.setPos(x, y - 10) indibot_line = QtWidgets.QGraphicsLineItem(0, y, x, y) #self.scene.addItem(indibot_text) self.scene.addItem(indibot_line) for item in textItems: self.scene.addItem(item) self.scene.addLine(0, 0, width, 0) self.scene.addLine(0, height, width, height) self.scene.addLine(0, 0, 0, height) self.scene.addLine(width, 0, width, height) def plot_currentCandle(self, candles): candleswidth = self.width() / len(candles) * self.candleWidth self.__calculate_chart_translations(candles) for candle in self.currentCandle: self.scene.removeItem(candle) self.currentCandle.clear() candle = candles[0] graphic = self.__get_candleGraphic(candle, candleswidth, 0) graphic[2].setWidth(1) graphic[2].setStyle(QtCore.Qt.DashLine) y = self.chart_y_trans.t(candle.close) newline2 = QtWidgets.QGraphicsLineItem( 0, int(y), self.width() - (self.width() * self.widthspace), int(y), ) newline2.setPen(graphic[2]) change = ((candle.close - candle.open) / candle.open) * 100 self.current_text.set_Widget(price=candle.close, precentage=change, posY=y) self.currentCandle.append(newline2) self.currentCandle.append(graphic[0]) self.currentCandle.append(graphic[1]) self.scene.addItem(newline2) self.scene.addItem(graphic[0]) self.scene.addItem(graphic[1]) def plot_chart(self, candles): candleswidth = self.width() / len(candles) * self.candleWidth self.__calculate_chart_translations(candles) for candle in self.candleItems: self.scene.removeItem(candle) self.candleItems.clear() for i in range(len(candles) - 1): if i is not 0: candle = candles[i] graphic = self.__get_candleGraphic(candle, candleswidth, i) self.candleItems.append(graphic[0]) self.candleItems.append(graphic[1]) self.scene.addItem(graphic[0]) self.scene.addItem(graphic[1]) def __get_candleGraphic(self, candle, candleswidth, i): dif = candle.close - candle.open if dif >= 0: color = self.green else: color = self.red pen = QtGui.QPen(color) brush = QtGui.QBrush(color) newrect = QtWidgets.QGraphicsRectItem( int(self.chart_x_trans.t(i) - candleswidth), int(self.chart_y_trans.t(candle.open)), int(candleswidth), int( self.chart_y_trans.t(candle.close) - self.chart_y_trans.t(candle.open))) newrect.setBrush(brush) newrect.setPen(pen) x = int(self.chart_x_trans.t(i) - (candleswidth / 2)) newline = QtWidgets.QGraphicsLineItem( x, int(self.chart_y_trans.t(candle.high)), x, int(self.chart_y_trans.t(candle.low))) newline.setPen(pen) return [newrect, newline, pen] def __calculate_chart_translations(self, candles): extremes = Helper.get_extremes(candles) maxP = extremes['max'] minP = extremes['min'] currentCandle = candles[0] topprecentage = ((maxP / currentCandle.close) - 1) * 100 botprecentage = ( (minP - currentCandle.close) / currentCandle.close) * 100 self.top_text.set_Widget(price=maxP, precentage=topprecentage) self.bot_text.set_Widget(price=minP, precentage=botprecentage) self.chart_y_trans = Translator( maxP, minP, self.height() * self.space, self.height() * (self.chartsize + self.space)) self.chart_x_trans = Translator( 0, len(candles), self.width() - (self.width() * self.widthspace), 0) def __calculate_indicator_translations(self, indicatordata): self.indi_y_trans = Translator( 150, -150, self.height() * (self.chartsize + (self.space * 2)), self.height() - (self.height() * self.space)) self.indi_x_trans = Translator( 0, len(indicatordata), self.width() - self.width() * self.widthspace, 0) def plot_indicator(self, indicatordata): self.lastprint = '' pen = QtGui.QPen(QtCore.Qt.black, 2) self.__calculate_indicator_translations(indicatordata) for indicator in self.indicatorItems: self.scene.removeItem(indicator) self.indicatorItems.clear() for j in range(len(indicatordata)): i = len(indicatordata) - j - 2 if i > 0: newline = QtWidgets.QGraphicsLineItem( self.indi_x_trans.t(i), self.indi_y_trans.t(indicatordata[i]), self.indi_x_trans.t(i - 1), self.indi_y_trans.t(indicatordata[i - 1])) newline.setPen(pen) currentindi = indicatordata[i] actionline = False if currentindi <= 100 and indicatordata[i + 1] > 100: if self.lastprint is not 'short': self.lastprint = 'short' actionline = True pen2 = QtGui.QPen(self.red, 2, QtCore.Qt.DashLine) if currentindi >= -100 and indicatordata[i + 1] < -100: if self.lastprint is not 'long': self.lastprint = 'long' actionline = True pen2 = QtGui.QPen(self.green, 2, QtCore.Qt.DashLine) if actionline is True: x = self.indi_x_trans.t(i) y1 = self.height() * self.space y2 = self.height() - (self.height() * self.space) newline2 = QtWidgets.QGraphicsLineItem(x, y1, x, y2) newline2.setPen(pen2) self.scene.addItem(newline2) self.indicatorItems.append(newline2) self.scene.addItem(newline) self.indicatorItems.append(newline) def plot_currentIndicator(self, indicatordata): pen = QtGui.QPen(QtCore.Qt.black, 2) for indicator in self.currentIndicator: self.scene.removeItem(indicator) self.currentIndicator.clear() newline = QtWidgets.QGraphicsLineItem( self.indi_x_trans.t(1), self.indi_y_trans.t(indicatordata[1]), self.indi_x_trans.t(0), self.indi_y_trans.t(indicatordata[0])) newline.setPen(pen) actionline = False currentindi = indicatordata[0] if currentindi <= 100 and indicatordata[1] > 100: actionline = True pen2 = QtGui.QPen(self.red, 2, QtCore.Qt.DashLine) if currentindi >= -100 and indicatordata[1] < -100: actionline = True pen2 = QtGui.QPen(self.green, 2, QtCore.Qt.DashLine) if actionline is True: x = self.indi_x_trans.t(0) y1 = self.height() * self.space y2 = self.height() - (self.height() * self.space) newline2 = QtWidgets.QGraphicsLineItem(x, y1, x, y2) newline2.setPen(pen2) self.scene.addItem(newline2) self.indicatorItems.append(newline2) self.scene.addItem(newline) self.currentIndicator.append(newline)
class IndicatorGraphicView(QtWidgets.QGraphicsView): def __init__(self, *args, **kwargs): super(IndicatorGraphicView,self).__init__(*args,**kwargs) self.scene = QtWidgets.QGraphicsScene(self) self.setScene(self.scene) #self.setSceneRect(0,0, 80, 375) self.mainScene_y = 0.85 self.xPos = 45 self.space = (1-self.mainScene_y) / 2 self.height = 375 self.width = 95 self.ytrans = Translator(150,-150,self.space*self.height,(1-self.space)*self.height) self.greentrans = Translator(150,-150,0,255) self.redtrans = Translator(-150,150,0,255) self.currentDot = QtWidgets.QGraphicsRectItem() self.plot_frame() self.scene.addItem(self.currentDot) def plot_frame(self): self.scene.addLine(0, self.height, 0, 0) self.scene.addLine(self.width, self.height, self.width, 0) self.scene.addLine(0, 0, self.width, 0) self.scene.addLine(0, self.height, self.width, self.height) pen = QtGui.QPen() pen.setWidth(2) self.scene.addLine(self.xPos, self.ytrans.t(100), self.xPos, self.ytrans.t(-100),pen) y = self.ytrans.t(100) self.scene.addLine(self.xPos+10, y, self.xPos-10, y) y = self.ytrans.t(-100) self.scene.addLine(self.xPos+10, y, self.xPos-10, y) pen.setColor(QtGui.QColor(255,0,0)) y = self.ytrans.t(150) - 10 self.scene.addLine(self.xPos, self.ytrans.t(100), self.xPos, y, pen) self.scene.addLine(self.xPos+5, y, self.xPos-5, y,pen) pen.setColor(QtGui.QColor(0,255,0)) y = self.ytrans.t(-150) + 10 self.scene.addLine(self.xPos, self.ytrans.t(-100), self.xPos, y,pen) self.scene.addLine(self.xPos+5, y, self.xPos-5, y,pen) #self.scene.addLine(self.xPos[0], self.ytrans.t(150), self.xPos[0], self.ytrans.t(-150)) def plot_currentIndicator(self, indicatordata): sign = lambda x: indicatordata and (1, -1) [indicatordata<0] if abs(indicatordata) > 150: indicatordata = 150 * sign yPos = self.ytrans.t(indicatordata) self.currentDot.setRect(self.xPos - 10, yPos - 5, 20, 10) pen = QtGui.QPen() brush = QtGui.QBrush() pen.setWidth(3) color = QtGui.QColor(self.redtrans.t(indicatordata), self.greentrans.t(indicatordata), 0) pen.setColor(color) brush.setColor(color) self.currentDot.setPen(pen) self.currentDot.setBrush(brush)