packages feed

hasquant-0.9.0.0: QuantLib/TermStructure.chs

{-# LANGUAGE TypeFamilies, FlexibleInstances #-}
module QuantLib.TermStructure
  (
    -- * Types
    -- ** Term-structure hierarchy
    GenTermStructure
  , TermStructure

    -- ** Coordinates
  , Reference(..)
  , CalendarReference(..)
  , TermPoint(..)
  , TermInterval(..)
  , RatePoint(..)
  , RateInterval(..)

    -- * Constructors
  , asTermStructure

    -- * Mutators
  , setExtrapolation

    -- * Inspectors
  , HasHelperUnderlying(..)
  , HasNodes(..)
  , referenceDate
  , maxDate
  , maxTime
  , allowsExtrapolation
  , timeFromReference
  ) where
import QuantLib.Internal hiding(maxDate)
import QuantLib.Internal.Type

#include "qlTypesC2HS.h"
#include "qlEnumC2HS.h"
#include "qlEnumObjects.h"

#include "ql.h"

{#pointer *QlBondHelper as BondHelper foreign -> CBondHelper' nocode#}
{#pointer *QlSwapRateHelper as SwapRateHelper foreign -> CSwapRateHelper' nocode#}
{#pointer *QlOISRateHelper as OISRateHelper foreign -> COISRateHelper' nocode#}
{#pointer *QlZeroCouponInflationSwapHelper as ZeroCouponInflationSwapHelper foreign -> CZeroCouponInflationSwapHelper nocode#}
{#pointer *QlYearOnYearInflationSwapHelper as YearOnYearInflationSwapHelper foreign -> CYearOnYearInflationSwapHelper nocode#}
{#pointer *QlSwaptionHelper as SwaptionHelper foreign -> CSwaptionHelper' nocode#}
{#pointer *QlBond as Bond foreign -> CBond' nocode#}
{#pointer *QlVanillaSwap as VanillaSwap foreign -> CVanillaSwap' nocode#}
{#pointer *QlOvernightIndexedSwap as OvernightIndexedSwap foreign -> COvernightIndexedSwap' nocode#}
{#pointer *QlZeroCouponInflationSwap as ZeroCouponInflationSwap foreign -> CZeroCouponInflationSwap' nocode#}
{#pointer *QlYearOnYearInflationSwap as YearOnYearInflationSwap foreign -> CYearOnYearInflationSwap' nocode#}
{#pointer *QlFixedVsFloatingSwap as FixedVsFloatingSwap foreign -> CFixedVsFloatingSwap' nocode#}
{#pointer *QlTermStructure as TermStructure foreign -> CTermStructure' nocode#}
{#pointer *QlPiecewiseYieldCurve as PiecewiseYieldCurve foreign -> CPiecewiseYieldCurve' nocode#}
{#pointer *QlPiecewiseDefaultCurve as PiecewiseDefaultCurve foreign -> CPiecewiseDefaultCurve' nocode#}
{#pointer *QlPiecewiseZeroInflationCurve as PiecewiseZeroInflationCurve foreign -> CPiecewiseZeroInflationCurve' nocode#}
{#pointer *QlPiecewiseYoYInflationCurve as PiecewiseYoYInflationCurve foreign -> CPiecewiseYoYInflationCurve' nocode#}

-- |Bootstrap and calibration helpers that build and hold the instrument whose market quote
-- they match. The class lives here, in the term-structure root module, because its instances
-- span "QuantLib.TermStructure.Yield", ".Inflation" and "QuantLib.Model", and an instance must
-- share a module with its class to stay non-orphan.
class HasHelperUnderlying h where
  type HelperUnderlying h
  -- |The instrument the helper prices. For helpers that build it internally --
  -- 'QuantLib.TermStructure.Yield.fixedRateBondHelper' and
  -- 'QuantLib.TermStructure.Inflation.cpiBondHelper' among them -- this is the only way to reach it.
  helperInstrument :: h -> IO (HelperUnderlying h)

instance HasHelperUnderlying BondHelper where
  type HelperUnderlying BondHelper = Bond
  helperInstrument = qlBondHelperBond
instance HasHelperUnderlying SwapRateHelper where
  type HelperUnderlying SwapRateHelper = VanillaSwap
  helperInstrument = qlSwapRateHelperSwap
instance HasHelperUnderlying OISRateHelper where
  type HelperUnderlying OISRateHelper = OvernightIndexedSwap
  helperInstrument = qlOISRateHelperSwap
instance HasHelperUnderlying ZeroCouponInflationSwapHelper where
  type HelperUnderlying ZeroCouponInflationSwapHelper = ZeroCouponInflationSwap
  helperInstrument = qlZeroCouponInflationSwapHelperSwap
instance HasHelperUnderlying YearOnYearInflationSwapHelper where
  type HelperUnderlying YearOnYearInflationSwapHelper = YearOnYearInflationSwap
  helperInstrument = qlYearOnYearInflationSwapHelperSwap
-- 'QuantLib.Model.helperSwaption' reaches the swaption wrapped around this swap.
instance HasHelperUnderlying SwaptionHelper where
  type HelperUnderlying SwaptionHelper = FixedVsFloatingSwap
  helperInstrument = qlSwaptionHelperUnderlying

{#fun qlBondHelperBond{withGenRateHelper*`BondHelper',preErrorCheck-`String'errorCheck*-}->`Bond'peekBond*#}
{#fun qlSwapRateHelperSwap{withGenRateHelper*`SwapRateHelper',preErrorCheck-`String'errorCheck*-}->`VanillaSwap'peekVanillaSwap*#}
{#fun qlOISRateHelperSwap{withGenRateHelper*`OISRateHelper',preErrorCheck-`String'errorCheck*-}->`OvernightIndexedSwap'peekOvernightIndexedSwap*#}
{#fun qlZeroCouponInflationSwapHelperSwap{withZeroCouponInflationSwapHelper*`ZeroCouponInflationSwapHelper',preErrorCheck-`String'errorCheck*-}->`ZeroCouponInflationSwap'peekZeroCouponInflationSwap*#}
{#fun qlYearOnYearInflationSwapHelperSwap{withYearOnYearInflationSwapHelper*`YearOnYearInflationSwapHelper',preErrorCheck-`String'errorCheck*-}->`YearOnYearInflationSwap'peekYearOnYearInflationSwap*#}
{#fun qlSwaptionHelperUnderlying{withSwaptionHelper*`SwaptionHelper',preErrorCheck-`String'errorCheck*-}->`FixedVsFloatingSwap'peekFixedVsFloatingSwap*#}

-- |Bootstrapped curve leaves that retain their native nodes. The class lives in this root module
-- for the same reason as 'HasHelperUnderlying': its instances span the topical curve modules.
class HasNodes c where
  -- |The curve's (date, value) nodes; reading them runs the bootstrap. The first node is the
  -- reference date (inflation curves: the base date), the rest are helper pillars. Each value is
  -- what the curve interpolates: the trait's quantity for yield and default curves, a
  -- discount-factor spread for 'QuantLib.TermStructure.Yield.piecewiseSpreadYieldCurve', and
  -- the rate before seasonality for inflation curves. Jump factors are applied separately and
  -- are not part of the nodes.
  --
  -- An interpolated curve of the same kind on these nodes, with the original day counter,
  -- interpolation, jumps and seasonality, reproduces the bootstrapped curve. The exception is a
  -- local bootstrap, whose convex-monotone shape also depends on state the nodes do not carry.
  bootstrappedNodes :: c -> IO [(Day, Double)]

instance HasNodes PiecewiseYieldCurve where
  bootstrappedNodes = fmap (uncurry zip) . qlPiecewiseYieldCurveNodes
instance HasNodes PiecewiseDefaultCurve where
  bootstrappedNodes = fmap (uncurry zip) . qlPiecewiseDefaultCurveNodes
instance HasNodes PiecewiseZeroInflationCurve where
  bootstrappedNodes = fmap (uncurry zip) . qlPiecewiseZeroInflationCurveNodes
instance HasNodes PiecewiseYoYInflationCurve where
  bootstrappedNodes = fmap (uncurry zip) . qlPiecewiseYoYInflationCurveNodes

{#fun qlPiecewiseYieldCurveNodes{withPiecewiseYieldCurve*`PiecewiseYieldCurve',preIntArray-`[Day]'&peekDayArray*,preDoubleArray-`[Double]'&peekDoubleArray*,preErrorCheck-`String'errorCheck*-}->`()'#}
{#fun qlPiecewiseDefaultCurveNodes{withPiecewiseDefaultCurve*`PiecewiseDefaultCurve',preIntArray-`[Day]'&peekDayArray*,preDoubleArray-`[Double]'&peekDoubleArray*,preErrorCheck-`String'errorCheck*-}->`()'#}
{#fun qlPiecewiseZeroInflationCurveNodes{withPiecewiseZeroInflationCurve*`PiecewiseZeroInflationCurve',preIntArray-`[Day]'&peekDayArray*,preDoubleArray-`[Double]'&peekDoubleArray*,preErrorCheck-`String'errorCheck*-}->`()'#}
{#fun qlPiecewiseYoYInflationCurveNodes{withPiecewiseYoYInflationCurve*`PiecewiseYoYInflationCurve',preIntArray-`[Day]'&peekDayArray*,preDoubleArray-`[Double]'&peekDoubleArray*,preErrorCheck-`String'errorCheck*-}->`()'#}

-- |A term-structure reference point. 'ReferenceDate' stays fixed for the object's lifetime;
-- 'SettlementDays' follows the global evaluation date using the supplied calendar.
data Reference
  = ReferenceDate !Day
  | SettlementDays !Word !Calendar
  deriving (Eq, Show)

-- |Reference-point variants for constructors that take a calendar independently in both
-- upstream overloads.
data CalendarReference
  = CalendarReferenceDate !Day
  | CalendarSettlementDays !Word
  deriving (Eq, Show)

-- |A date or year-fraction coordinate measured from a term structure's reference date.
data TermPoint = DatePoint !Day | TimePoint !Double
  deriving (Eq, Show)

-- |A same-representation interval. Keeping both endpoints in one constructor prevents mixed
-- date/time intervals that upstream does not accept.
data TermInterval
  = DateInterval !Day !Day
  | TimeInterval !Double !Double
  deriving (Eq, Show)

-- |A date or year-fraction coordinate where a date needs its day-counting rule.
data RatePoint
  = RateAtDate !Day !DayCounter
  | RateAtTime !Double
  deriving (Eq, Show)

-- |A same-representation interval where dates need their day-counting rule; times use the
-- curve's own.
data RateInterval
  = RateBetweenDates !Day !Day !DayCounter
  | RateBetweenTimes !Double !Double
  deriving (Eq, Show)

-- |the date at which discount = 1.0 and/or variance = 0.0
{#fun qlTermStructureReferenceDate as referenceDate{withTermStructure*`GenTermStructure t',preErrorCheck-`String'errorCheck*-}->`Day'toDay#}

-- |the latest date for which the curve can return values
{#fun qlTermStructureMaxDate as maxDate{withTermStructure*`GenTermStructure t',preErrorCheck-`String'errorCheck*-}->`Day'toDay#}

-- |'maxDate' expressed as a year fraction from the structure's reference date
{#fun qlTermStructureMaxTime as maxTime{withTermStructure*`GenTermStructure t',preErrorCheck-`String'errorCheck*-}->`Double'#}

-- |Whether calls beyond the term structure's maximum date are allowed by default.
{#fun qlTermStructureAllowsExtrapolation as allowsExtrapolation{withTermStructure*`GenTermStructure t'}->`Bool'#}

-- |Enable or disable default extrapolation for any term structure.
{#fun qlTermStructureSetExtrapolation as setExtrapolation{withTermStructure*`GenTermStructure t',`Bool'}->`()'#}

-- |Converts a date to a time (as a fraction of year) according to the term structure's day counter.
{#fun qlTermStructureTimeFromReference as timeFromReference{withTermStructure*`GenTermStructure t' -- ^term structure
  ,withDay*`Day' -- ^date
  ,preErrorCheck-`String'errorCheck*-}->`Double'#}

-- vim: set ff=unix ts=8 sts=2 sw=2 et: