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
    How I Stopped WordPress Comment Spam

    I love almost every part of being a tech blogger:  learning, preaching, bantering, researching.  The one part about blogging that I absolutely loathe:  dealing with SPAM comments.  For the past two years, my blog has registered 8,000+ SPAM comments per day.  PER DAY.  Bloating my database...

  • By
    Camera and Video Control with HTML5

    Client-side APIs on mobile and desktop devices are quickly providing the same APIs.  Of course our mobile devices got access to some of these APIs first, but those APIs are slowly making their way to the desktop.  One of those APIs is the getUserMedia API...

Incredible Demos

  • By
    Introducing MooTools ScrollSidebar

    How many times are you putting together a HTML navigation block or utility block of elements that you wish could be seen everywhere on a page? I've created a solution that will seamlessly allow you to do so: ScrollSidebar. ScrollSidebar allows you...

  • By
    Table Cell and Position Absolute

    If you follow me on Twitter, you saw me rage about trying to make position: absolute work within a TD element or display: table-cell element.  Chrome?  Check.  Internet Explorer?  Check.  Firefox?  Ugh, FML.  I tinkered in the console...and cussed.  I did some researched...and I...

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!