edexter
unread,Aug 15, 2010, 6:41:53 PM8/15/10Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Sign in to report message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to dexrow software and programming group
import glob
import ystockquote
# example program to autogenerate a matplotlab chart of portfolio
values using
# ystockquote each catagory you want to chart will be in a new
textfile and the
# columns are 0 ... catogory name 1 .... stock symbol and 2 ....
number of shares..currency
name = []
value = []
gcolors = []
totalvalue = 0
gdp = ystockquote.get_price('gbpusd=x')
print gdp
for files in glob.glob('*.txt'):
olddata = ''
#firstline = 'true'
openfile = open(files,'r')
totalvalue = 0
colornum = 1
lastcol = ''
for line in openfile:
#print data[0]
data = line.split(' ')
print data[0]
if data[0] != olddata:
name.append(data[0])
olddata = data[0]
#colornum = colornum + 1
if colornum == 1:
gcolors.append('red')
colornum = 2
lastcol = 'red'
if colornum == 2:
gcolors.append('blue')
colornum = 3
if colornum == 3:
gcolors.append('green')
colornum = 4
if colornum == 4:
gcolors.append('brown')
if lastcol == 'red':
colornum = 5
if lastcol == 'white':
colornum = 1
if colornum == 5:
gcolors.append('white')
colornum = 2
lastcol = 'white'
#firstline == 'false'
try:
price = float(ystockquote.get_price(data[1]))
try:
if data[3] == 'gbp':
price = (price * float(gdp) * .01)
print gdp, price
except:
asdfgjlkfa = 0
totalvalue = totalvalue + (price * float(data[2]))
print totalvalue
#print \n
except:
print 'bad data'
print data[0]
if totalvalue > 0:
value.append(str(totalvalue))
#print name
#print value
eol = '\n'
outfile = open('pychart.py','w')
outfile.write('from pylab import *')
outfile.write(eol)
outfile.write('figure(1, figsize=(6,6))')
outfile.write(eol)
outfile.write('ax = axes([0.1, 0.1, 0.8, 0.8])')
outfile.write(eol)
tv = 0 #total value get all the fractions
fracss = []
for v in value:
tv = tv + float(v)
for l in value: #compute the fractions
l2 = float(l)/tv
fracss.append(str(l2))
#this writes out the colors so that we don't write out on the black
outfile.write("""mycolors=""")
fn = 0
for c in gcolors:
if fn > 0:
outfile.write(', ')
outfile.write("""'""")
outfile.write(c)
outfile.write("""'""")
fn = 1
outfile.write(eol)
#this writes the line where labels = with all the names hardwired
outfile.write(eol)
outfile.write("""labels = """)
fn = 0 #is this the first in the list
for n in name:
if fn > 0:
outfile.write(', ')
outfile.write("""'""")
outfile.write(n)
outfile.write("""'""")
fn = 1
outfile.write(eol)
#This writes out the line with the fracs with all the percentages
hardwired
outfile.write("""fracs = [""")
ff = 0 #first frac?? don't want a coma befoure the number
for lo in fracss:
if ff > 0:
outfile.write(',')
outfile.write(lo)
outfile.write(' ')
ff = 1
outfile.write(']')
outfile.write(eol)
outfile.write('explode=(0,0.05,0,0)')
outfile.write(eol)
outfile.write("""pie(fracs, labels=labels, autopct='%1.1f%%',
shadow=True, colors = mycolors)""")
outfile.write(eol)
outfile.write("""title('Portfolio Percentages',
bbox={'facecolor':'0.8', 'pad':5})""")
outfile.write(eol)
outfile.write('show()')
outfile.write(eol)