Skip to main content

Commencing with Python for Real-Time Stock Quotes

If you haven’t already installed Python and experimented with basic commands, please review the initial part of this tutorial series. Now, let’s create a Python script to Live Stock Quotes from Google Finance. We’ll delve into two separate techniques to accomplish this.


To run this script, you’ll need to install the googlefinance module.

Open your command prompt and employ this command for module installation:

pip install googlefinance

The installation should finalize in under 30 seconds, depending on your internet speed.

Google Finance API

Python Script for Real-Time Stock Quotes

Below is a simple Python script to fetch real-time stock quotes.

					from googlefinance import getQuotes
import time
import json
import os
import sys

def fetchstockquotes(symbol):
    while True:
        os.system('cls' if'nt' else 'clear')
        print json.dumps(getQuotes(symbol), indent=2)


Copy this script, paste it into Notepad, and save it as “” in your working directory. Next, reopen the command prompt, navigate to your working directory, and execute the following command to launch Jupyter Notebook:

jupyter notebook

Once the notebook is accessible via your web browser, go to New –> Python2. This will open the editor where you can execute Python commands and scripts. Input the following command to run your script:


In this instance, “NSE:NIFTY” denotes the stock name for which you want to fetch data. You can replace it with any other symbol using the format “EXCHANGE_NAME : SYMBOL_NAME.”

You will observe real-time stock quotes refreshing every 5 seconds after executing the script.


Understanding the Script Logic

The initial import statements include necessary built-in modules for script execution. Moreover, we have incorporated the googlefinance module as installed in the first step.

Following that, there is a function definition that accepts a single argument, “symbol,” passed as a command line parameter during script execution. The function comprises an ongoing while loop that uses the getQuotes function to display stock data, with a 5-second delay between each iteration.

Here is another script that accomplishes the same result by utilizing web scraping to obtain real-time stock prices from the Google Finance website.

					import urllib
import re
import time
import sys

def fetchstockquotes(symbol):
    base_url = ''
    content = urllib.urlopen(base_url + symbol).read()
    m ='id="ref_(.*?)">(.*?)<', content)
    if m:
        quote =
        quote = 'no quote available for: ' + symbol
    return quote

while True:
    print str(time.ctime())+ " - " + symbol+ " - " + fetchstockquotes(symbol)




With this tutorial, you have taken your initial step into Algorithmic trading using Python. If you have any questions, please don’t hesitate to reach out.

Leave a Reply