Introduction: Getting Stock Prices on Raspberry Pi (using Python)

Picture of Getting Stock Prices on Raspberry Pi (using Python)

I'm working on some new projects involving getting stock price data from the web, which will be tracked and displayed via my Raspberry Pi. I wanted to share the setup on how to do this using Python.

This short Instructable will show you how install a stock querying library to get (mostly) realtime stock prices using Yahoo Finance API. There's no GitHub involved!

You can also use this stock price-gathering engine on any Linux server.

Step 1: Setup Your Raspberry Pi

Picture of Setup Your Raspberry Pi

If you haven't done so already, you'll want to do a basic configuration of your Raspberry Pi. You'll need internet access for this software installation.

If you are just getting started, my Ultimate Raspberry Pi Configuration Guide will get up and running.

I prefer using ssh and am using Terminal on a Mac, but as long as you have internet connection, you'll be fine.

We will be using the command line for this tutorial on a Raspberry Pi.

Step 2: Run Updates and Upgrades

Picture of Run Updates and Upgrades

Whenever you add new libraries to your Raspberry Pi, you want to update and upgrade your software packages. We'll start with this step.

Type in

sudo apt-get update

then

sudo apt-get upgrade

choose Y to continue when asked

You'll see the usual scroll of Linux and will have to wait several minutes. Get up and do some stretching after each of these commands.

Step 3: Install Pip

Picture of Install Pip

pip is a package manager and installer for Python. We want to have this installed to get the appropriate Yahoo python stock libraries loaded onto the Pi.

Type in:

sudo apt-get install python-pip

choose Y to continue when asked

This will take a few minutes.

Step 4: Install Ystockquote

Picture of Install Ystockquote

ystockquote is a Python module that will let you easily gather stock quotes from Yahoo. It does this reasonably quickly, a lot more so than BeautifulSoup, which would be another way to get stock data.

Type in:

sudo pip install ystockquote

Step 5: Write the Python Script

Picture of Write the Python Script

We will create a new directory and a Python script to get the stock data. Type in:

mkdir stockquote
cd stockquote
nano stockquote.py

This will bring up the nano editor.

Type in this following 4-line Python script

import ystockquote
tickerSymbol = 'ADSK'
allInfo = ystockquote.get_all(tickerSymbol)
print tickerSymbol + " Price = " + allInfo["price"]


cntrl-X, Y will save the .py file.

Step 6: Run It, Done!

Picture of Run It, Done!

Now run it.

python stockquote.py

You'll see the latest stock quote from Autodesk.

Buy! Buy! Buy!

I hope this was helpful!
Scott Kildall

For more on the my projects, you can find me here:
@kildall or www.kildall.com/blog

Comments

tonyD5 (author)2017-11-11

it worked

Overmind121 (author)2017-07-07

I have an error where it says, "KeyError: 'price'

BenH274 (author)Overmind1212017-10-30

I'm getting the same error, was a solution found?

Archer19 (author)2017-02-18

how does one import the ystockquote module for python 3?

mmanuel2 (author)2015-01-23

Script for multiple symbols:

import ystockquote

tickersymbols = ['ADSK','AAPL','ZNGA','GPRO','GOOG','EA']

for tickerSymbol in tickersymbols:

allInfo = ystockquote.get_all(tickerSymbol)

price = float(allInfo["price"])

change = float(allInfo["change"])

percent = change/(change + price)*100

peratio = allInfo["price_earnings_ratio"]

print tickerSymbol + "\t= %+3.2f (%+0.2f, %+0.2f%%) P/E %s" % (price, change, percent, peratio)

KingN7 (author)mmanuel22017-02-03

Type error: can only concatenate list (not "str") to list

KingN7 made it! (author)2017-02-03

Nice, but i would like to have multiple and auto refresh and best would be if it would loop on my led matrix so there would be the ADSK and a arrow up or down if it went minus or plus from the last number. Can you create a code for that?

eplambec (author)2016-04-11

Everything works great. Can you suggest how to multiply the stock price by a set number (for example #of shares) to display a new number?

PiAlex (author)2016-03-10

Is there a way to get it to also trade off the prices ?

PiAlex (author)2016-03-10

Is there a way to get it to also trade off the prices ?

DwayneWei (author)2015-06-21

I would like to see the expansion of this to stock trading

NoobT (author)2015-01-11

Awesome little project! I am working on expanding on it. Thanks!!!

Jacoby Yarrow (author)2014-12-20

Hi.

How do i make a code box when making an instructable, because i am making an instructable and without it it looks really bad?

esulat (author)2014-12-15

Thanks!

wilgubeast (author)2014-09-30

This project is clean, simple, and useful. I appreciate the integration with your RaspPi configuration 'ible, too.

Klappstuhl (author)2014-08-31

Now to write a program to make the Raspberry Pi trade stock on the market on its own, haha!

Nice instructable!

About This Instructable

60,634views

313favorites

License:

Bio: Scott Kildall is an new media artist and researcher. He works at Autodesk, Pier 9 and is an artist-in-residence with the SETI Institute
More by scottkildall:Pier 9 Guide: Fusion 360 to OMAX WaterjetStrewn Fields: Waterjet Etching Into StonePier 9 Resource: Setting up 2D profiles for CNC in Fusion 360
Add instructable to: