Caching the pricing engine

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Caching the pricing engine

Robert Kubrick
Is there a way to reset the process for the AnalyticEuropeanEngine  
without creating a new engine instance every time?
I have the following code:

   Date exerciseDate(Day(exercise.day()), Month(exercise.month
().as_enum()), Year(exercise.year()));
   shared_ptr<Exercise> pExercise(new EuropeanExercise(exerciseDate));

   Handle<Quote> underlyingHndl(shared_ptr<Quote>(new SimpleQuote
(price)));
   shared_ptr<StrikedTypePayoff> pPayoff(new PlainVanillaPayoff(side  
== CALL ? Option::Call : Option::Put, strike));

   Handle<BlackVolTermStructure> flatVolTS
(shared_ptr<BlackVolTermStructure>(new BlackConstantVol(_settlement,
                                                                         
                           _calendar,
                                                                         
                           vol,
                                                                         
                           _dayCounter)));

   Handle<YieldTermStructure> _flatTermStructure
(shared_ptr<YieldTermStructure>(new FlatForward(_settlement,
                                                                         
                         _riskFreeRate,
                                                                         
                         _dayCounter)));

   shared_ptr<BlackScholesProcess> pProcess(new BlackScholesProcess
(underlyingHndl,
                                                                     
_flatTermStructure,
                                                                     
flatVolTS));
   EuropeanOption eo(pPayoff, pExercise);
   shared_ptr<PricingEngine> pPE(new AnalyticEuropeanEngine(pProcess));
   eo.setPricingEngine(pPE);

I would like to cache most of the instances here and change the  
contract conditions at runtime to calculate option chains.

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Caching the pricing engine

javit
Robert,

The lazy object structure is built exactly for this purpose. Once you change a value at runtime, the observers notify the changes in observables. If I understand your question correctly, you don't have to worry about resetting the engines.

Thank you,
Javit

<quote author="Robert Kubrick">
Is there a way to reset the process for the AnalyticEuropeanEngine  
without creating a new engine instance every time?
I have the following code:

   Date exerciseDate(Day(exercise.day()), Month(exercise.month
().as_enum()), Year(exercise.year()));
   shared_ptr<Exercise> pExercise(new EuropeanExercise(exerciseDate));

   Handle<Quote> underlyingHndl(shared_ptr
(new SimpleQuote
(price)));
   shared_ptr<StrikedTypePayoff> pPayoff(new PlainVanillaPayoff(side  
== CALL ? Option::Call : Option::Put, strike));

   Handle<BlackVolTermStructure> flatVolTS
(shared_ptr<BlackVolTermStructure>(new BlackConstantVol(_settlement,
                                                                         
                           _calendar,
                                                                         
                           vol,
                                                                         
                           _dayCounter)));

   Handle<YieldTermStructure> _flatTermStructure
(shared_ptr<YieldTermStructure>(new FlatForward(_settlement,
                                                                         
                         _riskFreeRate,
                                                                         
                         _dayCounter)));

   shared_ptr<BlackScholesProcess> pProcess(new BlackScholesProcess
(underlyingHndl,
                                                                     
_flatTermStructure,
                                                                     
flatVolTS));
   EuropeanOption eo(pPayoff, pExercise);
   shared_ptr<PricingEngine> pPE(new AnalyticEuropeanEngine(pProcess));
   eo.setPricingEngine(pPE);

I would like to cache most of the instances here and change the  
contract conditions at runtime to calculate option chains.

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
QuantLib-users mailing list
QuantLib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Caching the pricing engine

Robert Kubrick
In reply to this post by Robert Kubrick
I found out I can change the option payoff (type/strike) and exercise  
through Option::setupArguments():

Option::arguments args;
args.payoff = pPayoff;
args.exercise = pExercise;
eo.setupArguments(&args);

Is there a way to dynamically change the engine process parameters,  
volatility and yield in particular?
I don't see any method to change the process term parameters after  
instantiation.


On Sep 13, 2008, at 5:47 PM, Robert Kubrick wrote:

> Is there a way to reset the process for the AnalyticEuropeanEngine  
> without creating a new engine instance every time?
> I have the following code:
>
>   Date exerciseDate(Day(exercise.day()), Month(exercise.month
> ().as_enum()), Year(exercise.year()));
>   shared_ptr<Exercise> pExercise(new EuropeanExercise(exerciseDate));
>
>   Handle<Quote> underlyingHndl(shared_ptr<Quote>(new SimpleQuote
> (price)));
>   shared_ptr<StrikedTypePayoff> pPayoff(new PlainVanillaPayoff(side  
> == CALL ? Option::Call : Option::Put, strike));
>
>   Handle<BlackVolTermStructure> flatVolTS
> (shared_ptr<BlackVolTermStructure>(new BlackConstantVol(_settlement,
>                                                                        
>                            _calendar,
>                                                                        
>                            vol,
>                                                                        
>                            _dayCounter)));
>
>   Handle<YieldTermStructure> _flatTermStructure
> (shared_ptr<YieldTermStructure>(new FlatForward(_settlement,
>                                                                        
>                          _riskFreeRate,
>                                                                        
>                          _dayCounter)));
>
>   shared_ptr<BlackScholesProcess> pProcess(new BlackScholesProcess
> (underlyingHndl,
>                                                                    
> _flatTermStructure,
>                                                                    
> flatVolTS));
>   EuropeanOption eo(pPayoff, pExercise);
>   shared_ptr<PricingEngine> pPE(new AnalyticEuropeanEngine(pProcess));
>   eo.setPricingEngine(pPE);
>
> I would like to cache most of the instances here and change the  
> contract conditions at runtime to calculate option chains.


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Caching the pricing engine

Ferdinando M. Ametrano-3
Hi Robert

> Is there a way to dynamically change the engine process parameters,
> volatility and yield in particular?
put them in a Quote, then change the Quote value

ciao -- Nando

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Caching the pricing engine

Luigi Ballabio
In reply to this post by Robert Kubrick
On Sun, 2008-09-14 at 19:16 -0400, Robert Kubrick wrote:
> Is there a way to dynamically change the engine process parameters,  
> volatility and yield in particular?

Yes---when you write:


> >   Handle<BlackVolTermStructure> flatVolTS
> > (shared_ptr<BlackVolTermStructure>(new BlackConstantVol(_settlement,
> >                                                                        
> >                            _calendar,
> >                                                                        
> >                            vol,
> >                                                                        
> >                            _dayCounter)));
> >

Do instead:

shared_ptr<SimpleQuote> volQuote(new SimpleQuote(0.20));

Handle<BlackVolTermStructure>
flatVolTS(shared_ptr<BlackVolTermStructure>(
    new BlackConstantVol(_settlement,
                         _calendar,
                         Handle<Quote>(volQuote),
                         _dayCounter)));

after you've built your options, you can execute:

volQuote->setValue(0.25);

and all the options will see the new volatility (i.e., they'll return a
different value when you call NPV() etc.)

Luigi



--

Better to remain silent and be thought a fool than to speak out and
remove all doubt.
-- Abraham Lincoln



-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Caching the pricing engine

Robert Kubrick
I'm doing exactly what you suggested for price, volatility and yield  
but I get the wrong npv and greeks values.


Calc::Calc(double riskFreeRate, boost::gregorian::date settlement):
   _riskFreeRate(riskFreeRate),
   _settlement(Day(settlement.day()), Month(settlement.month().as_enum
()), Year(settlement.year())),
   _dayCounter(Actual365Fixed()),
   _calendar(TARGET()),
   _underlyingQuote(new SimpleQuote(1)),
   _volQuote(new SimpleQuote(1)),
   _yieldQuote(new SimpleQuote(1))
{
   date today = day_clock::local_day();
   Settings::instance().evaluationDate() = Date(Day(today.day()),  
Month(today.month().as_enum()), Year(today.year()));

   Date exerciseDate(Day(30), Month(12), Year(2020));
   _pExercise = shared_ptr<Exercise>(new EuropeanExercise
(exerciseDate));

   _pPayoff = shared_ptr<StrikedTypePayoff>(new PlainVanillaPayoff
(Option::Call, 1));

   _pFlatVolTS = shared_ptr<BlackVolTermStructure>(new  
BlackConstantVol(_settlement,
                                                                         
_calendar,
                                                                         
Handle<Quote>(_volQuote),
                                                                         
_dayCounter));

   _pFlatTermStruct = shared_ptr<YieldTermStructure>(new FlatForward
(_settlement,
                                                                     
_riskFreeRate,
                                                                     
_dayCounter));

   _pFlatDividendTermStruct = shared_ptr<YieldTermStructure>(new  
FlatForward(_settlement,
                                                                         
      Handle<Quote>(_yieldQuote),
                                                                         
      _dayCounter));

   _pProcess = shared_ptr<BlackScholesMertonProcess>(new  
BlackScholesMertonProcess(Handle<Quote>(_underlyingQuote),
                                                                         
            Handle<YieldTermStructure>(_pFlatDividendTermStruct),
                                                                         
            Handle<YieldTermStructure>(_pFlatTermStruct),
                                                                         
            Handle<BlackVolTermStructure>(_pFlatVolTS)));

   _pPE = shared_ptr<PricingEngine>(new AnalyticEuropeanEngine
(_pProcess));

   _pOption = new EuropeanOption(_pPayoff, _pExercise);
   _pOption->setPricingEngine(_pPE);
}


void Calc::run(Side side, double price, int strike, double yield,  
const date& exercise, double vol)
{
   _underlyingQuote->setValue(price);
   _volQuote->setValue(vol);
   _yieldQuote->setValue(yield);

   QuantLib::Option::arguments args;
   args.payoff = shared_ptr<StrikedTypePayoff>(new PlainVanillaPayoff
(side == CALL ? Option::Call : Option::Put, strike));
   args.exercise = shared_ptr<Exercise>(new EuropeanExercise(Date(Day
(exercise.day()), Month(exercise.month().as_enum()), Year
(exercise.year()))));

   _pOption->setupArguments(&args);

   cout << "NPV: " << _pOption->NPV() << endl;
   cout << "Delta: " << _pOption->delta() << endl;
   cout << "Gamma: " << _pOption->gamma() << endl;

   /*
   _npv = _pOption->NPV();
   _delta = _pOption->delta();
   _gamma = _pOption->gamma();
   _vega = _pOption->vega();
   _theta = _pOption->theta();
   _rho = _pOption->rho();
   */
}

On Sep 15, 2008, at 3:57 AM, Luigi Ballabio wrote:

> On Sun, 2008-09-14 at 19:16 -0400, Robert Kubrick wrote:
>> Is there a way to dynamically change the engine process parameters,
>> volatility and yield in particular?
>
> Yes---when you write:
>
>
>>>   Handle<BlackVolTermStructure> flatVolTS
>>> (shared_ptr<BlackVolTermStructure>(new BlackConstantVol(_settlement,
>>>
>>>                            _calendar,
>>>
>>>                            vol,
>>>
>>>                            _dayCounter)));
>>>
>
> Do instead:
>
> shared_ptr<SimpleQuote> volQuote(new SimpleQuote(0.20));
>
> Handle<BlackVolTermStructure>
> flatVolTS(shared_ptr<BlackVolTermStructure>(
>     new BlackConstantVol(_settlement,
>                          _calendar,
>                          Handle<Quote>(volQuote),
>                          _dayCounter)));
>
> after you've built your options, you can execute:
>
> volQuote->setValue(0.25);
>
> and all the options will see the new volatility (i.e., they'll  
> return a
> different value when you call NPV() etc.)
>
> Luigi
>
>
>
> --
>
> Better to remain silent and be thought a fool than to speak out and
> remove all doubt.
> -- Abraham Lincoln
>
>


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Caching the pricing engine

Robert Kubrick
Oops, my bad. The problem was due to the option arguments, not the  
quotes. I thought setupArguments() was mean't to set payoff and  
exercise at runtime, but it actually retrieves the current values.

Is there a way to change strike and exercise of an existing option?  
Or can I setup an option with multiple strikes/exercises?


On Sep 20, 2008, at 6:56 PM, Robert Kubrick wrote:

> I'm doing exactly what you suggested for price, volatility and  
> yield but I get the wrong npv and greeks values.
>
>
> Calc::Calc(double riskFreeRate, boost::gregorian::date settlement):
>   _riskFreeRate(riskFreeRate),
>   _settlement(Day(settlement.day()), Month(settlement.month
> ().as_enum()), Year(settlement.year())),
>   _dayCounter(Actual365Fixed()),
>   _calendar(TARGET()),
>   _underlyingQuote(new SimpleQuote(1)),
>   _volQuote(new SimpleQuote(1)),
>   _yieldQuote(new SimpleQuote(1))
> {
>   date today = day_clock::local_day();
>   Settings::instance().evaluationDate() = Date(Day(today.day()),  
> Month(today.month().as_enum()), Year(today.year()));
>
>   Date exerciseDate(Day(30), Month(12), Year(2020));
>   _pExercise = shared_ptr<Exercise>(new EuropeanExercise
> (exerciseDate));
>
>   _pPayoff = shared_ptr<StrikedTypePayoff>(new PlainVanillaPayoff
> (Option::Call, 1));
>
>   _pFlatVolTS = shared_ptr<BlackVolTermStructure>(new  
> BlackConstantVol(_settlement,
>                                                                        
>  _calendar,
>                                                                        
>  Handle<Quote>(_volQuote),
>                                                                        
>  _dayCounter));
>
>   _pFlatTermStruct = shared_ptr<YieldTermStructure>(new FlatForward
> (_settlement,
>                                                                      
> _riskFreeRate,
>                                                                      
> _dayCounter));
>
>   _pFlatDividendTermStruct = shared_ptr<YieldTermStructure>(new  
> FlatForward(_settlement,
>                                                                        
>       Handle<Quote>(_yieldQuote),
>                                                                        
>       _dayCounter));
>
>   _pProcess = shared_ptr<BlackScholesMertonProcess>(new  
> BlackScholesMertonProcess(Handle<Quote>(_underlyingQuote),
>                                                                        
>             Handle<YieldTermStructure>(_pFlatDividendTermStruct),
>                                                                        
>             Handle<YieldTermStructure>(_pFlatTermStruct),
>                                                                        
>             Handle<BlackVolTermStructure>(_pFlatVolTS)));
>
>   _pPE = shared_ptr<PricingEngine>(new AnalyticEuropeanEngine
> (_pProcess));
>
>   _pOption = new EuropeanOption(_pPayoff, _pExercise);
>   _pOption->setPricingEngine(_pPE);
> }
>
>
> void Calc::run(Side side, double price, int strike, double yield,  
> const date& exercise, double vol)
> {
>   _underlyingQuote->setValue(price);
>   _volQuote->setValue(vol);
>   _yieldQuote->setValue(yield);
>
>   QuantLib::Option::arguments args;
>   args.payoff = shared_ptr<StrikedTypePayoff>(new PlainVanillaPayoff
> (side == CALL ? Option::Call : Option::Put, strike));
>   args.exercise = shared_ptr<Exercise>(new EuropeanExercise(Date(Day
> (exercise.day()), Month(exercise.month().as_enum()), Year
> (exercise.year()))));
>
>   _pOption->setupArguments(&args);
>
>   cout << "NPV: " << _pOption->NPV() << endl;
>   cout << "Delta: " << _pOption->delta() << endl;
>   cout << "Gamma: " << _pOption->gamma() << endl;
>
>   /*
>   _npv = _pOption->NPV();
>   _delta = _pOption->delta();
>   _gamma = _pOption->gamma();
>   _vega = _pOption->vega();
>   _theta = _pOption->theta();
>   _rho = _pOption->rho();
>   */
> }
>
> On Sep 15, 2008, at 3:57 AM, Luigi Ballabio wrote:
>
>> On Sun, 2008-09-14 at 19:16 -0400, Robert Kubrick wrote:
>>> Is there a way to dynamically change the engine process parameters,
>>> volatility and yield in particular?
>>
>> Yes---when you write:
>>
>>
>>>>   Handle<BlackVolTermStructure> flatVolTS
>>>> (shared_ptr<BlackVolTermStructure>(new BlackConstantVol
>>>> (_settlement,
>>>>
>>>>                            _calendar,
>>>>
>>>>                            vol,
>>>>
>>>>                            _dayCounter)));
>>>>
>>
>> Do instead:
>>
>> shared_ptr<SimpleQuote> volQuote(new SimpleQuote(0.20));
>>
>> Handle<BlackVolTermStructure>
>> flatVolTS(shared_ptr<BlackVolTermStructure>(
>>     new BlackConstantVol(_settlement,
>>                          _calendar,
>>                          Handle<Quote>(volQuote),
>>                          _dayCounter)));
>>
>> after you've built your options, you can execute:
>>
>> volQuote->setValue(0.25);
>>
>> and all the options will see the new volatility (i.e., they'll  
>> return a
>> different value when you call NPV() etc.)
>>
>> Luigi
>>
>>
>>
>> --
>>
>> Better to remain silent and be thought a fool than to speak out and
>> remove all doubt.
>> -- Abraham Lincoln
>>
>


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Caching the pricing engine

Luigi Ballabio
On Sun, 2008-09-21 at 13:43 -0400, Robert Kubrick wrote:
> Oops, my bad. The problem was due to the option arguments, not the  
> quotes. I thought setupArguments() was mean't to set payoff and  
> exercise at runtime, but it actually retrieves the current values.
>
> Is there a way to change strike and exercise of an existing option?  
> Or can I setup an option with multiple strikes/exercises?

No current way--the idea being that, while market conditions change, the
terms of an option don't.  You'll need to instantiate multiple options.

Luigi

P.S. You could also inherit your own class from StrikedTypePayoff and
allow the strike to change.


--

Call on God, but row away from the rocks.
-- Indian proverb



-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users