I hope you have already installed Python in your system and tested the execution of simple statements. If not, please go through the first part of this tutorial series right here. Now, let’s write a python script to fetch live stock quotes from Google finance. We would explore two different methods to fetch live stock quotes. First one uses googlefinance api, and the next uses web scraping.
You would need to install googlefinance module to execute this script.
Open command prompt and copy past the below command to install GoogleFinance module
pip install googlefinance
The installation wouldn’t take more than 30 seconds depending on your internet speed.
Python Script to fetch Live Stock Quotes
Below is the simple python script to fetch live 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 fetchstockquotes(symbol)
Copy this script, paste it in notepad and save as “FetchStockQuotes.py” in your working directory. Now open command prompt again, and goto your working directory path. Use below command to open Jupyter notebook:
Once the notebook is open in your web browser, goto New–>Python2, it will open up the editor where you can execute python statements and scripts. Type the below command to execute your script.
run FetchStockQuotes.py NSE:NIFTY
Here, NSE:NIFTY refers to the stock name you want to fetch data for. You can replace it with any other symbol using “EXCHANGE_NAME : SYMBOL_NAME” format.
You would see live stock quotes refreshing every 5 seconds once the script is executed.
Now, let’s understand the code logic and see how it works
The first few import statement are the necessary built-in modules to execute this script. You can see that we have also imported the googlefinance module installed in the first step.
Next, there is a function definition which takes a single argument “symbol”. The symbol name is passed as a command line argument during script execution. There is a infinite While loop in the function which uses getQuotes function to print the stock data. There is a time delay of 5 seconds between each loop.
See below another script to do the same:
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 while True: print print str(time.ctime())+ " - " + symbol+ " - " + fetchstockquotes(symbol) time.sleep(5)
This script uses web scraping to fetch the real-time stock price from Google finance website. Its output is the live closing price with time.
Here we complete our first exercise to start Algorithmic trading using Python. Let us know if you have any questions.