- 
                Notifications
    You must be signed in to change notification settings 
- Fork 673
TkInter Example
        Daniel Goldfarb edited this page Sep 26, 2021 
        ·
        2 revisions
      
    The following example code was take from the link in this comment: https://github.com/matplotlib/mplfinance/issues/46#issuecomment-596209866
import matplotlib as matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import pandas as pd
import talib as talib
import mplfinance as mpf
import tkinter as tk
def drawChart(df):
    # Calculate Stochastic
    df["slowk"], df["slowd"] = talib.STOCH(df["High"], df["Low"], df["Close"], fastk_period=5, 
                                           slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0)
    # Calculate Bollinger Band
    df["upperband"], df["middleband"], df["lowerband"] = talib.BBANDS(df["Close"], timeperiod=5, 
                                                                      nbdevup=2, nbdevdn=2, matype=0)
    # Prune data to last 30 values
    df = df[-30:]
    # Place Stochastic in Lower Panel
    mpfStoch = mpf.make_addplot(df[["slowk", "slowd"]], panel="lower")
    # Place Upper and Lower Bollinger Band in Upper Panel
    mpfBB = mpf.make_addplot(df[["upperband", "lowerband"]])
    # List of added Plots
    addedPlots = [mpfStoch, mpfBB]
    # Generate the plots and and return the figure
    fig, _ = mpf.plot(df, type='candlestick', volume=True, returnfig=True, addplot=addedPlots, figscale=2.0)
    # Make a top level window
    top = tk.Toplevel()
    # Add a canvas containing the figure
    canvas = FigureCanvasTkAgg(fig, top)
    # Draw it
    canvas.draw()
    canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)