Get Stock Quotes From Command Line

By  on  

When I conned my way into my first professional programming gig, I didn't really think much about money -- just that I was getting my foot in the door.  But as my career has gone on, I've been more aware of money, investing, and retirement.  I've recently regretfully gotten into stock trading -- you can imagine how many times a day I hit the ole F5 to see updated quotes, praying my investments push higher.

I also spend a large part of my day in iTerm, so switching back and forth between the browser and the terminal is time-consuming and annoying.  I looked for a way to get stock quotes from the command line, and thanks to Yahoo, it's a simple process!

Get Current Price

Since cURL is amazing and easy to use, we'll roll with that:

#One stock
curl -s 'http://download.finance.yahoo.com/d/quotes.csv?s=aapl&f=l1'

#Multiple stocks, separated by comma
curl -s 'http://download.finance.yahoo.com/d/quotes.csv?s=aapl,meip&f=l1'

Yahoo! Finance's information, which is fairly "real time", provides quotes within the s parameter and the f parameter of l1 provides the formatted result.

Get Current Price with Change

If you'd like to know the amount changed on the day, tag c1 to the f parameter:

curl -s 'http://download.finance.yahoo.com/d/quotes.csv?s=aapl&f=l1c1'

Not needing to jump over to the browser to get stock quotes has been nice...especially if the market is having a down day and you don't want to see a screen of red.  Booooo.

Recent Features

  • By
    Designing for Simplicity

    Before we get started, it's worth me spending a brief moment introducing myself to you. My name is Mark (or @integralist if Twitter happens to be your communication tool of choice) and I currently work for BBC News in London England as a principal engineer/tech...

  • By
    Serving Fonts from CDN

    For maximum performance, we all know we must put our assets on CDN (another domain).  Along with those assets are custom web fonts.  Unfortunately custom web fonts via CDN (or any cross-domain font request) don't work in Firefox or Internet Explorer (correctly so, by spec) though...

Incredible Demos

  • By
    Event Delegation with MooTools

    Events play a huge role in JavaScript. I can't name one website I've created in the past two years that hasn't used JavaScript event handling on some level. Ask yourself: how often do I inject elements into the DOM and not add an...

  • By
    prefers-color-scheme: CSS Media Query

    One device and app feature I've come to appreciate is the ability to change between light and dark modes. If you've ever done late night coding or reading, you know how amazing a dark theme can be for preventing eye strain and the headaches that result.

Discussion

  1. Suraj

    Is there a way to get quotes of multiple stocks from the same command?

    • I’ve updated my first example to show how to get multiple stocks! (separate by comma)

  2. Alper

    On Windows you may need to replace the single quotes with double quotes.

  3. I wrote you a simple shell script to use.

    https://gist.github.com/chrisopedia/1e30869645aab537be12

  4. Awesome share! Thank you.

  5. Harrison Delfino

    Using MarketXLS works for me. Its great.
    http://marketxls.com/stock-quotes-in-excel/

  6. Mike

    Any luck pulling an index?

  7. Before May 2017, I used Yahoo Finance, and it was great, but at the moment Yahoo! closed free access and I switched to https://eodhistoricaldata.com, highly recommend them, you can easy get everything, they have examples for Excel, python, R and even Matlab. Creating console example is very easy:

    curl -s "https://eodhistoricaldata.com/api/eod/AAPL.US?from=2017-01-05&to=2017-02-10&api_token=OeAFFmMliFG5orCUuwAKQ8l4WWFQ67YX&period=d"
    
  8. Joseph Spenner

    Just whipped this up using marketwatch.com. ie, for AAPL:

     curl -s https://www.marketwatch.com/investing/stock/aapl |grep '<meta name="price" content="' |cut -d'"' -f4
    
  9. JF

    If I use your command:

    curl -s 'http://download.finance.yahoo.com/d/quotes.csv?s=tsla&f=l1'
    

    And nothing is return, am I missing something ?

    I just want to put TESLA stock value within my ” conky config / to show that on my desktop

  10. while 1 ; do clear ; for STONK in AMC AQB GME TSLA ; do echo -e -n "\e[1;36m${STONK}\t\e[1;33m" ; curl -s https://www.marketwatch.com/investing/stock/${STONK} |grep '<meta name="price" content="' |cut -d'"' -f4 ; echo -e -n "\e[0m" ; done ; sleep 60 ; done

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!