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.

Prerequisites

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 os.name=='nt' else 'clear')
        print json.dumps(getQuotes(symbol), indent=2)
        time.sleep(5)

symbol=sys.argv[1]
fetchstockquotes(symbol)
				
			

Copy this script, paste it into Notepad, and save it as “FetchStockQuotes.py” 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:

run FetchStockQuotes.py NSE:NIFTY

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.

Python-Script-to-fetch-Live-Stock-Quotes

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 = 'http://finance.google.com/finance?q='
    content = urllib.urlopen(base_url + symbol).read()
    m = re.search('id="ref_(.*?)">(.*?)<', content)
    if m:
        quote = m.group(2)
    else:
        quote = 'no quote available for: ' + symbol
    return quote

symbol=sys.argv[1]
while True:
    print
    print str(time.ctime())+ " - " + symbol+ " - " + fetchstockquotes(symbol)
    time.sleep(5)

				
			

Python-Stock-Quotes

Conclusion


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