VanillaOption persistance from EquityOption example

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

VanillaOption persistance from EquityOption example

patrickinminneapolis
Hi sorry if I posted this message twice, I'm not sure if my webbrowser ate the first one.

I'm trying to modify the example EquityOption provided in the Quantlib/Examples folder. I am trying to instantiate a EuropeanOption and then use an inspector function to return the delta. In order to do this I try to make the object persist with a pointer that's located in my header file.

EquityOption.h

class EquityOption {
public:
        VanillaOption *ptrEuropeanOption; //<< added this pointer
         
        EquityOption();
        double GetDelta();
};


From my EquityOption constructor I instantiate a EuropeanOption with this command:

    VanillaOption europeanOption(stochasticProcess, payoff,
                                     europeanExercise);

I use the debugger to look inside it, it looks good.  Then I try to make the object persist outside the constructor with this code :
                ptrEuropeanOption = &europeanOption;

Then I try to access the delta member from the function GetDelta:

double EquityOption::getDelta(){
                VanillaOption z = *ptrEuropeanOption; //<<it breaks here.
                double ret = z.delta();
                return ret;        
}


The code break with "Unhandled exception at 0x00445a73 in EquityOption-vc71-mt-gd.exe: 0xC0000005: Access violation reading location 0xffffffff."

One interesting thing I notice is the instantiated EuropeanOption looks like this from the debugger (inside the EquityOption constructor function)
"QuantLib::OneAssetStrikedOption = {strikeSensitivity_=0.59151420002496735 }"

But when I try to access it from within the GetDelta function there is another member added to the object.
1) "[QuantLib::LazyObject] = {calculated_=true frozen_=false }"
2) "QuantLib::OneAssetStrikedOption = {strikeSensitivity_=0.59151420002496735 }"

I'm really not sure what to do about this, thank for any help you can provide, and also I'm really sorry if this is the second of these messages I've sent out.  Thanks for you reading and your help.
Patrick
 


Reply | Threaded
Open this post in threaded view
|

Re: VanillaOption persistance from EquityOption example

eric ehlers
Hello

On 2/17/07, patrickinminneapolis <[hidden email]> wrote:

>
> Hi sorry if I posted this message twice, I'm not sure if my webbrowser ate
> the first one.
>
> I'm trying to modify the example EquityOption provided in the
> Quantlib/Examples folder. I am trying to instantiate a EuropeanOption and
> then use an inspector function to return the delta. In order to do this I
> try to make the object persist with a pointer that's located in my header
> file.
>
> EquityOption.h
>
> class EquityOption {
> public:
>         VanillaOption *ptrEuropeanOption; //<< added this pointer
>
>         EquityOption();
>         double GetDelta();
> };
>
>
> >From my EquityOption constructor I instantiate a EuropeanOption with this
> command:
>
>     VanillaOption europeanOption(stochasticProcess, payoff,
>                                      europeanExercise);
>                 ptrEuropeanOption = &europeanOption;

try replacing the above with

    ptrEuropeanOption = new VanillaOption(stochasticProcess, payoff,
                                      europeanExercise);

and in the destructor of EquityOption  you should call delete on the pointer.

Regards,
Eric

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: VanillaOption persistance from EquityOption example

patrickinminneapolis
<quote author='eric ehlers'>
 

On 2/17/07, patrickinminneapolis <patrickinminneapolis@gmail.com> wrote:

> I'm trying to modify the example EquityOption provided in the
> Quantlib/Examples folder. I am trying to instantiate a EuropeanOption and
> then use an inspector function to return the delta. In order to do this I
> try to make the object persist with a pointer that's located in my header
> file.
>
> EquityOption.h
>
> class EquityOption {
> public:
>         VanillaOption *ptrEuropeanOption; //<< added this pointer
>
>         EquityOption();
>         double GetDelta();
> };
>
>
> >From my EquityOption constructor I instantiate a EuropeanOption with this
> command:
>
>     VanillaOption europeanOption(stochasticProcess, payoff,
>                                      europeanExercise);
>                 ptrEuropeanOption = &europeanOption;

try replacing the above with

    ptrEuropeanOption = new VanillaOption(stochasticProcess, payoff,
                                      europeanExercise);

and in the destructor of EquityOption  you should call delete on the pointer.

Regards,
Eric



Hi Eric,
Thanks for the quick response, I tried this combination:

.h
VanillaOption *ptrEuropeanOption

.cpp
EquityOption::EquityOption()
{
ptrEuropeanOption = new VanillaOption(stochasticProcess, payoff,
                                     europeanExercise); //<<this is different

method = "Black-Scholes";
                 
VanillaOption v = *ptrEuropeanOption;  //<<< i had to do this to set the pricing engine (Type of expression to the left of . or -> must be class, struct or union)

v.setPricingEngine(boost::shared_ptr<PricingEngine>(
                                     new AnalyticEuropeanEngine));


}
EquityOption::GetDelta()
{
        VanillaOption retEuropeanOption = *ptrEuropeanOption;
        double ret = retEuropeanOption.delta();   << error here (i think its a bad pointer)
        return ret ;
};

error =
1) __vfptr = 0x004a91d8 const QuantLib::Error::`vftable'
2) _m_what = 0x00000000 <Bad Ptr>
3) _m_doFree = 0



I also tried this for my a .h file:
VanillaOption ptrEuropeanOption //< without the pointer

but that gives me a "error C2512: 'QuantLib::VanillaOption' : no appropriate default constructor available"


Do you have any other suggestions?
Thanks
Patrick
Reply | Threaded
Open this post in threaded view
|

Re: VanillaOption persistance from EquityOption example

Luigi Ballabio

On Feb 17, 2007, at 3:16 AM, patrickinminneapolis wrote:

> I tried this combination:
>
> .h
> VanillaOption *ptrEuropeanOption
>
> .cpp
> EquityOption::EquityOption()
> {
> ptrEuropeanOption = new VanillaOption(stochasticProcess, payoff,
>                                      europeanExercise); //<<this is
> different
>
> method = "Black-Scholes";
>
> VanillaOption v = *ptrEuropeanOption;  //<<< i had to do this to set
> the
> pricing engine (Type of expression to the left of . or -> must be
> class,
> struct or union)

No, you don't have to do this. Delete the above line (which, by the
way, performs a copy. This way, you're setting the pricing engine to
the copy and leaving the original option untouched) and just write

ptrEuropeanOption->setPricingEngine(boost::shared_ptr<PricingEngine>(
                                      new AnalyticEuropeanEngine));

> EquityOption::GetDelta()
> {
> VanillaOption retEuropeanOption = *ptrEuropeanOption;
> double ret = retEuropeanOption.delta();   << error here (i think its
> a bad
> pointer)

Another unneeded (and probably harmful) copy. Just use

double ret = ptrEuropeanOption->delta();

> return ret ;
> };

Finally, as Eric suggested, add

EquityOption::~EquityOption() { delete ptrEuropeanOption; }

or you'll have a memory leak.

Hope this helps,
        Luigi

P.S. Out of curiosity, why are you wrapping the EuropeanOption inside
an EquityOption instead of using the former directly?




-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: VanillaOption persistance from EquityOption example

patrickinminneapolis
Thanks Eric and Luigi,
That worked right away. I have the EuropeanOption inside another class (EquityOption) because I'm using a managed to unmanaged wrapper ala:

OptionWrapper() : m_Impl( new EquityOption() )  {} "  
 return gcnew double( m_Impl->GetDelta());

to create a DLL for vb.net consumption. Anyway, thanks for the help, it made my day.
Patrick