FacebookLinkedInShare

Goal

Few years ago, I worked at the Israel stock market. My task was to build a market analysis simulator; in other words, a complex engine with a web interface which helps users manage their stocks/options portfolios. This post is not a course on market analysis, but we will use this field to implement a PHP class which resolves nice mathematical calculus. Now that NI delivers options to its employees, let’s see how we can track their values and make some speculations :-)

 

Market Introduction

Multiple market types exist but for this post, we will only focus on the Israel options market. There are 2 types of options: PUT and CALL. In summary, the Put gives you the right to sell a stock whereas the Call gives you the right to buy a stock.
Here are the characteristics of the Put/Call:
Strike price: It is a fixed price at which we can buy or sell the asset and is determined at the beginning of the transaction
Expiry date: It is the final date that we have to exercise our right to buy or sell the asset
Premium: Price which the buyer must pay to the seller to acquire an option

 

Ex for a Call:
On January 1st, A buy 100 Calls from B for 60$ each Call with an expiry date of February 1st. During the transaction (January 1st), the asset price is currently at 45$ and A pays to B a premium of 15$ per Call.
On the expiry date (February 1st):
– If the action price is 80$, A uses his Calls and gains (80$60$15$) * 100 = 500$
– If the action price is 50$, A doesn’t use his Calls and loose 15$ * 100 = 1500$

The Call protects against high asset price.

Profits from buying a call

Profits from buying a call

Profits from writing a call

Profits from writing a call

 

Ex for a Put:
On January 1st, A buy 100 Puts from B for 40$ each Put with an expiry date of February 1st. During the transaction (January 1st), the asset price is currently at 45$ and A pays to B a premium of 5$ per Put.
On the expiry date (February 1st):
– If the action price is 20$, A uses his Puts and gains (40$20$5$) * 100 = 1500$
– If the action price is 50$, A doesn’t use his Puts and loose 5$ * 100 = 500$

The Put protects against low asset price.

Payoff from buying a put

Payoff from buying a put

Payoff from writing a put

Payoff from writing a put

 

Black & Scholes

To go straight to the point, we need to buy low and sell high, and the difficulty resides in knowing when to buy/sell assets. No one knows how the asset prices will fluctuate, but some mathematical models exist to estimate the theoretical price of the asset. One of the most famous model is called Black & Scholes and uses the following parameters:

<<<wikipedia start<<<

  • S, be the price of the stock
  • V(S, t), the price of a derivative as a function of time and stock price.
  • C(S, t) the price of a call option
  • P(S, t) the price of a put option.
  • K, the strike price of the option.
  • r, the annualized risk-free interest rate, continuously compounded (the force of interest).
  • \mu, the drift rate of S, annualized.
  • \sigma, the standard deviation of the stock’s returns (the public stress about the option)
  • t, a time in years; we generally use: now=0, expiry=T.
  • \Pi, the value of a portfolio.
  • T - t, is the time to maturity

We will use N(x) to denote the standard normal cumulative distribution function,

standard normal cumulative distribution function

N'(x) will denote the standard normal probability density function,

probability density function

The Black & Scholes equation is as follow:

Black–Scholes equation

Black–Scholes equation

 

 

It means that we can calculate the value of a Put or Call as below:

Call value

Call value

Put value

Put value

<<<wikipedia end<<<

 

Greeks

Greek letters are defined as the sensitivities of the option price to a single-unit change in the value of either a state variable or a parameter (i.e. stock price, volatility, risk-free rate, time to expiration …etc). Such sensitivities can represent the different dimensions to the risk in an option. Each Greek letter has its own meaning and tally with a specific aspect of the asset value fluctuation but we won’t explain it further.

Calls Puts
Delta \frac{\partial C}{\partial S} N(d_1)\, -N(-d_1) = N(d_1) - 1\,
Gamma \frac{\partial^{2} C}{\partial S^{2}} \frac{N'(d_1)}{S\sigma\sqrt{T - t}}\,
Vega \frac{\partial C}{\partial \sigma} S N'(d_1) \sqrt{T-t}\,
Theta \frac{\partial C}{\partial t} -\frac{S N'(d_1) \sigma}{2 \sqrt{T - t}} - rKe^{-r(T - t)}N(d_2)\, -\frac{S N'(d_1) \sigma}{2 \sqrt{T - t}} + rKe^{-r(T - t)}N(-d_2)\,
Rho \frac{\partial C}{\partial r}  K(T - t)e^{-r(T - t)}N( d_2)\, -K(T - t)e^{-r(T - t)}N(-d_2)\,

 

PHP Class

On first sight, it looks like quite frightening, but let’s divide and conquer these formulas ! First we will define the derivative functions, then the Black & Scholes and last the Greeks.

class Simulator
{

	// Normal distribution
	public function N($d_)
	{
		$step = 0.01;
		$sum = 0;
		$x = -5 + $step / 2;
		while(($x < $d_) && ($x < 4))
		{
			$sum = $sum + exp(-$x * $x / 2) * $step;
			$x += $step;
		}
		return $sum / sqrt(2  * pi());
	}
	

	
	// N prime
	public function NPrime($d_)
	{
		return exp(-($d_ * $d_) / 2) / sqrt(2  * pi());
	}



	// d1
	// $stockPrice_		: spot (underlying) price
	// $strikePrice_	: strike (exercise) price
	// $interestRate_	: interest rate
	// $volatility_		: volatility
	// $time_			: time to maturity
	public function D1($stockPrice_, $strikePrice_, $interestRate_, $volatility_, $time_)
	{
		return (log($stockPrice_ / $strikePrice_) + ($interestRate_ + (pow($volatility_, 2) / 2)) * $time_) / ($volatility_ * sqrt($time_));
	}



	// d2
	// $stockPrice_		: spot (underlying) price
	// $strikePrice_	: strike (exercise) price
	// $interestRate_	: interest rate
	// $volatility_		: volatility
	// $time_			: time to maturity
	public function D2($stockPrice_, $strikePrice_, $interestRate_, $volatility_, $time_)
	{
		return (log($stockPrice_ / $strikePrice_) + ($interestRate_ - (pow($volatility_, 2) / 2)) * $time_) / ($volatility_ * sqrt($time_));
	}



	// Option price call black scholes
	// $stockPrice_		: spot (underlying) price
	// $strikePrice_	: strike (exercise) price
	// $interestRate_	: interest rate
	// $sigma_			: volatility
	// $time_			: time to maturity
	public function CallPrice($stockPrice_, $strikePrice_, $interestRate_, $sigma_, $time_)
	{
		$d1 = $this->D1($stockPrice_, $strikePrice_, $interestRate_, $sigma_, $time_);
		$d2 = $this->D2($stockPrice_, $strikePrice_, $interestRate_, $sigma_, $time_);
		return 100 * ($stockPrice_ * $this->N($d1) - $strikePrice_ * exp(-$interestRate_ * $time_) * $this->N($d2));
	}

	
	
	// Option price call black scholes
	// $stockPrice_		: spot (underlying) price
	// $strikePrice_	: strike (exercise) price
	// $interestRate_	: interest rate
	// $sigma_			: volatility
	// $time_			: time to maturity
	public function PutPrice($stockPrice_, $strikePrice_, $interestRate_, $sigma_, $time_)
	{
		$d1 = $this->D1($stockPrice_, $strikePrice_, $interestRate_, $sigma_, $time_);
		$d2 = $this->D2($stockPrice_, $strikePrice_, $interestRate_, $sigma_, $time_);
		return 100 * ($strikePrice_ * exp(-$interestRate_ * $time_) * $this->N(-$d2) - $stockPrice_ * $this->N(-$d1));
	}



	// Delta calculus
	public function Delta($d_)
	{
		return $this->N($d_);
	}
	
	

	// Vega calculus
	public function Vega($stockPrice_, $time_, $nPrime_)
	{
		return $stockPrice_ * sqrt($time_) * $nPrime_ / 100;
	}
	
	

	// Theta Call calculus
	public function Theta_c($stockPrice_, $strikePrice_, $interestRate_, $volatility_, $time_, $d1_, $d2_, $nPrime_)
	{
		return (-$stockPrice_ * $nPrime_ * $volatility_ / (2 * sqrt($time_)) - $interestRate_ * $strikePrice_ * exp(-$interestRate_ * $time_) * $this->N($d2_)) / 365;
	}
	
	

	// Theta Put calculus
	public function Theta_p($stockPrice_, $strikePrice_, $interestRate_, $volatility_, $time_, $d1_, $d2_, $nPrime_)
	{
		return (-$stockPrice_ * $nPrime_ * $volatility_ / (2 * sqrt($time_)) + $interestRate_ * $strikePrice_ * exp(-$interestRate_ * $time_) * $this->N(-$d2_)) / 365;
	}
	

	
	// Gamma calculus
	public function Gamma($stockPrice_, $volatility_, $time_, $nPrime_)
	{
		return $nPrime_ / ($stockPrice_ * $volatility_ * sqrt($time_));
	}
}

Sumary

In this post, we saw how we can build a quite complex mathematical model in php. My next post will concern the architecture of the whole system:

  • Design
  • Real time
  • Big data
  • Interfaces
  • Cron jobs

Thanks and please feel free to comment.

References

Options: http://en.wikipedia.org/wiki/Option_(finance)

Call: http://en.wikipedia.org/wiki/Call_option

Put: http://en.wikipedia.org/wiki/Put_option

Black & Scholes: http://en.wikipedia.org/wiki/Black%E2%80%93Scholes_model or greeksBS for details

Greeks: http://en.wikipedia.org/wiki/Greeks_(finance)