Strategy Design Pattern CS 631: Strategy Pattern 7/1/2016 1

advertisement
Strategy Design Pattern
7/1/2016
CS 631: Strategy Pattern
1
Outline
• Example
– Forward Price and Delivery Price
– Forward Price and Spot Price
– C++ Classes
• Strategy Pattern
– Design
– Analysis
– Strategy and C++ Policy
7/1/2016
CS 631: Strategy Pattern
2
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.
7/1/2016
CS 631: Strategy Pattern
3
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?
7/1/2016
CS 631: Strategy Pattern
4
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:
$340  $315  $25
7/1/2016
CS 631: Strategy Pattern
5
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:
$315  $300  $15
7/1/2016
CS 631: Strategy Pattern
6
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 < 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.
7/1/2016
CS 631: Strategy Pattern
7
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.
7/1/2016
CS 631: Strategy Pattern
8
Arbitrage C++ Classes
class Arbitrage
{
public:
Arbitrage(ArbitrageStrategy*);
void run(double maturity)
{
double rate = Market::getRate(maturity);
double spotPrice = // get from market;
double forwardPrice = // get from market;
this->strategy->setRate(rate);
this->strategy->setSpotPrice(spotPrice);
this->strategy->setForwardPrice(forwardPrice);
7/1/2016
CS 631: Strategy Pattern
9
Arbitrage Class
double profit = this->strategy->getProfit(maturity);
if (profit > 0)
cout << "Actions:" <<
this->strategy->getActions()
<< endl << "Profit:" << profit
<< endl;
}
protected:
ArbitrageStrategy* strategy;
string asset; // some asset
};
7/1/2016
CS 631: Strategy Pattern
10
ArbitrageStrategy Class
class ArbitrageStrategy
{
public:
virtual string getActions() = 0;
virtual double getProfit
(double maturity) = 0;
virtual void setRate(double r)
{
this-rate = r;
}
7/1/2016
CS 631: Strategy Pattern
11
ArbitrageStrategy (cont.)
virtual void setSpotPrice(double);
virtual void setForwardPrice(double);
protected:
double rate;
double spotPrice;
double forwardPrice;
};
7/1/2016
CS 631: Strategy Pattern
12
BuyArbitrageStrategy Classes
class BuyArbitrageStrategy : public ArbitrageStrategy
{
public:
virtual string getActions() {...}
virtual double getProfit(double maturity)
{
return this->forwardPrice –
this->spotPrice*
pow(1+this->rate,this->maturity);
}
};
7/1/2016
CS 631: Strategy Pattern
13
SellArbitrageStrategy Class
class SellArbitrageStrategy :
public ArbitrageStrategy
{
public:
...
virtual double getProfit(double maturity)
{
return
this->spotPrice*pow(1+this->rate,this->maturity)
– this->forwardPrice ;
}
};
// Client code:
Arbitrage a(new BuyArbitrageStrategy());
a.run();
...
7/1/2016
CS 631: Strategy Pattern
14
Strategy Pattern: UML Diagram
7/1/2016
CS 631: Strategy Pattern
15
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.
7/1/2016
CS 631: Strategy Pattern
16
Policy and Strategy
template <class T>
class Arbitrage
{
public:
Arbitrage() : strategy(new T()) {}
void run(double maturity)
{
...
this->strategy->setRate(rate);
this->strategy->setSpotPrice(spotPrice);
this->strategy->setForwardPrice(forwardPrice);
7/1/2016
CS 631: Strategy Pattern
17
Policy Based Arbitrage Class
double profit = this->strategy->getProfit(maturity);
if (profit > 0)
cout << "Actions:" <<
this->strategy->getActions()
<< endl << "Profit:" << profit
<< endl;
}
protected:
T* strategy;
string asset; // some asset
};
7/1/2016
CS 631: Strategy Pattern
18
Policy Implementation
class BuyArbitrageStrategy : public ArbitrageStrategy
{
public:
virtual string getActions() {...}
virtual double getProfit(double maturity)
{
return this->forwardPrice –
this->spotPrice *
pow(1+this->rate,this->maturity);
}
};
class SellArbitrageStrategy :
public ArbitrageStrategy
{
...
};
7/1/2016
CS 631: Strategy Pattern
19
Policy Implementation: Usage
// Policy is the Strategy design pattern with
// the twist that policies are compile-time bound!
Arbitrage<BuyArbitrageStrategy> a;
Arbitrage<SellArbitrageStrategy> b;
a.run();
b.run();
...
7/1/2016
CS 631: Strategy Pattern
20
Download