~anjan/ticker-portfolio

589e8759b3558e746733a28fbf1f7aec1dac3bd2 — Anjandev Momi 6 months ago master
initial commit
5 files changed, 174 insertions(+), 0 deletions(-)

A .gitignore
A LICENSE
A README.md
A example-portfolio
A ticker.sh
A  => .gitignore +1 -0
@@ 1,1 @@
portfolio
\ No newline at end of file

A  => LICENSE +19 -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.

A  => README.md +16 -0
@@ 1,16 @@
# ticker-portfolio.sh

Fork of https://github.com/pstadler/ticker.sh with portfolio support

## Usage

1. Make sure you have `jq` installed
2. Make a tab separated file with your portfolio. See `example-portfolio` in this directory
3. Run the following command:

```sh
while : ; do
clear && sh ticker.sh "$(cat portfolio)" && sleep 10m
done
```


A  => example-portfolio +2 -0
@@ 1,2 @@
AAPL	36.00	22	USD
MSFT	40.00	19	USD

A  => ticker.sh +136 -0
@@ 1,136 @@
#!/usr/bin/env bash
set -e

LANG=C
LC_NUMERIC=C

SYMBOLSALL=("$@")
SYMBOLS="$(echo "$SYMBOLSALL" | cut -f1 | sort -u | tr -t '\n' ' ')"

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 BTC-USD"
  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"

if [ -z "$NO_COLOR" ]; then
  : "${COLOR_BOLD:=\e[1;37m}"
  : "${COLOR_GREEN:=\e[32m}"
  : "${COLOR_RED:=\e[31m}"
  : "${COLOR_RESET:=\e[00m}"
fi

symbols=$(IFS=,; echo "${SYMBOLS[*]}" | sed 's/ /,/g')
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"
}

colorme () {

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

    echo $color
}

TOTMADECAD="0"
TOTSPENTCAD="0"


TOTMADEUSD="0"
TOTSPENTUSD="0"

while IFS='\n' read -r line;  do
  symbol=$(echo "$line" | cut -f1)
  BOUGHTPRICE=$(echo "$line" | cut -f2)
  BOUGHTNUM=$(echo "$line" | cut -f3)
  marketState="$(query $symbol 'marketState')"

  if [ -z $marketState ]; then
    printf 'No results for symbol "%s"\n' $symbol
    continue
  fi

  preMarketChange="$(query $symbol 'preMarketChange')"
  postMarketChange="$(query $symbol 'postMarketChange')"

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

  color="$(colorme $diff)"

  pricepaid=$(echo $BOUGHTPRICE*$BOUGHTNUM | bc -l)
  pricecur=$(echo $price*$BOUGHTNUM | bc -l)
  if [ $pricepaid != "0" ]; then
    gainpercent=$(echo $pricecur*100/$pricepaid-100 | bc -l)
  else
      gainpercent="NAN"
  fi
  moneymade=$(echo $pricecur-$pricepaid | bc -l)
  portfoliocolor=$(colorme $moneymade)

  todaydelta=$(echo $diff*$BOUGHTNUM | bc -l)

  currency="$(echo "$line" | cut -f4)"
  if [ $currency == "CAD" ]; then
    TOTMADECAD="$(echo $TOTMADECAD+$moneymade | bc -l)"
    TOTSPENTCAD="$(echo $TOTSPENTCAD+$pricepaid | bc -l)"
  elif [ $currency == "USD" ]; then
       TOTMADEUSD="$(echo $TOTMADEUSD+$moneymade | bc -l)"
       TOTSPENTUSD="$(echo $TOTSPENTUSD+$pricepaid | bc -l)"
  fi

  if [ "$price" != "null" ]; then
    printf "%-10s$COLOR_BOLD%8.2f$COLOR_RESET" $symbol $price
    printf "$color%10.2f%12s$COLOR_RESET" $diff $(printf "(%.2f%%)" $percent)
    printf "\tPortfolio:"
    printf " $portfoliocolor%10.2f%12s$COLOR_RESET" $moneymade $(printf "(%.2f%%)" $gainpercent)
    printf " $color%10.2f$COLOR_RESET" $todaydelta
    printf " $currency\n"
  fi
done <<<$(echo "$SYMBOLSALL")

colortotport="$(colorme $TOTMADECAD)"
gainpercentportcad=$(echo "($TOTMADECAD+$TOTSPENTCAD)*100/$TOTSPENTCAD-100" | bc -l)

gainpercentportusd=$(echo "($TOTMADEUSD+$TOTSPENTUSD)*100/$TOTSPENTUSD-100" | bc -l)

printf "\tOverall:"
printf " $portfoliocolor%10.2f%12s$COLOR_RESET USD" $TOTMADEUSD $(printf "(%.2f%%)" $gainpercentportusd)
printf " $portfoliocolor%10.2f%12s$COLOR_RESET CAD\n" $TOTMADECAD $(printf "(%.2f%%)" $gainpercentportcad)