Skip to content

Commit

Permalink
Adding GOLD/SILVER -g flag option, README Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
appatalks committed Sep 14, 2024
1 parent a96ccce commit 2ebbe7d
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 41 deletions.
123 changes: 88 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,108 @@

# ticker.sh

> **⚠️ Unfortunately, Yahoo decided to lock down their (undocumented) Finance API. This project is currently defunct. See [https://github.com/pstadler/ticker.sh/issues/45].**
`ticker.sh` is a Bash script that retrieves and displays live stock prices, along with precious metal prices (gold, silver, platinum) and the gold-to-silver ratio. It uses Yahoo Finance as the source for fetching data and can display results in color-coded output for better readability.

> Real-time stock tickers from the command-line.
## Features

`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 `*`).
- Fetch live stock prices by providing stock symbols (e.g., AAPL, MSFT, GOOG).
- Display live spot prices for gold, silver, and platinum.
- Calculate and display the Gold/Silver ratio.
- Color-coded output for price changes and metal prices.
- Option to disable colorization by setting the `NO_COLOR` environment variable.

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

## Install
Ensure the following dependencies are installed:

```sh
$ curl -o ticker.sh https://raw.githubusercontent.com/pstadler/ticker.sh/master/ticker.sh
```
- [curl](https://curl.se/)
- [jq](https://stedolan.github.io/jq/) - for JSON parsing.
- [bc](https://www.gnu.org/software/bc/) - for arithmetic operations.

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

1. Clone the repository or download the `ticker.sh` script.
2. Ensure the script is executable:

```bash
chmod +x ticker.sh
```
3. Install the required dependencies:

```bash
sudo apt-get install jq bc curl # For Debian-based systems
```

## Usage

```sh
# Single symbol:
$ ./ticker.sh AAPL
### Fetch Stock Prices

You can fetch live stock prices by passing the stock symbols as arguments. For example, to retrieve the prices for Apple (AAPL), Microsoft (MSFT), and Google (GOOG), use the following command:

```bash
./ticker.sh AAPL MSFT GOOG BTC-USD
```

This will display the current price, the price change, and the percentage change.

### Fetch Precious Metal Prices

To fetch the spot prices for gold, silver, platinum, and the gold-to-silver ratio, use the `-g` flag:

```bash
./ticker.sh -g
```

This will display:

- **Gold Spot**: The current price of gold.
- **Silver Spot**: The current price of silver.
- **Platinum Spot**: The current price of platinum.
- **Gold/Silver Ratio**: The ratio of the gold price to the silver price.

### Fetch Both Stock and Precious Metal Prices

You can also fetch both stock prices and precious metal prices in a single command:

```bash
./ticker.sh -g AAPL MSFT GOOG BTC-USD
```

This will display both the spot prices for the metals and the prices for the given stock symbols.

### Disable Color Output

If you are running the script in an environment that doesn't support color or if you prefer plain text output, you can disable colorization by setting the `NO_COLOR` environment variable:

```bash
NO_COLOR=1 ./ticker.sh AAPL MSFT GOOG BTC-USD
```

### PRO TIP

> [!NOTE]
> Use a foreloop for continious ```5 minute``` monitoring:
>
> ```bash
> while true; do ./ticker.sh -g SPY GOLD HNST MSFT PFE PLG PYPL RXT WEAT; sleep 300; clear; done
> ```
## Example Output
![ticker.sh](https://raw.githubusercontent.com/appatalks/ticker.sh/master/screenshot.png)
# Multiple symbols:
$ ./ticker.sh AAPL MSFT GOOG BTC-USD
## License
# Read from file:
$ echo "AAPL MSFT GOOG BTC-USD" > ~/.ticker.conf
$ ./ticker.sh $(cat ~/.ticker.conf)
This script is provided as-is under the MIT License. You are free to modify and distribute it under the terms of the license.
# Use different colors:
$ COLOR_BOLD="\e[38;5;248m" \
COLOR_GREEN="\e[38;5;154m" \
COLOR_RED="\e[38;5;202m" \
./ticker.sh AAPL
## Contributions
# Disable colors:
$ NO_COLOR=1 ./ticker.sh AAPL
Contributions are welcome! If you encounter bugs or have feature suggestions, feel free to submit an issue or pull request.
# 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.
### Author
This script works well with [GeekTool](https://www.tynsoe.org/geektool/) and similar software:
Originially Created @pstadler
Modifications by @appatalks
```sh
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
```
Binary file modified screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 53 additions & 6 deletions ticker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,38 @@ if [ -z "$NO_COLOR" ]; then
: "${COLOR_RESET:=$'\e[00m'}"
fi

SYMBOLS=("$@")
SYMBOLS=()
DISPLAY_METALS=false

# Parse options
while getopts "g" opt; do
case ${opt} in
g )
DISPLAY_METALS=true
;;
* )
echo "Usage: ./ticker.sh [-g] AAPL MSFT GOOG BTC-USD"
exit 1
;;
esac
done
shift $((OPTIND -1))

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

# Adding bc check for colors correct. Thank you @milanico2309
if ! $(type bc > /dev/null 2>&1); then
echo "'bc' is not in the PATH. (See: https://www.gnu.org/software/bc/)"
exit 1
fi

if [ -z "$SYMBOLS" ]; then
echo "Usage: ./ticker.sh AAPL MSFT GOOG BTC-USD"
exit
if [ -z "$SYMBOLS" ] && [ "$DISPLAY_METALS" = false ]; then
echo "Usage: ./ticker.sh [-g] AAPL MSFT GOOG BTC-USD"
exit 1
fi

[ ! -d "$SESSION_DIR" ] && mkdir -m 700 "$SESSION_DIR"
Expand All @@ -50,9 +66,40 @@ fetch_chart () {

[ ! -f "$COOKIE_FILE" ] && preflight

fetch_metal_prices () {
local gold_symbol="GC=F"
local silver_symbol="SI=F"
local platinum_symbol="PL=F"

local gold_price=$(fetch_chart "$gold_symbol" | jq -r '.chart.result[0].meta.regularMarketPrice')
local silver_price=$(fetch_chart "$silver_symbol" | jq -r '.chart.result[0].meta.regularMarketPrice')
local platinum_price=$(fetch_chart "$platinum_symbol" | jq -r '.chart.result[0].meta.regularMarketPrice')

local gold_silver_ratio=$(awk -v gold="$gold_price" -v silver="$silver_price" 'BEGIN {printf "%.2f", gold / silver}')

# Define colors
local COLOR_YELLOW=$'\e[33m'
local COLOR_SILVER=$'\e[37m'
local COLOR_LIGHT_GREY=$'\e[249m'
local COLOR_BRIGHT_PURPLE=$'\e[35;1m'
local COLOR_RESET=$'\e[0m'

echo "Precious Metal Spot Prices:"
echo "---------------------------"
printf "${COLOR_YELLOW}Gold Spot: $%.2f /OZ${COLOR_RESET}\n" "$gold_price"
printf "${COLOR_SILVER}Silver Spot: $%.2f /OZ${COLOR_RESET}\n" "$silver_price"
printf "${COLOR_LIGHT_GREY}Platinum Spot: $%.2f /OZ${COLOR_RESET}\n" "$platinum_price"
printf "${COLOR_BRIGHT_PURPLE}Gold/Silver Ratio: %.2f${COLOR_RESET}\n" "$gold_silver_ratio"
echo ""
}

# Initialize an array to hold background process IDs
pids=()

if [ "$DISPLAY_METALS" = true ]; then
fetch_metal_prices
fi

for symbol in "${SYMBOLS[@]}"; do
(
# Running in subshell
Expand All @@ -63,7 +110,6 @@ for symbol in "${SYMBOLS[@]}"; do
currency=$(echo "$results" | jq -r '.chart.result[0].meta.currency')
symbol=$(echo "$results" | jq -r '.chart.result[0].meta.symbol')

# see https://github.com/pstadler/ticker.sh/issues/40
[ "$previousClose" = "null" ] && previousClose="1.0"

priceChange=$(awk -v currentPrice="$currentPrice" -v previousClose="$previousClose" 'BEGIN {printf "%.2f", currentPrice - previousClose}')
Expand Down Expand Up @@ -96,3 +142,4 @@ done
for pid in "${pids[@]}"; do
wait "$pid"
done

0 comments on commit 2ebbe7d

Please sign in to comment.