Plot the technical data for a stock

Our Own Score

Welcome back, in this chapter we will continue to develop our stock and Forex application by plotting the technical data for any stock which has been selected from the stock symbol combo box. If you are really interested to develop your own Stock/Forex analysis application then alpha_vantage is probably your best choice, the data is great and the data returning speed is very fast plus one more, the API is completely free. Look no further than alpha vantage if you are a serious Forex or Stock application developer. I am not the affiliate of this program but just give you a suggestion.

As usual, we are going to create a button which will then call the plot method to retrieve and plot the technical data of that particular stock which has been selected through the combo box.

import json
from tkinter import *
import tkinter.ttk as tk
from alpha_vantage.foreignexchange import ForeignExchange
from alpha_vantage.techindicators import TechIndicators
from alpha_vantage.timeseries import TimeSeries
import matplotlib.pyplot as plt

win = Tk() # Create tk instance
win.title("Real Forex n Stock") # Add a title
win.resizable(0, 0) # Disable resizing the GUI
win.configure(background='white') # change window background color

selectorFrame = Frame(win, background="white") # create the top frame to hold base and quote currency combobox
selectorFrame.pack(anchor = "nw", pady = 2, padx=10)
currency_label = Label(selectorFrame, text = "Select base currency / quote currency / stock :", background="white")
currency_label.pack(anchor="w") # the currency pair label

curr1 = tuple() # the tuple which will be populated by base and quote currency
currency_list = ['AUD', 'BCH', 'BNB', 'BND', 'BTC', 'CAD', 'CHF', 'CNY', 'EOS', 'EUR', 'ETH', 'GBP', 'HKD', 'JPY', 'LTC', 'NZD', 'MYR', 'TRX', 'USD', 'USDT', 'XLM', 'XRP'] # major world currency pairs

# populate the combo box for both the base and quote currency
for key in currency_list:
    curr1 += (key, )

# populate the stock symbol tuple
f = open("stock.txt", "r")
curr2 = tuple()
for line in f.readlines():
    curr2 += (line.replace('\n', ''),)

# Create a combo box for base currency
base_currency = StringVar() # create a string variable
based = tk.Combobox(selectorFrame, textvariable=base_currency)
based['values'] = curr1
based.pack(side = LEFT, padx=3)

# Create a combo box for quote currency
quote_currency = StringVar() # create a string variable
quote = tk.Combobox(selectorFrame, textvariable=quote_currency)
quote['values'] = curr1
quote.pack(side = LEFT, padx=3)

# Create a combo box for stock
stock_symbol = StringVar() # create a string variable
stock = tk.Combobox(selectorFrame, textvariable=stock_symbol)
stock['values'] = curr2
stock.pack(side = LEFT, padx=3)

s = StringVar() # create string variable which will be used to fill up the Forex data
# create currency frame and text widget to display the incoming forex data
currencyFrame = Frame(win)
currency = Label(currencyFrame)
text_widget = Text(currency, fg='white', background='black')
s.set("Click the find button to find out the currency exchange rate")
text_widget.insert(END, s.get())

buttonFrame = Frame(win) # create a bottom frame to hold the find button
buttonFrame.pack(side = BOTTOM, fill=X, pady = 6)

# first get the api key and secret from the file
f = open("alpha.txt", "r")
api_key = f.readline()
api_key = api_key.replace('\n', '')

def get_exchange_rate(): # this method will display the incoming forex data after the api called

        cc = ForeignExchange(key= api_key)
        from_ = based.get()
        to_ = quote.get()

        countVar = StringVar()  # use to hold the character count
        text_widget.tag_remove("search", "1.0", "end")  # cleared the hightlighted currency pair

        if(from_ != '' and to_ != '' and from_ != to_):
            data, _ = cc.get_currency_exchange_rate(from_currency=from_, to_currency=to_)
            exchange_rate = dict(json.loads(json.dumps(data)))
            count = 1
            sell_buy = str(count) + ".) Pair : " + exchange_rate['1. From_Currency Code'] + "(" + exchange_rate['2. From_Currency Name'] + ")" + " / " + exchange_rate['3. To_Currency Code']+"(" + exchange_rate['4. To_Currency Name'] + ") : "  + str(exchange_rate['5. Exchange Rate']) + '\n'
            text_widget.delete('1.0', END)  # clear all those previous text first
            text_widget.insert(INSERT, s.get())  # display forex rate in text widget
            pos =, "1.0", stopindex="end", count=countVar)
            text_widget.tag_configure("search", background="green")
            end_pos = float(pos) + float(0.7)
            text_widget.tag_add("search", pos, str(end_pos))  # highlight the background of the searched currency pair
            pos = float(pos) + 2.0

        print("An exception occurred")

def plot_stock_echange():

        stock_symbol_text = stock.get() # get the selected symbol
        if(stock_symbol_text!= ''):
            ts = TimeSeries(key=api_key, output_format='pandas')
            data, meta_data = ts.get_intraday(symbol=stock_symbol_text, interval='1min', outputsize='full')
            data['4. close'].plot()
            stock_title = 'Intraday Times Series for the ' + stock_symbol_text + ' stock (1 min)'
        print("An exception occurred")

def plot_stock_technical():

        stock_symbol_text = stock.get() # get the selected symbol
        if(stock_symbol_text!= ''):
            ti = TechIndicators(key=api_key, output_format='pandas')
            data, meta_data = ti.get_bbands(symbol=stock_symbol_text, interval='60min', time_period=60)
            stock_title = 'BBbands indicator for ' + stock_symbol_text + ' (60 min)'
        print("An exception occurred")

action_vid = tk.Button(buttonFrame, text="Exchange Rate", command=get_exchange_rate) # button used to find out the exchange rate of currency pair
action_vid.pack(side=LEFT, padx=2)
action_stock_plot = tk.Button(buttonFrame, text="Plot Stock", command=plot_stock_echange) # button used to plot the intra-minute stock value
action_stock_plot.pack(side=LEFT, padx=2)
action_technical_plot = tk.Button(buttonFrame, text="Plot Technical", command=plot_stock_technical) # button used to plot the 60 minutes stock technical value
action_technical_plot.pack(side=LEFT, padx=2)


If you run the above python code and click on the Plot Technical button with any stock symbol selected then you will see the below outcome.

Help me to retweet or like the below tweet so more people can enjoy this little program.


Leave a Reply

Notify of