Tiny Core Linux

General TC => Programming & Scripting - Unofficial => Topic started by: Rich on January 07, 2017, 11:09:57 AM

Title: Getting stock quotes through the command line
Post by: Rich 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.
Title: Re: Getting stock quotes through the command line
Post by: patrikg 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
Title: Re: Getting stock quotes through the command line
Post by: Rich 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.
Title: Re: Getting stock quotes through the command line
Post by: coreplayer2 on January 07, 2017, 10:39:23 PM
This is interesting :)

Thanks for the script Rich
Title: Re: Getting stock quotes through the command line
Post by: Rich on November 14, 2021, 01:09:56 PM
Yahoo shut down their stocks API a couple of years ago which of course made the posted script useless. I finally got
around to creating a replacement. After some searching I came across blog that provided a URL that will return
stock quotes. Links to the blog and github repo are provided in the attached script for those that are interested.

The data that is returned includes UTF-8 graphics characters which I strip out since aterm/xterm doesn't support them.
The data is also colorized. Since busybox less doesn't handle color, I strip those escape codes out as well.
If you have GNU less installed, you can enable color by setting the  UseColor  variable to 1.
The  Stocks  variable can be set to which stocks to display by default.

This is the help message:
Code: [Select]
tc@E310:~$ PriceQuote --help
Usage:

    PriceQuote         Get quotes for stocks listed in the Stocks variable.
                       Stocks=jpm,hd,aapl,axp,unh,gs

    PriceQuote X,Y,Z   Get quotes for X, Y, and Z instead of Stocks variable.

    PriceQuote +X,Y,Z  Get quotes for Stocks variable plus X, Y, and Z.

tc@E310:~$

When run, the script launches a new terminal and displays the results using less:
Code: [Select]
Stock Name                       Current Price  Change  % Change  Day Range        52 Week Range   

 JPMorgan Chase & Co.             166.86         -0.75   -0.45     165.72 - 167.74  113.56 - 172.96

 The Home Depot                   372.63         5       1.36      366.70 - 373.50  246.59 - 375.15

 Apple Inc.                       149.99         2.12    1.43      147.48 - 150.40  112.59 - 157.26

 American Express Company         181.89         0.59    0.33      179.65 - 182.38  111.90 - 189.03

 UnitedHealth Group Incorporated  458.99         1.6     0.35      457.08 - 461.00  320.35 - 466.00

 The Goldman Sachs Group          404.91         2.57    0.64      399.20 - 405.10  218.33 - 426.16

2021-11-12T21:00:02.000Z

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

Of possible interest to anyone else who does any scripting:
I wrote a  ToUpper  function that allows you to pass a variable to it by reference instead of by value. In other words, you
pass the variable without the leading $ sign to the function:
Code: [Select]
ToUpper "VariableName"The function then dereferences the variable to retrieve the string, converts it to uppercase, and places the result back
into the variable.

There is also a short section that calculates what size terminal is required to display the results.