I am trying to make candlestick graphs, but even though the code appears to work, the charts don’t show up anywhere

Viewed 26

Full disclosure - I'm very new to coding, I suspect the answer here could be rather simple (maybe somewhere charts should show up? I've made graphs in the past that have showed up though...), but I've spent probably a dozen hours trying different methods of printing candlestick charts. I mostly use Spyder, but I tried using PyCharm thinking maybe the IDE was the problem. I also tried Jupyter Notebook, but I couldn’t figure out the interface.

I've tried making the graph using Plotly and with mpl finance.

The closest I seemed to get was to get a blank chart (which seemed weird since I definitely had data in the dataframe. I’m also not sure how I got that, because I can’t get it any more.

This is the code I tried with Plotly:

import datetime as dt

import pandas as pd

from pandas_datareader import data as pdr

import plotly.graph_objects as go

from plotly.subplots import make_subplots

import plotly.io as pio

pio.renderers.default = 'svg'

start = dt.datetime(2015, 2, 2)
end = "2022-08-01"

stock = ["AAPL"]

stock = pdr.DataReader(stock, 'yahoo', start, end)
print(stock.head())

ma50 = stock['Close'].rolling(window=50, min_periods=0).mean()

fig = make_subplots(rows=2, cols=1, shared_xaxes=True,vertical_spacing = .01, subplot_titles = ('price','volumne'))

fig = go.Figure(data=[go.Candlestick(x=stock.index,
                                     open=stock['Open'],
                                     high=stock['High'],
                                     low=stock['Low'],
                                     close=stock['Close'])])

This was the output:

Process finished with exit code 0

So I get that exit code 0 means there were no errors, but why is there also no graph, and how do I get the graph?

(Separately, why didn’t drop.na() remove the NaNs on the 50 day moving average?)

1 Answers

I think the reason why the graph is not displayed is due to the SVG specification. I don't know why, I tried running it several times in jupyterlab environment, but it kept freezing. You can get the candlestick graph by disabling pio.renderers.default = 'svg'.

import datetime as dt
import pandas as pd
from pandas_datareader import data as pdr
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import plotly.io as pio

# pio.renderers.default = 'svg'

start = dt.datetime(2015, 2, 2)
end = "2022-08-01"

stock = ["AAPL"]

stock = pdr.DataReader(stock, 'yahoo', start, end)
print(stock.head())

ma50 = stock['Close'].rolling(window=50, min_periods=0).mean()
stock.columns = stock.columns.droplevel(level=1)

fig = make_subplots(rows=2, cols=1, shared_xaxes=True, vertical_spacing=.3, subplot_titles=('price','volumne'))

fig.add_trace(go.Candlestick(x=stock.index,
                             open=stock['Open'],
                             high=stock['High'],
                             low=stock['Low'],
                             close=stock['Close'],
                             name='Candlestick'),
              row=1, col=1)

fig.add_trace(go.Bar(x=stock.index, y=stock['Volume'], name='Volume'), row=2,col=1)
fig.update_layout(autosize=True, height=600)

# fig.write_image("data/fig1.svg")
fig.show()

enter image description here

Related