Skip to content

[Question not an issue:] Run finplot without blocking main thread #55

@dhinesh03

Description

@dhinesh03

First of all thanks for your awesome library. I'm newbie to python, I need to run finplot without blocking main thread.
I have the following class, everything works as expected, but it blocks my main thread, i've lot of logics on main thread. it manages multiple background thread to collect data from multiple data sources.

`
import pandas as pd
import finplot as fplt

ax = fplt.create_plot('TradingView')

class PlotScheme(object):

def __init__(self, strategy):
    self.plots = []
    self.strategy = strategy
    self.candle = self.strategy.candle

def update(self):
    candlesticks = pd.DataFrame(columns=['datetime', 'open', 'close', 'high', 'low'])
    candlesticks['datetime'] = self.candle.datetime
    candlesticks['open'] = self.candle.open
    candlesticks['close'] = self.candle.close
    candlesticks['high'] = self.candle.high
    candlesticks['low'] = self.candle.low

    indicators = pd.DataFrame(columns=['datetime', 'SPH', 'SPL', 'LPH', 'LPL', 'Average', 'Average_long'])
    indicators['datetime'] = self.candle.datetime
    indicators['SPH'] = self.strategy.spm.lines.SPH
    indicators['SPL'] = self.strategy.spm.lines.SPL
    indicators['LPH'] = self.strategy.spm.lines.LPH
    indicators['LPL'] = self.strategy.spm.lines.LPL
    indicators['Average'] = self.strategy.bar_size.lines.av
    indicators['Average_long'] = self.strategy.large_bar_size.lines.av

    LPL_indicator = indicators['datetime LPL'.split()]
    SPL_indicator = indicators['datetime SPL'.split()]
    LPH_indicator = indicators['datetime LPH'.split()]
    SPH_indicator = indicators['datetime SPH'.split()]

    if not self.plots:
        # first time we create the plots
        self.plots.append(fplt.candlestick_ochl(candlesticks, ax=ax))
        self.plots.append(fplt.plot(LPH_indicator, ax=ax, color='#105900', width=2, legend='LPH'))
        self.plots.append(fplt.plot(SPH_indicator, ax=ax, color='#1a8c00', width=1, legend='SPH'))
        self.plots.append(fplt.plot(SPL_indicator, ax=ax, color='#FF5733', width=1, legend='SPL'))
        self.plots.append(fplt.plot(LPL_indicator, ax=ax, color='#900C3F', width=2, legend='LPL'))
    else:
        self.plots[0].update_data(candlesticks)
        self.plots[1].update_data(LPH_indicator)
        self.plots[1].update_data(SPH_indicator)
        self.plots[1].update_data(SPL_indicator)
        self.plots[1].update_data(LPL_indicator)

def plot(self):
    self.update()
    fplt.timer_callback(self.update, 2.0)  # update every N seconds
    fplt.show()

`

please guide me to show the fplt without blocking main thread. i've tried using thread but its not working.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions