Dotfiles for my tiling window manager + terminal workflow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.0 KiB

  1. #!/usr/bin/env bash
  2. set -e
  3. LANG=C
  4. LC_NUMERIC=C
  5. SYMBOLS=("$@")
  6. if ! $(type jq > /dev/null 2>&1); then
  7. echo "'jq' is not in the PATH. (See: https://stedolan.github.io/jq/)"
  8. exit 1
  9. fi
  10. if [ -z "$SYMBOLS" ]; then
  11. echo "Usage: ./ticker.sh AAPL MSFT GOOG BTC-USD"
  12. exit
  13. fi
  14. FIELDS=(symbol marketState regularMarketPrice regularMarketChange regularMarketChangePercent \
  15. preMarketPrice preMarketChange preMarketChangePercent postMarketPrice postMarketChange postMarketChangePercent)
  16. API_ENDPOINT="https://query1.finance.yahoo.com/v7/finance/quote?lang=en-US&region=US&corsDomain=finance.yahoo.com"
  17. if [ -z "$NO_COLOR" ]; then
  18. : "${COLOR_BOLD:=\e[1;37m}"
  19. : "${COLOR_GREEN:=\e[32m}"
  20. : "${COLOR_RED:=\e[31m}"
  21. : "${COLOR_RESET:=\e[00m}"
  22. fi
  23. symbols=$(IFS=,; echo "${SYMBOLS[*]}")
  24. fields=$(IFS=,; echo "${FIELDS[*]}")
  25. results=$(curl --silent "$API_ENDPOINT&fields=$fields&symbols=$symbols" \
  26. | jq '.quoteResponse .result')
  27. query () {
  28. echo $results | jq -r ".[] | select(.symbol == \"$1\") | .$2"
  29. }
  30. for symbol in $(IFS=' '; echo "${SYMBOLS[*]}" | tr '[:lower:]' '[:upper:]'); do
  31. marketState="$(query $symbol 'marketState')"
  32. if [ -z $marketState ]; then
  33. printf 'No results for symbol "%s"\n' $symbol
  34. continue
  35. fi
  36. preMarketChange="$(query $symbol 'preMarketChange')"
  37. postMarketChange="$(query $symbol 'postMarketChange')"
  38. if [ $marketState == "PRE" ] \
  39. && [ $preMarketChange != "0" ] \
  40. && [ $preMarketChange != "null" ]; then
  41. nonRegularMarketSign='*'
  42. price=$(query $symbol 'preMarketPrice')
  43. diff=$preMarketChange
  44. percent=$(query $symbol 'preMarketChangePercent')
  45. elif [ $marketState != "REGULAR" ] \
  46. && [ $postMarketChange != "0" ] \
  47. && [ $postMarketChange != "null" ]; then
  48. nonRegularMarketSign='*'
  49. price=$(query $symbol 'postMarketPrice')
  50. diff=$postMarketChange
  51. percent=$(query $symbol 'postMarketChangePercent')
  52. else
  53. nonRegularMarketSign=''
  54. price=$(query $symbol 'regularMarketPrice')
  55. diff=$(query $symbol 'regularMarketChange')
  56. percent=$(query $symbol 'regularMarketChangePercent')
  57. fi
  58. if [ "$diff" == "0" ]; then
  59. color=
  60. elif ( echo "$diff" | grep -q ^- ); then
  61. color=$COLOR_RED
  62. else
  63. color=$COLOR_GREEN
  64. fi
  65. if [ "$price" != "null" ]; then
  66. # printf "%-10s$COLOR_BOLD%8.2f$COLOR_RESET" $symbol $price
  67. # printf "$color%10.2f%12s$COLOR_RESET" $diff $(printf "(%.2f%%)" $percent)
  68. # printf " %s\n" "$nonRegularMarketSign"
  69. # echo \$$price
  70. # echo $price
  71. hour=$(date +"%H")
  72. if [[ "$symbol" == "USDCAD=X" ]]; then
  73. printf "%0.4f\n" "$price"
  74. elif [[ "$symbol" == "BTC-USD" || "$symbol" == "ETH-USD" ]]; then
  75. # echo "$price" | cut -c 2-3
  76. echo "$price" | cut -c -3
  77. elif (( $(echo "$price > 10000" | bc -l) )); then
  78. printf "%'0.0f\n" "$price" | cut -c 2-4
  79. # elif (( $(echo "$price < 1" | bc -l) )); then
  80. # printf "%0.3f\n" "$price"
  81. else
  82. printf "%0.2f\n" "$price" | cut -c 3-
  83. # printf "%0.2f\n" "$price"
  84. fi
  85. fi
  86. done