Search code examples
pythonplotlyplotly-pythontradingalgorithmic-trading

How to increment the size of candlesticks with plotly?


I've been working on this code to plot a candlestick chart with signals and indicators using plotly, but for some reason the candles are too small. Any help on how to increment the size of the candles (not the chart)?

def plot_chart(df):
fig = make_subplots(rows=2, cols=1, shared_xaxes=True, vertical_spacing=0.03, subplot_titles=('BTCUSDT'), row_heights = [1, 0.3])


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

fig.add_trace(go.Scatter(
    x = df.index,
    y = df['vwap'],
    opacity = 0.5,
    line = dict(color='blue'),
    name = 'vwap'),
    row = 1,
    col = 1)

fig.add_trace(go.Scatter(
    x = df.signal_buy.index, 
    y = buy, 
    mode = 'markers', 
    marker_symbol = 'triangle-up', 
    marker_color = 'green', 
    marker_size = 5,
    name = 'Buy signal'), 
    row = 1, 
    col = 1)

fig.add_trace(go.Scatter(
    x = df.signal_sell.index, 
    y = sell, 
    mode = 'markers', 
    marker_symbol = 'triangle-down', 
    marker_color = 'red', 
    marker_size = 5, 
    name = 'Sell signal'), 
    row = 1, 
    col = 1)
    
fig.add_trace(go.Scatter(
    x = df.index,
    y = df['rsi'],
    line = dict(color='white', width=1),
    showlegend = False),
    row = 2,
    col = 1)

fig.update_layout(
    title = 'BTCUSDT',
    yaxis_title = 'Price',
    xaxis_rangeslider_visible = False,
    template = 'plotly_dark',
    showlegend = True,
    #width = 1000,
    height = 800,)

fig.update_yaxes(type = 'log', row = 1, col = 1)
fig.update_yaxes(title_text = 'RSI', row = 2, col = 1)
fig.write_html('BTCUSDT.html', auto_open = True, full_html = True)

fig.show()

plot_chart(df)

This is the output of the program above


Solution

  • If you see the documentation, you will not find a parameter to increase the width of candlestick box. The only way to get clear view for the candlestick is to increase the width of the entire figure. Try this example:

    import plotly.graph_objects as go
    
    import pandas as pd
    from datetime import datetime
    
    df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
    
    fig = go.Figure(data=[go.Candlestick(x=df['Date'],
                    open=df['AAPL.Open'],
                    high=df['AAPL.High'],
                    low=df['AAPL.Low'],
                    close=df['AAPL.Close'])])
    fig.update_layout(width=10000)  # remove this line to see the default
    fig.show()