Category Archives: Portfolio
Get Stock Quote from Goolge Web API via Python
Posted by Shawn Zhang
on December 27, 2012
No comments
Background
Google has officially announce that it won't sustain ongoing of Finance API, but there still a available API via URL API . Return data from web server is xml format . We need to parse the return text in Python and extract data .
Get the Stock Data
Google Web API format (  http://www.google.com/ig/api?stock=C ) , this will request market data for security code "C" ( Citigroup :) )  xml return result:
<xml_api_reply version="1">
<finance module_id="0" Read more [...]
Implement VaR back testing in Python
Posted by Shawn Zhang
on November 16, 2012
No comments
Introduction
We are going to model a VaR Â back testing in Python, including customize confidence level, span of testing horizon, and statistic testing on VaR model and binomial test .
even in most case , functional programming will supply this practice enough , but I'll do it in OO for a more clear demonstration.
Model a VaR
let's see full code first, and broke them into segments and analysis by parts .
class VaR():
def __init__(self, return_list, conf = 0.95, n = 100):
self.return_list Read more [...]
Overloading in Python Class using Bond Class
Posted by Shawn Zhang
on November 12, 2012
No comments
I'm going to have a snippet code about Construct a Bond Class using overloading technics.
Frist, we create a Bond class in Python :
class Bond:
def __init__(self, price = 0, face_value=100, maturity=0, interest_rate=0, num = 0):
self.price = price
self.face_value = face_value
self.interest_rate = interest_rate
self.maturity = maturity
self.num = num
def __repr__(self):
return ''' Bond: Price: %s Number: %s Maturity: Read more [...]
Construction Bond Portfolio Management in a Object-Oriented R Way
Posted by Shawn Zhang
on July 8, 2012
No comments
I've been read a lot of articles, blogs and other online resources about programming R, most of them discuss a lot about it's powerful statistic features. But there are less topics about it's Object-Oriented features, I'm going to navigate these features to construct a Portfolio Management. (PS: all following R code will be in a S4 Style)
Define a Bond Class
A portfolio means a collection of assets, asset could be different financial products ,like Bond, Future, Stock or other derivatives . We Read more [...]
VaR Measurement with PerformanceAnalystics in R
Posted by Shawn Zhang
on July 5, 2012
No comments
VaR is most popular risk measurement of downside risk in recent years. It can aggregate risk across products departments ,and even across different risk type. Â I am going to dive into the VaR() function in the package "PerformanceAnalystics"
Here  is full parameter of function VaR()
VaR(R = NULL, p = 0.95, method = c("modified", "gaussian","historical", "kernel")...)
R can be assign a data.frame, vector, matrix, or xts, timeSeries object etc . p is assigned Read more [...]
Implement Portfolio Variance Calculation in R
Posted by Shawn Zhang
on May 31, 2012
No comments
It is critical to calculate total variance of portfolio given inputs of variance of assets within the portfolio .  There is a little hardship in dealing with the correlation of the assets .  Interaction for each pair of assets should be calculated separately. (See second term in below )
if we construct a function with input two vector, (weight vector and volatility vector) it is quite easy to implement first term with R by above:
sum(w^2 * s^2)
for the second term , we need a correlation Read more [...]
Portfolio Return Calculation in R (Code snippet)
Posted by Shawn Zhang
on March 16, 2012
No comments
There are three common way to calculate a returns on a portfolio :
Geometric Average Return:
Formula: Â
#geometri average return
GeoAvgRetrun <- function(r){
return((prod(1+r))^(1/length(r)) - 1)
}
  Tips:  prod  is the product of every element given in the arguments .
> prod(c(2,3,5))
[1] 30
Arithmetic Average Return:
  Formula: Read more [...]
#geometri average return
GeoAvgRetrun <- function(r){
return((prod(1+r))^(1/length(r)) - 1)
}
  Tips:  prod  is the product of every element given in the arguments .
> prod(c(2,3,5))
[1] 30
Arithmetic Average Return:
  Formula: Read more [...]