简体   繁体   中英

Graph Not Showing Up when running program (PLOTLY)

When I run my program, it does not throw any errors, however it seems to run as an infinite loop never finishing execution or showing me the graph output anywhere, when expected output should be a graph with a candlestick chart and multiple lines and volume bar chart:

import pandas_datareader as web
from datetime import datetime
import numpy as np
import pandas as pd
import chart_studio.plotly as plt

dataframe=\
    web.DataReader('SPY','yahoo',datetime(2020,10,16),datetime(2020,11,16))
dataframe.head()

INCREASING_COLOR = '#17BECF'
DECREASING_COLOR = '#7F7F7F'

data = [ dict(
    type='candlestick',
    open=dataframe.Open,
    high=dataframe.High,
    low=dataframe.Low,
    close=dataframe.Close,
    x=dataframe.index,
    yaxis = 'y2',
    name = 'SPY',
)]

layout = dict()
figure = dict(data=data,layout=layout)

figure['layout'] = dict()
figure['layout']['plot_bgcolor'] = 'rgb(250, 250, 250)'
figure['layout']['xaxis'] = dict( rangeselector = dict( visible = True ) )
figure['layout']['yaxis'] = dict( domain = [0, 0.2], showticklabels = False )
figure['layout']['yaxis2'] = dict( domain = [0.2, 0.8] )
figure['layout']['legend'] = dict( orientation = 'h', y=0.9, x=0.3, yanchor='bottom' )
figure['layout']['margin'] = dict( t=40, b=40, r=40, l=40 )

rangeselector=dict(
    visible=True,
    x=0, y=0.9,
    bgcolor='rgba(150,200,250,0.4)',
    font=dict(size=13),
    buttons=list([
        dict(count=1,
             label='reset',
             step='all'),
        dict(count=1,
             label='1yr',
             step='year',
             stepmode='backward'),
        dict(count=3,
             label='3mo',
             step='month',
             stepmode='backward'),
        dict(count=1,
             label='1mo',
             step='month',
             stepmode='backward'),
        dict(step='all')
    ]))
figure['layout']['xaxis']['rangeselector']=rangeselector

def movingaverage(interval,window_size=10):
    window=np.ones(int(window_size))/float(window_size)
    return np.convolve(interval,window,'same')

movingaverage_y=movingaverage(dataframe.Close)
movingaverage_x=list(dataframe.index)

# Clip the ends
movingaverage_x=movingaverage_x[5:-5]
movingaverage_y=movingaverage_y[5:-5]

figure['data'].append(dict(x=movingaverage_x,y=movingaverage_y,
                           type='scatter',mode='lines',
                           line=dict(width=1),
                           marker=dict(color='#E377C2'),
                           yaxis='y2',name='Moving Average'))

colors=[]
for i in range(len(dataframe.Close)):
    if i!=0:
        if dataframe.Close[i]>dataframe.Close[i-1]:
            colors.append(INCREASING_COLOR)
        else:
            colors.append(DECREASING_COLOR)
    else:
        colors.append(DECREASING_COLOR)

figure['data'].append(dict(x=dataframe.index,y=dataframe.Volume,
                           marker=dict(color=colors),
                           type='bar',yaxis='y',name='Volume'))

# ---------- BOLLINGER BANDS ------------
def bollinger_bands(price,window_size=10,num_of_std=5):
    rolling_mean = price.rolling(window=window_size).mean()
    rolling_std = price.rolling(window=window_size).std()
    upper_band = rolling_mean + (rolling_std * num_of_std)
    lower_band = rolling_mean - (rolling_std * num_of_std)
    return rolling_mean, upper_band, lower_band

bollinger_bands_average,upper_band,lower_band=bollinger_bands(dataframe.Close)
figure['data'].append(dict(x=dataframe.index,y=upper_band,type='scatter',yaxis='y2',
                            line=dict(width=1),
                            marker=dict(color='#ccc'), hoverinfo='none',
                            legendgroup='Bollinger Bands',name='Bollinger Bands'))
figure['data'].append(dict(x=dataframe.index,y=lower_band,type='scatter',yaxis='y2',
                            line=dict(width=1),
                            marker=dict(color='#ccc'), hoverinfo='none',
                            legendgroup='Bollinger Bands',showlegend=False))
# ----------------------------------------

plt.iplot(figure, filename='candlestick',validate=True)

Let me know if more information is needed

*** ANSWER ***

import chart_studio.plotly as plt requires some time of online compatability so in order to combat this I changed the import to this: from plotly.offline import plot

Then in order to be able top see this plot, I changed the final line from: plt.iplot(figure,filename='candlestick',validate=True)
to:

plot(figure, filename = 'candlestick-test-3.html', validate = False )

Thus the graph will be created and opened inside your browser!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM