never executed always true always false
    1 -- GENERATED by C->Haskell Compiler, version 0.28.8 Switcheroo, 25 November 2017 (Haskell)
    2 -- Edit the ORIGNAL .chs file instead!
    3 
    4 
    5 module QuantLib.Instrument.Credit
    6   (
    7     CreditDefaultSwap
    8   , ProtectionSide(..)
    9   , Claim(..)
   10 
   11   , creditDefaultSwap
   12   , creditDefaultSwap'
   13 
   14   , atmRate
   15   , cdsOption
   16   , impliedVolatility
   17   , riskyAnnuity
   18 
   19   , conventionalSpread
   20   , couponLegBPS
   21   , couponLegNPV
   22   , coupons
   23   , defaultLegNPV
   24   , fairUpfront
   25   , impliedHazardRate
   26   , upfrontBPS
   27   , upfrontNPV
   28   ) where
   29 import qualified Foreign.C.Types as C2HSImp
   30 import qualified Foreign.ForeignPtr as C2HSImp
   31 import qualified Foreign.Marshal.Utils as C2HSImp
   32 import qualified Foreign.Ptr as C2HSImp
   33 
   34 
   35 import QuantLib.Internal
   36 import QuantLib.Internal.Common
   37 import QuantLib.Internal.Type
   38 import QuantLib.Instrument(PricingModel)
   39 
   40 
   41 
   42 
   43 
   44 
   45 
   46 data ProtectionSide = Buyer
   47                     | Seller
   48   deriving (Enum,Show,Eq,Read)
   49 
   50 
   51 
   52 
   53 
   54 
   55 
   56 
   57 
   58 
   59 
   60 
   61 
   62 
   63 
   64 
   65 
   66 
   67 
   68 
   69 
   70 
   71 
   72 
   73 
   74 
   75 -- |CDS quoted as running-spread only.
   76 -- side Whether the protection is bought or sold. notional Notional value spread Running spread in fractional units. schedule Coupon schedule. paymentConvention Business-day convention for payment-date adjustment. dayCounter Day-count convention for accrual. settlesAccrual Whether or not the accrued coupon is due in the event of a default. paysAtDefaultTime If set to true, any payments triggered by a default event are due at default time. If set to false, they are due at the end of the accrual period. protectionStart The first date where a default event will trigger the contract.
   77 creditDefaultSwap :: (ProtectionSide) -> (Double) -- ^notional
   78  -> (Double) -- ^spread
   79  -> (Schedule) -> (BusinessDayConvention) -> (DayCounter) -> (Bool) -- ^settlesAccrual
   80  -> (Bool) -- ^paysAtDefaultTime
   81  -> (Maybe Day) -- ^protectionStart
   82  -> (Claim) -> (DayCounter) -- ^lastPeriodDayCounter
   83  -> (Bool) -- ^rebatesAccrual
   84  -> (Maybe Day) -- ^tradeDate
   85  -> (Word) -- ^cashSettlementDays
   86  -> IO ((CreditDefaultSwap))
   87 creditDefaultSwap a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 =
   88   let {a1' = (fromIntegral . fromEnum) a1} in 
   89   let {a2' = realToFrac a2} in 
   90   let {a3' = realToFrac a3} in 
   91   withSchedule a4 $ \a4' -> 
   92   let {a5' = fromEnumC a5} in 
   93   withDayCounter a6 $ \a6' -> 
   94   let {a7' = C2HSImp.fromBool a7} in 
   95   let {a8' = C2HSImp.fromBool a8} in 
   96   withMaybeDay a9 $ \a9' -> 
   97   withClaim a10 $ \a10' -> 
   98   withDayCounter a11 $ \a11' -> 
   99   let {a12' = C2HSImp.fromBool a12} in 
  100   withMaybeDay a13 $ \a13' -> 
  101   let {a14' = fromIntegral a14} in 
  102   preErrorCheck $ \a15' -> 
  103   creditDefaultSwap'_ a1' a2' a3' a4' a5' a6' a7' a8' a9' a10' a11' a12' a13' a14' a15' >>= \res ->
  104   peekCreditDefaultSwap res >>= \res' ->
  105   errorCheck  a15'>>
  106   return (res')
  107 
  108 
  109 
  110 -- |CDS quoted as upfront and running spread.
  111 -- side Whether the protection is bought or sold. notional Notional value upfront Upfront in fractional units. spread Running spread in fractional units. schedule Coupon schedule. paymentConvention Business-day convention for payment-date adjustment. dayCounter Day-count convention for accrual. settlesAccrual Whether or not the accrued coupon is due in the event of a default. paysAtDefaultTime If set to true, any payments triggered by a default event are due at default time. If set to false, they are due at the end of the accrual period. protectionStart The first date where a default event will trigger the contract. upfrontDate Settlement date for the upfront payment.
  112 creditDefaultSwap' :: (ProtectionSide) -> (Double) -- ^notional
  113  -> (Double) -- ^upfront
  114  -> (Double) -- ^spread
  115  -> (Schedule) -> (BusinessDayConvention) -> (DayCounter) -> (Bool) -- ^settlesAccrual
  116  -> (Bool) -- ^paysAtDefaultTime
  117  -> (Maybe Day) -- ^protectionStart
  118  -> (Maybe Day) -- ^upfrontDate
  119  -> (Claim) -> (DayCounter) -- ^lastPeriodDayCounter
  120  -> (Bool) -- ^rebatesAccrual
  121  -> (Maybe Day) -- ^tradeDate
  122  -> (Word) -- ^cashSettlementDays
  123  -> IO ((CreditDefaultSwap))
  124 creditDefaultSwap' a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 =
  125   let {a1' = (fromIntegral . fromEnum) a1} in 
  126   let {a2' = realToFrac a2} in 
  127   let {a3' = realToFrac a3} in 
  128   let {a4' = realToFrac a4} in 
  129   withSchedule a5 $ \a5' -> 
  130   let {a6' = fromEnumC a6} in 
  131   withDayCounter a7 $ \a7' -> 
  132   let {a8' = C2HSImp.fromBool a8} in 
  133   let {a9' = C2HSImp.fromBool a9} in 
  134   withMaybeDay a10 $ \a10' -> 
  135   withMaybeDay a11 $ \a11' -> 
  136   withClaim a12 $ \a12' -> 
  137   withDayCounter a13 $ \a13' -> 
  138   let {a14' = C2HSImp.fromBool a14} in 
  139   withMaybeDay a15 $ \a15' -> 
  140   let {a16' = fromIntegral a16} in 
  141   preErrorCheck $ \a17' -> 
  142   creditDefaultSwap''_ a1' a2' a3' a4' a5' a6' a7' a8' a9' a10' a11' a12' a13' a14' a15' a16' a17' >>= \res ->
  143   peekCreditDefaultSwap res >>= \res' ->
  144   errorCheck  a17'>>
  145   return (res')
  146 
  147 
  148 
  149 -- |The fair running spread implied by the underlying CDS's term structures at the option's exercise.
  150 atmRate :: (CdsOption) -> IO ((Double))
  151 atmRate a1 =
  152   withCdsOption a1 $ \a1' -> 
  153   preErrorCheck $ \a2' -> 
  154   atmRate'_ a1' a2' >>= \res ->
  155   let {res' = realToFrac res} in
  156   errorCheck  a2'>>
  157   return (res')
  158 
  159 
  160 
  161 -- |An option giving the right to enter the underlying CDS, buying protection and paying coupon.
  162 cdsOption :: (CreditDefaultSwap) -> (Exercise) -> (Bool) -- ^knocksOut
  163  -> IO ((CdsOption))
  164 cdsOption a1 a2 a3 =
  165   withGenInstrument a1 $ \a1' -> 
  166   withExercise a2 $ \a2' -> 
  167   let {a3' = C2HSImp.fromBool a3} in 
  168   preErrorCheck $ \a4' -> 
  169   cdsOption'_ a1' a2' a3' a4' >>= \res ->
  170   peekCdsOption res >>= \res' ->
  171   errorCheck  a4'>>
  172   return (res')
  173 
  174 
  175 
  176 -- |Volatility that reproduces a given option price under the pricing engine's volatility model.
  177 impliedVolatility :: (CdsOption) -> (Double) -- ^price
  178  -> (GenYieldTermStructure y) -> (DefaultProbabilityTermStructure) -> (Double) -- ^recoveryRate
  179  -> (Double) -- ^accuracy
  180  -> (Word) -- ^maxEvaluations
  181  -> (Double) -- ^minVol
  182  -> (Double) -- ^maxVol
  183  -> IO ((Double))
  184 impliedVolatility a1 a2 a3 a4 a5 a6 a7 a8 a9 =
  185   withCdsOption a1 $ \a1' -> 
  186   let {a2' = realToFrac a2} in 
  187   withYieldTermStructure a3 $ \a3' -> 
  188   withGenTermStructure a4 $ \a4' -> 
  189   let {a5' = realToFrac a5} in 
  190   let {a6' = realToFrac a6} in 
  191   let {a7' = fromIntegral a7} in 
  192   let {a8' = realToFrac a8} in 
  193   let {a9' = realToFrac a9} in 
  194   preErrorCheck $ \a10' -> 
  195   impliedVolatility'_ a1' a2' a3' a4' a5' a6' a7' a8' a9' a10' >>= \res ->
  196   let {res' = realToFrac res} in
  197   errorCheck  a10'>>
  198   return (res')
  199 
  200 
  201 
  202 -- |The risky annuity used to convert between the option's price and its implied volatility.
  203 riskyAnnuity :: (CdsOption) -> IO ((Double))
  204 riskyAnnuity a1 =
  205   withCdsOption a1 $ \a1' -> 
  206   preErrorCheck $ \a2' -> 
  207   riskyAnnuity'_ a1' a2' >>= \res ->
  208   let {res' = realToFrac res} in
  209   errorCheck  a2'>>
  210   return (res')
  211 
  212 
  213 
  214 -- |Conventional/standard upfront-to-spread conversion.
  215 -- Under a standard ISDA model and a set of standardised instrument characteristics, it is the running only quoted spread that will make a CDS contract have an NPV of 0 when quoted for that running only spread. Refer to: "ISDA Standard CDS converter specification." May 2009.The conventional recovery rate to apply in the calculation is as specified by ISDA, not necessarily equal to the market-quoted one. It is typically 0.4 for SeniorSec and 0.2 for subordinate.The conversion employs a flat hazard rate. As a result, you will not recover the market quotes.This method performs the calculation with the instrument characteristics. It will coincide with the ISDA calculation if your object has the standard characteristics. Notably: The calendar should have no bank holidays, just weekends.The yield curve should be LIBOR piecewise constant in fwd rates, with a discount factor of 1 on the calculation date, which coincides with the trade date.Convention should be Following for yield curve and contract cashflows.The CDS should pay accrued and mature on standard IMM dates, settle on trade date +1 and upfront settle on trade date +3.
  216 conventionalSpread :: (CreditDefaultSwap) -> (Double) -> (GenYieldTermStructure y) -> (DayCounter) -> (PricingModel) -- ^model
  217  -> IO ((Double))
  218 conventionalSpread a1 a2 a3 a4 a5 =
  219   withGenInstrument a1 $ \a1' -> 
  220   let {a2' = realToFrac a2} in 
  221   withYieldTermStructure a3 $ \a3' -> 
  222   withDayCounter a4 $ \a4' -> 
  223   let {a5' = (fromIntegral . fromEnum) a5} in 
  224   preErrorCheck $ \a6' -> 
  225   conventionalSpread'_ a1' a2' a3' a4' a5' a6' >>= \res ->
  226   let {res' = realToFrac res} in
  227   errorCheck  a6'>>
  228   return (res')
  229 
  230 
  231 
  232 -- |Returns the variation of the fixed-leg value given a one-basis-point change in the running spread.
  233 couponLegBPS :: (CreditDefaultSwap) -> IO ((Double))
  234 couponLegBPS a1 =
  235   withGenInstrument a1 $ \a1' -> 
  236   preErrorCheck $ \a2' -> 
  237   couponLegBPS'_ a1' a2' >>= \res ->
  238   let {res' = realToFrac res} in
  239   errorCheck  a2'>>
  240   return (res')
  241 
  242 
  243 
  244 -- |NPV of the coupon (premium) leg.
  245 couponLegNPV :: (CreditDefaultSwap) -> IO ((Double))
  246 couponLegNPV a1 =
  247   withGenInstrument a1 $ \a1' -> 
  248   preErrorCheck $ \a2' -> 
  249   couponLegNPV'_ a1' a2' >>= \res ->
  250   let {res' = realToFrac res} in
  251   errorCheck  a2'>>
  252   return (res')
  253 
  254 
  255 
  256 -- |The coupon-leg cash flows of the CDS.
  257 coupons :: (CreditDefaultSwap) -> IO ((Leg))
  258 coupons a1 =
  259   withGenInstrument a1 $ \a1' -> 
  260   preErrorCheck $ \a2' -> 
  261   coupons'_ a1' a2' >>= \res ->
  262   peekLeg res >>= \res' ->
  263   errorCheck  a2'>>
  264   return (res')
  265 
  266 
  267 
  268 -- |NPV of the default (protection) leg.
  269 defaultLegNPV :: (CreditDefaultSwap) -> IO ((Double))
  270 defaultLegNPV a1 =
  271   withGenInstrument a1 $ \a1' -> 
  272   preErrorCheck $ \a2' -> 
  273   defaultLegNPV'_ a1' a2' >>= \res ->
  274   let {res' = realToFrac res} in
  275   errorCheck  a2'>>
  276   return (res')
  277 
  278 
  279 
  280 -- |Returns the upfront spread that, given the running spread and the quoted recovery rate, will make the instrument have an NPV of 0.
  281 fairUpfront :: (CreditDefaultSwap) -> IO ((Double))
  282 fairUpfront a1 =
  283   withGenInstrument a1 $ \a1' -> 
  284   preErrorCheck $ \a2' -> 
  285   fairUpfront'_ a1' a2' >>= \res ->
  286   let {res' = realToFrac res} in
  287   errorCheck  a2'>>
  288   return (res')
  289 
  290 
  291 
  292 -- |Implied hazard rate calculation.
  293 -- This method performs the calculation with the instrument characteristics. It will coincide with the ISDA calculation if your object has the standard characteristics. Notably: The calendar should have no bank holidays, just weekends.The yield curve should be LIBOR piecewise constant in fwd rates, with a discount factor of 1 on the calculation date, which coincides with the trade date.Convention should be Following for yield curve and contract cashflows.The CDS should pay accrued and mature on standard IMM dates, settle on trade date +1 and upfront settle on trade date +3.
  294 impliedHazardRate :: (CreditDefaultSwap) -> (Double) -- ^targetNPV
  295  -> (GenYieldTermStructure y) -> (DayCounter) -> (Double) -- ^recoveryRate
  296  -> (Double) -- ^accuracy
  297  -> (PricingModel) -- ^model
  298  -> IO ((Double))
  299 impliedHazardRate a1 a2 a3 a4 a5 a6 a7 =
  300   withGenInstrument a1 $ \a1' -> 
  301   let {a2' = realToFrac a2} in 
  302   withYieldTermStructure a3 $ \a3' -> 
  303   withDayCounter a4 $ \a4' -> 
  304   let {a5' = realToFrac a5} in 
  305   let {a6' = realToFrac a6} in 
  306   let {a7' = (fromIntegral . fromEnum) a7} in 
  307   preErrorCheck $ \a8' -> 
  308   impliedHazardRate'_ a1' a2' a3' a4' a5' a6' a7' a8' >>= \res ->
  309   let {res' = realToFrac res} in
  310   errorCheck  a8'>>
  311   return (res')
  312 
  313 
  314 
  315 -- |Returns the variation of the upfront payment value given a one-basis-point change in the upfront.
  316 upfrontBPS :: (CreditDefaultSwap) -> IO ((Double))
  317 upfrontBPS a1 =
  318   withGenInstrument a1 $ \a1' -> 
  319   preErrorCheck $ \a2' -> 
  320   upfrontBPS'_ a1' a2' >>= \res ->
  321   let {res' = realToFrac res} in
  322   errorCheck  a2'>>
  323   return (res')
  324 
  325 
  326 
  327 -- |NPV of the upfront payment.
  328 upfrontNPV :: (CreditDefaultSwap) -> IO ((Double))
  329 upfrontNPV a1 =
  330   withGenInstrument a1 $ \a1' -> 
  331   preErrorCheck $ \a2' -> 
  332   upfrontNPV'_ a1' a2' >>= \res ->
  333   let {res' = realToFrac res} in
  334   errorCheck  a2'>>
  335   return (res')
  336 
  337 
  338 
  339 -- vim: set ff=unix ts=8 sts=2 sw=2 et:
  340 
  341 foreign import ccall safe "QuantLib/Instrument/Credit.chs.h qlCreditDefaultSwap"
  342   creditDefaultSwap'_ :: (C2HSImp.CInt -> (C2HSImp.CDouble -> (C2HSImp.CDouble -> ((C2HSImp.Ptr (CSchedule)) -> (C2HSImp.CInt -> ((C2HSImp.Ptr (CDayCounter)) -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> ((C2HSImp.Ptr (CQlClaim)) -> ((C2HSImp.Ptr (CDayCounter)) -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CUInt -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO (C2HSImp.Ptr (CCreditDefaultSwap'))))))))))))))))))
  343 
  344 foreign import ccall safe "QuantLib/Instrument/Credit.chs.h qlCreditDefaultSwap1"
  345   creditDefaultSwap''_ :: (C2HSImp.CInt -> (C2HSImp.CDouble -> (C2HSImp.CDouble -> (C2HSImp.CDouble -> ((C2HSImp.Ptr (CSchedule)) -> (C2HSImp.CInt -> ((C2HSImp.Ptr (CDayCounter)) -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> ((C2HSImp.Ptr (CQlClaim)) -> ((C2HSImp.Ptr (CDayCounter)) -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CUInt -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO (C2HSImp.Ptr (CCreditDefaultSwap'))))))))))))))))))))
  346 
  347 foreign import ccall safe "QuantLib/Instrument/Credit.chs.h qlCdsOptionAtmRate"
  348   atmRate'_ :: ((C2HSImp.Ptr (CCdsOption')) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CDouble)))
  349 
  350 foreign import ccall safe "QuantLib/Instrument/Credit.chs.h qlCdsOption"
  351   cdsOption'_ :: ((C2HSImp.Ptr (CCreditDefaultSwap')) -> ((QlExercise) -> (C2HSImp.CInt -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO (C2HSImp.Ptr (CCdsOption')))))))
  352 
  353 foreign import ccall safe "QuantLib/Instrument/Credit.chs.h qlCdsOptionImpliedVolatility"
  354   impliedVolatility'_ :: ((C2HSImp.Ptr (CCdsOption')) -> (C2HSImp.CDouble -> ((C2HSImp.Ptr (CYieldTermStructure')) -> ((C2HSImp.Ptr (CDefaultProbabilityTermStructure')) -> (C2HSImp.CDouble -> (C2HSImp.CDouble -> (C2HSImp.CUInt -> (C2HSImp.CDouble -> (C2HSImp.CDouble -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CDouble)))))))))))
  355 
  356 foreign import ccall safe "QuantLib/Instrument/Credit.chs.h qlCdsOptionRiskyAnnuity"
  357   riskyAnnuity'_ :: ((C2HSImp.Ptr (CCdsOption')) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CDouble)))
  358 
  359 foreign import ccall safe "QuantLib/Instrument/Credit.chs.h qlCreditDefaultSwapConventionalSpread"
  360   conventionalSpread'_ :: ((C2HSImp.Ptr (CCreditDefaultSwap')) -> (C2HSImp.CDouble -> ((C2HSImp.Ptr (CYieldTermStructure')) -> ((C2HSImp.Ptr (CDayCounter)) -> (C2HSImp.CInt -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CDouble)))))))
  361 
  362 foreign import ccall safe "QuantLib/Instrument/Credit.chs.h qlCreditDefaultSwapCouponLegBPS"
  363   couponLegBPS'_ :: ((C2HSImp.Ptr (CCreditDefaultSwap')) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CDouble)))
  364 
  365 foreign import ccall safe "QuantLib/Instrument/Credit.chs.h qlCreditDefaultSwapCouponLegNPV"
  366   couponLegNPV'_ :: ((C2HSImp.Ptr (CCreditDefaultSwap')) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CDouble)))
  367 
  368 foreign import ccall safe "QuantLib/Instrument/Credit.chs.h qlCreditDefaultSwapCoupons"
  369   coupons'_ :: ((C2HSImp.Ptr (CCreditDefaultSwap')) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO (C2HSImp.Ptr (CLeg')))))
  370 
  371 foreign import ccall safe "QuantLib/Instrument/Credit.chs.h qlCreditDefaultSwapDefaultLegNPV"
  372   defaultLegNPV'_ :: ((C2HSImp.Ptr (CCreditDefaultSwap')) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CDouble)))
  373 
  374 foreign import ccall safe "QuantLib/Instrument/Credit.chs.h qlCreditDefaultSwapFairUpfront"
  375   fairUpfront'_ :: ((C2HSImp.Ptr (CCreditDefaultSwap')) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CDouble)))
  376 
  377 foreign import ccall safe "QuantLib/Instrument/Credit.chs.h qlCreditDefaultSwapImpliedHazardRate"
  378   impliedHazardRate'_ :: ((C2HSImp.Ptr (CCreditDefaultSwap')) -> (C2HSImp.CDouble -> ((C2HSImp.Ptr (CYieldTermStructure')) -> ((C2HSImp.Ptr (CDayCounter)) -> (C2HSImp.CDouble -> (C2HSImp.CDouble -> (C2HSImp.CInt -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CDouble)))))))))
  379 
  380 foreign import ccall safe "QuantLib/Instrument/Credit.chs.h qlCreditDefaultSwapUpfrontBPS"
  381   upfrontBPS'_ :: ((C2HSImp.Ptr (CCreditDefaultSwap')) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CDouble)))
  382 
  383 foreign import ccall safe "QuantLib/Instrument/Credit.chs.h qlCreditDefaultSwapUpfrontNPV"
  384   upfrontNPV'_ :: ((C2HSImp.Ptr (CCreditDefaultSwap')) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CDouble)))