Title: Strategy Design Pattern
1Strategy Design Pattern
2Outline
- Example
- Forward Price and Delivery Price
- Forward Price and Spot Price
- C Classes
- Strategy Pattern
- Design
- Analysis
- Strategy and C Policy
3Forward Price and Delivery Price
- The forward price is the market price that would
be agreed to today for delivery in the future. - For example, 1 ounce of gold costs 315 today to
be delivered in one year. - 315 becomes the delivery price of 1-year forward
contract. - As we move through time the delivery price does
not change, but the forward price is likely to do
so. - In 6 months the 1-year forward price may change
to 295 if the market conditions change.
4Forward Price and Spot Price
- We illustrate the reason why spot and forward
prices are related. - We consider a forward contract on gold. Assume
there are no storage costs associated with gold
and that gold earns no income. - Suppose the following
- The spot price of gold is 300 per ounce.
- The risk-free interest rate for investment
lasting one year (e.g., CD in a bank) is 5. - What is a reasonable 1-year forward price of gold?
5Strategy 1 Buy
- Assume the forward price is 340.
- Trader's actions
- Borrow 300 at 5 for one year.
- Buy 1 ounce of gold for 300.
- Enter into a short forward contract to sell the
gold for 340 in one year. - At the end of one year, the trader needs to repay
the loan for 315, which is used from 340 he
gets from selling the gold. Hence the profit
6Strategy 2 Sell
- Assume the one-year forward price is 300.
- An investor who has gold in portfolio can
- Sell the gold for 300 per ounce
- Invest the proceeds at 5
- Enter into a long forward contract to repurchase
the gold in one year for 300 per ounce. - The profit is
7Arbitrage
- The first strategy is profitable when the
one-year price forward price of gold is greater
than 315. - As more traders attempt to take advantage of this
strategy, the demand for short forward contracts
will increase and the one-year forward price will
fall - The second strategy is profitable when one-year
forward price lt 315. - The demand for long forward contract will push
the forward price up. - Assuming that individuals are willing to take
advantage of arbitrage opportunities, the market
will push the forward price to be 315.
8Arbitrage Application
- Assume an application that deals with any
arbitrage opportunities on market. - The application is based on market listeners that
prompt the user on any arbitrage opportunity. - The listener would we triggered if a particular
arbitrage strategy can be applied. - Once the strategy can be applied, the listener
would inform the user of actions that need to be
taken, profit, etc. - At the heart of such application will be an
Arbitrage class. - The Arbitrage class keeps the knowledge of a the
strategy to be used.
9Arbitrage C Classes
class Arbitrage public
Arbitrage(ArbitrageStrategy) void
run(double maturity) double rate
MarketgetRate(maturity) double spotPrice
// get from market double forwardPrice
// get from market this-gtstrategy-gtsetRate
(rate) this-gtstrategy-gtsetSpotPrice(spotPri
ce) this-gtstrategy-gtsetForwardPrice(forward
Price)
10Arbitrage Class
double profit this-gtstrategy-gtgetProfit(ma
turity) if (profit gt 0) cout ltlt
"Actions" ltlt this-gtstrategy-gtgetActio
ns() ltlt endl ltlt "Profit" ltlt profit
ltlt endl protected
ArbitrageStrategy strategy string asset //
some asset
11ArbitrageStrategy Class
class ArbitrageStrategy public virtual
string getActions() 0 virtual double
getProfit (double maturity) 0
virtual void setRate(double r)
this-rate r
12ArbitrageStrategy (cont.)
virtual void setSpotPrice(double)
virtual void setForwardPrice(double)
protected double rate double
spotPrice double forwardPrice
13BuyArbitrageStrategy Classes
class BuyArbitrageStrategy public
ArbitrageStrategy public virtual string
getActions() ... virtual double
getProfit(double maturity) return
this-gtforwardPrice this-gtspotPrice
pow(1this-gtrate,this-gtmaturity)
14SellArbitrageStrategy Class
class SellArbitrageStrategy public
ArbitrageStrategy public ... virtual
double getProfit(double maturity)
return this-gtspotPricepow(1this-gtrate,t
his-gtmaturity) this-gtforwardPrice
// Client code Arbitrage a(new
BuyArbitrageStrategy()) a.run() ...
15Strategy Pattern UML Diagram
16Strategy Pattern Analysis
- Intent
- Define a family of algorithms, encapsulate each
one, and make them interchangeable. - Applicability
- many related classes differ only in their
behavior - different variants of an algorithm needed
- an algorithm uses data that clients should not
know about. - Participants
- Strategy (ArbitrageStrategy)
- declares a common interface to all supported
algorithms. - ConcreteStrategy (BuyArbitrageStrategy)
- implements the algorithm.
- Context (Arbitrage)
- uses Strategy.
17Policy and Strategy
template ltclass Tgt class Arbitrage public
Arbitrage() strategy(new T()) void
run(double maturity) ...
this-gtstrategy-gtsetRate(rate)
this-gtstrategy-gtsetSpotPrice(spotPrice)
this-gtstrategy-gtsetForwardPrice(forwardPrice)
18Policy Based Arbitrage Class
double profit this-gtstrategy-gtgetProfit(ma
turity) if (profit gt 0) cout ltlt
"Actions" ltlt this-gtstrategy-gtgetActio
ns() ltlt endl ltlt "Profit" ltlt profit
ltlt endl protected T
strategy string asset // some asset
19Policy Implementation
class BuyArbitrageStrategy public
ArbitrageStrategy public virtual string
getActions() ... virtual double
getProfit(double maturity) return
this-gtforwardPrice this-gtspotPrice
pow(1this-gtrate,this-gtmaturity)
class SellArbitrageStrategy public
ArbitrageStrategy ...
20Policy Implementation Usage
// Policy is the Strategy design pattern with //
the twist that policies are compile-time
bound! ArbitrageltBuyArbitrageStrategygt
a ArbitrageltSellArbitrageStrategygt
b a.run() b.run() ...