Logo Search packages:      
Sourcecode: quantlib version File versions  Download package


Go to the documentation of this file.
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
 Copyright (C) 2003, 2004, 2005, 2006 StatPro Italia srl

 This file is part of QuantLib, a free-software/open-source library
 for financial quantitative analysts and developers - http://quantlib.org/

 QuantLib is free software: you can redistribute it and/or modify it
 under the terms of the QuantLib license.  You should have received a
 copy of the license along with this program; if not, please email
 <quantlib-dev@lists.sf.net>. The license is also available online at

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.  See the license for more details.

/*! \file instrument.hpp
    \brief Abstract instrument class

#ifndef quantlib_instrument_hpp
#define quantlib_instrument_hpp

#include <ql/Patterns/lazyobject.hpp>
#include <ql/pricingengine.hpp>
#include <ql/errors.hpp>
#include <ql/Utilities/null.hpp>

namespace QuantLib {

    //! Abstract instrument class
    /*! This class is purely abstract and defines the interface of concrete
        instruments which will be derived from this one.

        \test observability of class instances is checked.
00041     class Instrument : public LazyObject {
        //! \name Inspectors
        //! returns the net present value of the instrument.
        Real NPV() const;
        //! returns the error estimate on the NPV when available.
        Real errorEstimate() const;
        //! returns whether the instrument is still tradable.
        virtual bool isExpired() const = 0;
        //! \name Modifiers
        //! set the pricing engine to be used.
        /*! \warning calling this method will have no effects in
                     case the <b>performCalculation</b> method
                     was overridden in a derived class.
        void setPricingEngine(const boost::shared_ptr<PricingEngine>&);
        /*! When a derived argument structure is defined for an
            instrument, this method should be overridden to fill
            it. This is mandatory in case a pricing engine is used.
        virtual void setupArguments(Arguments*) const;
        /*! When a derived result structure is defined for an
            instrument, this method should be overridden to read from
            it. This is mandatory in case a pricing engine is used.
        virtual void fetchResults(const Results*) const;
        //! \name Calculations
        void calculate() const;
        /*! This method must leave the instrument in a consistent
            state when the expiration condition is met.
        virtual void setupExpired() const;
        /*! In case a pricing engine is <b>not</b> used, this
            method must be overridden to perform the actual
            calculations and set any needed results. In case
            a pricing engine is used, the default implementation
            can be used.
        virtual void performCalculations() const;
        /*! \name Results
            The value of this attribute and any other that derived
            classes might declare must be set during calculation.
        mutable Real NPV_, errorEstimate_;
        boost::shared_ptr<PricingEngine> engine_;

    //! pricing results
00100     class Value : public virtual Results {
        Value() { reset(); }
        void reset() {
            value = errorEstimate = Null<Real>();
        Real value;
        Real errorEstimate;

    // inline definitions

    inline Instrument::Instrument()
    : NPV_(0.0), errorEstimate_(Null<Real>()) {}

00116     inline void Instrument::setPricingEngine(
                                  const boost::shared_ptr<PricingEngine>& e) {
        if (engine_)
        engine_ = e;
        if (engine_)
        // trigger (lazy) recalculation and notify observers

00127     inline void Instrument::setupArguments(Arguments*) const {
        QL_FAIL("setupArguments() not implemented");

00131     inline void Instrument::calculate() const {
        if (isExpired()) {
            calculated_ = true;
        } else {

00140     inline void Instrument::setupExpired() const {
        NPV_ = errorEstimate_ = 0.0;

00144     inline void Instrument::performCalculations() const {
        QL_REQUIRE(engine_, "null pricing engine");

00153     inline void Instrument::fetchResults(const Results* r) const {
        const Value* results = dynamic_cast<const Value*>(r);
        QL_ENSURE(results != 0,
                  "no results returned from pricing engine");
        NPV_ = results->value;
        errorEstimate_ = results->errorEstimate;

00161     inline Real Instrument::NPV() const {
        return NPV_;

00166     inline Real Instrument::errorEstimate() const {
        QL_REQUIRE(errorEstimate_ != Null<Real>(),
                   "error estimate not provided");
        return errorEstimate_;



Generated by  Doxygen 1.6.0   Back to index