Package QSTK :: Package Bin :: Module converter
[hide private]
[frames] | no frames]

Source Code for Module QSTK.Bin.converter

 1  #
 
 2  # fundsToPNG.py
 
 3  #
 
 4  # Short script which produces a graph of funds 
 
 5  # over time from a pickle file.
 
 6  #
 
 7  # Drew Bratcher
 
 8  #
 
 9  
 
10  from pylab import * 
11  from qstkutil import DataAccess as da 
12  from qstkutil import tsutil as tsu 
13  from quicksim import quickSim 
14  from copy import deepcopy 
15  import math 
16  from pandas import * 
17  import matplotlib.pyplot as plt 
18  import cPickle 
19  
 
20 -def fundsToPNG(funds,output_file):
21 plt.clf() 22 if(type(funds)==type(list())): 23 for i in range(0,len(funds)): 24 plt.plot(funds[i].index,funds[i].values) 25 else: 26 plt.plot(funds.index,funds.values) 27 plt.ylabel('Fund Value') 28 plt.xlabel('Date') 29 plt.draw() 30 savefig(output_file, format='png')
31
32 -def fundsAnalysisToPNG(funds,output_file):
33 plt.clf() 34 if(type(funds)!=type(list())): 35 print 'fundsmatrix only contains one timeseries, not able to analyze.' 36 #convert to daily returns 37 count=list() 38 dates=list() 39 sum=list() 40 for i in range(0,len(funds)): 41 ret=tsu.daily(funds[i].values) 42 for j in range(0, len(ret)): 43 if (funds[i].index[j] in dates): 44 sum[dates.index(funds[i].index[j])]+=ret[j] 45 count[dates.index(funds[i].index[j])]+=1 46 else: 47 dates.append(funds[i].index[j]) 48 count.append(1) 49 sum.append(ret[j]) 50 #compute average 51 tot_ret=deepcopy(sum) 52 for i in range(0,len(sum)): 53 tot_ret[i]=sum[i]/count[i] 54 55 #compute std 56 std=zeros(len(sum)) 57 for i in range(0,len(funds)): 58 temp=tsu.daily(funds[i].values) 59 for j in range(0,len(temp)): 60 std[dates.index(funds[i].index[j])]=0 61 std[dates.index(funds[i].index[j])]+=math.pow(temp[j]-tot_ret[dates.index(funds[i].index[j])],2) 62 63 for i in range(1, len(std)): 64 # std[i]=math.sqrt(std[i]/count[i])+std[i-1] 65 std[i]=math.sqrt(std[i]/count[i]) 66 67 #compute total returns 68 lower=deepcopy(tot_ret) 69 upper=deepcopy(tot_ret) 70 tot_ret[0]=funds[0].values[0] 71 lower[0]=funds[0].values[0] 72 upper[0]=lower[0] 73 # for i in range(1,len(tot_ret)): 74 # tot_ret[i]=tot_ret[i-1]+(tot_ret[i])*tot_ret[i-1] 75 # lower[i]=tot_ret[i-1]-(std[i])*tot_ret[i-1] 76 # upper[i]=tot_ret[i-1]+(std[i])*tot_ret[i-1] 77 for i in range(1,len(tot_ret)): 78 lower[i]=(tot_ret[i]-std[i]+1)*lower[i-1] 79 upper[i]=(tot_ret[i]+std[i]+1)*upper[i-1] 80 tot_ret[i]=(tot_ret[i]+1)*tot_ret[i-1] 81 82 83 plt.clf() 84 plt.plot(dates,tot_ret) 85 plt.plot(dates,lower) 86 plt.plot(dates,upper) 87 plt.legend(('Tot_Ret','Lower','Upper'),loc='upper left') 88 plt.ylabel('Fund Total Return') 89 plt.ylim(ymin=0,ymax=2*tot_ret[0]) 90 plt.draw() 91 savefig(output_file, format='png')
92