Strategy Design Pattern - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Strategy Design Pattern

Description:

The spot price of gold is $300 per ounce. ... What is a reasonable 1-year forward price of gold? 7/20/09. CS 631: Strategy Pattern ... – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 21
Provided by: ValuedGate2244
Category:

less

Transcript and Presenter's Notes

Title: Strategy Design Pattern


1
Strategy Design Pattern
2
Outline
  • Example
  • Forward Price and Delivery Price
  • Forward Price and Spot Price
  • C Classes
  • Strategy Pattern
  • Design
  • Analysis
  • Strategy and C Policy

3
Forward 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.

4
Forward 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?

5
Strategy 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

6
Strategy 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

7
Arbitrage
  • 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.

8
Arbitrage 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.

9
Arbitrage 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)
10
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
ArbitrageStrategy strategy string asset //
some asset
11
ArbitrageStrategy Class
class ArbitrageStrategy public virtual
string getActions() 0 virtual double
getProfit (double maturity) 0
virtual void setRate(double r)
this-rate r
12
ArbitrageStrategy (cont.)
virtual void setSpotPrice(double)
virtual void setForwardPrice(double)
protected double rate double
spotPrice double forwardPrice
13
BuyArbitrageStrategy Classes
class BuyArbitrageStrategy public
ArbitrageStrategy public virtual string
getActions() ... virtual double
getProfit(double maturity) return
this-gtforwardPrice this-gtspotPrice
pow(1this-gtrate,this-gtmaturity)
14
SellArbitrageStrategy 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() ...
15
Strategy Pattern UML Diagram
16
Strategy 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.

17
Policy 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)
18
Policy 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
19
Policy 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 ...
20
Policy 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() ...
Write a Comment
User Comments (0)
About PowerShow.com