예제 #1
0
def plot_candlechart(ohlc_data):
    app = ParaMakerApplication([])
    #app.setStyleSheet("background-color:black;")

    series = QCandlestickSeries()
    series.setBodyOutlineVisible(False)
    series.setDecreasingColor(Qt.red)
    series.setIncreasingColor(Qt.green)

    rsi = qc.QLineSeries()  # 5-days average data line
    rsi.append(QPointF(ohlc_data[300].timestamp, ohlc_data[300].closed))
    rsi.append(QPointF(ohlc_data[700].timestamp, ohlc_data[700].closed))
    #rsi.append(QPointF(ohlc_data[150].timestamp, ohlc_data[100].closed))
    tm = []  # stores str type data
    # in a loop,  series and rsi append corresponding data
    for candle in ohlc_data:
        series.append(
            QCandlestickSet(candle.opened, candle.high, candle.low,
                            candle.closed))
        #rsi.append(QPointF(num, m))
        tm.append(str(candle.timestamp))
        #rsi.append(str(candle.timestamp))

    #rsi_values = calculate_rsi(14, ohlc_data)

    chart = QChart()
    chart.setBackgroundVisible(False)
    chart.setPlotAreaBackgroundVisible(False)
    chart.addSeries(series)  # candle
    chart.addSeries(rsi)  # rsi line

    #chart.axisX(rsi).setRange(ohlc_data[0].timestamp, ohlc_data[-1].timestamp)

    chart.createDefaultAxes()

    axisXRSI = QValueAxis()
    axisYRSI = QValueAxis()
    axisXRSI.setRange(ohlc_data[0].timestamp, ohlc_data[-1].timestamp)
    axisYRSI.setRange(ohlc_data[0].closed, ohlc_data[-1].closed)
    axisXRSI.setGridLineVisible(False)
    axisYRSI.setGridLineVisible(False)

    chart.setAxisX(axisXRSI, rsi)
    chart.setAxisY(axisYRSI, rsi)

    chart.legend().hide()

    chart.axisX(series).setCategories(tm)
    #chart.axisX(series).setGridLineVisible(False)
    #chart.axisY(series).setGridLineVisible(False)
    ###chart.axisX(rsi).setVisible(False)

    chartview = QChartView(chart)
    chartview.setRenderHint(QPainter.Antialiasing)
    ui = ParaMakerWindow()
    ui.setCentralWidget(chartview)
    sys.exit(app.exec_())
예제 #2
0
class ChartTab(QChartView):
    def __init__(self):
        QChartView.__init__(self)
        self.chart = QChart()
        self.candleSeries = QCandlestickSeries(self)
        self.candleSeries.setIncreasingColor(Qt.blue)
        self.candleSeries.setBodyOutlineVisible(False)
        self.candleSeries.setDecreasingColor(Qt.red)
        self.candleSeries.setMaximumColumnWidth(20)
        self.candleSeries.clicked.connect(lambda set: print(set.high()))

        self.xAxis = QDateTimeAxis()
        self.xAxis.setTickCount(10)
        self.xAxis.setFormat("h:mm")
        self.yAxis = QValueAxis()

        for candle in data:
            self.addCandle(
                self.get_integer_price(candle[0]),
                self.get_integer_price(candle[1]),
                self.get_integer_price(candle[2]),
                self.get_integer_price(candle[3]),
                datetime.strptime(candle[4], '%Y%m%d%H%M%S').timestamp() *
                1000)

        self.chart.addSeries(self.candleSeries)
        self.setChart(self.chart)
        self.chart.setAxisX(self.xAxis, self.candleSeries)
        self.chart.setAxisY(self.yAxis, self.candleSeries)
        self.chart.legend().setVisible(False)

    def mouseMoveEvent(self, QMouseEvent):
        chartValues = self.chart.mapToValue(QMouseEvent.pos())
        price = chartValues.y()
        print(price)

    def addCandle(self, open, close, high, low, timestamp):
        candlestickSet = QCandlestickSet()
        candlestickSet.setOpen(open)
        candlestickSet.setClose(close)
        candlestickSet.setHigh(high)
        candlestickSet.setLow(low)
        candlestickSet.setTimestamp(timestamp)

        pen = QPen()
        candlestickSet.setPen(pen)
        self.candleSeries.append(candlestickSet)

    def get_integer_price(self, str_price):
        return int(str_price.strip()[1:])
예제 #3
0
    def __drawChart(self):  ##绘制图表
        self.chart.removeAllSeries()  #删除所有序列
        self.chart.setTitle("股票日线图--" + self.ui.tabWidget.tabText(0))

        ## 1. 创建蜡烛图
        seriesCandle = QCandlestickSeries()
        seriesCandle.setName("蜡烛图")
        seriesCandle.setIncreasingColor(Qt.red)  #暴涨
        seriesCandle.setDecreasingColor(Qt.darkGreen)  #暴跌

        visible = self.ui.chkBox_Outline.isChecked()
        seriesCandle.setBodyOutlineVisible(visible)
        seriesCandle.setCapsVisible(self.ui.chkBox_Caps.isChecked())

        self.chart.addSeries(seriesCandle)
        seriesCandle.attachAxis(self.__axisX)
        seriesCandle.attachAxis(self.__axisY)

        seriesCandle.clicked.connect(self.do_candleClicked)
        seriesCandle.hovered.connect(self.do_candleHovered)

        ## 2. 创建MA曲线
        pen = QPen()
        pen.setWidth(2)

        seriesMA1 = QLineSeries()  #不能使用QSplineSeries
        seriesMA1.setName("MA5")
        pen.setColor(Qt.magenta)
        seriesMA1.setPen(pen)
        self.chart.addSeries(seriesMA1)
        seriesMA1.attachAxis(self.__axisX)
        seriesMA1.attachAxis(self.__axisY)

        seriesMA2 = QLineSeries()
        seriesMA2.setName("MA10")
        pen.setColor(Qt.yellow)
        seriesMA2.setPen(pen)
        self.chart.addSeries(seriesMA2)
        seriesMA2.attachAxis(self.__axisX)
        seriesMA2.attachAxis(self.__axisY)

        seriesMA3 = QLineSeries()
        seriesMA3.setName("MA20")
        pen.setColor(Qt.cyan)
        seriesMA3.setPen(pen)
        self.chart.addSeries(seriesMA3)
        seriesMA3.attachAxis(self.__axisX)
        seriesMA3.attachAxis(self.__axisY)

        seriesMA4 = QLineSeries()
        seriesMA4.setName("MA60")
        pen.setColor(Qt.green)  #green
        seriesMA4.setPen(pen)
        self.chart.addSeries(seriesMA4)
        seriesMA4.attachAxis(self.__axisX)
        seriesMA4.attachAxis(self.__axisY)

        ## 3. 填充数据到序列
        dataRowCount = self.itemModel.rowCount()  #数据点个数
        for i in range(dataRowCount):
            dateStr = self.itemModel.item(i, 0).text()  #日期字符串,如"2017/02/03"
            dateValue = QDate.fromString(dateStr, "yyyy/MM/dd")  #QDate
            dtValue = QDateTime(dateValue)  #日期时间 QDateTime
            timeStamp = dtValue.toMSecsSinceEpoch()  #毫秒数

            oneCandle = QCandlestickSet()  #QCandlestickSet
            oneCandle.setOpen(float(self.itemModel.item(i, 1).text()))  #开盘
            oneCandle.setHigh(float(self.itemModel.item(i, 2).text()))  #最高
            oneCandle.setLow(float(self.itemModel.item(i, 3).text()))  #最低
            oneCandle.setClose(float(self.itemModel.item(i, 4).text()))  #收盘
            oneCandle.setTimestamp(timeStamp)  #时间戳
            seriesCandle.append(oneCandle)  #添加到序列

            M1 = float(self.itemModel.item(i, 5).text())
            M2 = float(self.itemModel.item(i, 6).text())
            M3 = float(self.itemModel.item(i, 7).text())
            M4 = float(self.itemModel.item(i, 8).text())

            seriesMA1.append(timeStamp, M1)
            seriesMA2.append(timeStamp, M2)
            seriesMA3.append(timeStamp, M3)
            seriesMA4.append(timeStamp, M4)

    ## 4. 设置坐标轴范围
        minDateStr = self.itemModel.item(0, 0).text()  #日期字符串,如"2017/02/03"
        minDate = QDate.fromString(minDateStr, "yyyy/MM/dd")  #QDate
        minDateTime = QDateTime(minDate)  #最小日期时间,QDateTime

        maxDateStr = self.itemModel.item(dataRowCount - 1,
                                         0).text()  #日期字符串,如"2017/05/03"
        maxDate = QDate.fromString(maxDateStr, "yyyy/MM/dd")
        maxDateTime = QDateTime(maxDate)  #最大日期时间

        self.__axisX.setRange(minDateTime, maxDateTime)  #日期时间范围
        dateFormat = self.ui.comboDateFormat.currentText()  #格式,如"MM-dd"
        self.__axisX.setFormat(dateFormat)  #标签格式

        self.__axisY.applyNiceNumbers()  #自动

        for marker in self.chart.legend().markers():  #QLegendMarker类型列表
            marker.clicked.connect(self.do_LegendMarkerClicked)