Categories
Development

Pyqtgraph candlestick

I used this code: https://github.com/pyqtgraph/pyqtgraph/blob/develop/examples/customGraphicsItem.py

I replaced data with a file.csv
The lines are written this way:

2019-11-07 21:40:00+00:00,9190.5,36024221.0,9200.0,3922.7451237799987,1067.0,9169.0,9180.5,XBTUSD,6202,392274512378,36024221,9183.5798
timestamp,close,foreignNotional,high,homeNotional,lastSize,low,open,symbol,trades,turnover,volume,vwap

I would like to know how to get the date on the x-axis.
For the moment I use:

self.x = [i for i in range (len(self.open))]

and w = 1/3

Exemple :

import sys
from PyQt5.QtWidgets import QApplication,QWidget,QHBoxLayout
from pyqtgraph import PlotWidget

app = QApplication(sys.argv)
date_time_str = ['2019-11-08 22:55:00+00:00','2019-11-08 23:00:00+00:00','2019-11-08 23:05:00+00:00']

a = QWidget()
a.setGeometry(500,500,500,500)
graphicsView = PlotWidget(a)

layout = QHBoxLayout()
layout.addWidget(graphicsView)
a.setLayout(layout)

a.show()
app.exec_()

So I would like to have the dates on the x-axis, I’ve seen a lot of examples except that I can’t do it.
Sorry for my bad english…

Edit again, My code :


import sys
from PyQt5.QtWidgets import *
import pyqtgraph as pg
import pandas as pd
from pyqtgraph import QtCore, QtGui

class Graph(pg.GraphicsObject):
    def __init__(self, data):
        pg.GraphicsObject.__init__(self)
        self.data = data
        self.time = self.data.iloc[:,0]
        self.symbol = self.data.iloc[:,8]
        self.open = self.data.iloc[:,7]
        self.high = self.data.iloc[:,3]
        self.low = self.data.iloc[:,6]
        self.close = self.data.iloc[:,1]
        self.x = [i for i in range (len(self.open))]
        self.Graph()

    def Graph(self):
        self.picture = QtGui.QPicture()
        p = QtGui.QPainter(self.picture)

        w = 1/3
        for i in range (len(self.open)):

            if self.open[i] > self.close[i]:
                p.setPen(pg.mkPen('#D56161'))
                p.setBrush(pg.mkBrush('#E8704F'))
            else:
                p.setPen(pg.mkPen('#388D53'))
                p.setBrush(pg.mkBrush('#52B370'))

            p.drawLine(QtCore.QPointF(self.x[i],self.low[i]), QtCore.QPointF(self.x[i], self.high[i]))
            p.drawRect(QtCore.QRectF(self.x[i] - w, self.open[i], w * 2,self.close[i] - self.open[i]))

        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__':
    app = QApplication(sys.argv)
    data = pd.read_csv('data.csv',header=1)
    item = Graph(data)
    plt = pg.plot()
    plt.addItem(item)
    app.exec()

csv exemple :
timestamp,close,foreignNotional,high,homeNotional,lastSize,low,open,symbol,trades,turnover,volume,vwap
2019-10-20 17:00:00+00:00,8078.5,154841879.0,8088.0,19223.600399969982,23842.0,8017.5,8028.0,XBTUSD,41817,1922360039997,154841879,8055.4213
2019-10-20 18:00:00+00:00,8144.5,280217689.0,8182.0,34504.91333102001,130.0,8053.0,8078.5,XBTUSD,61279,3450491333102,280217689,8121.4976
2019-10-20 19:00:00+00:00,8147.5,86195447.0,8179.5,10566.52104377998,987.0,8142.0,8144.5,XBTUSD,25756,1056652104378,86195447,8157.9377
2019-10-20 20:00:00+00:00,8243.5,198237577.0,8245.0,24146.92298704002,2500.0,8147.5,8147.5,XBTUSD,46908,2414692298704,198237577,8210.1806
2019-10-20 21:00:00+00:00,8227.0,254994382.0,8318.0,30868.515543060086,1500.0,8223.0,8243.5,XBTUSD,55899,3086851554306,254994382,8261.0492
2019-10-20 22:00:00+00:00,8209.0,91400801.0,8240.0,11122.650644769996,500.0,8201.0,8227.0,XBTUSD,25873,1112265064477,91400801,8217.6021
2019-10-20 23:00:00+00:00,8209.5,39426905.0,8238.0,4795.436614659997,15000.0,8204.0,8209.0,XBTUSD,14218,479543661466,39426905,8222.3319
2019-10-21 00:00:00+00:00,8233.0,53840147.0,8233.5,6560.546599359996,4353.0,8189.0,8209.5,XBTUSD,18495,656054659936,53840147,8206.8117
2019-10-21 01:00:00+00:00,8218.5,52397863.0,8244.5,6367.683420369996,713.0,8218.0,8233.0,XBTUSD,16345,636768342037,52397863,8229.0981
2019-10-21 02:00:00+00:00,8234.5,43951039.0,8235.0,5348.280126209993,827.0,8205.0,8218.5,XBTUSD,14458,534828012621,43951039,8218.2774
2019-10-21 03:00:00+00:00,8251.5,82423423.0,8265.0,9993.37239766001,210.0,8226.0,8234.5,XBTUSD,21817,999337239766,82423423,8248.1029
2019-10-21 04:00:00+00:00,8233.5,44887749.0,8254.0,5452.105217430002,2000.0,8222.5,8251.5,XBTUSD,12642,545210521743,44887749,8233.1632
2019-10-21 05:00:00+00:00,8229.5,26846500.0,8242.5,3261.311743019997,18.0,8222.5,8233.5,XBTUSD,9361,326131174302,26846500,8232.4854
2019-10-21 06:00:00+00:00,8240.5,32483541.0,8250.0,3942.694545749997,10.0,8223.0,8229.5,XBTUSD,11307,394269454575,32483541,8239.2684
2019-10-21 07:00:00+00:00,8236.0,45066767.0,8259.5,5463.278301509992,88.0,8235.5,8240.5,XBTUSD,13719,546327830151,45066767,8249.4638
2019-10-21 08:00:00+00:00,8227.0,56438775.0,8242.5,6860.100145159987,945.0,8215.5,8236.0,XBTUSD,17919,686010014516,56438775,8227.744
2019-10-21 09:00:00+00:00,8228.0,38635736.0,8229.5,4701.095349329996,1074.0,8207.5,8227.0,XBTUSD,13970,470109534933,38635736,8218.9529
2019-10-21 10:00:00+00:00,8206.5,60328052.0,8228.0,7348.453413279994,7100.0,8196.5,8228.0,XBTUSD,16040,734845341328,60328052,8210.1806
2019-10-21 11:00:00+00:00,8279.5,127010213.0,8286.0,15389.35053766001,20000.0,8206.5,8206.5,XBTUSD,33152,1538935053766,127010213,8253.549
2019-10-21 12:00:00+00:00,8236.5,116027090.0,8287.0,14060.464374579973,24732.0,8225.0,8279.5,XBTUSD,29582,1406046437458,116027090,8252.1868
2019-10-21 13:00:00+00:00,8234.0,47412057.0,8266.5,5748.923697889997,140.0,8233.5,8236.5,XBTUSD,16910,574892369789,47412057,8247.4227
2019-10-21 14:00:00+00:00,8245.0,40690172.0,8248.5,4941.487864969995,11112.0,8225.5,8234.0,XBTUSD,14657,494148786497,40690172,8234.5191
2019-10-21 15:00:00+00:00,8262.5,39416238.0,8262.5,4778.849582899994,4274.0,8237.5,8245.0,XBTUSD,13939,477884958290,39416238,8248.1029
2019-10-21 16:00:00+00:00,8196.0,415288710.0,8357.0,50402.029403120236,1000.0,8172.5,8262.5,XBTUSD,83422,5040202940312,415288710,8239.9473
2019-10-21 17:00:00+00:00,8182.5,94496569.0,8199.0,11554.150982099984,10.0,8163.5,8196.0,XBTUSD,26236,1155415098210,94496569,8178.6211
2019-10-21 18:00:00+00:00,8193.5,29341964.0,8193.5,3583.5984401699993,476.0,8179.0,8182.5,XBTUSD,9353,358359844017,29341964,8187.9964
2019-10-21 19:00:00+00:00,8172.5,56485931.0,8196.0,6911.166630270001,2222.0,8155.5,8193.5,XBTUSD,17155,691116663027,56485931,8173.2734
2019-10-21 20:00:00+00:00,8197.5,33753968.0,8197.5,4125.745949229999,50.0,8165.0,8172.5,XBTUSD,12119,412574594923,33753968,8181.9669
2019-10-21 21:00:00+00:00,8199.5,35461120.0,8207.5,4322.615503060002,1.0,8197.0,8197.5,XBTUSD,10668,432261550306,35461120,8204.1185
2019-10-21 22:00:00+00:00,8211.0,41307035.0,8227.5,5028.335066819987,1000.0,8199.5,8199.5,XBTUSD,18528,502833506682,41307035,8214.9018
2019-10-21 23:00:00+00:00,8195.5,21374572.0,8217.5,2606.443551740002,59.0,8194.0,8211.0,XBTUSD,8995,260644355174,21374572,8200.7545
2019-10-22 00:00:00+00:00,8204.5,33884077.0,8218.0,4129.356671389998,400.0,8195.0,8195.5,XBTUSD,11905,412935667139,33884077,8206.1382
2019-10-22 01:00:00+00:00,8190.5,43297309.0,8207.5,5284.066043759995,1000.0,8182.0,8204.5,XBTUSD,15671,528406604376,43297309,8194.0347
2019-10-22 02:00:00+00:00,8175.5,46881832.0,8208.5,5723.464042740003,10945.0,8175.0,8190.5,XBTUSD,14587,572346404274,46881832,8191.3499
2019-10-22 03:00:00+00:00,8196.0,32678079.0,8202.5,3990.02842927,1.0,8174.0,8175.5,XBTUSD,11220,399002842927,32678079,8190.0082
2019-10-22 04:00:00+00:00,8212.0,27897571.0,8215.5,3399.848777840005,1500.0,8192.0,8196.0,XBTUSD,9866,339984877784,27897571,8206.1382
2019-10-22 05:00:00+00:00,8227.0,44943254.0,8230.0,5470.7486356099935,41.0,8201.5,8212.0,XBTUSD,13486,547074863561,44943254,8215.5767
2019-10-22 06:00:00+00:00,8219.0,26888207.0,8229.0,3272.2463457699955,3712.0,8207.0,8227.0,XBTUSD,8369,327224634577,26888207,8217.6021
2019-10-22 07:00:00+00:00,8212.5,23508627.0,8219.0,2863.559965620001,2000.0,8203.0,8219.0,XBTUSD,8185,286355996562,23508627,8210.1806
2019-10-22 08:00:00+00:00,8224.0,62093345.0,8245.0,7547.823796970001,100.0,8203.5,8212.5,XBTUSD,16716,754782379697,62093345,8227.0671
2019-10-22 09:00:00+00:00,8266.0,82258138.0,8273.0,9970.321142679997,62.0,8223.5,8224.0,XBTUSD,22487,997032114268,82258138,8250.8251
2019-10-22 10:00:00+00:00,8284.0,108785007.0,8291.0,13153.806958030007,3749.0,8252.0,8266.0,XBTUSD,27933,1315380695803,108785007,8270.6145
2019-10-22 11:00:00+00:00,8250.5,118088939.0,8298.0,14293.879427429973,2000.0,8235.0,8284.0,XBTUSD,28673,1429387942743,118088939,8261.7317
2019-10-22 12:00:00+00:00,8288.0,80818461.0,8290.0,9776.564437229998,7460.0,8241.5,8250.5,XBTUSD,24332,977656443723,80818461,8267.1958
2019-10-22 13:00:00+00:00,8250.5,133723299.0,8313.0,16162.358747389975,1500.0,8245.5,8288.0,XBTUSD,36637,1616235874739,133723299,8274.0361
2019-10-22 14:00:00+00:00,8230.0,115914128.0,8257.0,14089.296940339973,1.0,8203.0,8250.5,XBTUSD,29893,1408929694034,115914128,8227.744
2019-10-22 15:00:00+00:00,8203.0,91839526.0,8236.0,11176.665736619992,2000.0,8193.0,8230.0,XBTUSD,23429,1117666573662,91839526,8217.6021
2019-10-22 16:00:00+00:00,8215.5,112462861.0,8216.0,13724.355157599992,2650.0,8168.0,8203.0,XBTUSD,27337,1372435515760,112462861,8194.7062
2019-10-22 17:00:00+00:00,8205.5,51735312.0,8222.5,6300.16049759,50.0,8193.5,8215.5,XBTUSD,16942,630016049759,51735312,8212.2033
2019-10-22 18:00:00+00:00,8213.0,29559888.0,8213.0,3603.124125960001,5278.0,8196.5,8205.5,XBTUSD,11624,360312412596,29559888,8204.1185
2019-10-22 19:00:00+00:00,8198.0,31009104.0,8213.0,3782.9535256699987,500.0,8188.0,8213.0,XBTUSD,12438,378295352567,31009104,8197.3932
2019-10-22 20:00:00+00:00,8167.0,57606288.0,8198.0,7046.476551900002,108.0,8156.0,8198.0,XBTUSD,18916,704647655190,57606288,8175.278
2019-10-22 21:00:00+00:00,8118.5,292199866.0,8167.0,36027.308415119966,26724.0,8077.0,8167.0,XBTUSD,58206,3602730841512,292199866,8110.9579
2019-10-22 22:00:00+00:00,8093.0,35959006.0,8119.0,4436.190867529989,6343.0,8093.0,8118.5,XBTUSD,12623,443619086753,35959006,8106.3554
2019-10-22 23:00:00+00:00,8073.5,74258877.0,8106.5,9187.56941494,30560.0,8052.0,8093.0,XBTUSD,22645,918756941494,74258877,8082.7675
2019-10-23 00:00:00+00:00,8019.0,218903941.0,8079.5,27269.048647549967,8536.0,7981.5,8073.5,XBTUSD,46903,2726904864755,218903941,8027.615
2019-10-23 01:00:00+00:00,8028.5,73236636.0,8038.0,9133.41465087998,1.0,7999.0,8019.0,XBTUSD,17964,913341465088,73236636,8018.6032
2019-10-23 02:00:00+00:00,8033.0,35820355.0,8044.5,4457.459950769997,50.0,8028.0,8028.5,XBTUSD,9893,445745995077,35820355,8036.6471
2019-10-23 03:00:00+00:00,8035.0,34241628.0,8037.0,4264.124695339997,25756.0,8022.5,8033.0,XBTUSD,10196,426412469534,34241628,8030.1935
2019-10-23 04:00:00+00:00,7951.0,185126726.0,8035.5,23228.54440930001,5000.0,7920.0,8035.0,XBTUSD,37348,2322854440930,185126726,7970.0327
2019-10-23 05:00:00+00:00,7964.5,88669128.0,7970.5,11154.902925860013,300.0,7910.0,7951.0,XBTUSD,20593,1115490292586,88669128,7949.1256
2019-10-23 06:00:00+00:00,7950.0,65320662.0,7964.5,8222.575866200013,3000.0,7916.0,7964.5,XBTUSD,19345,822257586620,65320662,7944.0737
2019-10-23 07:00:00+00:00,7961.5,45262709.0,7976.0,5686.676923120002,1000.0,7937.5,7950.0,XBTUSD,14224,568667692312,45262709,7959.8822
2019-10-23 08:00:00+00:00,7956.0,43216387.0,7977.0,5424.929821600003,625.0,7956.0,7961.5,XBTUSD,11337,542492982160,43216387,7966.8579
2019-10-23 09:00:00+00:00,7972.0,49593823.0,7983.5,6221.086694860001,29.0,7953.0,7956.0,XBTUSD,15226,622108669486,49593823,7971.9388
2019-10-23 10:00:00+00:00,7987.5,43815416.0,7994.5,5488.1117564000015,1572.0,7969.5,7972.0,XBTUSD,13132,548811175640,43815416,7984.0319
2019-10-23 11:00:00+00:00,8000.0,51382222.0,8008.0,6423.6102146800085,5000.0,7987.0,7987.5,XBTUSD,15658,642361021468,51382222,7999.3601
2019-10-23 12:00:00+00:00,7966.0,74114611.0,8004.5,9290.533766550016,29000.0,7955.0,8000.0,XBTUSD,20079,929053376655,74114611,7977.6625
2019-10-23 13:00:00+00:00,7524.0,850368746.0,7966.5,110994.59022028961,3000.0,7407.0,7966.0,XBTUSD,117329,11099459022029,850368746,7661.661
2019-10-23 14:00:00+00:00,7472.0,500710490.0,7560.0,66937.07818962997,360.0,7392.0,7524.0,XBTUSD,93358,6693707818963,500710490,7480.5506
2019-10-23 15:00:00+00:00,7463.0,135855271.0,7490.0,18185.118651749963,2000.0,7441.0,7472.0,XBTUSD,37298,1818511865175,135855271,7471.0497
2019-10-23 16:00:00+00:00,7423.5,142065689.0,7475.0,19070.137261829976,1500.0,7411.0,7463.0,XBTUSD,36121,1907013726183,142065689,7449.8994
2019-10-23 17:00:00+00:00,7464.5,360235223.0,7485.0,48635.222300109934,1.0,7298.0,7423.5,XBTUSD,66629,4863522230011,360235223,7407.4074
2019-10-23 18:00:00+00:00,7491.5,55854491.0,7500.0,7473.204260169998,1000.0,7454.0,7464.5,XBTUSD,18783,747320426017,55854491,7474.4002
2019-10-23 19:00:00+00:00,7502.0,77019968.0,7514.5,10269.538074200018,300.0,7484.0,7491.5,XBTUSD,20939,1026953807420,77019968,7500.1875
2019-10-23 20:00:00+00:00,7482.0,54873067.0,7511.5,7324.076460700008,6510.0,7479.0,7502.0,XBTUSD,15552,732407646070,54873067,7492.3204
2019-10-23 21:00:00+00:00,7482.5,43139021.0,7491.0,5774.726628589998,400.0,7461.5,7482.0,XBTUSD,12885,577472662859,43139021,7470.4916






Leave a Reply

Your email address will not be published. Required fields are marked *