Observer Design Pattern 7/1/2016 CS 631: Behavioral Design Patterns: Observer 1 Outline • Application • Options – Call options – Put options • Option Positions • Option Evaluation • Option Price ticker – C++ classes • Observer Pattern – Design – Analysis 7/1/2016 CS 631: Behavioral Design Patterns: Observer 2 Options: Definitions • A call option gives the holder the right to buy the underlying asset by a certain date for a certain price. • A put option gives the holder the right to sell the underlying asset by a certain date for a certain price. • The price in the contract is known as the strike price. • The date in the contact is known as expiration date or maturity. • American options can be exercised any time up to the expiration date. • European options can be exercised only on the expiration date itself. 7/1/2016 CS 631: Behavioral Design Patterns: Observer 3 Call Option • Example: – An investor buys a European call option with a strike price of $60 to purchase 100 Microsoft shares. – The current stock price is $58. – The expiration date in in 4 months. – The price of an option to purchase one share is $5. • The initial investment is $5 x 100 = $500 • Exercise: – In four months the stock price = $75 • The investor buys 100 shares at $60 and immediately sells them for $75 making a gain of 100 ($75-$60) = $1,500. • The net profit is $1,500 - $500 = $1000 – In four months the stock price <= $60 • The net loss is the initial investment of $500. 7/1/2016 CS 631: Behavioral Design Patterns: Observer 4 Profit ($) Call Option: Profit Chart 25 20 15 10 5 0 -5 -10 0 30 40 50 60 65 75 85 Terminal stock price ($) 7/1/2016 CS 631: Behavioral Design Patterns: Observer 5 Put Option • Example: – An investor buys a European put option with a strike price of $60 to purchase 100 Microsoft shares. – The current stock price is $58. – The expiration date in in 4 months. – The price of an option to purchase one share is $5. • The initial investment is $5 x 100 = $500 • Exercise: – In four months the stock price = $50 • The investor buys 100 shares at $50 and sells them under the contract for $60 making a gain of 100 ($60-$50) = $1,000. • The net profit is $1,000 - $500 = $500 – In four months the stock price >= $60 • The net loss is the initial investment of $500. 7/1/2016 CS 631: Behavioral Design Patterns: Observer 6 Put Option: Profit Chart Profit ($) 60 40 20 0 -20 0 55 60 100 150 Terminal stock price ($) 7/1/2016 CS 631: Behavioral Design Patterns: Observer 7 Option Positions • There two sides to every option contract: – An investor who has taken a long position, i.e. bought the option. – An investor who has taken a short position, i.e. has sold or written the option. – The writer's profit or loss is the reverse of that for the purchaser of the option. • There are four types of option positions: – – – – 7/1/2016 A long position in a call option. A long position in a put option. A short position in a call option. A short position in a put option. CS 631: Behavioral Design Patterns: Observer 8 Option Positions: Payoff • Assume K is the strike price and ST is the final price of the underlying asset. • European option positions can be characterized in terms of the payoff to the investor at maturity. – The payoff from a long position in a European call option is max(ST - K,0) • Reflects the fact that the option will be exercised if ST > K and will not be exercised if ST K. – The payoff to the holder of a short position in the call option is -max(ST - K,0) = min(K - ST,0) – The payoff to the holder of a long position in a put option is max(K - ST,0) – The payoff to the holder of a short position in a put option is -max(K - ST,0) = min(ST - K,0) 7/1/2016 CS 631: Behavioral Design Patterns: Observer 9 Options: Evaluation Let r be the current risk-free interest rate, and p be the option's price. Then the value of a long position in the European call option can be calculated as: f max( ST K ,0)(1 r ) T p Then the value of a short position in the European call option can be calculated as: f p min( K ST )(1 r ) T In other words, the values of long and short positions are: f l PT (1 r ) T p f s p PT (1 r ) T where PT is a payoff at the time of maturity T. 7/1/2016 CS 631: Behavioral Design Patterns: Observer 10 Application: Option Price Ticker • Consider an application that updates values (prices) of different option positions on the same asset, say Microsoft stock. The updates will be displayed during the day when an event happens: – – – – Change in the interest rate. Change in the option price. Change in the strike price. Change in the current asset's price. • The application will be based on the following classes: – MarketWatch: watches the market and updates the values of observed options. – OptionPosition: calculates the value of an option position. 7/1/2016 CS 631: Behavioral Design Patterns: Observer 11 MarketWatch Class class MarketWatch { public: MarketWatch(); void attach(OptionPosition& pos); vector<double> getValues(); double update(); double double double double 7/1/2016 getRate(); getAssetPrice(); getOptionPrice(); getStrikePrice(); CS 631: Behavioral Design Patterns: Observer 12 MarketWatch Class (cont.) protected: vector<OptionPosition> positions; string asset; double double double double rate; assetPrice; optionPrice; strikePrice; }; 7/1/2016 CS 631: Behavioral Design Patterns: Observer 13 MarketWatch Class: Updating State // Check the market and update the // rates and positions if necessary void MarketWatch::update() { bool update_flag = false; double new_rate = Market::getRate(); if (this->rate != new_rate) { this->rate = new_rate; update_flag = true; } ... 7/1/2016 CS 631: Behavioral Design Patterns: Observer 14 Updating State // notify all option positions of // the market update if (update_flag) { for (int i=0; i<this->positions.size(); i++) positions[i].notify(*this); } } 7/1/2016 CS 631: Behavioral Design Patterns: Observer 15 OptionPosition Class class MarketWatch; class OptionPosition { public: OptionPosition(mat) : value(0), maturity(mat); virtual void notify(MarketWatch* mw); virtual double getValue() { return this->value; } 7/1/2016 CS 631: Behavioral Design Patterns: Observer 16 OptionPosition Class (cont.) protected: virtual double getPayoff (double strikePrice, finalPrice) = 0; virtual double getCost (double optionPrice) = 0; double value; double maturity; }; 7/1/2016 CS 631: Behavioral Design Patterns: Observer 17 OptionPosition: notify() Method void OptionPosition::notify(MarketWatch* mw) { double rate mw->getRate(); double assetPrice = mw->getAssetPrice(); double optionPrice = mw->getOptionPrice(); double strikePrice = mw->getStrikePrice(); this->value() = getCost(optionPrice) + getPayoff()*((1+rate)^ (-1.0*this->maturity)); } 7/1/2016 CS 631: Behavioral Design Patterns: Observer 18 OptionPosition: Subclasses class LongCallOptionPosition : public class OpenPosition { protected: virtual getCost(double price) { return –price; } virtual double getPayoff(double strike, double final) { return max(final-strike,0); } }; 7/1/2016 CS 631: Behavioral Design Patterns: Observer 19 Observer: Design 7/1/2016 CS 631: Behavioral Design Patterns: Observer 20 Observer: Analysis • Intent – Define a one-to-many dependency between objects, so that when one object changes state, all its dependents are notified and updated. • Applicability – when an abstraction has two aspects, one dependent on another. – when an object should notify other objects without making concrete assumptions about them. • Participants – Subject (MarketWatch) • knows its observers. – Observer (OptionPosition) • defines a notification and update interface. – ConcreteObserver(LongCallOptionPosition) • implements the Observer interface. 7/1/2016 CS 631: Behavioral Design Patterns: Observer 21