Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pstadler committed Oct 7, 2017
0 parents commit 09af429
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (C) 2017 by Patrick Stadler

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# ticker.sh

> 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.

![ticker.sh](https://raw.githubusercontent.com/pstadler/ticker.sh/master/screenshot.png)

## Install

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

Make sure to install [jq](https://stedolan.github.io/jq/), a versatile command-line JSON processor.

## Usage

```sh
# Single symbol:
$ ./ticker.sh AAPL

# Multiple symbols:
$ ./ticker.sh AAPL MSFT GOOG BTCUSD=X

# Read from file:
$ echo "AAPL MSFT GOOG BTCUSD=X" > ~/.ticker.conf
$ ./ticker.sh $(cat ~/.ticker.conf)

# Update every five seconds:
$ while true; do clear; ./ticker.sh AAPL MSFT GOOG BTCUSD=X; sleep 5; done
```

This script works well with [GeekTool](https://www.tynsoe.org/v2/geektool/) and similar software.
Binary file added screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 73 additions & 0 deletions ticker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/bin/sh
set -e

SYMBOLS=("$@")

if ! $(type jq > /dev/null 2>&1); then
echo "'jq' is not in the PATH. (See: https://stedolan.github.io/jq/)"
exit 1
fi

if [ -z "$SYMBOLS" ]; then
echo "Usage: ./ticker.sh AAPL MSFT GOOG BTCUSD=X"
exit
fi

FIELDS=(symbol marketState regularMarketPrice regularMarketChange regularMarketChangePercent \
preMarketPrice preMarketChange preMarketChangePercent postMarketPrice postMarketChange postMarketChangePercent)
API_ENDPOINT="https://query1.finance.yahoo.com/v7/finance/quote?lang=en-US&region=US&corsDomain=finance.yahoo.com"

COLOR_BOLD='\e[1;37m'
COLOR_GREEN='\e[32m'
COLOR_RED='\e[31m'
COLOR_RESET='\e[00m'

symbols=$(IFS=,; echo "${SYMBOLS[*]}")
fields=$(IFS=,; echo "${FIELDS[*]}")

results=$(curl --silent "$API_ENDPOINT&fields=$fields&symbols=$symbols" \
| jq '.quoteResponse .result')

query () {
echo $results | jq -r ".[] | select (.symbol == \"$1\") | .$2"
}

for symbol in $(IFS=' '; echo "${SYMBOLS[*]}"); do
if [ -z "$(query $symbol 'marketState')" ]; then
printf 'No results for symbol "%s"\n' $symbol
continue
fi

if [ $(query $symbol 'marketState') == "PRE" ] \
&& [ "$(query $symbol 'preMarketChange')" != "0" ] \
&& [ "$(query $symbol 'preMarketChange')" != "null" ]; then
nonRegularMarketSign='*'
price=$(query $symbol 'preMarketPrice')
diff=$(query $symbol 'preMarketChange')
percent=$(query $symbol 'preMarketChangePercent')
elif [ $(query $symbol 'marketState') != "REGULAR" ] \
&& [ "$(query $symbol 'postMarketChange')" != "0" ] \
&& [ "$(query $symbol 'postMarketChange')" != "null" ]; then
nonRegularMarketSign='*'
price=$(query $symbol 'postMarketPrice')
diff=$(query $symbol 'postMarketChange')
percent=$(query $symbol 'postMarketChangePercent')
else
nonRegularMarketSign=''
price=$(query $symbol 'regularMarketPrice')
diff=$(query $symbol 'regularMarketChange')
percent=$(query $symbol 'regularMarketChangePercent')
fi

if [ "$diff" == "0" ]; then
color=
elif ( echo "$diff" | grep -q ^- ); then
color=$COLOR_RED
else
color=$COLOR_GREEN
fi

printf "%-10s$COLOR_BOLD%7.2f$COLOR_RESET" $symbol $price
printf "$color%10.2f%12s$COLOR_RESET" $diff $(printf "(%.2f%%)" $percent)
printf " %s\n" "$nonRegularMarketSign"
done

0 comments on commit 09af429

Please sign in to comment.