Category Archives: Finance
RMBS Prototype Model in Python
Posted by Shawn Zhang
on April 18, 2013
No comments
Parameters
Parameters including : Total balance , Attach point, mortgage rate , Tranche Rate, Payment term and Prepayment CPR .
Cash Flow Chart result:
Full code(2 functions):
def rmbs_show(self):
self.clr_update_frame()
Label( self.update_frame, text="CPR" ).grid( row = 0, column= 0 )
w = Scale( self.update_frame, from_=0.01, to=0.20, resolution = 0.005, orient=HORIZONTAL, variable=self.rmbs_cpr, showvalue = True )
w.grid(row = Read more [...]
A Walk-through on Modeling MBS/Tranche in C#
Posted by Shawn Zhang
on April 12, 2013
No comments
Modeling on MBS/Tranche involves 3 entities . Mortgages , Mortgage Pool and Tranches . Mortgage Pool is key in this relationship . Mortgage Pool collects cash flows from Mortgages and redistribute into different Tranches.
Class MortgagesPool
One Mortgage Pool has many Mortgages and One Mortgage Pool has many Tranches. So , the class of Mortgage Pool may like this :
class MortgagesPool{
public ArrayList tranches = new ArrayList();
public ArrayList mortgages = new ArrayList();
// add a Read more [...]
Bond Calcuation with C#
Posted by Shawn Zhang
on April 11, 2013
No comments
I've been read a tutorial about C# in last 2 days . Before that, I haven't read a single line of C# code in since I started to programming. C# syntax is very similar to C++ and Java . But develop in C# is not easy as its syntax seems. I've wrote some trivial codes about some financial calculation of a bond, it's quite similar to the code I've written before in Python, but this one focus more in a risk neutral perspective.
Basic Bond Class with No Coupon
class ZeroBond{
protected float Read more [...]
Get Stock Quote Price API from Yahoo Finance via Python
Posted by Shawn Zhang
on December 28, 2012
No comments
Difference against Google Finance / Sina.com
Yahoo provide more rich function regarding to the Stock API than Google / Sina.com does. particular ,it provide customize return format which customized by supply a parameter in calling URL . Full parameter specification pls refer to this link .(credit to Kelly Elias ) .
200 calls per second , you will be warned if you exceed this limits
csv file returned
No market depth as SIna.com provide
Provide stock indication such as " Earnings Read more [...]
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 Durbin-Watson Auto-correlation test in Python
Posted by Shawn Zhang
on November 22, 2012
No comments
Assumption Under Single Regression
In a single factor regression test, there are 3 assumptions about the error item:
error term follows a normal distribution
error term are uncorrelated
error term has same variance (homoskedasticity)
we focus test on Assumption 2. Given a list of error item , how can we infer that it is not auto-correlated ?
In statistics, the Durbin–Watson statistic is a test statistic used to detect the presence of autocorrelation (a relationship between values 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 [...]
Black–Scholes Option Pricing/Greeks in Python
Posted by Shawn Zhang
on November 14, 2012
No comments
Black-Scholes Option Pricing Equation is widely accepted in industry, in this post, I'd like to dive into a guide on how to implement BS equation in Python.
BS formula to calculate EURO call Option:
C : price of the European call option , r : risk free rate, t : time to maturity
S : Spot price , K: Strike price,
: cumulative distribution function of the standard normal distribution
$$d1 Read more [...]
C : price of the European call option , r : risk free rate, t : time to maturity
S : Spot price , K: Strike price,
: cumulative distribution function of the standard normal distribution
$$d1 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 [...]