Chapter 30, 30.5 DELTA HEDGING, Portfolio class design

Chapter 30, 30.5 DELTA HEDGING, Portfolio class design

Postby nuclph » Wed Nov 12, 2008 12:23 am

Dear All,



I was inspired by the book to create Portfolio class based on template

provided in a the book:



class Portfolio: public PortfolioThing

{

private:

list<pair<int, Option*> > contents;



public:

// Usual stuff



double delta() const;

};



Below, i have implementation and test for this class.

I have several question about implementation:



1. Is there another good way to implement Portfolio Class? Can anyone share it with me?



2. As you can notice, the example below is for Calendar portfolio, so this portfolio contains 12 options on 12 (jan-dec) futures contracts. I would like sometime to have quarterly portfolio, for example for jan-apr, or even portfolio with custom number of months/options. I guess this question is based on question one. I am wondering from design perspective, how one should design/think about such a portfolio ?



Thank you very much for any suggestions/comments.





class Portfolio: public PortfolioThing

{



private:



list<pair<int, Option*> > contents;



public:



Portfolio();

Portfolio(const list<pair<int, Option*> > &contents_);



SimplePropertySet<string, double> propertylist() const;



double Price() const;

double Delta() const;

double Gamma() const;

double Vega() const;

double Theta() const;

double Rho() const;

double Coc() const;

};



Portfolio::Portfolio()

{

contents = list<pair<int, Option*> >();

}

//_____________________________________________________________________________

Portfolio::Portfolio(const list<pair<int, Option*> > &contents_)

{

contents = contents_;

}

//_____________________________________________________________________________

double Portfolio::Price() const

{

list<pair<int, Option*> >::const_iterator it;

double sum(0);



for(it = contents.begin(); it != contents.end(); it++)

sum += (*it).first * (*it).second->Price();



return sum;

}

//_____________________________________________________________________________

double Portfolio::Delta() const

{

list<pair<int, Option*> >::const_iterator it;

double sum(0);



for(it = contents.begin(); it != contents.end(); it++)

sum += (*it).first * (*it).second->Delta();



return sum;

}

//_____________________________________________________________________________

double Portfolio::Gamma() const

{

list<pair<int, Option*> >::const_iterator it;

double sum(0);



for(it = contents.begin(); it != contents.end(); it++)

sum += (*it).first * (*it).second->Gamma();



return sum;

}

//_____________________________________________________________________________

double Portfolio::Vega() const

{

list<pair<int, Option*> >::const_iterator it;

double sum(0);



for(it = contents.begin(); it != contents.end(); it++)

sum += (*it).first * (*it).second->Vega();



return sum;

}

//_____________________________________________________________________________

double Portfolio::Theta() const

{

list<pair<int, Option*> >::const_iterator it;

double sum(0);



for(it = contents.begin(); it != contents.end(); it++)

sum += (*it).first * (*it).second->Theta();



return sum;

}

//_____________________________________________________________________________

double Portfolio::Rho() const

{

list<pair<int, Option*> >::const_iterator it;

double sum(0);



for(it = contents.begin(); it != contents.end(); it++)

sum += (*it).first * (*it).second->Rho();



return sum;

}

//_____________________________________________________________________________

double Portfolio::Coc() const

{

list<pair<int, Option*> >::const_iterator it;

double sum(0);



for(it = contents.begin(); it != contents.end(); it++)

sum += (*it).first * (*it).second->Coc();



return sum;

}

//_____________________________________________________________________________

SimplePropertySet<string, double> Portfolio::propertylist() const

{



SimplePropertySet<string, double> result;



result.add (Property<string, double> ("Portfolio Value", Price() ) );

result.add (Property<string, double> ("Delta",Delta() ) );

result.add (Property<string, double> ("Gamma",Gamma() ) );

result.add (Property<string, double> ("Vega",Vega() ) );

result.add (Property<string, double> ("Theta",Theta() ) );

result.add (Property<string, double> ("Rho",Rho() ) );

result.add (Property<string, double> ("Cost of Carry",Coc() ) ); // Cost of carry



return result;



}



// Testing portfolio. It should be portfolio of calendar vanilla options

// Jan - F1, r1, K, T1

// Feb - F2, r2, K, T2

// ..

// Jan - F12, r12, K, T12

// For now, assume that all r1=r2=..=r12, vol1=vol2=...=vol12;



Vector<ExactEuropeanOption> opt(12);

Vector<double> T(12);

Vector<int> Weights(12);

Vector<double> IntRate(12);

Vector<double> Volatility(12);

Vector<double> Strike(12);

Vector<double> Spot(12);

Vector<double> CostOfCarry(12);





list<pair<int, Option*> > contents;



T[1] = 0.0833;

for(int i = T.MinIndex()+1; i <= T.MaxIndex(); i++)

T[i] += T[i-1] + 0.0833;



for(int i = Weights.MinIndex(); i <= Weights.MaxIndex(); i++)

{

Weights[i] = 1;

IntRate[i] = 0.025;

Volatility[i] = 0.55;

Strike[i] = 3.0;

Spot[i] = 3.5;

CostOfCarry[i] = 0; // futures

}



for(int i = opt.MinIndex(); i <= opt.MaxIndex(); i++)

{

opt[i].U = (Spot[i]);

opt[i].K = (Strike[i]);

opt[i].T = (T[i]);

opt[i].r = (IntRate[i]);

opt[i].sig = (Volatility[i]);

opt[i].b = (CostOfCarry[i]);

contents.push_front(pair<int,Option*>(Weights[i],&opt[i]));

}



Portfolio Calendar(contents);



SimplePropertySet<string,double> CalProp = Calendar.propertylist();

SimplePropertySet<string,double>::const_iterator calIt;



for(calIt = CalProp.Begin(); calIt != CalProp.End(); calIt++)

cout << (*calIt).name() << " , " << (*calIt)() << endl;
nuclph
 
Posts: 5
Joined: Fri Nov 07, 2008 11:39 pm

Postby Cuchulainn » Sun Jan 18, 2009 4:45 pm

At this stage some of the code in my 2004 C++ book has been superceded and improved in the 2006 C++ Introduction book.



My suggestion is that you give a description of the structure of your portfolio class and what kinds of functions it should offer. Then I can guve some hints on design and coding. Thanks.D
User avatar
Cuchulainn
 
Posts: 669
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby nuclph » Wed Jan 21, 2009 5:50 pm

Dear Daniel,



Thank you for the reply. Below, is a sketch of implementation of portfolio



Below, T mean type of option.



template <class T>

class Portfolio



{

private:



std::list<std::pair<double, T> > contents_;



public:



PVanilla();

PVanilla(const PVanilla& p);

PVanilla& operator = (const PVanilla& p);

virtual ~PVanilla();



double Price() const

double Delta() const;

double Gamma() const;

double Vega() const;

double Theta() const;



}





for example, to calculate delta of the portfolio I will do the following thing:



template <class T>

double Portfolio<T>::Delta() const

{

std::list<std::pair<double, T > >::const_iterator it;

double sum(0);



for(it = contents_.begin(); it != contents_.end(); it++)

sum += (*it).first * (*it).second.Delta();



return sum;

}



The same for all other greeks as long as Price(),Delta(),Gamma() defined in

T option (European, for instance).



One of the problem which I see is that Portfolio can have only one type of

options which probably not a good idea.



Do you thing is it an appropriate design for the portfolio class?



Thank you again,



nuclph.





















Cuchulainn wrote:At this stage some of the code in my 2004 C++ book has been superceded and improved in the 2006 C++ Introduction book.



My suggestion is that you give a description of the structure of your portfolio class and what kinds of functions it should offer. Then I can guve some hints on design and coding. Thanks.D
nuclph
 
Posts: 5
Joined: Fri Nov 07, 2008 11:39 pm

Postby Cuchulainn » Thu Jan 22, 2009 2:50 pm

Hi n

. why not use the new PSet<K,V> instead of list<pair>?

. If you want hetergeneous data classes(yes?) you need to use boost::any data type



hope this helps

Daniel
User avatar
Cuchulainn
 
Posts: 669
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands


Return to Financial Instrument Pricing using C++ (Duffy)

Who is online

Users browsing this forum: No registered users and 1 guest

cron