In [1]:
returns = randn(10000) # create a 1000-day returns vector. Note: don't use 'return' as a variable name, this keyword is reserved.
plot(returns) #plot the time series
grid(True) #use grid in the plot
#-------add labels to the chart
title('Daily returns')
xlabel('day')
ylabel('return [%]')
Out[1]:
In [2]:
h = hist(returns,100) # make a histogram of returns, using 100 bins. h is used as a return variable to avoid text output
print 'Mean:' , returns.mean() # print mean
print 'Standard deviation:%.2f' % returns.std() # print sigma, note the 2 decimal precision notation
In [3]:
price = 100*(returns/100+1).cumprod() # normalized price
plot(price) # plot cumulative sum of returns
title('normalized price') # set plot title
xlabel('day number') # add x label to the chart
ylabel('price [$]') # add y label
Out[3]:
In [4]:
returns = randn(1000,5000) # generate 1000x5000 random return series
prices = 100*(returns/100+1).cumprod(axis=0) # normalized price, you need to specify along which axis
#(0 is along columns) the cumulative product is calculated.
h = plot(prices[:,:500]) # plot cumulative sum of returns. Because plotting all 5000 prices would take a long time, only first 500 are plotted
xlabel('day number')
ylabel('price [$]')
Out[4]:
In [5]:
finalPrice = prices[-1,:] # get the last row of prices table
h = hist(finalPrice,50) # plot histogram with 50 bins
xlabel('price [$]') # add axis labels
ylabel('frequency [#]')
print 'Mean:', finalPrice.mean() # show mean value
print 'Std:', finalPrice.std() # show standard deviation
In [6]:
returns3x = returns*3. # leveraged returns are 3x the non-leveraged returns
prices3x = 100*(returns3x/100+1).cumprod(axis=0) # normalized price, you need to specify along which axis
finalPrice3x = prices3x[-1,:] # get the last row of prices table
h1 = hist(finalPrice3x,500) # plot histogram with 500 bins. more bins are needed to keep the bin width small.
h2 = hist(finalPrice,100) # plot histogram with 100 bins
xlim([0,200]) # set x axis limits to 0..200
legend(['3x leverage','1x leverage'])
print 'Mean 3x:', finalPrice3x.mean()
In [ ]: