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.TermStructure.Commodity
    6   (
    7     CommodityCurve
    8   , commodityCurve
    9   , commodityCurveName
   10   , commodityCurveCommodityType
   11   , commodityCurveUnitOfMeasure
   12   , commodityCurveCurrency
   13   , commodityCurveDates
   14   , commodityCurvePrices
   15   , commodityCurveEmpty
   16   , commodityCurveBasisOfCurve
   17   , setCommodityCurveBasisOfCurve
   18   , commodityCurvePrice
   19   , commodityCurveBasisOfPrice
   20   , ExchangeContract
   21   , ExchangeContracts
   22   , commodityCurvePriceNearby
   23   , commodityCurveUnderlyingPriceDate
   24   ) where
   25 import qualified Foreign.C.String as C2HSImp
   26 import qualified Foreign.C.Types as C2HSImp
   27 import qualified Foreign.ForeignPtr as C2HSImp
   28 import qualified Foreign.Marshal.Utils as C2HSImp
   29 import qualified Foreign.Ptr as C2HSImp
   30 import qualified System.IO.Unsafe as C2HSImp
   31 
   32 
   33 import QuantLib.Internal
   34 import QuantLib.Internal.Type
   35 
   36 
   37 
   38 
   39 
   40 
   41 
   42 
   43 
   44 
   45 
   46 
   47 
   48 
   49 
   50 
   51 
   52 
   53 
   54 
   55 -- |Construct a commodity price curve: a named, interpolated (forward-flat) price curve over a
   56 -- fixed set of dates, for a given commodity type\/currency\/unit of measure. QuantLib's no-dates
   57 -- constructor (populated later via the mutator @setPrices@) is not bound -- per the standing
   58 -- setter-confirmation rule, only 'setCommodityCurveBasisOfCurve' was confirmed, not @setPrices@ --
   59 -- so this with-dates constructor is the only way to build one.
   60 commodityCurve :: (String) -- ^name
   61  -> (CommodityType) -> (Currency) -> (UnitOfMeasure) -> (Calendar) -> ([Day]) -> ([Double]) -> (DayCounter) -> IO ((CommodityCurve))
   62 commodityCurve a1 a2 a3 a4 a5 a6 a7 a8 =
   63   C2HSImp.withCString a1 $ \a1' -> 
   64   withCommodityType a2 $ \a2' -> 
   65   withCurrency a3 $ \a3' -> 
   66   withUnitOfMeasure a4 $ \a4' -> 
   67   withCalendar a5 $ \a5' -> 
   68   withDayArray a6 $ \(a6'1, a6'2) -> 
   69   withDoubleArray a7 $ \(a7'1, a7'2) -> 
   70   withDayCounter a8 $ \a8' -> 
   71   preErrorCheck $ \a9' -> 
   72   commodityCurve'_ a1' a2' a3' a4' a5' a6'1  a6'2 a7'1  a7'2 a8' a9' >>= \res ->
   73   peekCommodityCurve res >>= \res' ->
   74   errorCheck  a9'>>
   75   return (res')
   76 
   77 
   78 
   79 -- |The curve's name, as given at construction.
   80 commodityCurveName :: (CommodityCurve) -> IO ((String))
   81 commodityCurveName a1 =
   82   withGenTermStructure a1 $ \a1' -> 
   83   commodityCurveName'_ a1' >>= \res ->
   84   peekDynString res >>= \res' ->
   85   return (res')
   86 
   87 
   88 
   89 -- |The commodity type this curve prices.
   90 commodityCurveCommodityType :: (CommodityCurve) -> IO ((CommodityType))
   91 commodityCurveCommodityType a1 =
   92   withGenTermStructure a1 $ \a1' -> 
   93   preErrorCheck $ \a2' -> 
   94   commodityCurveCommodityType'_ a1' a2' >>= \res ->
   95   peekCommodityType res >>= \res' ->
   96   errorCheck  a2'>>
   97   return (res')
   98 
   99 
  100 
  101 -- |The unit of measure this curve's prices are quoted in.
  102 commodityCurveUnitOfMeasure :: (CommodityCurve) -> IO ((UnitOfMeasure))
  103 commodityCurveUnitOfMeasure a1 =
  104   withGenTermStructure a1 $ \a1' -> 
  105   preErrorCheck $ \a2' -> 
  106   commodityCurveUnitOfMeasure'_ a1' a2' >>= \res ->
  107   peekUnitOfMeasure res >>= \res' ->
  108   errorCheck  a2'>>
  109   return (res')
  110 
  111 
  112 
  113 -- |The currency this curve's prices are quoted in.
  114 commodityCurveCurrency :: (CommodityCurve) -> IO ((Currency))
  115 commodityCurveCurrency a1 =
  116   withGenTermStructure a1 $ \a1' -> 
  117   preErrorCheck $ \a2' -> 
  118   commodityCurveCurrency'_ a1' a2' >>= \res ->
  119   peekCurrency res >>= \res' ->
  120   errorCheck  a2'>>
  121   return (res')
  122 
  123 
  124 
  125 -- |The curve's node dates, as given at construction.
  126 commodityCurveDates :: (CommodityCurve) -> IO (([Day]))
  127 commodityCurveDates a1 =
  128   withGenTermStructure a1 $ \a1' -> 
  129   preArray $ \(a2'1, a2'2) -> 
  130   preErrorCheck $ \a3' -> 
  131   commodityCurveDates'_ a1' a2'1  a2'2 a3' >>
  132   peekDayArray  a2'1  a2'2>>= \a2'' -> 
  133   errorCheck  a3'>>
  134   return (a2'')
  135 
  136 
  137 
  138 -- |The curve's node prices, as given at construction. Paired positionally with 'commodityCurveDates'
  139 -- -- upstream's own @nodes()@ getter is just their zip, so it isn't bound separately (per "bind
  140 -- few inspectors").
  141 commodityCurvePrices :: (CommodityCurve) -> IO (([Double]))
  142 commodityCurvePrices a1 =
  143   withGenTermStructure a1 $ \a1' -> 
  144   preArray $ \(a2'1, a2'2) -> 
  145   preErrorCheck $ \a3' -> 
  146   commodityCurvePrices'_ a1' a2'1  a2'2 a3' >>
  147   peekDoubleArray  a2'1  a2'2>>= \a2'' -> 
  148   errorCheck  a3'>>
  149   return (a2'')
  150 
  151 
  152 
  153 -- |Whether this curve has any nodes.
  154 commodityCurveEmpty :: (CommodityCurve) -> (Bool)
  155 commodityCurveEmpty a1 =
  156   C2HSImp.unsafePerformIO $
  157   withGenTermStructure a1 $ \a1' -> 
  158   commodityCurveEmpty'_ a1' >>= \res ->
  159   let {res' = C2HSImp.toBool res} in
  160   return (res')
  161 
  162 
  163 
  164 -- |The basis curve this curve was chained to via 'setCommodityCurveBasisOfCurve', if any.
  165 commodityCurveBasisOfCurve :: (CommodityCurve) -> IO ((Maybe CommodityCurve))
  166 commodityCurveBasisOfCurve a1 =
  167   withGenTermStructure a1 $ \a1' -> 
  168   commodityCurveBasisOfCurve'_ a1' >>= \res ->
  169   peekMaybeCommodityCurve res >>= \res' ->
  170   return (res')
  171 
  172 
  173 
  174 -- |Chain this curve to a basis curve: prices returned by 'commodityCurvePrice'\/'commodityCurveBasisOfPrice'
  175 -- then include the basis curve's price on top of this curve's own. Confirmed with the user as the
  176 -- one 'CommodityCurve' mutator worth binding (unlike @setPrices@, which stays unbound).
  177 setCommodityCurveBasisOfCurve :: (CommodityCurve) -> (CommodityCurve) -> IO ()
  178 setCommodityCurveBasisOfCurve a1 a2 =
  179   withGenTermStructure a1 $ \a1' -> 
  180   withGenTermStructure a2 $ \a2' -> 
  181   preErrorCheck $ \a3' -> 
  182   setCommodityCurveBasisOfCurve'_ a1' a2' a3' >>
  183   errorCheck  a3'>>
  184   return ()
  185 
  186 
  187 
  188 -- |A dated exchange contract: a code, its expiration date, and the start/end dates of the
  189 -- underlying delivery period it corresponds to. A plain tuple, per the @Money@/'Quantity'-as-tuple
  190 -- convention -- it carries no calculation of its own upstream, only three inspectors that would
  191 -- just be tuple projections.
  192 type ExchangeContract = (String, Day, Day, Day) -- ^code, expirationDate, underlyingStartDate, underlyingEndDate
  193 
  194 -- |QuantLib's @std::map\<Date,ExchangeContract\>@: a set of exchange contracts, keyed by the date
  195 -- 'commodityCurvePriceNearby'\/'commodityCurveUnderlyingPriceDate' roll onto (upstream's own
  196 -- @lower_bound@ walk finds the first key at or after the query date, then steps @nearbyOffset - 1@
  197 -- further). Marshalled as an association list, not an actual 'Data.Map.Map' -- the C shim rebuilds
  198 -- the real @std::map@ itself so key order doesn't need to be pre-sorted on the Haskell side.
  199 type ExchangeContracts = [(Day, ExchangeContract)]
  200 
  201 -- |Split an 'ExchangeContracts' into the five parallel lists the low-level bindings below take.
  202 -- Not a single combined marshaller: c2hs's @&@ tuple-splitter only ever consumes two C arguments
  203 -- (confirmed against its source, same reasoning as 'QuantLib.Commodity.Quantity'), so each list is
  204 -- passed as its own flat, individually-marshalled argument instead of one bundled continuation.
  205 splitExchangeContracts :: ExchangeContracts -> ([Day], [String], [Day], [Day], [Day])
  206 splitExchangeContracts ecs =
  207   ( map fst ecs
  208   , [c | (_, (c, _, _, _)) <- ecs]
  209   , [x | (_, (_, x, _, _)) <- ecs]
  210   , [s | (_, (_, _, s, _)) <- ecs]
  211   , [e | (_, (_, _, _, e)) <- ecs] )
  212 
  213 qlCommodityCurvePrice_ :: (CommodityCurve) -> (Day) -> ([Day]) -> ([String]) -> ([Day]) -> ([Day]) -> ([Day]) -> (Int) -- ^nearbyOffset
  214  -> IO ((Double))
  215 qlCommodityCurvePrice_ a1 a2 a3 a4 a5 a6 a7 a8 =
  216   withGenTermStructure a1 $ \a1' -> 
  217   withDay a2 $ \a2' -> 
  218   withDayArray a3 $ \(a3'1, a3'2) -> 
  219   withStringArray a4 $ \(a4'1, a4'2) -> 
  220   withDayArray a5 $ \(a5'1, a5'2) -> 
  221   withDayArray a6 $ \(a6'1, a6'2) -> 
  222   withDayArray a7 $ \(a7'1, a7'2) -> 
  223   let {a8' = fromIntegral a8} in 
  224   preErrorCheck $ \a9' -> 
  225   qlCommodityCurvePrice_'_ a1' a2' a3'1  a3'2 a4'1  a4'2 a5'1  a5'2 a6'1  a6'2 a7'1  a7'2 a8' a9' >>= \res ->
  226   let {res' = realToFrac res} in
  227   errorCheck  a9'>>
  228   return (res')
  229 
  230 
  231 
  232 -- |The curve's price for a date, plus any chained basis curve's price, rolling forward onto
  233 -- nearby exchange contracts when @nearbyOffset > 0@ (upstream's own @price@ never touches
  234 -- @exchangeContracts@ otherwise). 'commodityCurvePrice' is this with no exchange contracts and
  235 -- offset @0@, which reproduces the flat (no-rolling) case exactly.
  236 commodityCurvePriceNearby :: CommodityCurve -> Day -> ExchangeContracts -> Int -> IO Double
  237 commodityCurvePriceNearby curve date ecs nearbyOffset =
  238   qlCommodityCurvePrice_ curve date keys codes expirations starts ends nearbyOffset
  239   where (keys, codes, expirations, starts, ends) = splitExchangeContracts ecs
  240 
  241 -- |The curve's price for a date, plus any chained basis curve's price. This is
  242 -- 'commodityCurvePriceNearby' with no exchange contracts and offset @0@ -- the flat (no
  243 -- nearby-rolling) case, which never touches @exchangeContracts@ upstream either way.
  244 commodityCurvePrice :: CommodityCurve -> Day -> IO Double
  245 commodityCurvePrice curve date = commodityCurvePriceNearby curve date [] 0
  246 
  247 -- |The chained basis curve's price alone (excluding this curve's own price), for a date.
  248 commodityCurveBasisOfPrice :: (CommodityCurve) -> (Day) -> IO ((Double))
  249 commodityCurveBasisOfPrice a1 a2 =
  250   withGenTermStructure a1 $ \a1' -> 
  251   withDay a2 $ \a2' -> 
  252   preErrorCheck $ \a3' -> 
  253   commodityCurveBasisOfPrice'_ a1' a2' a3' >>= \res ->
  254   let {res' = realToFrac res} in
  255   errorCheck  a3'>>
  256   return (res')
  257 
  258 
  259 
  260 qlCommodityCurveUnderlyingPriceDate_ :: (CommodityCurve) -> (Day) -> ([Day]) -> ([String]) -> ([Day]) -> ([Day]) -> ([Day]) -> (Int) -- ^nearbyOffset
  261  -> IO ((Day))
  262 qlCommodityCurveUnderlyingPriceDate_ a1 a2 a3 a4 a5 a6 a7 a8 =
  263   withGenTermStructure a1 $ \a1' -> 
  264   withDay a2 $ \a2' -> 
  265   withDayArray a3 $ \(a3'1, a3'2) -> 
  266   withStringArray a4 $ \(a4'1, a4'2) -> 
  267   withDayArray a5 $ \(a5'1, a5'2) -> 
  268   withDayArray a6 $ \(a6'1, a6'2) -> 
  269   withDayArray a7 $ \(a7'1, a7'2) -> 
  270   let {a8' = fromIntegral a8} in 
  271   preErrorCheck $ \a9' -> 
  272   qlCommodityCurveUnderlyingPriceDate_'_ a1' a2' a3'1  a3'2 a4'1  a4'2 a5'1  a5'2 a6'1  a6'2 a7'1  a7'2 a8' a9' >>= \res ->
  273   let {res' = toDay res} in
  274   errorCheck  a9'>>
  275   return (res')
  276 
  277 
  278 
  279 -- |The date whose price a nearby roll (@nearbyOffset > 0@) actually reads: the underlying
  280 -- contract's start date at the @nearbyOffset@\'th exchange contract at or after @date@. Throws if
  281 -- @nearbyOffset <= 0@, or if fewer than @nearbyOffset@ contracts are available from @date@ onward.
  282 commodityCurveUnderlyingPriceDate :: CommodityCurve -> Day -> ExchangeContracts -> Int -> IO Day
  283 commodityCurveUnderlyingPriceDate curve date ecs nearbyOffset =
  284   qlCommodityCurveUnderlyingPriceDate_ curve date keys codes expirations starts ends nearbyOffset
  285   where (keys, codes, expirations, starts, ends) = splitExchangeContracts ecs
  286 
  287 -- vim: set ff=unix ts=8 sts=2 sw=2 et:
  288 
  289 foreign import ccall safe "QuantLib/TermStructure/Commodity.chs.h qlCommodityCurve"
  290   commodityCurve'_ :: ((C2HSImp.Ptr C2HSImp.CChar) -> ((C2HSImp.Ptr (CCommodityType)) -> ((C2HSImp.Ptr (CCurrency)) -> ((C2HSImp.Ptr (CUnitOfMeasure)) -> ((C2HSImp.Ptr (CCalendar)) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr C2HSImp.CInt) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr C2HSImp.CDouble) -> ((C2HSImp.Ptr (CDayCounter)) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO (C2HSImp.Ptr (CCommodityCurve'))))))))))))))
  291 
  292 foreign import ccall safe "QuantLib/TermStructure/Commodity.chs.h qlCommodityCurveName"
  293   commodityCurveName'_ :: ((C2HSImp.Ptr (CCommodityCurve')) -> (IO (C2HSImp.Ptr C2HSImp.CChar)))
  294 
  295 foreign import ccall safe "QuantLib/TermStructure/Commodity.chs.h qlCommodityCurveCommodityType"
  296   commodityCurveCommodityType'_ :: ((C2HSImp.Ptr (CCommodityCurve')) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO (C2HSImp.Ptr (CCommodityType)))))
  297 
  298 foreign import ccall safe "QuantLib/TermStructure/Commodity.chs.h qlCommodityCurveUnitOfMeasure"
  299   commodityCurveUnitOfMeasure'_ :: ((C2HSImp.Ptr (CCommodityCurve')) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO (C2HSImp.Ptr (CUnitOfMeasure)))))
  300 
  301 foreign import ccall safe "QuantLib/TermStructure/Commodity.chs.h qlCommodityCurveCurrency"
  302   commodityCurveCurrency'_ :: ((C2HSImp.Ptr (CCommodityCurve')) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO (C2HSImp.Ptr (CCurrency)))))
  303 
  304 foreign import ccall safe "QuantLib/TermStructure/Commodity.chs.h qlCommodityCurveDates"
  305   commodityCurveDates'_ :: ((C2HSImp.Ptr (CCommodityCurve')) -> ((C2HSImp.Ptr C2HSImp.CUInt) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CInt)) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO ())))))
  306 
  307 foreign import ccall safe "QuantLib/TermStructure/Commodity.chs.h qlCommodityCurvePrices"
  308   commodityCurvePrices'_ :: ((C2HSImp.Ptr (CCommodityCurve')) -> ((C2HSImp.Ptr C2HSImp.CUInt) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CDouble)) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO ())))))
  309 
  310 foreign import ccall safe "QuantLib/TermStructure/Commodity.chs.h qlCommodityCurveEmpty"
  311   commodityCurveEmpty'_ :: ((C2HSImp.Ptr (CCommodityCurve')) -> (IO C2HSImp.CInt))
  312 
  313 foreign import ccall safe "QuantLib/TermStructure/Commodity.chs.h qlCommodityCurveBasisOfCurve"
  314   commodityCurveBasisOfCurve'_ :: ((C2HSImp.Ptr (CCommodityCurve')) -> (IO (C2HSImp.Ptr (CCommodityCurve'))))
  315 
  316 foreign import ccall safe "QuantLib/TermStructure/Commodity.chs.h qlCommodityCurveSetBasisOfCurve"
  317   setCommodityCurveBasisOfCurve'_ :: ((C2HSImp.Ptr (CCommodityCurve')) -> ((C2HSImp.Ptr (CCommodityCurve')) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO ()))))
  318 
  319 foreign import ccall safe "QuantLib/TermStructure/Commodity.chs.h qlCommodityCurvePrice"
  320   qlCommodityCurvePrice_'_ :: ((C2HSImp.Ptr (CCommodityCurve')) -> (C2HSImp.CInt -> (C2HSImp.CUInt -> ((C2HSImp.Ptr C2HSImp.CInt) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr C2HSImp.CInt) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr C2HSImp.CInt) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr C2HSImp.CInt) -> (C2HSImp.CInt -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CDouble)))))))))))))))
  321 
  322 foreign import ccall safe "QuantLib/TermStructure/Commodity.chs.h qlCommodityCurveBasisOfPrice"
  323   commodityCurveBasisOfPrice'_ :: ((C2HSImp.Ptr (CCommodityCurve')) -> (C2HSImp.CInt -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CDouble))))
  324 
  325 foreign import ccall safe "QuantLib/TermStructure/Commodity.chs.h qlCommodityCurveUnderlyingPriceDate"
  326   qlCommodityCurveUnderlyingPriceDate_'_ :: ((C2HSImp.Ptr (CCommodityCurve')) -> (C2HSImp.CInt -> (C2HSImp.CUInt -> ((C2HSImp.Ptr C2HSImp.CInt) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr C2HSImp.CInt) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr C2HSImp.CInt) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr C2HSImp.CInt) -> (C2HSImp.CInt -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CInt)))))))))))))))