highfestiva / finplot

Performant and effortless finance plotting for Python
MIT License
926 stars 187 forks source link

Loosing colors of bars after zoom in #478

Open Protomolekule opened 10 months ago

Protomolekule commented 10 months ago

I use the code below which shows candles and volume. Red and green candles are not displayed correctly. I changed colors and some other things. No luck at all.

Runing code:

import yfinance
import finplot as fplt
from PyQt6.QtWidgets import QApplication, QVBoxLayout, QWidget, QDialog, QLabel, QPushButton
from PyQt6.QtCore import Qt
import sys

class CustomWidget(QWidget):
    def __init__(self, df):
        super().__init__()

        # Create a layout
        layout = QVBoxLayout(self)

        # Create two plots. Second plot is an "overlay" of the first plot
        self.ax = fplt.create_plot('AAPL', init_zoom_periods=100)
        self.ax2 = self.ax.overlay()

        # Create candlestick plot on first plot
        self.candle_plot = fplt.candlestick_ochl(df[['Open', 'High', 'Low', 'Close']], ax=self.ax)

        # doesnt solve the problem with candle color and zoom
        #self.candle_plot.colors.update (dict (bull_body='#0a0', bull_shadow='#100', bull_frame='#200',
        #                       bear_body='#a00', bear_shadow='#300', bear_frame='#400'))

        # Create volume plot on second plot
        self.volume_plot = fplt.volume_ocv(df[['Open', 'Close', 'Volume']], ax=self.ax2)

        # Add plots to the layout
        layout.addWidget(self.ax.vb.win)
        layout.addWidget(self.ax2.vb.win)

    def update_data(self, df):
        # Update the data for the plots
        self.candle_plot.update_data(df[['Open', 'High', 'Low', 'Close']])
        self.volume_plot.update_data(df[['Open', 'Close', 'Volume']])

class MyDialog(QDialog):
   def __init__(self):
       super().__init__()

       df = yfinance.download ('AAPL')
       # Create an instance of the widget
       self.ftplt = CustomWidget(df)

       # Create a vertical layout
       layout = QVBoxLayout()

       layout.addWidget(QLabel("Here comes the widget"))

       # Add the widget to the layout
       layout.addWidget(self.ftplt)

       layout.addWidget(QLabel("End of the widget"))

       button = QPushButton ("new Data")
       button.clicked.connect (self.loaddata)
       layout.addWidget (button)

       self.setLayout(layout)

   def loaddata(self):
       df = yfinance.download ('ADBE')

       # Update the plots with the new data
       self.ftplt.update_data(df)
       self.ftplt.update()

def main():
    app = QApplication(sys.argv)
    widget = MyDialog()
    widget.show()
    sys.exit(app.exec())

if __name__ == "__main__":
    main()

Full data:

grafik

Zoomed data:

grafik

Same issue if I run the widget allone:

def main():
    app = QApplication(sys.argv)
    #widget = MyDialog()
    df = yfinance.download ('AAPL')
    widget = CustomWidget(df)
    fplt.show(qt_exec=False)
    widget.show()
    sys.exit(app.exec())

How can I solve this behavior? thxia

highfestiva commented 9 months ago

You're plotting the columns in the wrong order, finplot uses OCHL, not OHLC. So if you plot df[['Open', 'Close', 'High', 'Low']] instead, it works.

The reason it looks "ok" when zoomed out is that finplot resamples your data. To disable resampling, do this:

        self.candle_plot.resamp = None
        self.volume_plot.resamp = None