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.Energy
6 (
7 SecondaryCosts
8 , SecondaryCostAmounts
9 , PricingErrorLevel(..)
10 , PricingError(..)
11 , PricingErrors
12 , EnergyDailyPosition(..)
13 , EnergyDailyPositions
14 , CommodityCashFlow
15 , CommodityCashFlows
16
17 , commodityCashFlowDate
18 , commodityCashFlowDiscountedAmount
19 , commodityCashFlowUndiscountedAmount
20 , commodityCashFlowDiscountedPaymentAmount
21 , commodityCashFlowUndiscountedPaymentAmount
22 , commodityCashFlowDiscountFactor
23 , commodityCashFlowPaymentDiscountFactor
24 , commodityCashFlowFinalized
25
26 , addPricingError
27 , secondaryCostAmounts
28 , pricingErrors
29
30 , quantity
31
32 , EnergyFuture
33 , energyFuture
34
35 , EnergySwap
36 , dailyPositions
37 , paymentCashFlows
38
39 , EnergyVanillaSwap
40 , energyVanillaSwap
41
42 , EnergyBasisSwap
43 , energyBasisSwap
44
45 , DeliverySchedule(..)
46 , QuantityPeriodicity(..)
47 , createPricingPeriods
48 ) where
49 import qualified Foreign.C.String as C2HSImp
50 import qualified Foreign.C.Types as C2HSImp
51 import qualified Foreign.ForeignPtr as C2HSImp
52 import qualified Foreign.Marshal.Utils as C2HSImp
53 import qualified Foreign.Ptr as C2HSImp
54 import qualified System.IO.Unsafe as C2HSImp
55
56
57 import QuantLib.Internal
58 import QuantLib.Internal.Type
59 import QuantLib.Internal.Common
60 import QuantLib.Commodity
61 import Foreign.Marshal.Alloc(alloca)
62 import Foreign.Marshal.Utils(fromBool)
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99 -- |QuantLib's @map<string, ext::any>@, used with exactly two concrete alternatives across the
100 -- module (@CommodityUnitCost@\/@Money@, confirmed from @energycommodity.cpp@'s two @any_cast@
101 -- branches) -- bound as a real 2-variant sum rather than a generic @any@. Passed optionally to
102 -- every energy-instrument constructor below; @[]@ stands in for upstream's null @shared_ptr@.
103 type SecondaryCosts = [(String, Either CommodityUnitCost (Double, Currency))]
104
105 -- |The computed, currency-resolved output of 'SecondaryCosts' -- @Commodity::secondaryCostAmounts()@,
106 -- a @map<string, Money>@.
107 type SecondaryCostAmounts = [(String, (Double, Currency))]
108
109 -- |A single entry of 'PricingErrors' -- @tradeId@ is never set by any constructor path that
110 -- reaches 'addPricingError' (upstream's own call sites all default it to empty), so it isn't
111 -- bound.
112 data PricingError = PricingError
113 { pricingErrorLevel :: PricingErrorLevel
114 , pricingErrorMessage :: String
115 , pricingErrorDetail :: String
116 } deriving (Show, Eq)
117
118 type PricingErrors = [PricingError]
119
120 -- |One day's position detail from an 'EnergySwap' leaf's @dailyPositions()@ -- a flat record
121 -- rather than a @(Day, ...)@ pair, since 'edpDate' already carries the map key.
122 data EnergyDailyPosition = EnergyDailyPosition
123 { edpDate :: Day
124 , edpQuantityAmount :: Double
125 , edpPayLegPrice :: Double
126 , edpReceiveLegPrice :: Double
127 , edpRiskDelta :: Double
128 , edpUnrealized :: Bool
129 } deriving (Show, Eq)
130
131 type EnergyDailyPositions = [EnergyDailyPosition]
132
133 type CommodityCashFlows = [CommodityCashFlow]
134
135 -- |The cash flow's date -- also the @paymentCashFlows()@ map's own key, so it isn't duplicated as
136 -- a separate tuple field alongside the list of 'CommodityCashFlow's.
137 commodityCashFlowDate :: (CommodityCashFlow) -> (Day)
138 commodityCashFlowDate a1 =
139 C2HSImp.unsafePerformIO $
140 withCommodityCashFlow a1 $ \a1' ->
141 commodityCashFlowDate'_ a1' >>= \res ->
142 let {res' = toDay res} in
143 return (res')
144
145
146
147 -- |The discounted amount, in the global commodity base currency ('QuantLib.Commodity.commoditySettingsCurrency').
148 --
149 -- Not 'pure': the shim's own @ret(new Currency(...))@ is a 'bad_alloc'-only throw point, and letting
150 -- a C++ exception unwind across the FFI boundary from an 'unsafePerformIO'-backed pure binding is
151 -- undefined behavior, so this needs the same 'char **e'\/'preErrorCheck' error channel as any other
152 -- throwing call.
153 commodityCashFlowDiscountedAmount :: (CommodityCashFlow) -> IO ((Double), (Currency))
154 commodityCashFlowDiscountedAmount a1 =
155 withCommodityCashFlow a1 $ \a1' ->
156 alloca $ \a2' ->
157 preErrorCheck $ \a3' ->
158 commodityCashFlowDiscountedAmount'_ a1' a2' a3' >>= \res ->
159 let {res' = realToFrac res} in
160 peekCurrencyPtr a2'>>= \a2'' ->
161 errorCheck a3'>>
162 return (res', a2'')
163
164
165 -- |As 'commodityCashFlowDiscountedAmount', without the discount factor applied.
166 commodityCashFlowUndiscountedAmount :: (CommodityCashFlow) -> IO ((Double), (Currency))
167 commodityCashFlowUndiscountedAmount a1 =
168 withCommodityCashFlow a1 $ \a1' ->
169 alloca $ \a2' ->
170 preErrorCheck $ \a3' ->
171 commodityCashFlowUndiscountedAmount'_ a1' a2' a3' >>= \res ->
172 let {res' = realToFrac res} in
173 peekCurrencyPtr a2'>>= \a2'' ->
174 errorCheck a3'>>
175 return (res', a2'')
176
177
178 -- |The discounted amount, in the payment (leg) currency.
179 commodityCashFlowDiscountedPaymentAmount :: (CommodityCashFlow) -> IO ((Double), (Currency))
180 commodityCashFlowDiscountedPaymentAmount a1 =
181 withCommodityCashFlow a1 $ \a1' ->
182 alloca $ \a2' ->
183 preErrorCheck $ \a3' ->
184 commodityCashFlowDiscountedPaymentAmount'_ a1' a2' a3' >>= \res ->
185 let {res' = realToFrac res} in
186 peekCurrencyPtr a2'>>= \a2'' ->
187 errorCheck a3'>>
188 return (res', a2'')
189
190
191 -- |As 'commodityCashFlowDiscountedPaymentAmount', without the discount factor applied.
192 commodityCashFlowUndiscountedPaymentAmount :: (CommodityCashFlow) -> IO ((Double), (Currency))
193 commodityCashFlowUndiscountedPaymentAmount a1 =
194 withCommodityCashFlow a1 $ \a1' ->
195 alloca $ \a2' ->
196 preErrorCheck $ \a3' ->
197 commodityCashFlowUndiscountedPaymentAmount'_ a1' a2' a3' >>= \res ->
198 let {res' = realToFrac res} in
199 peekCurrencyPtr a2'>>= \a2'' ->
200 errorCheck a3'>>
201 return (res', a2'')
202
203
204 -- |The discount factor applied to the base-currency amount.
205 commodityCashFlowDiscountFactor :: (CommodityCashFlow) -> (Double)
206 commodityCashFlowDiscountFactor a1 =
207 C2HSImp.unsafePerformIO $
208 withCommodityCashFlow a1 $ \a1' ->
209 commodityCashFlowDiscountFactor'_ a1' >>= \res ->
210 let {res' = realToFrac res} in
211 return (res')
212
213
214 -- |The discount factor applied to the payment-currency amount.
215 commodityCashFlowPaymentDiscountFactor :: (CommodityCashFlow) -> (Double)
216 commodityCashFlowPaymentDiscountFactor a1 =
217 C2HSImp.unsafePerformIO $
218 withCommodityCashFlow a1 $ \a1' ->
219 commodityCashFlowPaymentDiscountFactor'_ a1' >>= \res ->
220 let {res' = realToFrac res} in
221 return (res')
222
223
224 -- |Whether this cash flow's payment date has already occurred as of the evaluation date.
225 commodityCashFlowFinalized :: (CommodityCashFlow) -> (Bool)
226 commodityCashFlowFinalized a1 =
227 C2HSImp.unsafePerformIO $
228 withCommodityCashFlow a1 $ \a1' ->
229 commodityCashFlowFinalized'_ a1' >>= \res ->
230 let {res' = C2HSImp.toBool res} in
231 return (res')
232
233
234
235 -- |Record a pricing diagnostic against a 'Commodity'\/'EnergyCommodity' leaf (any of 'EnergyFuture',
236 -- 'EnergyVanillaSwap', 'EnergyBasisSwap'), retrievable afterwards via 'pricingErrors'. Mirrors
237 -- upstream's own default empty @detail@ with a plain @\"\"@ argument.
238 addPricingError :: (GenCommodity c) -> (PricingErrorLevel) -> (String) -> (String) -> IO ()
239 addPricingError a1 a2 a3 a4 =
240 withCommodity a1 $ \a1' ->
241 let {a2' = fromEnumC a2} in
242 C2HSImp.withCString a3 $ \a3' ->
243 C2HSImp.withCString a4 $ \a4' ->
244 addPricingError'_ a1' a2' a3' a4' >>
245 return ()
246
247
248
249 qlCommoditySecondaryCostAmounts_ :: (GenCommodity c) -> IO (([String]), ([Double]), ([Currency]))
250 qlCommoditySecondaryCostAmounts_ a1 =
251 withCommodity a1 $ \a1' ->
252 preArray $ \(a2'1, a2'2) ->
253 preArray $ \(a3'1, a3'2) ->
254 preArray $ \(a4'1, a4'2) ->
255 preErrorCheck $ \a5' ->
256 qlCommoditySecondaryCostAmounts_'_ a1' a2'1 a2'2 a3'1 a3'2 a4'1 a4'2 a5' >>
257 peekCStringArray a2'1 a2'2>>= \a2'' ->
258 peekDoubleArray a3'1 a3'2>>= \a3'' ->
259 peekCurrencyArray a4'1 a4'2>>= \a4'' ->
260 errorCheck a5'>>
261 return (a2'', a3'', a4'')
262
263
264
265 -- |The currency-resolved secondary costs computed during the most recent pricing
266 -- (@performCalculations()@) of this 'Commodity'\/'EnergyCommodity' leaf -- a plain member read, not
267 -- itself a pricing trigger, so call 'QuantLib.Instrument.npv' first if it hasn't been priced yet.
268 secondaryCostAmounts :: GenCommodity c -> IO SecondaryCostAmounts
269 secondaryCostAmounts o = do
270 (keys, amts, ccys) <- qlCommoditySecondaryCostAmounts_ o
271 pure $ zip keys (zip amts ccys)
272
273 qlCommodityPricingErrors_ :: (GenCommodity c) -> IO (([PricingErrorLevel]), ([String]), ([String]))
274 qlCommodityPricingErrors_ a1 =
275 withCommodity a1 $ \a1' ->
276 preArray $ \(a2'1, a2'2) ->
277 preArray $ \(a3'1, a3'2) ->
278 preArray $ \(a4'1, a4'2) ->
279 preErrorCheck $ \a5' ->
280 qlCommodityPricingErrors_'_ a1' a2'1 a2'2 a3'1 a3'2 a4'1 a4'2 a5' >>
281 peekPricingErrorLevelArray a2'1 a2'2>>= \a2'' ->
282 peekCStringArray a3'1 a3'2>>= \a3'' ->
283 peekCStringArray a4'1 a4'2>>= \a4'' ->
284 errorCheck a5'>>
285 return (a2'', a3'', a4'')
286
287
288
289 -- |Every pricing diagnostic recorded so far (via upstream's own internal calls, or via
290 -- 'addPricingError') against this 'Commodity'\/'EnergyCommodity' leaf.
291 pricingErrors :: GenCommodity c -> IO PricingErrors
292 pricingErrors o = do
293 (levels, errs, details) <- qlCommodityPricingErrors_ o
294 pure $ zipWith3 PricingError levels errs details
295
296 qlEnergyCommodityQuantity_ :: (GenEnergyCommodity e) -> IO ((Double), (CommodityType), (UnitOfMeasure))
297 qlEnergyCommodityQuantity_ a1 =
298 withEnergyCommodity a1 $ \a1' ->
299 alloca $ \a2' ->
300 alloca $ \a3' ->
301 preErrorCheck $ \a4' ->
302 qlEnergyCommodityQuantity_'_ a1' a2' a3' a4' >>= \res ->
303 let {res' = realToFrac res} in
304 peekCommodityTypePtr a2'>>= \a2'' ->
305 peekUnitOfMeasurePtr a3'>>= \a3'' ->
306 errorCheck a4'>>
307 return (res', a2'', a3'')
308
309
310
311 -- |The commodity quantity of this 'EnergyCommodity' leaf -- either the plain quantity given at
312 -- construction ('EnergyFuture') or the sum across every 'PricingPeriod' ('EnergySwap' and its
313 -- leaves, a real computed sum -- see @energyswap.cpp@). One binding covers both, dispatched
314 -- virtually on the C++ side.
315 quantity :: GenEnergyCommodity e -> IO Quantity
316 quantity o = do
317 (amt, ct, uom) <- qlEnergyCommodityQuantity_ o
318 pure (ct, uom, amt)
319
320 -- |Construct an energy future: a single mark-to-market position against a 'CommodityIndex',
321 -- struck at a fixed 'CommodityUnitCost' trade price. @buySell@ is a signed multiplier (@1@ to buy,
322 -- @-1@ to sell), matching upstream's own @Integer buySell@ (not a @Bool@). @tradePrice@\/@index@
323 -- are not bound as getters -- both are plain, never-mutated echoes of these same constructor
324 -- arguments (per CLAUDE.md's trivial-getter rule); use 'quantity' for the one genuinely-shared
325 -- accessor.
326 energyFuture :: Int -- ^buySell
327 -> Quantity
328 -> CommodityUnitCost -- ^tradePrice
329 -> CommodityIndex
330 -> CommodityType
331 -> SecondaryCosts
332 -> IO EnergyFuture
333 energyFuture buySell (qCt, qUom, qAmt) (tpAmt, tpCcy, tpUom) index cmdType secCosts =
334 qlEnergyFuture_ buySell qCt qUom qAmt tpAmt tpCcy tpUom index cmdType
335 scKeys scIsUnitCost scAmts scCcys scUoms
336 where (scKeys, scIsUnitCost, scAmts, scCcys, scUoms) = secondaryCostsFields secCosts
337
338 qlEnergyFuture_ :: (Int) -> (CommodityType) -> (UnitOfMeasure) -> (Double) -> (Double) -> (Currency) -> (UnitOfMeasure) -> (CommodityIndex) -> (CommodityType) -> ([String]) -> ([Bool]) -> ([Double]) -> ([Currency]) -> ([Maybe UnitOfMeasure]) -> IO ((EnergyFuture))
339 qlEnergyFuture_ a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 =
340 let {a1' = fromIntegral a1} in
341 withCommodityType a2 $ \a2' ->
342 withUnitOfMeasure a3 $ \a3' ->
343 let {a4' = realToFrac a4} in
344 let {a5' = realToFrac a5} in
345 withCurrency a6 $ \a6' ->
346 withUnitOfMeasure a7 $ \a7' ->
347 withCommodityIndex a8 $ \a8' ->
348 withCommodityType a9 $ \a9' ->
349 withStringArray a10 $ \(a10'1, a10'2) ->
350 withBoolArray a11 $ \(a11'1, a11'2) ->
351 withDoubleArray a12 $ \(a12'1, a12'2) ->
352 withCurrencyArray a13 $ \(a13'1, a13'2) ->
353 withMaybeUnitOfMeasureArray a14 $ \(a14'1, a14'2) ->
354 preErrorCheck $ \a15' ->
355 qlEnergyFuture_'_ a1' a2' a3' a4' a5' a6' a7' a8' a9' a10'1 a10'2 a11'1 a11'2 a12'1 a12'2 a13'1 a13'2 a14'1 a14'2 a15' >>= \res ->
356 peekEnergyFuture res >>= \res' ->
357 errorCheck a15'>>
358 return (res')
359
360
361
362 qlEnergySwapDailyPositions_ :: (GenEnergySwap s) -> IO (([Day]), ([Double]), ([Double]), ([Double]), ([Double]), ([Bool]))
363 qlEnergySwapDailyPositions_ a1 =
364 withEnergySwap a1 $ \a1' ->
365 preArray $ \(a2'1, a2'2) ->
366 preArray $ \(a3'1, a3'2) ->
367 preArray $ \(a4'1, a4'2) ->
368 preArray $ \(a5'1, a5'2) ->
369 preArray $ \(a6'1, a6'2) ->
370 preArray $ \(a7'1, a7'2) ->
371 preErrorCheck $ \a8' ->
372 qlEnergySwapDailyPositions_'_ a1' a2'1 a2'2 a3'1 a3'2 a4'1 a4'2 a5'1 a5'2 a6'1 a6'2 a7'1 a7'2 a8' >>
373 peekDayArray a2'1 a2'2>>= \a2'' ->
374 peekDoubleArray a3'1 a3'2>>= \a3'' ->
375 peekDoubleArray a4'1 a4'2>>= \a4'' ->
376 peekDoubleArray a5'1 a5'2>>= \a5'' ->
377 peekDoubleArray a6'1 a6'2>>= \a6'' ->
378 peekBoolArray a7'1 a7'2>>= \a7'' ->
379 errorCheck a8'>>
380 return (a2'', a3'', a4'', a5'', a6'', a7'')
381
382
383
384 -- |The per-day pricing breakdown computed during this 'EnergySwap' leaf's most recent
385 -- @performCalculations()@ -- populated only after pricing (call 'QuantLib.Instrument.npv' first).
386 dailyPositions :: GenEnergySwap s -> IO EnergyDailyPositions
387 dailyPositions s = do
388 (dates, qtyAmts, payPrices, recvPrices, riskDeltas, unrealized) <- qlEnergySwapDailyPositions_ s
389 pure $ zipWith6 EnergyDailyPosition dates qtyAmts payPrices recvPrices riskDeltas unrealized
390
391 -- |The realized\/unrealized payment cash flows computed during this 'EnergySwap' leaf's most
392 -- recent @performCalculations()@ -- populated only after pricing.
393 paymentCashFlows :: (GenEnergySwap s) -> IO (([CommodityCashFlow]))
394 paymentCashFlows a1 =
395 withEnergySwap a1 $ \a1' ->
396 preArray $ \(a2'1, a2'2) ->
397 preErrorCheck $ \a3' ->
398 paymentCashFlows'_ a1' a2'1 a2'2 a3' >>
399 peekCommodityCashFlowArray a2'1 a2'2>>= \a2'' ->
400 errorCheck a3'>>
401 return (a2'')
402
403
404
405 -- |Split a 'SecondaryCosts' list into the five parallel arrays every energy-instrument
406 -- constructor's C shim consumes it as (keys, is-a-'CommodityUnitCost'-flag, amounts, currencies,
407 -- units-of-measure -- 'Nothing' for a plain 'Money' entry).
408 secondaryCostsFields :: SecondaryCosts -> ([String], [Bool], [Double], [Currency], [Maybe UnitOfMeasure])
409 secondaryCostsFields entries =
410 ( map fst entries
411 , map (either (const True) (const False) . snd) entries
412 , map (either (\(a,_,_) -> a) fst . snd) entries
413 , map (either (\(_,c,_) -> c) snd . snd) entries
414 , map (either (\(_,_,u) -> Just u) (const Nothing) . snd) entries
415 )
416
417 -- |Split a 'PricingPeriods' list into the six parallel arrays every energy-swap constructor's C
418 -- shim consumes it as (start dates, end dates, payment dates, quantity commodity types, quantity
419 -- units of measure, quantity amounts).
420 pricingPeriodsFields :: PricingPeriods -> ([Day], [Day], [Day], [CommodityType], [UnitOfMeasure], [Double])
421 pricingPeriodsFields pps =
422 ( map pricingPeriodStartDate pps
423 , map pricingPeriodEndDate pps
424 , map pricingPeriodPaymentDate pps
425 , map (\(t,_,_) -> t) qtys
426 , map (\(_,u,_) -> u) qtys
427 , map (\(_,_,a) -> a) qtys
428 )
429 where qtys = map pricingPeriodQuantity pps
430
431 -- |Construct a vanilla energy swap: fixed 'CommodityUnitCost' price against a floating
432 -- 'CommodityIndex' quote, over one or more 'PricingPeriod's. @payer@ selects which leg (fixed or
433 -- floating) is paid. @payReceive@\/@fixedPrice@\/@fixedPriceUnitOfMeasure@\/@index@ are not bound
434 -- as getters -- all are plain, never-mutated echoes of this constructor's own arguments (per
435 -- CLAUDE.md's trivial-getter rule; @payReceive@ specifically is just @if payer then 1 else 0@,
436 -- reproducible with no C++ call at all).
437 energyVanillaSwap :: Bool -- ^payer
438 -> Calendar
439 -> (Double, Currency) -- ^fixedPrice
440 -> UnitOfMeasure -- ^fixedPriceUnitOfMeasure
441 -> CommodityIndex
442 -> Currency -- ^payCurrency
443 -> Currency -- ^receiveCurrency
444 -> PricingPeriods
445 -> CommodityType
446 -> SecondaryCosts
447 -> GenYieldTermStructure y1 -- ^payLegTermStructure
448 -> GenYieldTermStructure y2 -- ^receiveLegTermStructure
449 -> GenYieldTermStructure y3 -- ^discountTermStructure
450 -> IO EnergyVanillaSwap
451 energyVanillaSwap payer calendar (fpAmt, fpCcy) fpUom index payCcy receiveCcy pps cmdType secCosts
452 payLegTS receiveLegTS discountTS =
453 qlEnergyVanillaSwap_ payer calendar fpAmt fpCcy fpUom index payCcy receiveCcy
454 ppStarts ppEnds ppPays ppTypes ppUoms ppAmts
455 cmdType scKeys scIsUnitCost scAmts scCcys scUoms
456 payLegTS receiveLegTS discountTS
457 where
458 (ppStarts, ppEnds, ppPays, ppTypes, ppUoms, ppAmts) = pricingPeriodsFields pps
459 (scKeys, scIsUnitCost, scAmts, scCcys, scUoms) = secondaryCostsFields secCosts
460
461 qlEnergyVanillaSwap_ :: (Bool) -> (Calendar) -> (Double) -> (Currency) -> (UnitOfMeasure) -> (CommodityIndex) -> (Currency) -> (Currency) -> ([Day]) -> ([Day]) -> ([Day]) -> ([CommodityType]) -> ([UnitOfMeasure]) -> ([Double]) -> (CommodityType) -> ([String]) -> ([Bool]) -> ([Double]) -> ([Currency]) -> ([Maybe UnitOfMeasure]) -> (GenYieldTermStructure y1) -> (GenYieldTermStructure y2) -> (GenYieldTermStructure y3) -> IO ((EnergyVanillaSwap))
462 qlEnergyVanillaSwap_ a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 a21 a22 a23 =
463 let {a1' = fromBool a1} in
464 withCalendar a2 $ \a2' ->
465 let {a3' = realToFrac a3} in
466 withCurrency a4 $ \a4' ->
467 withUnitOfMeasure a5 $ \a5' ->
468 withCommodityIndex a6 $ \a6' ->
469 withCurrency a7 $ \a7' ->
470 withCurrency a8 $ \a8' ->
471 withDayArray a9 $ \(a9'1, a9'2) ->
472 withDayArray a10 $ \(a10'1, a10'2) ->
473 withDayArray a11 $ \(a11'1, a11'2) ->
474 withCommodityTypeArray a12 $ \(a12'1, a12'2) ->
475 withUnitOfMeasureArray a13 $ \(a13'1, a13'2) ->
476 withDoubleArray a14 $ \(a14'1, a14'2) ->
477 withCommodityType a15 $ \a15' ->
478 withStringArray a16 $ \(a16'1, a16'2) ->
479 withBoolArray a17 $ \(a17'1, a17'2) ->
480 withDoubleArray a18 $ \(a18'1, a18'2) ->
481 withCurrencyArray a19 $ \(a19'1, a19'2) ->
482 withMaybeUnitOfMeasureArray a20 $ \(a20'1, a20'2) ->
483 withYieldTermStructure a21 $ \a21' ->
484 withYieldTermStructure a22 $ \a22' ->
485 withYieldTermStructure a23 $ \a23' ->
486 preErrorCheck $ \a24' ->
487 qlEnergyVanillaSwap_'_ a1' a2' a3' a4' a5' a6' a7' a8' a9'1 a9'2 a10'1 a10'2 a11'1 a11'2 a12'1 a12'2 a13'1 a13'2 a14'1 a14'2 a15' a16'1 a16'2 a17'1 a17'2 a18'1 a18'2 a19'1 a19'2 a20'1 a20'2 a21' a22' a23' a24' >>= \res ->
488 peekEnergyVanillaSwap res >>= \res' ->
489 errorCheck a24'>>
490 return (res')
491
492
493
494 -- |Construct an energy basis swap: two floating 'CommodityIndex' legs (pay\/receive), one of them
495 -- offset by a fixed 'CommodityUnitCost' basis, over one or more 'PricingPeriod's. @spreadToPayLeg@
496 -- selects which leg the basis is added to. @payIndex@\/@receiveIndex@\/@basis@ are not bound as
497 -- getters -- all are plain, never-mutated echoes of this constructor's own arguments.
498 energyBasisSwap :: Calendar
499 -> CommodityIndex -- ^spreadIndex
500 -> CommodityIndex -- ^payIndex
501 -> CommodityIndex -- ^receiveIndex
502 -> Bool -- ^spreadToPayLeg
503 -> Currency -- ^payCurrency
504 -> Currency -- ^receiveCurrency
505 -> PricingPeriods
506 -> CommodityUnitCost -- ^basis
507 -> CommodityType
508 -> SecondaryCosts
509 -> GenYieldTermStructure y1 -- ^payLegTermStructure
510 -> GenYieldTermStructure y2 -- ^receiveLegTermStructure
511 -> GenYieldTermStructure y3 -- ^discountTermStructure
512 -> IO EnergyBasisSwap
513 energyBasisSwap calendar spreadIndex payIndex receiveIndex spreadToPayLeg payCcy receiveCcy pps
514 (basisAmt, basisCcy, basisUom) cmdType secCosts payLegTS receiveLegTS discountTS =
515 qlEnergyBasisSwap_ calendar spreadIndex payIndex receiveIndex spreadToPayLeg payCcy receiveCcy
516 ppStarts ppEnds ppPays ppTypes ppUoms ppAmts
517 basisAmt basisCcy basisUom
518 cmdType scKeys scIsUnitCost scAmts scCcys scUoms
519 payLegTS receiveLegTS discountTS
520 where
521 (ppStarts, ppEnds, ppPays, ppTypes, ppUoms, ppAmts) = pricingPeriodsFields pps
522 (scKeys, scIsUnitCost, scAmts, scCcys, scUoms) = secondaryCostsFields secCosts
523
524 qlEnergyBasisSwap_ :: (Calendar) -> (CommodityIndex) -> (CommodityIndex) -> (CommodityIndex) -> (Bool) -> (Currency) -> (Currency) -> ([Day]) -> ([Day]) -> ([Day]) -> ([CommodityType]) -> ([UnitOfMeasure]) -> ([Double]) -> (Double) -> (Currency) -> (UnitOfMeasure) -> (CommodityType) -> ([String]) -> ([Bool]) -> ([Double]) -> ([Currency]) -> ([Maybe UnitOfMeasure]) -> (GenYieldTermStructure y1) -> (GenYieldTermStructure y2) -> (GenYieldTermStructure y3) -> IO ((EnergyBasisSwap))
525 qlEnergyBasisSwap_ a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 a21 a22 a23 a24 a25 =
526 withCalendar a1 $ \a1' ->
527 withCommodityIndex a2 $ \a2' ->
528 withCommodityIndex a3 $ \a3' ->
529 withCommodityIndex a4 $ \a4' ->
530 let {a5' = fromBool a5} in
531 withCurrency a6 $ \a6' ->
532 withCurrency a7 $ \a7' ->
533 withDayArray a8 $ \(a8'1, a8'2) ->
534 withDayArray a9 $ \(a9'1, a9'2) ->
535 withDayArray a10 $ \(a10'1, a10'2) ->
536 withCommodityTypeArray a11 $ \(a11'1, a11'2) ->
537 withUnitOfMeasureArray a12 $ \(a12'1, a12'2) ->
538 withDoubleArray a13 $ \(a13'1, a13'2) ->
539 let {a14' = realToFrac a14} in
540 withCurrency a15 $ \a15' ->
541 withUnitOfMeasure a16 $ \a16' ->
542 withCommodityType a17 $ \a17' ->
543 withStringArray a18 $ \(a18'1, a18'2) ->
544 withBoolArray a19 $ \(a19'1, a19'2) ->
545 withDoubleArray a20 $ \(a20'1, a20'2) ->
546 withCurrencyArray a21 $ \(a21'1, a21'2) ->
547 withMaybeUnitOfMeasureArray a22 $ \(a22'1, a22'2) ->
548 withYieldTermStructure a23 $ \a23' ->
549 withYieldTermStructure a24 $ \a24' ->
550 withYieldTermStructure a25 $ \a25' ->
551 preErrorCheck $ \a26' ->
552 qlEnergyBasisSwap_'_ a1' a2' a3' a4' a5' a6' a7' a8'1 a8'2 a9'1 a9'2 a10'1 a10'2 a11'1 a11'2 a12'1 a12'2 a13'1 a13'2 a14' a15' a16' a17' a18'1 a18'2 a19'1 a19'2 a20'1 a20'2 a21'1 a21'2 a22'1 a22'2 a23' a24' a25' a26' >>= \res ->
553 peekEnergyBasisSwap res >>= \res' ->
554 errorCheck a26'>>
555 return (res')
556
557
558
559 qlCreatePricingPeriods_ :: (Day) -> (Day) -> (CommodityType) -> (UnitOfMeasure) -> (Double) -> (DeliverySchedule) -> (QuantityPeriodicity) -> (PaymentTerm) -> IO (([Day]), ([Day]), ([Day]), ([CommodityType]), ([UnitOfMeasure]), ([Double]))
560 qlCreatePricingPeriods_ a1 a2 a3 a4 a5 a6 a7 a8 =
561 withDay a1 $ \a1' ->
562 withDay a2 $ \a2' ->
563 withCommodityType a3 $ \a3' ->
564 withUnitOfMeasure a4 $ \a4' ->
565 let {a5' = realToFrac a5} in
566 let {a6' = fromEnumC a6} in
567 let {a7' = fromEnumC a7} in
568 withPaymentTerm a8 $ \a8' ->
569 preArray $ \(a9'1, a9'2) ->
570 preArray $ \(a10'1, a10'2) ->
571 preArray $ \(a11'1, a11'2) ->
572 preArray $ \(a12'1, a12'2) ->
573 preArray $ \(a13'1, a13'2) ->
574 preArray $ \(a14'1, a14'2) ->
575 preErrorCheck $ \a15' ->
576 qlCreatePricingPeriods_'_ a1' a2' a3' a4' a5' a6' a7' a8' a9'1 a9'2 a10'1 a10'2 a11'1 a11'2 a12'1 a12'2 a13'1 a13'2 a14'1 a14'2 a15' >>
577 peekDayArray a9'1 a9'2>>= \a9'' ->
578 peekDayArray a10'1 a10'2>>= \a10'' ->
579 peekDayArray a11'1 a11'2>>= \a11'' ->
580 peekCommodityTypeArray a12'1 a12'2>>= \a12'' ->
581 peekUnitOfMeasureArray a13'1 a13'2>>= \a13'' ->
582 peekDoubleArray a14'1 a14'2>>= \a14'' ->
583 errorCheck a15'>>
584 return (a9'', a10'', a11'', a12'', a13'', a14'')
585
586
587
588 -- |Split @[startDate, endDate)@ into 'PricingPeriod's of the given quantity, per a
589 -- 'DeliverySchedule'\/'QuantityPeriodicity'\/'PaymentTerm' combination --
590 -- @CommodityPricingHelper::createPricingPeriods@. Upstream only actually implements two
591 -- combinations ('Monthly' with 'PerMonth', 'Daily' with 'PerDay'); every other 'DeliverySchedule'
592 -- silently returns @[]@ (no periods, no error -- checked directly against
593 -- @commoditypricinghelpers.cpp@, not assumed from the header).
594 createPricingPeriods :: Day -> Day -> Quantity -> DeliverySchedule -> QuantityPeriodicity -> PaymentTerm -> IO PricingPeriods
595 createPricingPeriods startDate endDate (ct, uom, amt) deliverySchedule qtyPeriodicity pmtTerm = do
596 (starts, ends, pays, types, uoms, amts) <-
597 qlCreatePricingPeriods_ startDate endDate ct uom amt deliverySchedule qtyPeriodicity pmtTerm
598 pure $ zipWith6 (\s e p t u a -> pricingPeriod s e p (t, u, a)) starts ends pays types uoms amts
599
600 -- vim: set ff=unix ts=8 sts=2 sw=2 et:
601
602 foreign import ccall safe "QuantLib/Instrument/Energy.chs.h qlCommodityCashFlowDate"
603 commodityCashFlowDate'_ :: ((C2HSImp.Ptr (CCommodityCashFlow)) -> (IO C2HSImp.CInt))
604
605 foreign import ccall safe "QuantLib/Instrument/Energy.chs.h qlCommodityCashFlowDiscountedAmount"
606 commodityCashFlowDiscountedAmount'_ :: ((C2HSImp.Ptr (CCommodityCashFlow)) -> ((C2HSImp.Ptr (C2HSImp.Ptr (CCurrency))) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CDouble))))
607
608 foreign import ccall safe "QuantLib/Instrument/Energy.chs.h qlCommodityCashFlowUndiscountedAmount"
609 commodityCashFlowUndiscountedAmount'_ :: ((C2HSImp.Ptr (CCommodityCashFlow)) -> ((C2HSImp.Ptr (C2HSImp.Ptr (CCurrency))) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CDouble))))
610
611 foreign import ccall safe "QuantLib/Instrument/Energy.chs.h qlCommodityCashFlowDiscountedPaymentAmount"
612 commodityCashFlowDiscountedPaymentAmount'_ :: ((C2HSImp.Ptr (CCommodityCashFlow)) -> ((C2HSImp.Ptr (C2HSImp.Ptr (CCurrency))) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CDouble))))
613
614 foreign import ccall safe "QuantLib/Instrument/Energy.chs.h qlCommodityCashFlowUndiscountedPaymentAmount"
615 commodityCashFlowUndiscountedPaymentAmount'_ :: ((C2HSImp.Ptr (CCommodityCashFlow)) -> ((C2HSImp.Ptr (C2HSImp.Ptr (CCurrency))) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CDouble))))
616
617 foreign import ccall safe "QuantLib/Instrument/Energy.chs.h qlCommodityCashFlowDiscountFactor"
618 commodityCashFlowDiscountFactor'_ :: ((C2HSImp.Ptr (CCommodityCashFlow)) -> (IO C2HSImp.CDouble))
619
620 foreign import ccall safe "QuantLib/Instrument/Energy.chs.h qlCommodityCashFlowPaymentDiscountFactor"
621 commodityCashFlowPaymentDiscountFactor'_ :: ((C2HSImp.Ptr (CCommodityCashFlow)) -> (IO C2HSImp.CDouble))
622
623 foreign import ccall safe "QuantLib/Instrument/Energy.chs.h qlCommodityCashFlowFinalized"
624 commodityCashFlowFinalized'_ :: ((C2HSImp.Ptr (CCommodityCashFlow)) -> (IO C2HSImp.CInt))
625
626 foreign import ccall safe "QuantLib/Instrument/Energy.chs.h qlCommodityAddPricingError"
627 addPricingError'_ :: ((C2HSImp.Ptr (CCommodity')) -> (C2HSImp.CInt -> ((C2HSImp.Ptr C2HSImp.CChar) -> ((C2HSImp.Ptr C2HSImp.CChar) -> (IO ())))))
628
629 foreign import ccall safe "QuantLib/Instrument/Energy.chs.h qlCommoditySecondaryCostAmounts"
630 qlCommoditySecondaryCostAmounts_'_ :: ((C2HSImp.Ptr (CCommodity')) -> ((C2HSImp.Ptr C2HSImp.CUInt) -> ((C2HSImp.Ptr (C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar))) -> ((C2HSImp.Ptr C2HSImp.CUInt) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CDouble)) -> ((C2HSImp.Ptr C2HSImp.CUInt) -> ((C2HSImp.Ptr (C2HSImp.Ptr (C2HSImp.Ptr (CCurrency)))) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO ())))))))))
631
632 foreign import ccall safe "QuantLib/Instrument/Energy.chs.h qlCommodityPricingErrors"
633 qlCommodityPricingErrors_'_ :: ((C2HSImp.Ptr (CCommodity')) -> ((C2HSImp.Ptr C2HSImp.CUInt) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CInt)) -> ((C2HSImp.Ptr C2HSImp.CUInt) -> ((C2HSImp.Ptr (C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar))) -> ((C2HSImp.Ptr C2HSImp.CUInt) -> ((C2HSImp.Ptr (C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar))) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO ())))))))))
634
635 foreign import ccall safe "QuantLib/Instrument/Energy.chs.h qlEnergyCommodityQuantity"
636 qlEnergyCommodityQuantity_'_ :: ((C2HSImp.Ptr (CEnergyCommodity')) -> ((C2HSImp.Ptr (C2HSImp.Ptr (CCommodityType))) -> ((C2HSImp.Ptr (C2HSImp.Ptr (CUnitOfMeasure))) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CDouble)))))
637
638 foreign import ccall safe "QuantLib/Instrument/Energy.chs.h qlEnergyFuture"
639 qlEnergyFuture_'_ :: (C2HSImp.CInt -> ((C2HSImp.Ptr (CCommodityType)) -> ((C2HSImp.Ptr (CUnitOfMeasure)) -> (C2HSImp.CDouble -> (C2HSImp.CDouble -> ((C2HSImp.Ptr (CCurrency)) -> ((C2HSImp.Ptr (CUnitOfMeasure)) -> ((C2HSImp.Ptr (CCommodityIndex')) -> ((C2HSImp.Ptr (CCommodityType)) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr C2HSImp.CInt) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr C2HSImp.CDouble) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr (C2HSImp.Ptr (CCurrency))) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr (C2HSImp.Ptr (CUnitOfMeasure))) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO (C2HSImp.Ptr (CEnergyFuture')))))))))))))))))))))))
640
641 foreign import ccall safe "QuantLib/Instrument/Energy.chs.h qlEnergySwapDailyPositions"
642 qlEnergySwapDailyPositions_'_ :: ((C2HSImp.Ptr (CEnergySwap')) -> ((C2HSImp.Ptr C2HSImp.CUInt) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CInt)) -> ((C2HSImp.Ptr C2HSImp.CUInt) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CDouble)) -> ((C2HSImp.Ptr C2HSImp.CUInt) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CDouble)) -> ((C2HSImp.Ptr C2HSImp.CUInt) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CDouble)) -> ((C2HSImp.Ptr C2HSImp.CUInt) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CDouble)) -> ((C2HSImp.Ptr C2HSImp.CUInt) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CInt)) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO ())))))))))))))))
643
644 foreign import ccall safe "QuantLib/Instrument/Energy.chs.h qlEnergySwapPaymentCashFlows"
645 paymentCashFlows'_ :: ((C2HSImp.Ptr (CEnergySwap')) -> ((C2HSImp.Ptr C2HSImp.CUInt) -> ((C2HSImp.Ptr (C2HSImp.Ptr (C2HSImp.Ptr (CCommodityCashFlow)))) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO ())))))
646
647 foreign import ccall safe "QuantLib/Instrument/Energy.chs.h qlEnergyVanillaSwap"
648 qlEnergyVanillaSwap_'_ :: (C2HSImp.CInt -> ((C2HSImp.Ptr (CCalendar)) -> (C2HSImp.CDouble -> ((C2HSImp.Ptr (CCurrency)) -> ((C2HSImp.Ptr (CUnitOfMeasure)) -> ((C2HSImp.Ptr (CCommodityIndex')) -> ((C2HSImp.Ptr (CCurrency)) -> ((C2HSImp.Ptr (CCurrency)) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr C2HSImp.CInt) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr C2HSImp.CInt) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr C2HSImp.CInt) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr (C2HSImp.Ptr (CCommodityType))) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr (C2HSImp.Ptr (CUnitOfMeasure))) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr C2HSImp.CDouble) -> ((C2HSImp.Ptr (CCommodityType)) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr C2HSImp.CInt) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr C2HSImp.CDouble) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr (C2HSImp.Ptr (CCurrency))) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr (C2HSImp.Ptr (CUnitOfMeasure))) -> ((C2HSImp.Ptr (CYieldTermStructure')) -> ((C2HSImp.Ptr (CYieldTermStructure')) -> ((C2HSImp.Ptr (CYieldTermStructure')) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO (C2HSImp.Ptr (CEnergyVanillaSwap'))))))))))))))))))))))))))))))))))))))
649
650 foreign import ccall safe "QuantLib/Instrument/Energy.chs.h qlEnergyBasisSwap"
651 qlEnergyBasisSwap_'_ :: ((C2HSImp.Ptr (CCalendar)) -> ((C2HSImp.Ptr (CCommodityIndex')) -> ((C2HSImp.Ptr (CCommodityIndex')) -> ((C2HSImp.Ptr (CCommodityIndex')) -> (C2HSImp.CInt -> ((C2HSImp.Ptr (CCurrency)) -> ((C2HSImp.Ptr (CCurrency)) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr C2HSImp.CInt) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr C2HSImp.CInt) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr C2HSImp.CInt) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr (C2HSImp.Ptr (CCommodityType))) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr (C2HSImp.Ptr (CUnitOfMeasure))) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr C2HSImp.CDouble) -> (C2HSImp.CDouble -> ((C2HSImp.Ptr (CCurrency)) -> ((C2HSImp.Ptr (CUnitOfMeasure)) -> ((C2HSImp.Ptr (CCommodityType)) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr C2HSImp.CInt) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr C2HSImp.CDouble) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr (C2HSImp.Ptr (CCurrency))) -> (C2HSImp.CUInt -> ((C2HSImp.Ptr (C2HSImp.Ptr (CUnitOfMeasure))) -> ((C2HSImp.Ptr (CYieldTermStructure')) -> ((C2HSImp.Ptr (CYieldTermStructure')) -> ((C2HSImp.Ptr (CYieldTermStructure')) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO (C2HSImp.Ptr (CEnergyBasisSwap'))))))))))))))))))))))))))))))))))))))))
652
653 foreign import ccall safe "QuantLib/Instrument/Energy.chs.h qlCreatePricingPeriods"
654 qlCreatePricingPeriods_'_ :: (C2HSImp.CInt -> (C2HSImp.CInt -> ((C2HSImp.Ptr (CCommodityType)) -> ((C2HSImp.Ptr (CUnitOfMeasure)) -> (C2HSImp.CDouble -> (C2HSImp.CInt -> (C2HSImp.CInt -> ((C2HSImp.Ptr (CPaymentTerm)) -> ((C2HSImp.Ptr C2HSImp.CUInt) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CInt)) -> ((C2HSImp.Ptr C2HSImp.CUInt) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CInt)) -> ((C2HSImp.Ptr C2HSImp.CUInt) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CInt)) -> ((C2HSImp.Ptr C2HSImp.CUInt) -> ((C2HSImp.Ptr (C2HSImp.Ptr (C2HSImp.Ptr (CCommodityType)))) -> ((C2HSImp.Ptr C2HSImp.CUInt) -> ((C2HSImp.Ptr (C2HSImp.Ptr (C2HSImp.Ptr (CUnitOfMeasure)))) -> ((C2HSImp.Ptr C2HSImp.CUInt) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CDouble)) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO ()))))))))))))))))))))))