WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Getting stock quotes through the command line  (Read 329 times)

Offline Rich

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 5201
Getting stock quotes through the command line
« on: January 07, 2017, 11:09:57 AM »
Attached is a very short script for getting stock quotes. You can pass the stock symbols of interest via a command line
parameter like this:
Code: [Select]
tc@box:~$
tc@box:~$ ./pricequote jpm,hd,aapl,axp,unh,gs
Symbol  Price   Change  Date        Time      Dividend  Name
"jpm"   86.12   +0.01   "1/6/2017"  "4:00pm"  1.92      "JP Morgan Chase & Co."
"hd"    133.53  -0.37   "1/6/2017"  "4:00pm"  2.76      "Home Depot                         Inc. (The)"
"aapl"  117.91  +1.30   "1/6/2017"  "4:00pm"  2.28      "Apple Inc."
"axp"   75.47   +0.15   "1/6/2017"  "4:00pm"  1.28      "American Express Company"
"unh"   162.41  +0.23   "1/6/2017"  "4:01pm"  2.50      "UnitedHealth Group Incorporated"
"gs"    244.90  +3.58   "1/6/2017"  "4:01pm"  2.60      "Goldman Sachs Group                Inc. (The)"
tc@box:~$
The stock symbols are not case sensitive and should be comma separated with no intervening spaces. If no symbols are
entered at the command line it defaults to a file called  stocks  in your home directory:
Code: [Select]
tc@box:~$
tc@box:~$ echo "mcd,mrk,cat,pg,wmt,jnj,mmm,msft,utx,trv" > stocks
tc@box:~$ cat stocks
mcd,mrk,cat,pg,wmt,jnj,mmm,msft,utx,trv
tc@box:~$
tc@box:~$ ./pricequote
Symbol  Price   Change  Date        Time      Dividend  Name
"mcd"   120.76  +1.06   "1/6/2017"  "4:02pm"  3.76      "McDonald's Corporation"
"mrk"   60.27   +0.16   "1/6/2017"  "4:00pm"  1.88      "Merck & Company                    Inc.  (new)"
"cat"   93.04   +0.04   "1/6/2017"  "4:00pm"  3.08      "Caterpillar                        Inc."
"pg"    85.03   -0.03   "1/6/2017"  "4:01pm"  2.68      "Procter & Gamble Company (The)"
"wmt"   68.26   -0.95   "1/6/2017"  "4:02pm"  2.00      "Wal-Mart Stores                    Inc."
"jnj"   116.30  -0.56   "1/6/2017"  "4:02pm"  3.20      "Johnson & Johnson"
"mmm"   178.23  +0.52   "1/6/2017"  "4:04pm"  4.44      "3M Company"
"msft"  62.84   +0.54   "1/6/2017"  "4:00pm"  1.56      "Microsoft Corporation"
"utx"   112.55  +1.20   "1/6/2017"  "4:02pm"  2.64      "United Technologies Corporation"
"trv"   118.27  -0.06   "1/6/2017"  "4:02pm"  2.68      "The Travelers Companies            Inc."
tc@box:~$
The script depends on  util-linux.tcz  for the  column  command. The data is retrieved by this line in the script:
Code: [Select]
wget -qO- "http://download.finance.yahoo.com/d/quotes.csv?s=$STOCKS&f=sl1c1d1t1dn" >> /tmp/stocktableThese are the codes in the above line that determine the data returned:
Code: [Select]
sl1c1d1t1dnThe meaning of these codes and others can be found here:
http://wern-ancheta.com/blog/2015/04/05/getting-started-with-the-yahoo-finance-api/
The  n  code returns the company name. If used you might want to make the terminal wider since this usually results in
the lines being more than 80 characters long.


The stocks listed here were used as examples only and chosen at random from the Dow 30. I own none of the stocks mentioned.

Offline patrikg

  • Full Member
  • ***
  • Posts: 234
Re: Getting stock quotes through the command line
« Reply #1 on: January 07, 2017, 02:08:50 PM »
Grate script, thank you.

You could do it one liner, if you want not to store in to temp.
Code: (bash) [Select]
(echo "Symbol,Price,Change,Date,Time,Dividend,Name" & echo $(wget -qO- "http://d
ownload.finance.yahoo.com/d/quotes.csv?s=$STOCKS&f=sl1c1d1t1dn") ) | column -s',
' -t

Offline Rich

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 5201
Re: Getting stock quotes through the command line
« Reply #2 on: January 07, 2017, 05:01:25 PM »
Hi patrikg
Thanks, glad you like it. Personally I try to avoid one liners. I feel it hurts readability.

Offline coreplayer2

  • Hero Member
  • *****
  • Posts: 2300
Re: Getting stock quotes through the command line
« Reply #3 on: January 07, 2017, 10:39:23 PM »
This is interesting :)

Thanks for the script Rich