Friday, August 21, 2009

Pricing European Options by using Monte Carlo Simulation


In my previous article, price of european option (put and call) is estimated by using binomial model (Cox-Ross-Rubinstein (CRR) approach [1]). In this article, i provide java and Scilab (similar to Matlab)
source code to estimate these option prices by Monte Carlo simulation.

In binomial model, intrinsic value of an asset (S_T) at expiry t
ime (T) is estimated with a sequence of discrete time steps, at each step, stock price is estimated with a probability (either down or up probability. In monte carlo simulation, intrinsic value of an asset (S_T) at expiry time (T) is obtained from a normal distribution such as [2]:
where, r is annual interest rate S_t asset price at time t and sigma is volatility and x is normal distribution variable.

Having estimated S_T, option's payoff can be estimated easily (max (0, X-S_T) or max (0, S_T-X) for call and put options respectively, where X is options strike, exercise price). Based on expected values of pay off functions which generated with monte carlo simulation, option prices can be calculated as follows for european call option:


Source code of this estimation in java and
Scilab is listed


1:  // SciLab (Matlab) Source code
2: // Estimate European Option price by Monte Carlo Simulation
3: // denizstij (http://denizstij.blogspot.com/),
4: // Aug-2009
5: asset=230; //S
6: strike=210;//X
7: volatility=0.25; //sigma
8: r=0.04545;
9: time=0.5;
10: num_sims=10000;
11: R= (r-0.5*volatility^2)*time;
12: SD=volatility*sqrt(time);
13: sum_call_payoffs=0.0;
14: sum_put_payoffs=0.0;
15: for i=0:num_sims,
16: S_T= asset*exp(R+SD*rand(1,'normal'));
17: sum_call_payoffs=sum_call_payoffs+max([0,S_T-strike]);
18: sum_put_payoffs=sum_put_payoffs+max([0,strike-S_T]);
19: end
20: eu_call_option_price= exp(-r*time)*(sum_call_payoffs/double(num_sims))
21: eu_put_option_price= exp(-r*time)*(sum_put_payoffs/double(num_sims))


1:  package com.denizstij.finance.pricing;
2: import java.util.ArrayList;
3: import java.util.List;
4: import java.util.Random;
5: /**
6: *
7: * Estimate European Option price by Monte Carlo Simulation
8: * @author denizstij (http://denizstij.blogspot.com/)
9: * Aug-2009
10: *
11: */
12: public class EuropeanOptionPricingByMonteCarlo {
13: /**
14: * Estimate European Option price by Monte Carlo Simulation
15: *
16: * @param asset Current Asset Price
17: * @param strike Exercise Price
18: * @param volatility Annual volatility
19: * @param intRate Annual interest rate
20: * @param expiry: Time to maturity (in terms of year)
21: * @param num_sim : Number of simulations
22: * @return Put and call price of european options based on
23: * Monte Carlo Simulation
24: */
25: public strictfp List<Double> estimatePrice(double asset,
26: double strike,
27: double volatility,
28: double intRate,
29: double time,
30: int num_sim) {
31: List<Double> results = new ArrayList<Double>();
32: double R = (intRate - 0.5 *Math.pow(volatility,2))*time;
33: double SD = volatility * Math.sqrt(time);
34: double dF = Math.exp(-intRate*time); // discount Factor
35: double sumCallPayoffs=0.0;
36: double sumPutPayoffs=0.0;
37: Random random= new Random();
38: for (int i = 0; i <= num_sim; i++) {
39: double nextGaussian = random.nextGaussian();
40: double S_T= asset*Math.exp(R+SD*nextGaussian);
41: sumCallPayoffs+=callPayOff(S_T,strike);
42: sumPutPayoffs+=putPayOff(S_T,strike);
43: }
44: double callOptionPrices= dF*sumCallPayoffs/num_sim;
45: double putOptionPrices= dF*sumPutPayoffs/num_sim;
46: results.add(callOptionPrices);
47: results.add(putOptionPrices);
48: return results;
49: }
50: // Pay off method for put options
51: private double putPayOff(double stockPrice, double strike) {
52: return Math.max(strike - stockPrice, 0);
53: }
54: // Pay off method for call options
55: private double callPayOff(double stockPrice, double strike) {
56: return Math.max(stockPrice - strike, 0);
57: }
58: public static void main(String args[]) {
59: EuropeanOptionPricingByMonteCarlo euOptionPricing = new EuropeanOptionPricingByMonteCarlo();
60: List<Double> results = euOptionPricing.estimatePrice(
61: 230,
62: 210,
63: 0.25,
64: 0.04545,
65: 0.5, // In terms of year
66: 10000);
67: Double callOptionPrice = results.get(0);
68: Double putOptionPrice = results.get(1);
69: System.out.println("call Option Price:" + callOptionPrice);
70: System.out.println("put Option Price:" + putOptionPrice);
71: }
72: }

Wednesday, August 19, 2009

Pricing European Options by using Binomial Model

Binomial Model is one of the simplest pricing model for European options. Below you can find an implementation of Cox-Ross-Rubinstein (CRR) approach [1] in java for pricing of put and call European options.

With following parameters, sensitivity of the binomial pricing model as function of number of time steps can be seen in figure 1. With high number of time steps, binomial model with CRR approach converges with optimal Black-Scholes formula (e.x: 30.741 for call option)

Figure 1: Option price as a function of number of time steps.

1:  package com.denizstij.finance.pricing;
2:
3: import java.util.ArrayList;
4: import java.util.List;
5:
6: /**
7: *
8: * Estimate European Option price based on Cox, Ross and Rubinstein model
9: * @author denizstij (http://denizstij.blogspot.com/)
10: *
11: */
12: public strictfp class EuropeanOptionPricingByBinomial {
13:
14: /**
15: * Estimate European Option price based on Cox, Ross and Rubinstein model
16: *
17: * @param asset Current Asset Price
18: * @param strike Exercise Price
19: * @param volatility Annual volatility
20: * @param intRate Annual interest rate
21: * @param expiry: Time to maturity (in terms of year)
22: * @param steps : Number of steps
23: * @return Put and call price of european options based on Cox, Ross and Rubinstein model
24: */
25: public List<Double> estimatePrice(double asset,
26: double strike,
27: double volatility,
28: double intRate,
29: double expiry,
30: int steps) {
31: List<Double> results = new ArrayList<Double>();
32:
33: List<Double> stockPrices = new ArrayList<Double>();
34: List<Double> callOptionPrices = new ArrayList<Double>();
35: List<Double> putOptionPrices = new ArrayList<Double>();
36:
37: double time_step = (expiry) / steps;
38: double R = Math.exp(intRate * time_step);
39: double dF = 1 / R; // discount Factor
40:
41: double u = Math.exp(volatility * Math.sqrt(time_step)); // up boundary
42: double d = 1 / u; // down boundary (Cox, Ross and Rubinstein constraint)
43: // at leaf node, price difference factor between each node
44: double uu = u * u; // (u*d)
45: double p_up = (R - d) / (u - d); // up probability
46: double p_down = 1 - p_up; // down probability
47:
48: // initiliaze stock prices
49: for (int i = 0; i <= steps; i++) {
50: stockPrices.add(i, 0.0d);
51: }
52:
53: double sDown = asset * Math.pow(d, steps);
54: stockPrices.set(0, sDown);
55:
56: // Estimate stock prices in leaf nodes
57: for (int i = 1; i <= steps; i++) {
58: double sD = uu * stockPrices.get(i - 1);
59: stockPrices.set(i, sD);
60: }
61:
62: // estimate option's intrinsic values at leaf nodes
63: for (int i = 0; i <= steps; i++) {
64: double callOptionPrice = callPayOff(stockPrices.get(i), strike);
65: callOptionPrices.add(i, callOptionPrice);
66: double putOptionPrice = putPayOff(stockPrices.get(i), strike);
67: putOptionPrices.add(i, putOptionPrice);
68: }
69:
70: // and lets estimate option prices
71: for (int i = steps; i > 0; i--) {
72: for (int j = 0; j <= i - 1; j++) {
73: double callV = dF*(p_up * callOptionPrices.get(j + 1) +
74: p_down* callOptionPrices.get(j));
75: callOptionPrices.set(j, callV);
76: double putV = dF*(p_up * putOptionPrices.get(j + 1) +
77: p_down * putOptionPrices.get(j));
78: putOptionPrices.set(j, putV);
79: }
80: }
81:
82: // first elements holds option's price
83: results.add(callOptionPrices.get(0));
84: results.add(putOptionPrices.get(0));
85: return results;
86: }
87:
88: // Pay off method for put options
89: private double putPayOff(double stockPrice, double strike) {
90: return Math.max(strike - stockPrice, 0);
91: }
92:
93: // Pay off method for call options
94: private double callPayOff(double stockPrice, double strike) {
95: return Math.max(stockPrice - strike, 0);
96: }
97:
98: public static void main(String args[]) {
99:
100: EuropeanOptionPricingByBinomial euOptionPricing = new EuropeanOptionPricingByBinomial();
101: List<Double> results = euOptionPricing.estimatePrice(
102: 230,
103: 210,
104: 0.25,
105: 0.04545,
106: 0.5, // In terms of year
107: 10);
108: Double callOptionPrice = results.get(0);
109: Double putOptionPrice = results.get(1);
110: System.out.println("call Option Price:" + callOptionPrice);
111: System.out.println("put Option Price:" + putOptionPrice);
112: }
113: }
114:

Tuesday, August 11, 2009

Volume Weighted Average Price (VWAP)

Recently i was skimming through an ebook about electronic trading (Electronic and Algorithmic Trading Technology) after reading this news article. This ebook outlines algorithmic trading in a concise way. According to this book and wikipedia entry, more than half of the orders in London Stock Exchange in the last year were entered by algo traders.

According to the ebook, one of the fundamental and basic algo trading is Volume Weighted Average Price (VWAP). VWAP is the ratio of value traded to total quantity of trades in a time period. It is average price of an security in terms of quantity. It is calculated as follows:



VWAP algorithm implies that at a given time if VWAP is greater than the price of share, then the share is under valued and it is a good candidate for buying. But if VWAP is lesser than the price of share, then it should be considered for selling. Even though, it is a very simple algorithm, it and its variations are commonly used in electronic trading.

I have implemented a sample java application (algo trading) which estimates VWAP of an equity by fetching real time (15 min delayed) financial quotes from Yahoo Finance, UK. Equity quotes (15 min delated) can be downloaded free of charge from yahoo finance pages in CSV format. The sample application periodically downloads predetermined quotes (for example VODAFONE GRP --VOD.L) in CSV format and estimates VWAP. Unfortonately, since downloaded data do not consists of trade quantity values (CSV files has following data: symbol, mid trade, time, date, change, lowest trade, highest trade and volume), quantity value for each quote is estimated based on the volume and mid price.

The sample application has multi layered architecture with observer design pattern. It uses Apache HTTPClient to download CVS files periodically from yahoo finance pages. After quotes are extracted from CSV files, they are appended to a linkedlist based time series and then observers (VWAP estimator and then VWAP clients(UI layer)) are notified for the latest updates.

Click here to download source code of the sample application (algo trading) in java.