-2

I want to visualize my data using candlestick and boxplot charts. I have done the candlestick already, but I'm getting stuck with the boxplot.

I have the code provided:

def visualization(df, trading_days):
    dt_range = pd.date_range(start="2015-01-01", end="2015-03-01")
    df = df[df.index.isin(dt_range)]

    # Calculate the average values of 'trading_days' consecutive days
    df = df.rolling(window=trading_days, min_periods=1).mean()

    df = delete_gaps(df, trading_days)

    hover = HoverTool(
        tooltips=[
            ('Date', '@Date{%Y-%m-%d}'),  # Display the 'Date' value in 'yyyy-mm-dd' format
            ('Open', '@Open'),  # Display the 'Open' column value with formatting
            ('High', '@High'),  # Debugging: Print High value
            ('Low', '@Low'),  # Debugging: Print Low value
            ('Close', '@Close'),  # Debugging: Print Close value
            ('Volume', '@Volume')  # Debugging: Print Volume value
        ],
        formatters={'@Date': 'datetime'},
        mode='mouse'
    )

    source = ColumnDataSource(data=df)

    # Create ColumnDataSources for increasing and decreasing values
    inc_source = ColumnDataSource(data=df[df.Close > df.Open])
    dec_source = ColumnDataSource(data=df[df.Open > df.Close])

    w = 12 * 60 * 60 * 1000

    candle = figure(x_axis_type="datetime", width=800, height=500, title="Apple, March - 2020")

    candle.segment('Date', 'High', 'Date', 'Low', source=source, color="black")

    # Create vbar glyphs for increasing and decreasing values with different colors
    candle.vbar('Date', w, 'Open', 'Close', source=inc_source, fill_color="red", line_color="red",
             legend_label="Increasing")
    candle.vbar('Date', w, 'Open', 'Close', source=dec_source, fill_color="green", line_color="green",
             legend_label="Decreasing")

    candle.add_tools(hover)

    ## Boxplot Chart
    box = figure(x_axis_type="datetime", width=800, height=200)

    box.vbar('Date', width=w, top='Volume', source=source, fill_color="dodgerblue", alpha=0.8)

    box.add_tools(hover)

    box.xaxis.axis_label = "Date in March-2020"
    box.yaxis.axis_label = "Volume"
    candle.yaxis.axis_label = "Price ($)"

    show(column(candle, box))

Output of df:

                 Open       High        Low      Close  Adj Close       Volume
Date                                                                          
2015-01-06  14.388444  14.532222  13.880667  14.237333  14.237333   81974000.0
2015-01-09  14.112889  14.190222  13.883333  13.960666  13.960666   55396000.0
2015-01-14  13.160000  13.495111  13.003555  13.314445  13.314445  109897500.0
2015-01-20  12.868000  12.985778  12.593111  12.819333  12.819333   66614500.0
2015-01-23  13.040889  13.453777  12.956444  13.321778  13.321778   58542500.0
2015-01-28  13.608000  13.844889  13.394889  13.597778  13.597778   45825500.0
2015-02-02  13.533333  13.897778  13.395555  13.772000  13.772000   53521500.0
2015-02-05  14.475334  14.829555  14.393556  14.620000  14.620000   58272500.0
2015-02-10  14.554000  14.707334  14.299778  14.469556  14.469556   60534000.0
2015-02-13  13.526222  13.862667  13.366000  13.765556  13.765556  158048500.0
2015-02-19  13.663778  13.873556  13.507778  13.789333  13.789333   59236500.0
2015-02-24  14.082889  14.290889  13.729778  13.968000  13.968000  105427500.0
2015-02-27  13.685334  13.928444  13.502222  13.650889  13.650889   71322500.0

I want to visualize the dataset followed the following requirements: The top of the whisker is the high value, the bottom of the whisker is low value, the top of the bar is open, the bottom of the bar is close, the mid is the median price

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
  • The typical tool for this is [tag:mplfinance]. – Trenton McKinney Aug 30 '23 at 13:56
  • `df = delete_gaps(df, trading_days)`: neither `delete_gaps`, not `trading_days` are defined. If we can't copy, paste, and run your code, then it's not a complete [mre]. Please review #3 and $9 of [Creating Effective Stack Overflow Questions](https://trenton3983.github.io/files/Creating_Effective_StackOverflow_Questions.html) and [edit] your question. – Trenton McKinney Aug 30 '23 at 14:03
  • Also `box = figure(...)` figure is not defined. – Trenton McKinney Aug 30 '23 at 14:08

0 Answers0