Skip to content

Instantly share code, notes, and snippets.

@agarzola
Created May 12, 2016 02:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agarzola/2f659369cc8994f53e34395abaac6292 to your computer and use it in GitHub Desktop.
Save agarzola/2f659369cc8994f53e34395abaac6292 to your computer and use it in GitHub Desktop.
Some functions to get stock info from the command line because who needs friends?
#!/bin/zsh
function stock {
if [[ $* == *--clean* ]]
then
clean=true
else
clean=false
fi
html=$(curl -s https://finviz.com/quote.ashx?t=$1);
[[ $html =~ 'Price<\/td><td[^>]*><b>([0-9\.\-]*)<\/b><\/td>' ]] \
&& price=$match[1] || price='[not found]';
[[ $html =~ 'EPS \(ttm\)<\/td><td[^>]*><b>([0-9\.\-]*)<\/b><\/td>' ]] \
&& eps=$match[1] || eps='[not found]';
[[ $html =~ 'Book\/sh<\/td><td[^>]*><b>([0-9\.\-]*)<\/b><\/td>' ]] \
&& book=$match[1] || book='[not found]';
if [[ $clean == true ]]
then
echo $price\\n$eps\\n$book;
else
echo price: \$$price, eps: $eps, book: $book;
fi
price=''; eps=''; book='';
}
function graham {
s=$(stock $1 --clean);
n=("${(f)s}");
price=$n[1];
eps=$n[2];
book=$n[3];
m=(22.5*$eps*$book);
if [[ $eps < 0 ]]
then
graham=(EPS is negative: $eps);
elif [[ $book < 0 ]]
then
graham=(Book value is negative: $book);
else
graham=$(echo "sqrt ($m)" | bc -l);
fi
echo Graham: \$$(printf "%.3f\n" $graham);
echo Now: \$$price;
s=''; n=''; price=''; eps=''; book=''; graham='';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment