2

I have three separated datasets of stocks/indexes. I can plot them one by one with PlotLy. But I want to create a button or dropdown menu that will allow me to switch the datasets from it.

Every dataset looks like this:

            open    high    low     close   volume  
Date                            
2015-08-06  5146.63 5149.93 5035.41 5056.44 2290950000  
2006-08-16  2127.06 2149.54 2120.11 2149.54 2474760000  
1995-06-19  909.90  922.09  909.84  922.09  407000000   
2009-10-19  2162.41 2180.11 2150.42 2176.32 1970440000  
1997-05-23  1377.73 1389.75 1377.73 1389.72 539440000   

Below is my code. But I don't know where to put the three datasets that will substitute "df". I was thinking of something like a list of datasets maybe but I am not used to working with python. I would appreciate some help.

I want the button or the drop-down menu to change between df = [msft_data, spy_500, nasdaq]

def plot_interactive_stock_data(title):

fig = go.Figure()

fig_high = go.Scatter(x=df.index, y=df['high'], name="high ($)",
                     line_color='deepskyblue')

fig_low = go.Scatter(x=df.index, y=df['low'], name="low ($)",
                     line_color='green')

fig_open = go.Scatter(x=df.index, y=df['open'], name="open ($)",
                     line_color='maroon')

fig_close - go.Scatter(x=df.index, y=df['close'], name="close ($)",
                     line_color='orange')

fig_volume = go.Scatter(x=df.index, y=df['volume'], name="volume",
                     line_color='brown')

fig.update_layout(title_text='{}'.format(title),
              xaxis_rangeslider_visible=True)


data = [fig_high, fig_low, fig_open, fig_close, fig_volume]

updatemenus = list([
    dict(active = -1,
         buttons = list([
             dict(label = 'Microsoft',
                 method = 'update',
                 args = {'visible':  [True, False, False]}),

             dict(label = 'S&P 500',
                 method = 'update',
                 args = {'visible':  [False, True, False]}),

             dict(label = 'Nasdaq',
                 method = 'update',
                 args = {'visible':  [False, False, True]})
        ]),
    )
])

layout = dict(title=title, showlegend = False,
              updatemenus = updatemenus)

fig = dict(data=data, layout=layout)

plotly.offline.plot(fig, auto_open=False, show_link=False)
5
  • Please consider providing a data sample. The indentation of your code also does not look right since you're starting with def plot_interactive_stock_data(title): Commented Nov 15, 2019 at 9:58
  • I have added a sample of one of the datasets. The others look the same. I am sure the code is not good. I was looking at some examples and just cannot figure out how to work with PlotLy. I just want to be able to switch between the datasets and at the same time to switch between the columns, so I can plot each column separately. Commented Nov 15, 2019 at 13:21
  • I'll take a look over the weekend. Commented Nov 15, 2019 at 13:50
  • 1
    Thank you. Meanwhile, I have found a solution. It may not be the best, but it is still a solution. You can check it out in the answer below. Commented Nov 15, 2019 at 14:48
  • @ If you'd like to show one column then this is fairly eeasy. Given that you have OHLCV in your sample, I thought you wanted to show different candlestick with volumes for different stocks... Commented Nov 15, 2019 at 14:51

1 Answer 1

2

I have found an answer which is not perfect but still, the code does what I wanted it to do. I have used information from plotly and from stackoverflow. Switching between datasets is very slow though. So I would appreciate it if somebody can show me a better solution.

Below is the code for the plot function:

def plot_data(df):    
fig = go.Figure()
fig.add_trace(go.Scatter(
                x=df.index,
                y=df['open'],
                name="Open",
                line_color='blueviolet',
                opacity=0.8))

fig.add_trace(go.Scatter(
                x=df.index,
                y=df['high'],
                name="High",
                line_color='green',
                opacity=0.8))

fig.add_trace(go.Scatter(
                x=df.index,
                y=df['low'],
                name="Low",
                line_color='red',
                opacity=0.8))

fig.add_trace(go.Scatter(
                x=df.index,
                y=df['close'],
                name="Close",
                line_color='darkkhaki',
                opacity=0.8))

fig.add_trace(go.Scatter(
                x=df.index,
                y=df['volume'],
                name="Volume",
                line_color='darkgoldenrod',
                opacity=0.8))

fig.add_trace(go.Scatter(
                x=df.index,
                y=df['dividends'],
                name="Dividends",
                line_color='brown',
                opacity=0.8))

fig.add_trace(go.Scatter(
                x=df.index,
                y=df['stock_splits'],
                name="Stock splits",
                line_color='brown',
                opacity=0.8))

fig.update_layout(title_text='Explore data',
              xaxis_rangeslider_visible=True)   
fig.show()

This is the code for the drop-down menu:

@interact(control=widgets.Dropdown(
options=["msft_data",
         "spy_500",
         "nasdaq"
         ],
description='Datasets'))

def plot_df(control):
    plt.figure(figsize = (14,8), linewidth=3, frameon = False)
    data = eval(control)
    plot_data(data)

You can see the image of my work too:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

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.