Fchpro01
Fchpro01

Reputation: 11

creating multiple candlestick charts on same page with plotly

I'm creating a script that takes stock data from csv (name of the stock , start date , end date) and create a candlestick chart , then add some lines to analyse it all with plotly example in this screenshot url

example of chart code :

import yfinance as yf
import plotly.graph_objects as go

df = yf.download(stock, start="2022-04-15", end="2022-07-15",interval="1h")
fig = go.Figure(data=[go.Candlestick(x=df.index,
                    open=df['Open'],
                    high=df['High'],
                    low=df['Low'],
                    close=df['Close'],name=stock)]) 

    v = df['Volume'].values
    tp = (df['Low'] + df['Close'] + df['High']).div(3).values
    df = df.assign(vwap=((tp * v).cumsum() / v.cumsum()))

    fig.add_trace(go.Scatter(
        x=df.index,
        y=df['vwap'],
        mode='lines',
        name='vwap',
        line=dict(color='royalblue',width=2)
    ))
    

I made a loop to create multiple plots , but I can only show them one at a different page. I want to show them in same page vertically. I tried various method to create subplots and I failed . please kind help

Upvotes: 1

Views: 2056

Answers (1)

Dakshitha Wijesinghe
Dakshitha Wijesinghe

Reputation: 63

use subplot in plotly package

from plotly.subplots import make_subplots

and make separate plots and use fig.add_trace to combine them

fig = make_subplots(rows=2, cols=1,
                    shared_xaxes=True,
                    vertical_spacing=0.02)

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

fig.add_trace(go.Scatter(
    x=df.index,
    y=df['vwap'],
    mode='lines',
    name='vwap',
    line=dict(color='royalblue',width=2),
               row=2,col=1)

fig.update_layout(height=800, width=1200,
                  title_text="Your Chart Title")
fig.show()

Upvotes: 1

Related Questions