highfestiva / finplot

Performant and effortless finance plotting for Python
MIT License
907 stars 186 forks source link

Two question about finplot update plotting #391

Closed sunjar2020 closed 1 year ago

sunjar2020 commented 1 year ago

I've recently been writing a program that displays stock prices in turn using the finplot library, and now it basically works, it reads all the filenames from the data directory and calls finplot in a loop to display candlestick and volume and stuff. But there are still two problems:

  1. When the program starts to display the first stock, if I close the current window, it will display the next one, which is correct. But when I use the "key_pressed(vb, ev)" method, I want the program will update dataframe content when I press "right arrow" key, it doesn't work, because I don't know how to pass the parameter into the "key_pressed" function, if I write like this: fplt.timer_callback(update(code)) It will prompt me that "code" is undefined.

  2. At present, the program can only display one main chart(candlestick). If I want to add more sub-charts, such as ax2, ax3, how should I add codes? I commented out two lines of code, and they don't work if I uncomment them, because I don't know the correct format to write them. If I add ax = ax2 directly after the commented code, it will prompt me that ax2 is not defined. Even if I add "global ax2" to the update function, it won't work. I refer to the example of bfx.py in the sample folder, but I can't find the corresponding method?

Could you take a look at these two issues when you're not busy? thank you very much.

here is all file( including code and sample datas): https://xfl.jp/5mYPAb

Here is my code:

import os
import pandas as pd
import finplot as fplt

path = 'l:\\temp\\'
stock_list = []

for filename in os.listdir(path):
        stock_list.append(filename)

def _key_pressed(vb, ev):
        if ev.key() == fplt.QtCore.Qt.Key_Right:
                fplt.timer_callback(update(code), 0, single_shot=True) #Press "Right arrow key to update plotting
        elif ev.key() == fplt.QtCore.Qt.Key_Down: #Press "Down" arrow key to quit program
                quit()

def update(code):
    df = pd.read_csv(path + code)
    df['Date'] = pd.to_datetime(df['Date'])
    df = df.set_index('Date')
    print(code)

    if not plots:
        plots.append(fplt.candlestick_ochl(df[['Open', 'Close', 'High', 'Low']]))
        #plots.append(fplt.volume_ocv(df[['Open','Close','Volume']], ax = ax2)) #subplot 2, second axes
    else:
        plots[0].update_data(df[['Open', 'Close', 'High', 'Low']])
        #plots[1].update_data(df[['Open','Close','Volume']])

for stock in stock_list:
    ax = fplt.create_plot('Finplot update program test', init_zoom_periods=100, maximize=True)
    plots = []
    update(stock)
    fplt._key_pressed = _key_pressed
    fplt.show()
highfestiva commented 1 year ago

Those are generic programming questions that are too broad to answer here (has nothing to do with finplot itself).

sunjar2020 commented 1 year ago

Sorry to bother you with some basic problems, I have solved these two problems after two hours of hard work. But could you please answer another little question? I think it has something with finplot.

That is, how to update the legend? My code is as follows. I don't know how to update the legend on subplot, the stock_list[i][0:6] is string type, it has changed over the course of the program. In my code's two way(Commented out and uncommented), the legend not update.

if not plots:
        plots.append(fplt.candlestick_ochl(df[['Open', 'Close', 'High', 'Low']]))
        plots.append(fplt.volume_ocv(df[['Open','Close','Volume']], ax = ax2)) #subplot 2, second axes
        plots.append(fplt.plot(df['Flag'], color='#000000', ax = ax3, legend = 'StockCode '+ stock_list[i][0:6]))
        #plots.append(fplt.add_legend(stock_list[i][0:6], ax = ax3))

else:
        plots[0].update_data(df[['Open', 'Close', 'High', 'Low']])
        plots[1].update_data(df[['Open','Close','Volume']])
        plots[2].update_data(df[['Open','Close','Flag']])
        #plots[2].update_date(fplt.add_legend(stock_list[i][0:6]))
highfestiva commented 1 year ago

See examples/snp500.py.