getting stock data from yahoo finance

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Message
Author
User avatar
Dave_G
Posts: 453
Joined: Thu 21 Jul 2011, 13:53

#31 Post by Dave_G »

OK, I stand corrected.

User avatar
smokey01
Posts: 2813
Joined: Sat 30 Dec 2006, 23:15
Location: South Australia :-(
Contact:

#32 Post by smokey01 »

pstocks work fine from the command line like:

Code: Select all

pstocks goog


but doesn't seem to be able to load the quotepage file and parse the info within.

Cheers

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#33 Post by jpeps »

smokey01 wrote:pstocks work fine from the command line like:

Code: Select all

pstocks goog


but doesn't seem to be able to load the quotepage file and parse the info within.

Cheers
What distro are you using? What do you get when running "pstocks --q" from terminal? What does "cat /root/quotepage" show?"

does [ ${1} == '--q' ] work? alt: [ "$1" == "--q" ]

User avatar
smokey01
Posts: 2813
Joined: Sat 30 Dec 2006, 23:15
Location: South Australia :-(
Contact:

#34 Post by smokey01 »

jpeps wrote:
smokey01 wrote:pstocks work fine from the command line like:

Code: Select all

pstocks goog


but doesn't seem to be able to load the quotepage file and parse the info within.

Cheers
What distro are you using? What do you get when running "pstocks --q" from terminal? What does "cat /root/quotepage" show?"
Lucid 525.
Attachments
pstocks.jpg
(25.7 KiB) Downloaded 748 times
cat.jpg
(16.58 KiB) Downloaded 758 times

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#35 Post by jpeps »

Yeah...the test is failing. Try the alternative posted.


testit script; run "testit --q"

Code: Select all

#!/bin/bash


if [ "$1" == "--q" ]; then 
   echo "passed test 1"
else
   echo "failed test 1"
fi

if [ -f  "/root/quotepage" ]; then 
  echo "passed test 2"
else
  echo "failed test 2"
fi

User avatar
smokey01
Posts: 2813
Joined: Sat 30 Dec 2006, 23:15
Location: South Australia :-(
Contact:

#36 Post by smokey01 »

# ./testit --q
passed test 1
passed test 2
#

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#37 Post by jpeps »

Probably you copied something incorrectly. Everything seems to be working. It's a very simple script. The " passed" test is the test I posted. Try a fresh copy of the edited script

It's definitely the --q test, or it wouldn't try to load a "--Q" symbol. Yet the identical posted test worked fine.

note: I have a hunch you copied the first /bin/ash instead of the second /bin/bash script. That would explain it.

User avatar
smokey01
Posts: 2813
Joined: Sat 30 Dec 2006, 23:15
Location: South Australia :-(
Contact:

#38 Post by smokey01 »

I don't know what I did before but it working fine now.

I needed to change this:

Code: Select all

if [ "$1" == "--q" ]; then
add the extra - before the --q

I don't think I was running it with the --q option either

Code: Select all

pstocks --q
works fine

Thanks

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#39 Post by jpeps »

This version finds the volume columns and divides by 1000, so that volume and/or average volume are more readable. It should work even if you move them around. You can disable by commenting out "DivideVol" It kicks in if there's at least one stock with over 200,000 shares.

It's interesting that I had to reformat the volume numbers with printf '%d' to get them to work in math or test functions.
I think the pstocks -q option works now without the error.

Code: Select all


#!/bin/bash
  
## original pstocks script by Technosaurus, adapted by jpeps  

if [ "$1" == "-h" -o  "$1" == "" ]; then 
   echo 'Usage: pstocks [sym] [sym] ...
       pstocks -q           ## quotepage 
      '
   exit
fi
if [ "$1" == "-q" ]; then

     if [ -f "/root/quotepage" ]; then 
          args="$(cat /root/quotepage)"
     else
          args=""
     fi
else
  args="$@"

fi

DivideVol() {

## Find columns with volume 


 export var="x"

 while read line; do
    N="1"
    var="x"
    while  ([ "$var" ]); do 
    var="$(echo "$line" | cut -d\| -f"${N}")"
     var1=`printf '%d' "$var" 2>/dev/null`
        if [ "$var1" -gt 200000 ]; then  
             if [ ! "$col1" ]; then  
                      col1="$N"
             else
                      col2="$N"
             fi
        fi
    N="$(( ${N} + 1 ))"
    done
 done </tmp/list
## Divide volume by 1000

if [ "$col1" ]; then 
   while read line; do 
     oldvol="$(echo ${line} | cut -d\| -f${col1} )"
      oldvol="$(printf '%d' ${oldvol} 2>/dev/null)"
     newvol="$((${oldvol} / 1000))" 
     sed -i "s/|${oldvol}/|${newvol}/" /tmp/list 
   done </tmp/list
fi

if [ "$col2" ]; then
   while read line; do
      oldavol="$(echo ${line} | cut -d\| -f${col2} )"
      oldavol="$(printf '%d' ${oldavol} 2>/dev/null)"
      newavol="$((${oldavol} / 1000))"
      sed -i "s/|${oldavol}/|${newavol}/" /tmp/list

   done </tmp/list
fi

}
export -f DivideVol



quoteEdit() {

echo -e "`Xdialog --stdout --no-cancel --editbox /root/quotepage 18 70`" >/root/quotepage
}

export -f quoteEdit

wget -q -O - "http://download.finance.yahoo.com/d/quotes.csv?f=nsl1cova2&s='`echo ${args// /,}`'" |tr "," "|"  >/tmp/list

#wget -q -O - "http://uk.finance.yahoo.com/d/quotes.csv?f=nsl1opva2&s='`echo ${@// /,}`'" |tr "," "|"  >/tmp/list

## Remove extra "|" in some names 
while read line; do
 oldname="$( echo ${line} | cut -d\" -f2)" 
 newname="$( echo ${line} | cut -d\" -f2 | tr -d "|" )"

sed -i "s/${oldname}/${newname}/g"  /tmp/list
 
 done </tmp/list

DivideVol


GTKDIALOG="$(which gtkdialog)"
[ "$GTKDIALOG" ] || GTKDIALOG="gtkdialog"

export F='
<window title="Pstocks" icon-name="gtk-index" default_height="360" default_width="700"> 
<vbox>
<tree hover-selection="true" rules_hint="true"> 
<label>Name|Symbol|Quote|Change|Opening|Vol (000)|Avg Vol</label> 
<input>cat /tmp/list</input> 
</tree>
<hbox>
<button tooltip-text="Edit the symbols file">
  <label>Edit</label>
  <action>quoteEdit</action>
</button>
<button ok></button>
</hbox>
</vbox> 
</window> 
' 
"$GTKDIALOG" -p F 
Last edited by jpeps on Tue 03 Jan 2012, 15:42, edited 1 time in total.

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

#40 Post by vovchik »

Dear jpeps,

Nice work. I would change the <tree> line to this:

Code: Select all

<tree hover-selection="true" rules_hint="true">
just to get stripes. It makes lists easier to read.

With kind regards and Happy New Year,
vovchik

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#41 Post by jpeps »

vovchik wrote:Dear jpeps,

Nice work. I would change the <tree> line to this:

Code: Select all

<tree hover-selection="true" rules_hint="true">
just to get stripes. It makes lists easier to read.

With kind regards and Happy New Year,
vovchik
Thanks vovchik. Good idea...done! Best wishes to you as well.

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#42 Post by seaside »

jpeps,

Just tried it out -works very nicely, thanks.

Now for a feature request-
** Button (Stocks - Guaranteed to double and never drop) **

Code: Select all

<action>only retrieve stocks in above category</action>
It could be a New Year's high resolution :) :)

Regards,
s

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#43 Post by jpeps »

seaside wrote:jpeps,

Just tried it out -works very nicely, thanks.

Now for a feature request-
** Button (Stocks - Guaranteed to double and never drop) **

Code: Select all

<action>only retrieve stocks in above category</action>
It could be a New Year's high resolution :) :)

Regards,
s
ah...a button that automatically shorts all your positions; interesting idea.

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#44 Post by technosaurus »

This is what I love about the Puppy community. You post 5 lines of code and the community builds it into something useful. Anywhere else, it would sift itself to the no reply code dungeon. Great job guys.
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

Stock-ticker

#45 Post by seaside »

Now you can watch a list of stocks on a ticker notifier located in the JWM tray. Information is stock symbol (only room for 3 chars) and last bid price (limited to 5 digits). The background is green if the stock is up, red if down and blue if unchanged. The stocks run in a repeating loop.

If you left-click the icon, you can edit the file containing the list of stock symbols to watch. Middle-clicking the icon kills the program.

You'll need yad version 16.2.

Go Green, :)
s

Code: Select all

#!/bin/bash 
# stock ticker- seaside February 6, 2012
# yad version 16.2 required
# stock retrieval ideas from technosaurus

PIPE=/tmp/stockpipe
mkfifo $PIPE
exec 3<> $PIPE
trap on_exit EXIT


function on_exit () {
    echo "quit" >&3
    rm -f $PIPE
}

export -f on_exit

yad --notification --kill-parent --listen \
    --image=gtk-stop --text="Stock Ticker" \
    --command="geany /root/.stocksfile.txt" <&3 &
    
# set sample stock info
    
[[ ! -f /root/.stocksfile.txt ]] && echo 'ibm
dis
goog ' >/root/.stocksfile.txt

LINE=1

while true; do


STK=`sed -n "${LINE}p" /root/.stocksfile.txt`

[ "$STK" = "" ] && LINE=1 && STK=`sed -n "${LINE}p" /root/.stocksfile.txt` #ran out of lines


QUOTE=`wget -q -O - "http://download.finance.yahoo.com/d/quotes.csv?f=sb3c1&s=$STK"` 
#returns "IBM",180.27,-1.77 

 oldIFS=$IFS
 IFS=","
  read STOCK BID CHANGE<<<"$QUOTE"
 IFS=$oldIFS
 
 STOCK="${STOCK//\"/}" 
 STOCK="${STOCK:0:3}"
 BID="${BID:0:5}" 

  case $CHANGE in
 -*)  BG=#FF6F55 ;; #light red
 +*)  BG=#90EE90 ;; #light green
 *)  BG=#C7E7F1 ;;  #light blue
 esac

  
  # technosaurus' use of svg for icons
	echo '<svg>
   <rect width="48" height="48" x="0" y="0"  
     style="fill:'"$BG"'"/>
   <text x="0.79569316" y="22"  
     style="font-size:22px"
     >'"$STOCK"'</text>
   <text x="1" y="42" 
     style="font-size:18px;font-weight:bold"
     >'"$BID"'</text>
 </svg> ' >/tmp/stockicon.svg
	 
	echo icon:/tmp/stockicon.svg >$PIPE # send notice to change icon
		
  
  LINE=$[LINE+1]
    
  sleep 3
  # Wait before checking again.

done
Attachments
stock-ticker.png
Stock ticker notifier
(8.57 KiB) Downloaded 536 times

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#46 Post by jpeps »

Cute! Glad it eliminates all those pesky 4 letter stocks, like MSFT.

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#47 Post by jpeps »

seaside's script; reads 4 char symbols

Code: Select all

#!/bin/sh


#!/bin/bash
# stock ticker- seaside February 6, 2012
# yad version 16.2 required
# stock retrieval ideas from technosaurus

symlist="/root/quotepage"

PIPE=/tmp/stockpipe
mkfifo $PIPE
exec 3<> $PIPE
trap on_exit EXIT


function on_exit () {
    echo "quit" >&3
    rm -f $PIPE
}

export -f on_exit

yad --notification --kill-parent --listen \
    --image=gtk-stop --text="Stock Ticker" \
    --command="geany $symlist" <&3 &
   
# set sample stock info
   
[[ ! -f $symlist ]] && echo 'ibm
dis
goog ' >$symlist

LINE=1

while true; do


STK=`sed -n "${LINE}p" $symlist`

[ "$STK" = "" ] && LINE=1 && STK=`sed -n "${LINE}p" $symlist` #ran out of lines


QUOTE=`wget -q -O - "http://download.finance.yahoo.com/d/quotes.csv?f=sb3c1&s=$STK"`
#returns "IBM",180.27,-1.77

 oldIFS=$IFS
 IFS=","
  read STOCK BID CHANGE<<<"$QUOTE"
 IFS=$oldIFS
 
 STOCK="${STOCK//\"/}"
 STOCK="${STOCK:0:4}"
 BID="${BID:0:5}"

  case $CHANGE in
 -*)  BG=#FF6F55 ;; #light red
 +*)  BG=#90EE90 ;; #light green
 *)  BG=#C7E7F1 ;;  #light blue
 esac

 
  # technosaurus' use of svg for icons
   echo '<svg>
   <rect width="64" height="64" x="0" y="0" 
     style="fill:'"$BG"'"/>
   <text x="0.79569316" y="22" 
     style="font-size:22px"
     >'"$STOCK"'</text>
   <text x="1" y="48"
     style="font-size:22px;font-weight:bold"
     >'"$BID"'</text>
 </svg> ' >/tmp/stockicon.svg
   
   echo icon:/tmp/stockicon.svg >$PIPE # send notice to change icon
      
 
  LINE=$[LINE+1]
  
  sleep 3
  # Wait before checking again.

done 
Last edited by jpeps on Tue 07 Feb 2012, 16:49, edited 1 time in total.

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#48 Post by seaside »

jpeps wrote:seaside's script; reads 4 char symbols
jpeps,

Great! I don't know how you squeezed that extra char out by increasing the bottom price pixel to 22. It is counter-intuitive and absolutely beyond me. :) :)

Anyway, no one should have any excuses for losing money on the stock market, given these tools. :)

Regards,
s

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#49 Post by jpeps »

seaside wrote:
jpeps wrote:seaside's script; reads 4 char symbols
jpeps,

Great! I don't know how you squeezed that extra char out by increasing the bottom price pixel to 22. It is counter-intuitive and absolutely beyond me. :) :)

Anyway, no one should have any excuses for losing money on the stock market, given these tools. :)

Regards,
s
The icon size appears to be fixed, so increasing the rect ht, width numbers scales the font size smaller. When I don't understand something, trial & error often works wonders!

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#50 Post by seaside »

jpeps,

Good find. It just never even occurred to me to change the width height bigger than the tray icon.

Regards,
s
(Of course, the hard part then is to see exactly what the effect is with each change..... maybe a GUI for that :) )

Post Reply