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
    Welcome to My New Office

    My first professional web development was at a small print shop where I sat in a windowless cubical all day. I suffered that boxed in environment for almost five years before I was able to find a remote job where I worked from home. The first...

  • 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
    Do / Undo Functionality with MooTools

    We all know that do/undo functionality is a God send for word processing apps. I've used those terms so often that I think of JavaScript actions in terms of "do" an "undo." I've put together a proof of concept Do/Undo class with MooTools. The MooTools...

  • By
    Create a Dynamic Flickr Image Search with the Dojo Toolkit

    The Dojo Toolkit is a treasure chest of great JavaScript classes.  You can find basic JavaScript functionality classes for AJAX, node manipulation, animations, and the like within Dojo.  You can find elegant, functional UI widgets like DropDown Menus, tabbed interfaces, and form element replacements within...

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!