Code Monkey home page Code Monkey logo

ticker.sh's Introduction

ticker.sh

⚠️ Unfortunately, Yahoo decided to lock down their (undocumented) Finance API. This project is currently defunct. See [#45].

Real-time stock tickers from the command-line.

ticker.sh is a simple shell script using the Yahoo Finance API as a data source. It features colored output and is able to display pre- and post-market prices (denoted with *).

ticker.sh

Install

$ curl -o ticker.sh https://raw.githubusercontent.com/pstadler/ticker.sh/master/ticker.sh

Make sure to install jq, a versatile command-line JSON processor.

Usage

# Single symbol:
$ ./ticker.sh AAPL

# Multiple symbols:
$ ./ticker.sh AAPL MSFT GOOG BTC-USD

# Read from file:
$ echo "AAPL MSFT GOOG BTC-USD" > ~/.ticker.conf
$ ./ticker.sh $(cat ~/.ticker.conf)

# Use different colors:
$ COLOR_BOLD="\e[38;5;248m" \
  COLOR_GREEN="\e[38;5;154m" \
  COLOR_RED="\e[38;5;202m" \
  ./ticker.sh AAPL

# Disable colors:
$ NO_COLOR=1 ./ticker.sh AAPL

# Update every five seconds:
$ watch -n 5 -t -c ./ticker.sh AAPL MSFT GOOG BTC-USD
# Or if `watch` is not available:
$ while true; do clear; ./ticker.sh AAPL MSFT GOOG BTC-USD; sleep 5; done

Please note that ticker.sh may require periodic updates of its session with Yahoo Finance. During these instances, the script may take slightly longer to complete.

This script works well with GeekTool and similar software:

PATH=/usr/local/bin:$PATH # make sure to include the path where jq is located
~/GitHub/ticker.sh/ticker.sh AAPL MSFT GOOG BTC-USD

ticker.sh's People

Contributors

calvin-barker avatar giuli007 avatar mfrancis95 avatar pstadler avatar stummi avatar techie2000 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ticker.sh's Issues

python?

There is a Python module yfinance. Could be some script written in python to fetch data from Yahoo finance?

Simple demo:

$ cat msft-test.py
#!/usr/bin/env python3

import yfinance as yf
import json

msft = yf.Ticker("MSFT")

# get all stock info
msft.info

print(json.dumps(msft.info))
$ python --version
Python 3.8.10
$ python3 msft-test.py | jq .
{
  "address1": "One Microsoft Way",
  "city": "Redmond",
  "state": "WA",
  "zip": "98052-6399",
  "country": "United States",
  "phone": "425 882 8080",
  "website": "https://www.microsoft.com",
  "industry": "Software - Infrastructure",
  "industryKey": "software-infrastructure",
...
$ python3 msft-test.py | jq . | grep -i price
  "priceHint": 2,
  "priceToSalesTrailing12Months": 13.704965,
  "priceToBook": 13.553296,
  "currentPrice": 402.56,
  "targetHighPrice": 480,
  "targetLowPrice": 298.1,
  "targetMeanPrice": 421.32,
  "targetMedianPrice": 425,

Cookies are stored in SQLite database, file $HOME/.cache/py-yfinance/cookies.db

Invalid number

Hello,

I get this when I try your script:

nils@HAL9000:/tmp$ ./ticker.sh AAPL AAPL 220,00./ticker.sh: line 73: printf: -0.5380001: invalid number ./ticker.sh: line 73: printf: -1.1900024: invalid number 0,00 (0,00%)

Do you know what's wrong?

Upgrade request - Display personal gains/ loose for each symbol

Hi,

could you please upgrade your script by considering this small parameter addon.

Your script can quickly help us to see how much we gain or loose with this tips.

Let me explain by this example :
I bought 25 stocks of AAPL at the price of USD100.05
Then if I mention it as parameter in your script (by considering you parse the symbols content) it could display in one shot which symbol allow us to gain or loose.
Your script will then become something like this :
$ ./ticker.sh [25*100.05]AAPL MSFT GOOG BTC-USD

That could give as result :
AAPL 325.05 0.10 (0.03%) * [25*100.05]5625
MSFT 185.30 -0.05 (-0.03%) *
GOOG 1520.84 0.10 (0.01%) *
BTC-USD 9585.88 -341.63 (-3.44%)

As I just mentioned the AAPL amount of stock and price I bought between brackets "[25*100.05]" only this line have to display this gain or loose result.
Like you did previously for a great visibility this "[25*100.05]" should be print in grey and the result in green for gains or red for loose.

By hoping you could add this small functionality to your script it could sincerely be appreciated by everybody.

Have a nice day.

Symbol with "-" are not supported

Hi,

because I have some symbols where the caracter "-" is included your script return those errors :
No results for symbol "AGS-BE"
No results for symbol "XFAB-FR"

It concern the stocks AGEAS and X-FAB.

Sincerely thank you to solve this problem because I suppose I am not alone in this case.

Have a nice day.

Miguipda

7 Decimal places for Crypto Currency Quotes

Hey bud, I came across your code doing a search for a replacement for what I had been using before to display ticker data on my custom display screen. After I came across your solution, within a few minutes I was able to get it up and running and was incredibly surprised that I was able to also get Crypto data. The only thing however is that the decimal places for all the quotes stop at 2 ( .00). Where Crypto is concerned those few decimal places are definitely something people like to see and watch. For instance, TRX-USD shows up as 0.02 when in fact it's actual .0248. KIN-USD shows up as 0.00, but it is actually 0.0000957. So, can you make it that the code displays up to 7 decimal places, which is what all existing Crypto tracker apps and tickers do? I suppose you would also have to do the same for the change amount. I don't have any problem hitting you up with a few bucks for your time. Whatever the case, code seems to work pretty well and any consideration regarding the above issue is greatly appreciated. Thanks!

Other possible field items?

Thank you for sharing this code.

Are there any field items for Open, High, Low and Volume?

Do you know of a web site describing this api?

Thank you,

AB.

Add `date` to output

Adding the date to the output is nice if you need to capture stock prices at a certain time or if you just want to confirm your prices are updating as they should.

problem with floating point conversion

floating point conversion with printf seems to blow up when there are trailing spaces in variables
I added 3 lines to the script and it works fine now.
Thank you for the script.

else
nonRegularMarketSign=''
price=$(query $symbol 'regularMarketPrice')
diff=$(query $symbol 'regularMarketChange')
percent=$(query $symbol 'regularMarketChangePercent')
fi

  • price=${price%%[[:space:]]}
  • diff=${diff%%[[:space:]]}
  • percent=${percent%%[[:space:]]}

Doesn't work with dash

I believe the #!/bin/sh shebang translates into something other than bash (mine, for example, a standard Ubuntu Server, defaults to dash).

Since the script seems to work on bash only, why not specify bash there? (Maybe it works with zsh? I don't know, but I believe bash is still pre-installed in all linuxes out there.)

stopped working again

Think it is the cookie stuff. It stopped on one machine a few days ago and I copied the cook cache from another machine and it started working. Now fails on all systems

Script stopped working

sh ticker2.sh AAPL

No results for symbol "AAPL"

could it be yahoo API changes were made?

Needs US locale / $LANG

Nice work! But it does not work in my locale/$LANG (de_DE.UTF-8):

→ ticker.sh AAPL
/Users/okketimm/bin/ticker.sh: line 70: printf: 173.75: invalid number

It works with US locale/$LANG (en_US.UTF-8):

→ LANG=en_US.UTF-8 ticker.sh AAPL
AAPL       173.75      5.64     (3.35%) *

Too Many Requests

Running the script has failed for the past couple of days.

crumb.txt says "Too Many Requests".

I thought my IP may have been banned, so I tried via an Amazon AWS server and received the same result.

Symbol listed twice crashes ticker.sh script

Symbol listed twice crashes ticker.sh script. Example:

$ ./ticker.sh GOOG GOOG
./ticker.sh: line 43: [: POSTPOST: binary operator expected
./ticker.sh: line 51: [: too many arguments
./ticker.sh: line 58: [: too many arguments
GOOG       1265.131265.13       0.00./ticker.sh: line 81: printf: (0.33%)(0.33%): invalid number
      4.14   4.1400146      0.00

Problem is that once symbol is listed more than once, "jq" returns not a string but an array.

The fix:

$ git diff
diff --git a/ticker.sh b/ticker.sh
index 6bceda6..acf2435 100755
--- a/ticker.sh
+++ b/ticker.sh
@@ -34,13 +34,13 @@ results=$(curl --silent "$API_ENDPOINT&fields=$fields&symbols=$symbols" \
   | jq '.quoteResponse .result')
 
 query () {
-  echo $results | jq -r ".[] | select(.symbol == \"$1\") | .$2"
+  echo $results | jq -r "[.[] | select(.symbol == \"$1\") | .$2][0]"
 }
 
 for symbol in $(IFS=' '; echo "${SYMBOLS[*]}"); do
   marketState="$(query $symbol 'marketState')"
 
-  if [ -z $marketState ]; then
+  if [ "$marketState" = "null" ]; then
     printf 'No results for symbol "%s"\n' $symbol
     continue
   fi

Example after the fix:

$ ./ticker.sh GOOG GOOG
GOOG       1270.00      4.87     (0.38%) *
GOOG       1270.00      4.87     (0.38%) *

There is one more problem with some symbols those are not active anymore. I am not sure, for example "YHOO" or "NV". printf command crashes because it cannot print "null" as number. I suggest following fix but I am not sure if it is the right solution; maybe some message should be written:

@@ -69,6 +69,12 @@ for symbol in $(IFS=' '; echo "${SYMBOLS[*]}"); do
     percent=$(query $symbol 'regularMarketChangePercent')
   fi
 
+  if [ "$price" = "null" ]; then
+    price="0"
+    diff="0"
+    percent="0"
+  fi
+
   if [ "$diff" == "0" ]; then
     color=
   elif ( echo "$diff" | grep -q ^- ); then

a couple of simple improvements

For your consideration...

#!/usr/bin/env bash
for those of us who had to upgrade bash into a different directory (Mac users)

add symbol=${symbol^^} uppercase prior to marketState query (so "aapl" & "AAPL"work the same)

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.