Welcome, guest | Sign In | My Account | Store | Cart

Origin: http://wiki.tcl.tk/636 Author: The original author is unknown.

Here's a simple Tcl script which will fire up a window to track a small portfolio of stocks. Nothing too fancy. I've included an example portfolio of three stocks, and a cash holding. To see your own portfolio, simply edit the 'shares' array, and the 'cash' variable

Tcl, 109 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
#!/bin/sh
 #
 # Portfolio tracking by Vince Darley
 #
 # This file may be freely modified, sold, changed, etc.  If you do make
 # useful changes, I would like a copy, although you aren't obligated to
 # send me anything.
 #
 # Ideas for future improvements: auto-update every N minutes, graphs of
 # total portfolio value over time, ability to record changes in portfolio
 # over time...
 #
 #\
 exec wish "$0"

 package require http

 namespace eval portfolio {}

 set shares(AAPL) 550
 set shares(ORCL) 100
 set shares(INKT) 100
 set cash 3285.74

 ##
  # -------------------------------------------------------------------------
  #
  # "portfolio::getInformation" --
  #
  #  Takes a list of symbols, from Nasdaq, NYSE or even mutual funds etc,
  #  and returns a list of results, one for each symbol given.  Each returned
  #  result is a string containing comma separated values, in the order
  #
  #  symbol, price, date, ...
  #
  #  We use a quote server from yahoo.
  # -------------------------------------------------------------------------
  ##
 proc portfolio::getInformation {symbols} {
     set query "http://quote.yahoo.com"
     append query "/d/quotes.csv?s=[join $symbols +]&f=sl1d1t1c1ohgv&e=.csv"
     set token [http::geturl $query]

     set actual {}
     foreach res [split [string trim [http::data $token]] "\r\n"] {
	 if {$res != ""} {
	     set res [split $res ,]
	     lappend actual [lreplace $res 0 0 [string trim [lindex $res 0] {{}""}]]
	 }
     }
     return $actual
 }

 proc portfolio::dumpData {data} {
     foreach result $data {
	 set items [split $result ","]
	 puts stdout $items
     }
 }

 proc portfolio::createWindow {portfolio} {
     foreach {s q} $portfolio {
	 set w [symbolToWin $s]
	 label ${w}symb -text $s
	 label ${w}price -text ""
	 label ${w}change -text ""
	 label ${w}quantity -text $q
	 label ${w}value -text ""
	 grid ${w}symb ${w}price ${w}change ${w}quantity ${w}value
     }
     button .update -text "Update" -command portfolio::updateAll
     label .total -text ""
     grid .update .total
     wm title . "Portfolio"
     wm withdraw .
     portfolio::updateAll
     wm deiconify .
 }

 proc portfolio::symbolToWin {symbol} {
     return ".[join [string tolower $symbol]]"
 }

 proc portfolio::updateWindow {data} {
     global cash
     if {[info exists cash]} {
	 set total $cash
     } else {
	 set total 0.0
     }
     foreach items $data {
	 set w [symbolToWin [lindex $items 0]]
	 set price [lindex $items 1]
	 set change [lindex $items 4]
	 ${w}price configure -text $price
	 ${w}change configure -text $change
	 set val [expr {[${w}quantity cget -text] * $price}]
	 ${w}value configure -text $val
	 set total [expr {$total + $val}]
     }
     .total configure -text $total
 }

 proc portfolio::updateAll {} {
     global shares
     updateWindow [getInformation [array names shares]]
 }

 portfolio::createWindow [array get shares]
Created by andreas kupries on Tue, 11 Jun 2002 (MIT)
Tcl recipes (162)
andreas kupries's recipes (20)

Required Modules

  • (none specified)

Other Information and Tasks