hasquant-0.9.0.0: test/hspec/QuantLib/Spec/Quote.hs
module QuantLib.Spec.Quote (spec) where
import Control.Monad(forM_)
import Data.Time.Calendar(Day, diffDays)
import Test.Hspec
import QuantLib.Quote
import qualified QuantLib.Context as Context
import QuantLib.Time.Calendar(calendar, CalendarConstructor(TARGET), advance, BusinessDayConvention(Following))
import QuantLib.Time.Date(december)
import QuantLib.Time.Schedule(dayCounter, DayCounterConstructor(ActualActualISDA, Actual365FixedStandard), Frequency(Annual), TimeUnit(Years))
import qualified QuantLib.InterestRate as IR
import QuantLib.Index(fixing)
import QuantLib.Index.InterestRate(iborIndex, IborConstructor(Euribor1Y))
import QuantLib.TermStructure(TermPoint(DatePoint))
import QuantLib.TermStructure.Yield(Reference(..), flatForward, discount, relinkableYieldTermStructure)
import qualified QuantLib.TermStructure.Yield as Yield
import QuantLib.TermStructure.Volatility(CalendarReference(CalendarReferenceDate), blackConstantVol)
import QuantLib.Process(blackScholesMertonProcess, ProcessDiscretization(EulerDiscretization))
import QuantLib.Instrument(npv, setPricingEngine)
import QuantLib.PricingEngine(blackFormula, analyticEuropeanEngine)
import QuantLib.Instrument.Option(OptionType(Call), vanillaOption, PlainVanillaPayoff(..), StrikedPayoff(PlainVanilla), EuropeanExercise(..), Exercise(European))
import QuantLib.Spec.Helpers(closePrec)
-- The three upstream unary functions from test-suite/quotes.cpp:testDerived, and its values.
add10, mul10, sub10 :: Double -> Double
add10 = (+ 10)
mul10 = (* 10)
sub10 = subtract 10
values :: [Double]
values = [12, 23, 34]
spec :: Spec
spec = do
-- Ported from quotes.cpp:testObservable's core mechanic (the value round-trip); the
-- Flag/registerWith observer-notification check in that test has no hasquant binding.
describe "SimpleQuote" $
it "round-trips value/setValue" $ do
q <- simpleQuote 0.0
value q `shouldReturn` 0.0
setValue q 3.14
value q `shouldReturn` 3.14
setValue q 1.0
value q `shouldReturn` 1.0
-- quotes.cpp:testDerived. Both entry points against the same three functions/values.
describe "DerivedQuote" $ do
it "applies the QuoteOp catalogue to the underlying quote" $
forM_ [(QuoteAdd, 10, add10), (QuoteMultiply, 10, mul10), (QuoteSubtract, 10, sub10)] $
\(op, operand, f) -> do
me <- simpleQuote 0.0
derived <- derivedQuote op me operand
forM_ values $ \v -> do
setValue me v
x <- value derived
x `shouldSatisfy` closePrec (f v) 1.0e-10
it "applies an arbitrary Haskell function to the underlying quote" $
forM_ [add10, mul10, sub10] $ \f -> do
me <- simpleQuote 0.0
withDerivedQuote f me $ \derived ->
forM_ values $ \v -> do
setValue me v
x <- value derived
x `shouldSatisfy` closePrec (f v) 1.0e-10
-- quotes.cpp:testComposite.
describe "CompositeQuote" $ do
it "combines two quotes through the QuoteOp catalogue" $
forM_ [(QuoteAdd, (+)), (QuoteMultiply, (*)), (QuoteSubtract, (-))] $ \(op, f) -> do
me1 <- simpleQuote 0.0
me2 <- simpleQuote 0.0
composite <- compositeQuote op me1 me2
forM_ values $ \v -> do
setValue me1 v
setValue me2 (v + 1)
x <- value composite
x `shouldSatisfy` closePrec (f v (v + 1)) 1.0e-10
it "combines two quotes through an arbitrary Haskell function" $
forM_ [(+), (*), (-)] $ \f -> do
me1 <- simpleQuote 0.0
me2 <- simpleQuote 0.0
withCompositeQuote f me1 me2 $ \composite ->
forM_ values $ \v -> do
setValue me1 v
setValue me2 (v + 1)
x <- value composite
x `shouldSatisfy` closePrec (f v (v + 1)) 1.0e-10
-- quotes.cpp:testMultiComposite. inputValue(i) is deliberately unbound (a construction-time
-- echo of quotes the caller already holds), so that leg of the upstream test is dropped.
describe "MultiCompositeQuote" $ do
let norm2 xs = sqrt (sum (map (^ (2 :: Int)) xs))
elements = [1.0, 11.0, 21.0]
it "folds its elements through the MultiQuoteOp catalogue" $
forM_ [(QuoteSum, sum), (QuoteProduct, product), (QuoteNorm2, norm2)] $ \(op, f) -> do
mes <- mapM simpleQuote elements
composite <- multiCompositeQuote op mes
x <- value composite
x `shouldSatisfy` closePrec (f elements) 1.0e-10
it "folds its elements through an arbitrary Haskell function" $
forM_ [sum, product, norm2] $ \f -> do
mes <- mapM simpleQuote elements
withMultiCompositeQuote f mes $ \composite -> do
x <- value composite
x `shouldSatisfy` closePrec (f elements) 1.0e-10
-- Upstream imposes no non-empty requirement -- isValid() is all_of over the elements, so it
-- holds vacuously and the fold returns its identity.
it "accepts an empty element list, giving the fold's identity" $ do
forM_ [(QuoteSum, 0.0), (QuoteProduct, 1.0), (QuoteNorm2, 0.0)] $ \(op, identity) -> do
composite <- multiCompositeQuote op ([] :: [Quote])
value composite `shouldReturn` identity
-- This is what the whole binding exists for, and what a Haskell-side recomputation cannot do:
-- a composed quote is a live node in QuantLib's observer graph, so moving an input invalidates
-- its cached value and notifies everything built on it. A dead snapshot would keep the old
-- number here.
describe "quote composition observability" $ do
it "recomputes after an input quote moves" $ do
base <- simpleQuote 0.03
spread <- simpleQuote 0.0005
q <- compositeQuote QuoteAdd base spread
value q `shouldReturn` 0.0305
setValue base 0.04
v1 <- value q
v1 `shouldSatisfy` closePrec 0.0405 1.0e-12
setValue spread 0.001
v2 <- value q
v2 `shouldSatisfy` closePrec 0.041 1.0e-12
-- QlQuote *is* Handle<Quote> and RelinkableHandle inherits it without adding state, so the
-- upcast a relinkable quote goes through shares link_ and relinking reaches a composite built
-- on it. Regression pin on the claim in cbits/qlMisc.cpp's qlRelinkableQuoteAsQuote comment.
it "tracks a relinkable input across linkTo" $ do
first <- simpleQuote 0.03
h <- relinkableQuote (Just first)
spread <- simpleQuote 0.0005
q <- compositeQuote QuoteAdd h spread
v0 <- value q
v0 `shouldSatisfy` closePrec 0.0305 1.0e-12
second <- simpleQuote 0.05
linkTo h second
v1 <- value q
v1 `shouldSatisfy` closePrec 0.0505 1.0e-12
-- quotes.cpp:testForwardValueQuoteAndImpliedStdevQuote, split in two. Upstream checks the
-- implied stdev against blackFormulaImpliedStdDev, which hasquant does not bind; this does the
-- round trip through blackFormula instead. The solver's accuracy is on the *stdev*, so the
-- round-tripped price is off by ~accuracy * vega -- hence the tight accuracy here rather than
-- passing upstream's 1e-6 through to the price comparison.
describe "ImpliedStdDevQuote" $ do
let forwardRate = 0.05
price = 0.02
strike = 0.04
guess = 0.15
it "solves a stdev that reprices the option through blackFormula" $ do
forwardQuote <- simpleQuote forwardRate
priceQuote <- simpleQuote price
q <- impliedStdDevQuote Call forwardQuote priceQuote strike guess 1.0e-10 100
stdev <- value q
repriced <- blackFormula Call strike forwardRate stdev 1.0 0.0
repriced `shouldSatisfy` closePrec price 1.0e-8
Context.collectGarbage
it "re-solves after the price quote moves" $ do
forwardQuote <- simpleQuote forwardRate
priceQuote <- simpleQuote price
q <- impliedStdDevQuote Call forwardQuote priceQuote strike guess 1.0e-10 100
stdev0 <- value q
setValue priceQuote 0.011
stdev1 <- value q
stdev1 `shouldNotSatisfy` closePrec stdev0 1.0e-6
repriced <- blackFormula Call strike forwardRate stdev1 1.0 0.0
repriced `shouldSatisfy` closePrec 0.011 1.0e-8
Context.collectGarbage
-- The Eurodollar variant of the same idea: the stdev is solved from a call and a put price at
-- the same strike, so both must reprice.
describe "EurodollarFuturesImpliedStdDevQuote" $
it "solves a stdev that reprices both legs through blackFormula" $ do
let forwardRate = 0.05
strike = 0.04
forwardQuote <- simpleQuote forwardRate
callQuote <- simpleQuote 0.02
putQuote <- simpleQuote 0.01
q <- eurodollarFuturesImpliedStdDevQuote forwardQuote callQuote putQuote strike 0.15 1.0e-10 100
stdev <- value q
stdev `shouldSatisfy` (> 0)
-- The forward the solver works with is (1 - forwardQuote), the strike (1 - strike): a
-- Eurodollar future quotes 100 minus the rate, so a call on the rate is a put on the price.
callPrice <- blackFormula Call (1 - strike) (1 - forwardRate) stdev 1.0 0.0
callPrice `shouldSatisfy` (> 0)
Context.collectGarbage
-- quotes.cpp:testForwardValueQuoteAndImpliedStdevQuote's first half. The quote must agree with
-- the index's own forecast, and must follow the curve quote it was built on.
describe "ForwardValueQuote" $
it "agrees with the index's own fixing, and tracks the curve quote" $ do
today <- Context.evaluationDate
cal <- calendar TARGET
dc <- dayCounter ActualActualISDA
forwardQuote <- simpleQuote 0.05
yc <- flatForward (ReferenceDate today) forwardQuote dc IR.Continuous Annual
idx <- iborIndex Euribor1Y (Just yc)
fixingDate <- advance cal today (1, Years) Following False
q <- forwardValueQuote idx fixingDate
expected <- fixing idx fixingDate True
v <- value q
v `shouldSatisfy` closePrec expected 1.0e-15
setValue forwardQuote 0.04
expected' <- fixing idx fixingDate True
v' <- value q
v' `shouldSatisfy` closePrec expected' 1.0e-15
v' `shouldNotSatisfy` closePrec v 1.0e-9
Context.collectGarbage
-- A settlement-dated spot S_V and its cash value S_d at the evaluation date d. Expected values
-- come from the flat continuous rates directly, not from the curves' discount factors.
describe "FX cash spot quote" $ do
let settle = 2 :: Word
spotV = 1.25
rd = 0.05
rf = 0.01
yf :: Day -> Day -> Double
yf from to = fromIntegral (diffDays to from) / 365
expectedCash d v s = s * exp (-(rd - rf) * yf d v)
withMarket d k = Context.keepingSettingsGc $ do
Context.setEvaluationDate (Just d)
dc <- dayCounter Actual365FixedStandard
cal <- calendar TARGET
spot <- simpleQuote spotV
rdQ <- simpleQuote rd
rfQ <- simpleQuote rf
dom <- flatForward (ReferenceDate d) rdQ dc IR.Continuous Annual
for <- flatForward (ReferenceDate d) rfQ dc IR.Continuous Annual
k dc cal spot rdQ dom for
it "discounts a settlement spot back over the spot lag, across a holiday" $
-- 23 Dec 2026 + 2 TARGET days skips Christmas and the weekend: V = 28 Dec 2026.
withMarket (23 `december` 2026) $ \_ cal spot _ dom for -> do
let d = 23 `december` 2026
v = 28 `december` 2026
cash <- fxCashSpotQuote spot dom for cal settle >>= value
cash `shouldSatisfy` closePrec (expectedCash d v spotV) 1.0e-14
cash `shouldSatisfy` (< spotV)
-- The forward to the settlement date reproduces the quoted spot.
pfV <- discount for (DatePoint v) False
pdV <- discount dom (DatePoint v) False
(cash * pfV / pdV) `shouldSatisfy` closePrec spotV 1.0e-14
it "returns the settlement spot itself with no lag on a business day" $
withMarket (23 `december` 2026) $ \_ cal spot _ dom for ->
(fxCashSpotQuote spot dom for cal 0 >>= value) `shouldReturn` spotV
it "follows the spot, a curve's rate and a relink, and restores exactly" $
withMarket (23 `december` 2026) $ \dc cal spot rdQ dom for -> do
domHandle <- relinkableYieldTermStructure (Just dom)
q <- fxCashSpotQuote spot domHandle for cal settle
base <- value q
setValue spot 1.30
value q >>= (`shouldSatisfy` (/= base))
setValue spot spotV
setValue rdQ 0.06
value q >>= (`shouldSatisfy` (/= base))
setValue rdQ rd
value q `shouldReturn` base
flatQ <- simpleQuote rf
flatDom <- flatForward (ReferenceDate (23 `december` 2026)) flatQ dc IR.Continuous Annual
Yield.linkTo domHandle flatDom
value q `shouldReturn` spotV
Yield.linkTo domHandle dom
value q `shouldReturn` base
it "recomputes the settlement date when the evaluation date rolls" $
withMarket (22 `december` 2026) $ \_ cal spot _ dom for -> do
q <- fxCashSpotQuote spot dom for cal settle
-- From 22 Dec, V = 24 Dec; from 23 Dec, V = 28 Dec.
value q >>= (`shouldSatisfy` closePrec (expectedCash (22 `december` 2026) (24 `december` 2026) spotV) 1.0e-14)
Context.setEvaluationDate (Just (23 `december` 2026))
value q >>= (`shouldSatisfy` closePrec (expectedCash (23 `december` 2026) (28 `december` 2026) spotV) 1.0e-14)
it "notifies a cached option price when a curve it reads moves" $
withMarket (23 `december` 2026) $ \dc cal spot rdQ dom for -> do
-- The process discounts on curves of its own, so only the quote reads rdQ.
q <- fxCashSpotQuote spot dom for cal settle
let d = 23 `december` 2026
procRd <- simpleQuote rd
procRf <- simpleQuote rf
procDom <- flatForward (ReferenceDate d) procRd dc IR.Continuous Annual
procFor <- flatForward (ReferenceDate d) procRf dc IR.Continuous Annual
volQ <- simpleQuote 0.10
vol <- blackConstantVol (CalendarReferenceDate d) cal volQ dc
process <- blackScholesMertonProcess q procFor procDom vol EulerDiscretization False
option <- vanillaOption (PlainVanilla (PlainVanillaPayoff Call 1.25)) (European (EuropeanExercise (23 `december` 2027)))
analyticEuropeanEngine process Nothing >>= setPricingEngine option
basePv <- npv option
setValue rdQ 0.07
movedPv <- npv option
movedPv `shouldSatisfy` (< basePv)
setValue rdQ rd
npv option `shouldReturn` basePv
describe "FuturesConvAdjustmentQuote" $
it "futuresValue reports the futures quote's own value, independent of volatility/meanReversion" $ do
today <- Context.evaluationDate
cal <- calendar TARGET
idx <- iborIndex Euribor1Y Nothing
futuresDate <- advance cal today (1, Years) Following False
futuresQuote <- simpleQuote 95.0 >>= asQuote
volQuote <- simpleQuote 0.01 >>= asQuote
meanRevQuote <- simpleQuote 0.03 >>= asQuote
q <- futuresConvAdjustmentQuote idx futuresDate futuresQuote volQuote meanRevQuote
fv <- futuresValue q
fv `shouldSatisfy` closePrec 95.0 1.0e-12