2

I am trying to create a plotyly chart with some subplots based on Use button to filter different data in plotly python The chart generation function takes as input a stock symbol, dict of periods (m1, m3, m5... for different minutes) and stock specific period dataframes.

I am trying the put the periods as buttons, so that on a period button click, the corresponding dataframe (OHLC) can be loaded along with period dependent indicators - MACD, RSI and ADX.

The issue is only the last period df is loaded and the buttons are not showing/ loading the period specific OHLCV.

Below is the function

def plot_plotly_v3(in_stock, in_period_stock_mdf_df_dict,n):
    f_stock = in_stock
    f_period_stock_mdf_df_dict = in_period_stock_mdf_df_dict
    period_buttons = []
    i = 0
    period_length = len(f_period_stock_mdf_df_dict) # to calculate visible args
    period_frequency_dict = config.c_g_period_python_freq_dict # broker period to python period
    for period, stock_period_df in f_period_stock_mdf_df_dict.items():
        stock_period_df.index = stock_period_df.index.droplevel([1, 2])
        fig = make_subplots(rows=4, cols=1, shared_xaxes=True, vertical_spacing=0.007, row_heights=[.35, .20, .20, .25],
                            subplot_titles=('', 'MACD', 'RSI', 'ADX'))
        # removing all empty dates and build complete timeline from start date to end date
        py_frequency = period_frequency_dict.get(period) # broker period to python period mapping
        dt_all = pd.date_range(start=stock_period_df.index[0], end=stock_period_df.index[-1], freq=py_frequency)

        # retrieve the dates that ARE in the original datset
        dt_obs = [d.strftime("%Y-%m-%d %H:%M:%S") for d in pd.to_datetime(stock_period_df.index)]

        # define dates with missing values
        dt_breaks = [d for d in dt_all.strftime("%Y-%m-%d %H:%M:%S").tolist() if not d in dt_obs]
        in_period_int = int(config.g_period_2_period_int_dict.get(period))
        dvalue_ms = in_period_int * 60 * 1000
        fig.update_xaxes(rangebreaks=[dict(values=dt_breaks, dvalue=dvalue_ms)])
        fig_title = in_stock + ' for period ' + period + ' for range ' + 'From: ' + \
                    str(stock_period_df.index[0]) + ' To: ' + str(stock_period_df.index[-1])
        for annotation in fig['layout']['annotations']:
            annotation['textangle'] = -90
        fig.update_annotations(xshift=-620, yshift=-100)

        # Plot OHLC and MAs on 1st subplot    
        # Plot MACD trace on 2nd row    
        # Plot RSI trace on 3rd row
        # Plot ADX trace on 4th row
        # create visible args - True/ False list depending upon period/df dict
        visible_args = create_true_false_list_v0(i, period_length)
        # create a button object for the period we are on
        button = dict(label=period, method="update", args=[{"visible": visible_args}])

        # add the button to our list of buttons
        period_buttons.append(button)

        # i is an iterable used to tell our "args" list which value to set to True
        i += 1
    fig.update_layout(updatemenus=[dict(type="buttons",direction="right", x = 1,y = 1,buttons = period_buttons)],
                      height=800, width=1350, title=fig_title, xaxis_rangeslider_visible=False)
    fig.show()

plotly chart

Would appreciate any support/ guidance.

Question: 1] Is it possible to display the title in the same level as toolbar/modebar. 2] Is it possible to display the period buttons in the same level as the toolbar.

3
  • If it is the last two questions, I can answer them quickly. The first is that you can set the yref to the container to make it the same level as the toolbar, fig.update_layout(title=dict(text="Title",yref='container',y=1.0,)) and the second is that the button should be positioned to the maximum extent possible, right below the toolbar. updatemenus=[dict(...,y=1.6,...) For the main question, I think it's better to simplify the code so that more people can see it and have a better chance of getting an answer. Commented Feb 8, 2022 at 8:07
  • Thanks @r-beginners. I have simplified the code. The title display is now happening in the same level as the toolbar. However, for the buttons, I need to display them as the same level as the toolbar after the title and not below the toolbar. Commented Feb 8, 2022 at 9:03
  • The position of the button can only be moved to the baseline of the toolbar. Try editing my comment in the example in the official reference. Commented Feb 8, 2022 at 9:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.