hasquant-0.7.0.0: test/example/QuantLib/Example/FxForward.hs
module QuantLib.Example.FxForward
(
Result(..)
, run
) where
import QuantLib.Currency
import QuantLib.Instrument
import QuantLib.Instrument.Forward
import qualified QuantLib.InterestRate as IR
import QuantLib.PricingEngine(discountingFxForwardEngine)
import QuantLib.Quote(simpleQuote)
import QuantLib.Context(setEvaluationDate)
import QuantLib.TermStructure.Yield(Reference(..), flatForward)
import QuantLib.Time.Calendar
import QuantLib.Time.Date
import QuantLib.Time.Schedule(dayCounter, DayCounterConstructor(..), Frequency(..), TimeUnit(..))
data Result = Result
{ npvR :: Double
, fairForwardRateR :: Double
, npvSourceCurrencyR :: Double
, npvTargetCurrencyR :: Double
, npvAtFairRateR :: Double
} deriving Show
run :: IO Result
run = do
setEvaluationDate $ Just evalDate
dc <- dayCounter Actual365FixedStandard
cal <- calendar Null
sourceQ <- simpleQuote sourceRate
targetQ <- simpleQuote targetRate
sourceCurve <- flatForward (ReferenceDate evalDate) sourceQ dc IR.Continuous Annual
targetCurve <- flatForward (ReferenceDate evalDate) targetQ dc IR.Continuous Annual
spotFxQ <- simpleQuote spotFx
eur <- currency EUR
usd <- currency USD
maturity <- advance cal evalDate (1, Years) Unadjusted False
fwd <- fxForward sourceNominal eur targetNominal usd maturity True 2 cal
engine <- discountingFxForwardEngine sourceCurve targetCurve spotFxQ
setPricingEngine fwd engine
npvV <- npv fwd
ffr <- fairForwardRate fwd
npvSrc <- npvSourceCurrency fwd
npvTgt <- npvTargetCurrency fwd
-- exercise the rate-based constructor (fxForwardFromRate): a contract struck at
-- the just-computed fair rate must have ~0 NPV, an economic invariant
-- independent of the nominal-based constructor tested above.
fwdAtFairRate <- fxForwardFromRate sourceNominal eur usd ffr maturity True 2 cal
setPricingEngine fwdAtFairRate engine
npvFair <- npv fwdAtFairRate
return Result
{ npvR = npvV
, fairForwardRateR = ffr
, npvSourceCurrencyR = npvSrc
, npvTargetCurrencyR = npvTgt
, npvAtFairRateR = npvFair
}
where
evalDate = 2 `january` 2024
sourceRate = 0.03
targetRate = 0.05
spotFx = 1.10
sourceNominal = 1000000.0
targetNominal = 1100000.0
-- vim: set ft=haskell ff=unix ts=8 sts=2 sw=2 et: