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.Currency
6 (
7 -- * Types
8 -- ** Currencies
9 Currency
10 , Ccy(..)
11 -- ** Exchange rates and money
12 , ExchangeRate
13 , ExchangeRateType(..)
14 , MoneyConversionType(..)
15
16 -- * Constructors
17 -- ** Currencies
18 , currency
19 , customCurrency
20 -- ** Exchange rates
21 , exchangeRate
22 , chainExchangeRate
23
24 -- * Mutators
25 , addExchangeRate
26 , clearExchangeRates
27 , setMoneyConversionType
28 , setMoneyBaseCurrency
29
30 -- * Inspectors
31 -- ** Currency conversion
32 , exchange
33 , convertToBaseCurrency
34 -- ** Currencies
35 , code
36 , fractionsPerUnit
37 , fractionSymbol
38 , numericCode
39 , symbol
40 -- ** Exchange rates and settings
41 , rate
42 , exchangeRateType
43 , lookupExchangeRate
44 , moneyConversionType
45 , moneyBaseCurrency
46 ) where
47 import qualified Foreign.C.String as C2HSImp
48 import qualified Foreign.C.Types as C2HSImp
49 import qualified Foreign.ForeignPtr as C2HSImp
50 import qualified Foreign.Ptr as C2HSImp
51 import qualified System.IO.Unsafe as C2HSImp
52
53
54 import Control.Exception (mask_)
55 import System.IO.Unsafe (unsafePerformIO)
56 import QuantLib.Internal
57 import QuantLib.Internal.Type
58 import QuantLib.Internal.Common
59 import Foreign.Marshal.Alloc(alloca)
60
61
62
63
64
65
66
67 data MoneyConversionType = NoConversion
68 | BaseCurrencyConversion
69 | AutomatedConversion
70 deriving (Enum,Show,Eq,Read)
71
72
73 data ExchangeRateType = Direct
74 | Derived
75 deriving (Enum,Show,Eq,Read)
76
77
78 data Ccy = ARS
79 | ATS
80 | AUD
81 | BCH
82 | BDT
83 | BEF
84 | BGL
85 | BRL
86 | BTC
87 | BYR
88 | CAD
89 | CHF
90 | CLP
91 | CNY
92 | COP
93 | CYP
94 | CZK
95 | DASH
96 | DEM
97 | DKK
98 | EEK
99 | ESP
100 | ETC
101 | ETH
102 | EUR
103 | FIM
104 | FRF
105 | GBP
106 | GRD
107 | HKD
108 | HUF
109 | IDR
110 | IEP
111 | ILS
112 | INR
113 | IQD
114 | IRR
115 | ISK
116 | ITL
117 | JPY
118 | KRW
119 | KWD
120 | KZT
121 | LTC
122 | LTL
123 | LUF
124 | LVL
125 | MTL
126 | MXN
127 | MYR
128 | NGN
129 | NLG
130 | NOK
131 | NPR
132 | NZD
133 | PEH
134 | PEI
135 | PEN
136 | PKR
137 | PLN
138 | PTE
139 | ROL
140 | RON
141 | RUB
142 | SAR
143 | SEK
144 | SGD
145 | SIT
146 | SKK
147 | THB
148 | TRL
149 | TRY
150 | TTD
151 | TWD
152 | UAH
153 | USD
154 | VEB
155 | VND
156 | XRP
157 | ZAR
158 | ZEC
159 | AED
160 | AOA
161 | BGN
162 | BHD
163 | BWP
164 | CLF
165 | CNH
166 | COU
167 | EGP
168 | ETB
169 | GEL
170 | GHS
171 | HRK
172 | JOD
173 | KES
174 | LKR
175 | MAD
176 | MKD
177 | MUR
178 | MXV
179 | OMR
180 | PHP
181 | QAR
182 | RSD
183 | TND
184 | UGX
185 | UYU
186 | UZS
187 | XOF
188 | ZMW
189 deriving (Show,Eq,Read,Bounded)
190 instance Enum Ccy where
191 succ ARS = ATS
192 succ ATS = AUD
193 succ AUD = BCH
194 succ BCH = BDT
195 succ BDT = BEF
196 succ BEF = BGL
197 succ BGL = BRL
198 succ BRL = BTC
199 succ BTC = BYR
200 succ BYR = CAD
201 succ CAD = CHF
202 succ CHF = CLP
203 succ CLP = CNY
204 succ CNY = COP
205 succ COP = CYP
206 succ CYP = CZK
207 succ CZK = DASH
208 succ DASH = DEM
209 succ DEM = DKK
210 succ DKK = EEK
211 succ EEK = ESP
212 succ ESP = ETC
213 succ ETC = ETH
214 succ ETH = EUR
215 succ EUR = FIM
216 succ FIM = FRF
217 succ FRF = GBP
218 succ GBP = GRD
219 succ GRD = HKD
220 succ HKD = HUF
221 succ HUF = IDR
222 succ IDR = IEP
223 succ IEP = ILS
224 succ ILS = INR
225 succ INR = IQD
226 succ IQD = IRR
227 succ IRR = ISK
228 succ ISK = ITL
229 succ ITL = JPY
230 succ JPY = KRW
231 succ KRW = KWD
232 succ KWD = KZT
233 succ KZT = LTC
234 succ LTC = LTL
235 succ LTL = LUF
236 succ LUF = LVL
237 succ LVL = MTL
238 succ MTL = MXN
239 succ MXN = MYR
240 succ MYR = NGN
241 succ NGN = NLG
242 succ NLG = NOK
243 succ NOK = NPR
244 succ NPR = NZD
245 succ NZD = PEH
246 succ PEH = PEI
247 succ PEI = PEN
248 succ PEN = PKR
249 succ PKR = PLN
250 succ PLN = PTE
251 succ PTE = ROL
252 succ ROL = RON
253 succ RON = RUB
254 succ RUB = SAR
255 succ SAR = SEK
256 succ SEK = SGD
257 succ SGD = SIT
258 succ SIT = SKK
259 succ SKK = THB
260 succ THB = TRL
261 succ TRL = TRY
262 succ TRY = TTD
263 succ TTD = TWD
264 succ TWD = UAH
265 succ UAH = USD
266 succ USD = VEB
267 succ VEB = VND
268 succ VND = XRP
269 succ XRP = ZAR
270 succ ZAR = ZEC
271 succ ZEC = AED
272 succ AED = AOA
273 succ AOA = BGN
274 succ BGN = BHD
275 succ BHD = BWP
276 succ BWP = CLF
277 succ CLF = CNH
278 succ CNH = COU
279 succ COU = EGP
280 succ EGP = ETB
281 succ ETB = GEL
282 succ GEL = GHS
283 succ GHS = HRK
284 succ HRK = JOD
285 succ JOD = KES
286 succ KES = LKR
287 succ LKR = MAD
288 succ MAD = MKD
289 succ MKD = MUR
290 succ MUR = MXV
291 succ MXV = OMR
292 succ OMR = PHP
293 succ PHP = QAR
294 succ QAR = RSD
295 succ RSD = TND
296 succ TND = UGX
297 succ UGX = UYU
298 succ UYU = UZS
299 succ UZS = XOF
300 succ XOF = ZMW
301 succ ZMW = error "Ccy.succ: ZMW has no successor"
302
303 pred ATS = ARS
304 pred AUD = ATS
305 pred BCH = AUD
306 pred BDT = BCH
307 pred BEF = BDT
308 pred BGL = BEF
309 pred BRL = BGL
310 pred BTC = BRL
311 pred BYR = BTC
312 pred CAD = BYR
313 pred CHF = CAD
314 pred CLP = CHF
315 pred CNY = CLP
316 pred COP = CNY
317 pred CYP = COP
318 pred CZK = CYP
319 pred DASH = CZK
320 pred DEM = DASH
321 pred DKK = DEM
322 pred EEK = DKK
323 pred ESP = EEK
324 pred ETC = ESP
325 pred ETH = ETC
326 pred EUR = ETH
327 pred FIM = EUR
328 pred FRF = FIM
329 pred GBP = FRF
330 pred GRD = GBP
331 pred HKD = GRD
332 pred HUF = HKD
333 pred IDR = HUF
334 pred IEP = IDR
335 pred ILS = IEP
336 pred INR = ILS
337 pred IQD = INR
338 pred IRR = IQD
339 pred ISK = IRR
340 pred ITL = ISK
341 pred JPY = ITL
342 pred KRW = JPY
343 pred KWD = KRW
344 pred KZT = KWD
345 pred LTC = KZT
346 pred LTL = LTC
347 pred LUF = LTL
348 pred LVL = LUF
349 pred MTL = LVL
350 pred MXN = MTL
351 pred MYR = MXN
352 pred NGN = MYR
353 pred NLG = NGN
354 pred NOK = NLG
355 pred NPR = NOK
356 pred NZD = NPR
357 pred PEH = NZD
358 pred PEI = PEH
359 pred PEN = PEI
360 pred PKR = PEN
361 pred PLN = PKR
362 pred PTE = PLN
363 pred ROL = PTE
364 pred RON = ROL
365 pred RUB = RON
366 pred SAR = RUB
367 pred SEK = SAR
368 pred SGD = SEK
369 pred SIT = SGD
370 pred SKK = SIT
371 pred THB = SKK
372 pred TRL = THB
373 pred TRY = TRL
374 pred TTD = TRY
375 pred TWD = TTD
376 pred UAH = TWD
377 pred USD = UAH
378 pred VEB = USD
379 pred VND = VEB
380 pred XRP = VND
381 pred ZAR = XRP
382 pred ZEC = ZAR
383 pred AED = ZEC
384 pred AOA = AED
385 pred BGN = AOA
386 pred BHD = BGN
387 pred BWP = BHD
388 pred CLF = BWP
389 pred CNH = CLF
390 pred COU = CNH
391 pred EGP = COU
392 pred ETB = EGP
393 pred GEL = ETB
394 pred GHS = GEL
395 pred HRK = GHS
396 pred JOD = HRK
397 pred KES = JOD
398 pred LKR = KES
399 pred MAD = LKR
400 pred MKD = MAD
401 pred MUR = MKD
402 pred MXV = MUR
403 pred OMR = MXV
404 pred PHP = OMR
405 pred QAR = PHP
406 pred RSD = QAR
407 pred TND = RSD
408 pred UGX = TND
409 pred UYU = UGX
410 pred UZS = UYU
411 pred XOF = UZS
412 pred ZMW = XOF
413 pred ARS = error "Ccy.pred: ARS has no predecessor"
414
415 enumFromTo from to = go from
416 where
417 end = fromEnum to
418 go v = case compare (fromEnum v) end of
419 LT -> v : go (succ v)
420 EQ -> [v]
421 GT -> []
422
423 enumFrom from = enumFromTo from ZMW
424
425 fromEnum ARS = 0
426 fromEnum ATS = 1
427 fromEnum AUD = 2
428 fromEnum BCH = 3
429 fromEnum BDT = 4
430 fromEnum BEF = 5
431 fromEnum BGL = 6
432 fromEnum BRL = 7
433 fromEnum BTC = 8
434 fromEnum BYR = 9
435 fromEnum CAD = 10
436 fromEnum CHF = 11
437 fromEnum CLP = 12
438 fromEnum CNY = 13
439 fromEnum COP = 14
440 fromEnum CYP = 15
441 fromEnum CZK = 16
442 fromEnum DASH = 17
443 fromEnum DEM = 18
444 fromEnum DKK = 19
445 fromEnum EEK = 20
446 fromEnum ESP = 21
447 fromEnum ETC = 22
448 fromEnum ETH = 23
449 fromEnum EUR = 24
450 fromEnum FIM = 25
451 fromEnum FRF = 26
452 fromEnum GBP = 27
453 fromEnum GRD = 28
454 fromEnum HKD = 29
455 fromEnum HUF = 30
456 fromEnum IDR = 31
457 fromEnum IEP = 32
458 fromEnum ILS = 33
459 fromEnum INR = 34
460 fromEnum IQD = 35
461 fromEnum IRR = 36
462 fromEnum ISK = 37
463 fromEnum ITL = 38
464 fromEnum JPY = 39
465 fromEnum KRW = 40
466 fromEnum KWD = 41
467 fromEnum KZT = 42
468 fromEnum LTC = 43
469 fromEnum LTL = 44
470 fromEnum LUF = 45
471 fromEnum LVL = 46
472 fromEnum MTL = 47
473 fromEnum MXN = 48
474 fromEnum MYR = 49
475 fromEnum NGN = 50
476 fromEnum NLG = 51
477 fromEnum NOK = 52
478 fromEnum NPR = 53
479 fromEnum NZD = 54
480 fromEnum PEH = 55
481 fromEnum PEI = 56
482 fromEnum PEN = 57
483 fromEnum PKR = 58
484 fromEnum PLN = 59
485 fromEnum PTE = 60
486 fromEnum ROL = 61
487 fromEnum RON = 62
488 fromEnum RUB = 63
489 fromEnum SAR = 64
490 fromEnum SEK = 65
491 fromEnum SGD = 66
492 fromEnum SIT = 67
493 fromEnum SKK = 68
494 fromEnum THB = 69
495 fromEnum TRL = 70
496 fromEnum TRY = 71
497 fromEnum TTD = 72
498 fromEnum TWD = 73
499 fromEnum UAH = 74
500 fromEnum USD = 75
501 fromEnum VEB = 76
502 fromEnum VND = 77
503 fromEnum XRP = 78
504 fromEnum ZAR = 79
505 fromEnum ZEC = 80
506 fromEnum AED = 81
507 fromEnum AOA = 82
508 fromEnum BGN = 83
509 fromEnum BHD = 84
510 fromEnum BWP = 85
511 fromEnum CLF = 86
512 fromEnum CNH = 87
513 fromEnum COU = 88
514 fromEnum EGP = 89
515 fromEnum ETB = 90
516 fromEnum GEL = 91
517 fromEnum GHS = 92
518 fromEnum HRK = 93
519 fromEnum JOD = 94
520 fromEnum KES = 95
521 fromEnum LKR = 96
522 fromEnum MAD = 97
523 fromEnum MKD = 98
524 fromEnum MUR = 99
525 fromEnum MXV = 100
526 fromEnum OMR = 101
527 fromEnum PHP = 102
528 fromEnum QAR = 103
529 fromEnum RSD = 104
530 fromEnum TND = 105
531 fromEnum UGX = 106
532 fromEnum UYU = 107
533 fromEnum UZS = 108
534 fromEnum XOF = 109
535 fromEnum ZMW = 110
536
537 toEnum 0 = ARS
538 toEnum 1 = ATS
539 toEnum 2 = AUD
540 toEnum 3 = BCH
541 toEnum 4 = BDT
542 toEnum 5 = BEF
543 toEnum 6 = BGL
544 toEnum 7 = BRL
545 toEnum 8 = BTC
546 toEnum 9 = BYR
547 toEnum 10 = CAD
548 toEnum 11 = CHF
549 toEnum 12 = CLP
550 toEnum 13 = CNY
551 toEnum 14 = COP
552 toEnum 15 = CYP
553 toEnum 16 = CZK
554 toEnum 17 = DASH
555 toEnum 18 = DEM
556 toEnum 19 = DKK
557 toEnum 20 = EEK
558 toEnum 21 = ESP
559 toEnum 22 = ETC
560 toEnum 23 = ETH
561 toEnum 24 = EUR
562 toEnum 25 = FIM
563 toEnum 26 = FRF
564 toEnum 27 = GBP
565 toEnum 28 = GRD
566 toEnum 29 = HKD
567 toEnum 30 = HUF
568 toEnum 31 = IDR
569 toEnum 32 = IEP
570 toEnum 33 = ILS
571 toEnum 34 = INR
572 toEnum 35 = IQD
573 toEnum 36 = IRR
574 toEnum 37 = ISK
575 toEnum 38 = ITL
576 toEnum 39 = JPY
577 toEnum 40 = KRW
578 toEnum 41 = KWD
579 toEnum 42 = KZT
580 toEnum 43 = LTC
581 toEnum 44 = LTL
582 toEnum 45 = LUF
583 toEnum 46 = LVL
584 toEnum 47 = MTL
585 toEnum 48 = MXN
586 toEnum 49 = MYR
587 toEnum 50 = NGN
588 toEnum 51 = NLG
589 toEnum 52 = NOK
590 toEnum 53 = NPR
591 toEnum 54 = NZD
592 toEnum 55 = PEH
593 toEnum 56 = PEI
594 toEnum 57 = PEN
595 toEnum 58 = PKR
596 toEnum 59 = PLN
597 toEnum 60 = PTE
598 toEnum 61 = ROL
599 toEnum 62 = RON
600 toEnum 63 = RUB
601 toEnum 64 = SAR
602 toEnum 65 = SEK
603 toEnum 66 = SGD
604 toEnum 67 = SIT
605 toEnum 68 = SKK
606 toEnum 69 = THB
607 toEnum 70 = TRL
608 toEnum 71 = TRY
609 toEnum 72 = TTD
610 toEnum 73 = TWD
611 toEnum 74 = UAH
612 toEnum 75 = USD
613 toEnum 76 = VEB
614 toEnum 77 = VND
615 toEnum 78 = XRP
616 toEnum 79 = ZAR
617 toEnum 80 = ZEC
618 toEnum 81 = AED
619 toEnum 82 = AOA
620 toEnum 83 = BGN
621 toEnum 84 = BHD
622 toEnum 85 = BWP
623 toEnum 86 = CLF
624 toEnum 87 = CNH
625 toEnum 88 = COU
626 toEnum 89 = EGP
627 toEnum 90 = ETB
628 toEnum 91 = GEL
629 toEnum 92 = GHS
630 toEnum 93 = HRK
631 toEnum 94 = JOD
632 toEnum 95 = KES
633 toEnum 96 = LKR
634 toEnum 97 = MAD
635 toEnum 98 = MKD
636 toEnum 99 = MUR
637 toEnum 100 = MXV
638 toEnum 101 = OMR
639 toEnum 102 = PHP
640 toEnum 103 = QAR
641 toEnum 104 = RSD
642 toEnum 105 = TND
643 toEnum 106 = UGX
644 toEnum 107 = UYU
645 toEnum 108 = UZS
646 toEnum 109 = XOF
647 toEnum 110 = ZMW
648 toEnum unmatched = error ("Ccy.toEnum: Cannot match " ++ show unmatched)
649
650
651
652
653
654
655
656
657
658
659 -- |Look up one of the built-in ISO 4217 currencies by its enum tag.
660 currency :: (Ccy) -> IO ((Currency))
661 currency a1 =
662 let {a1' = (fromIntegral . fromEnum) a1} in
663 preErrorCheck $ \a2' ->
664 currency'_ a1' a2' >>= \res ->
665 peekCurrency res >>= \res' ->
666 errorCheck a2'>>
667 return (res')
668
669
670
671 -- |The currency's ISO 4217 three-letter code, e.g. \"USD\".
672 code :: Currency -> String
673 code x = unsafePerformIO $ mask_ (codeRaw x)
674 codeRaw :: (Currency) -> IO ((String))
675 codeRaw a1 =
676 withCurrency a1 $ \a1' ->
677 codeRaw'_ a1' >>= \res ->
678 peekDynString res >>= \res' ->
679 return (res')
680
681
682
683 -- |The number of fractional units (e.g. cents) in one unit of the currency.
684 fractionsPerUnit :: (Currency) -> (Int)
685 fractionsPerUnit a1 =
686 C2HSImp.unsafePerformIO $
687 withCurrency a1 $ \a1' ->
688 fractionsPerUnit'_ a1' >>= \res ->
689 let {res' = fromIntegral res} in
690 return (res')
691
692
693
694 -- |The currency's fractional-unit symbol, e.g. \"ยข\".
695 fractionSymbol :: (Currency) -> (String)
696 fractionSymbol a1 =
697 C2HSImp.unsafePerformIO $
698 withCurrency a1 $ \a1' ->
699 fractionSymbol'_ a1' >>= \res ->
700 C2HSImp.peekCString res >>= \res' ->
701 return (res')
702
703
704
705 -- |The currency's ISO 4217 numeric code, e.g. 840 for USD.
706 numericCode :: (Currency) -> (Int)
707 numericCode a1 =
708 C2HSImp.unsafePerformIO $
709 withCurrency a1 $ \a1' ->
710 numericCode'_ a1' >>= \res ->
711 let {res' = fromIntegral res} in
712 return (res')
713
714
715
716 -- |The currency's symbol, e.g. \"$\".
717 symbol :: (Currency) -> (String)
718 symbol a1 =
719 C2HSImp.unsafePerformIO $
720 withCurrency a1 $ \a1' ->
721 symbol'_ a1' >>= \res ->
722 C2HSImp.peekCString res >>= \res' ->
723 return (res')
724
725
726
727 -- |Construct a custom currency from its name, codes, symbols, rounding convention and an
728 -- optional triangulation currency used for indirect exchange.
729 customCurrency :: (String) -- ^name
730 -> (String) -- ^code
731 -> (Int) -- ^numericCode
732 -> (String) -- ^symbol
733 -> (String) -- ^fractionSymbol
734 -> (Int) -- ^fractionsPerUnit
735 -> (Maybe Rounding) -> (Maybe Currency) -- ^triangulationCurrency
736 -> IO ((Currency))
737 customCurrency a1 a2 a3 a4 a5 a6 a7 a8 =
738 C2HSImp.withCString a1 $ \a1' ->
739 C2HSImp.withCString a2 $ \a2' ->
740 let {a3' = fromIntegral a3} in
741 C2HSImp.withCString a4 $ \a4' ->
742 C2HSImp.withCString a5 $ \a5' ->
743 let {a6' = fromIntegral a6} in
744 withMaybeRounding a7 $ \a7' ->
745 withMaybeCurrency a8 $ \a8' ->
746 preErrorCheck $ \a9' ->
747 customCurrency'_ a1' a2' a3' a4' a5' a6' a7' a8' a9' >>= \res ->
748 peekCurrency res >>= \res' ->
749 errorCheck a9'>>
750 return (res')
751
752
753
754 -- |Construct a Direct exchange rate: a unit of @source@ is worth @rate@ units of @target@.
755 exchangeRate :: (Currency) -- ^source
756 -> (Currency) -- ^target
757 -> (Double) -- ^rate
758 -> IO ((ExchangeRate))
759 exchangeRate a1 a2 a3 =
760 withCurrency a1 $ \a1' ->
761 withCurrency a2 $ \a2' ->
762 let {a3' = realToFrac a3} in
763 preErrorCheck $ \a4' ->
764 exchangeRate'_ a1' a2' a3' a4' >>= \res ->
765 peekExchangeRate res >>= \res' ->
766 errorCheck a4'>>
767 return (res')
768
769
770
771 -- |The rate itself: a unit of the source currency is worth this many units of the target.
772 rate :: (ExchangeRate) -> IO ((Double))
773 rate a1 =
774 withExchangeRate a1 $ \a1' ->
775 rate'_ a1' >>= \res ->
776 let {res' = realToFrac res} in
777 return (res')
778
779
780
781 -- |Whether the rate was given directly or derived by chaining two other rates.
782 exchangeRateType :: (ExchangeRate) -> IO ((ExchangeRateType))
783 exchangeRateType a1 =
784 withExchangeRate a1 $ \a1' ->
785 exchangeRateType'_ a1' >>= \res ->
786 let {res' = (toEnum . fromIntegral) res} in
787 return (res')
788
789
790
791 -- |Apply an exchange rate to a cash amount (a @(Double, Currency)@ pair, standing in for
792 -- QuantLib's @Money@), returning the converted amount as a pair in the other currency of the
793 -- rate. Throws if the given currency is on neither side of the rate.
794 exchange :: (ExchangeRate) -> ((Double, Currency)) -- ^amount
795 -> IO ((Double), (Currency))
796 exchange a1 a2 =
797 withExchangeRate a1 $ \a1' ->
798 withMoney a2 $ \(a2'1, a2'2) ->
799 alloca $ \a3' ->
800 preErrorCheck $ \a4' ->
801 exchange'_ a1' a2'1 a2'2 a3' a4' >>= \res ->
802 let {res' = realToFrac res} in
803 peekCurrencyPtr a3'>>= \a3'' ->
804 errorCheck a4'>>
805 return (res', a3'')
806
807
808
809 -- |Combine two exchange rates sharing a common currency into a derived rate between their
810 -- other two currencies. Throws if the rates don't share a common currency.
811 chainExchangeRate :: (ExchangeRate) -> (ExchangeRate) -> IO ((ExchangeRate))
812 chainExchangeRate a1 a2 =
813 withExchangeRate a1 $ \a1' ->
814 withExchangeRate a2 $ \a2' ->
815 preErrorCheck $ \a3' ->
816 chainExchangeRate'_ a1' a2' a3' >>= \res ->
817 peekExchangeRate res >>= \res' ->
818 errorCheck a3'>>
819 return (res')
820
821
822
823 -- |Register an exchange rate with the global exchange-rate repository, valid between the given
824 -- dates (inclusive). Use 'minDate'/'maxDate' for an always-valid rate.
825 addExchangeRate :: (ExchangeRate) -> (Day) -> (Day) -> IO ()
826 addExchangeRate a1 a2 a3 =
827 withExchangeRate a1 $ \a1' ->
828 withDay a2 $ \a2' ->
829 withDay a3 $ \a3' ->
830 preErrorCheck $ \a4' ->
831 addExchangeRate'_ a1' a2' a3' a4' >>
832 errorCheck a4'>>
833 return ()
834
835
836
837 -- |Look up a (possibly derived) exchange rate between two currencies at a given date (or the
838 -- current evaluation date if 'Nothing'). Throws if no rate can be found. Pre-populated with a
839 -- set of known historical rates even before any 'addExchangeRate' call.
840 lookupExchangeRate :: (Currency) -> (Currency) -> (Maybe Day) -> (ExchangeRateType) -> IO ((ExchangeRate))
841 lookupExchangeRate a1 a2 a3 a4 =
842 withCurrency a1 $ \a1' ->
843 withCurrency a2 $ \a2' ->
844 withMaybeDay a3 $ \a3' ->
845 let {a4' = (fromIntegral . fromEnum) a4} in
846 preErrorCheck $ \a5' ->
847 lookupExchangeRate'_ a1' a2' a3' a4' a5' >>= \res ->
848 peekExchangeRate res >>= \res' ->
849 errorCheck a5'>>
850 return (res')
851
852
853
854 -- |Reset the exchange-rate repository back to its built-in set of known historical rates,
855 -- discarding anything added via 'addExchangeRate'.
856 clearExchangeRates :: IO ()
857 clearExchangeRates =
858 clearExchangeRates'_ >>
859 return ()
860
861
862
863 -- |The global 'Money' arithmetic setting controlling how amounts in different currencies are
864 -- combined (no conversion, convert to the base currency, or convert automatically).
865 moneyConversionType :: IO ((MoneyConversionType))
866 moneyConversionType =
867 moneyConversionType'_ >>= \res ->
868 let {res' = (toEnum . fromIntegral) res} in
869 return (res')
870
871
872
873 -- |Set the global 'Money' arithmetic conversion setting.
874 setMoneyConversionType :: (MoneyConversionType) -> IO ()
875 setMoneyConversionType a1 =
876 let {a1' = (fromIntegral . fromEnum) a1} in
877 setMoneyConversionType'_ a1' >>
878 return ()
879
880
881
882 -- |The global base currency used by 'MoneyConversionType' base-currency conversion, if set.
883 moneyBaseCurrency :: IO ((Maybe Currency))
884 moneyBaseCurrency =
885 preErrorCheck $ \a1' ->
886 moneyBaseCurrency'_ a1' >>= \res ->
887 peekMaybeCurrency res >>= \res' ->
888 errorCheck a1'>>
889 return (res')
890
891
892
893 -- |Set the global base currency used by 'MoneyConversionType' base-currency conversion.
894 setMoneyBaseCurrency :: (Currency) -> IO ()
895 setMoneyBaseCurrency a1 =
896 withCurrency a1 $ \a1' ->
897 setMoneyBaseCurrency'_ a1' >>
898 return ()
899
900
901
902 -- |Convert a cash amount to the configured base currency (via 'setMoneyBaseCurrency'), using
903 -- 'lookupExchangeRate' and rounding the result per the target currency's convention. Throws if
904 -- no base currency is set or no rate path exists.
905 convertToBaseCurrency :: ((Double, Currency)) -- ^amount
906 -> IO ((Double), (Currency))
907 convertToBaseCurrency a1 =
908 withMoney a1 $ \(a1'1, a1'2) ->
909 alloca $ \a2' ->
910 preErrorCheck $ \a3' ->
911 convertToBaseCurrency'_ a1'1 a1'2 a2' a3' >>= \res ->
912 let {res' = realToFrac res} in
913 peekCurrencyPtr a2'>>= \a2'' ->
914 errorCheck a3'>>
915 return (res', a2'')
916
917
918
919 -- vim: set ff=unix ts=8 sts=2 sw=2 et:
920
921 foreign import ccall safe "QuantLib/Currency.chs.h qlCurrency"
922 currency'_ :: (C2HSImp.CInt -> ((C2HSImp.Ptr (C2HSImp.Ptr ())) -> (IO (C2HSImp.Ptr (CCurrency)))))
923
924 foreign import ccall safe "QuantLib/Currency.chs.h qlCurrencyCode"
925 codeRaw'_ :: ((C2HSImp.Ptr (CCurrency)) -> (IO (C2HSImp.Ptr C2HSImp.CChar)))
926
927 foreign import ccall safe "QuantLib/Currency.chs.h qlCurrencyFractionsPerUnit"
928 fractionsPerUnit'_ :: ((C2HSImp.Ptr (CCurrency)) -> (IO C2HSImp.CInt))
929
930 foreign import ccall safe "QuantLib/Currency.chs.h qlCurrencyFractionSymbol"
931 fractionSymbol'_ :: ((C2HSImp.Ptr (CCurrency)) -> (IO (C2HSImp.Ptr C2HSImp.CChar)))
932
933 foreign import ccall safe "QuantLib/Currency.chs.h qlCurrencyNumericCode"
934 numericCode'_ :: ((C2HSImp.Ptr (CCurrency)) -> (IO C2HSImp.CInt))
935
936 foreign import ccall safe "QuantLib/Currency.chs.h qlCurrencySymbol"
937 symbol'_ :: ((C2HSImp.Ptr (CCurrency)) -> (IO (C2HSImp.Ptr C2HSImp.CChar)))
938
939 foreign import ccall safe "QuantLib/Currency.chs.h qlCreateCurrency"
940 customCurrency'_ :: ((C2HSImp.Ptr C2HSImp.CChar) -> ((C2HSImp.Ptr C2HSImp.CChar) -> (C2HSImp.CInt -> ((C2HSImp.Ptr C2HSImp.CChar) -> ((C2HSImp.Ptr C2HSImp.CChar) -> (C2HSImp.CInt -> ((C2HSImp.Ptr (CRounding)) -> ((C2HSImp.Ptr (CCurrency)) -> ((C2HSImp.Ptr (C2HSImp.Ptr ())) -> (IO (C2HSImp.Ptr (CCurrency))))))))))))
941
942 foreign import ccall safe "QuantLib/Currency.chs.h qlExchangeRate"
943 exchangeRate'_ :: ((C2HSImp.Ptr (CCurrency)) -> ((C2HSImp.Ptr (CCurrency)) -> (C2HSImp.CDouble -> ((C2HSImp.Ptr (C2HSImp.Ptr ())) -> (IO (C2HSImp.Ptr (CExchangeRate)))))))
944
945 foreign import ccall safe "QuantLib/Currency.chs.h qlExchangeRateRate"
946 rate'_ :: ((C2HSImp.Ptr (CExchangeRate)) -> (IO C2HSImp.CDouble))
947
948 foreign import ccall safe "QuantLib/Currency.chs.h qlExchangeRateType_"
949 exchangeRateType'_ :: ((C2HSImp.Ptr (CExchangeRate)) -> (IO C2HSImp.CInt))
950
951 foreign import ccall safe "QuantLib/Currency.chs.h qlExchangeRateExchange"
952 exchange'_ :: ((C2HSImp.Ptr (CExchangeRate)) -> (C2HSImp.CDouble -> ((C2HSImp.Ptr (CCurrency)) -> ((C2HSImp.Ptr (C2HSImp.Ptr (CCurrency))) -> ((C2HSImp.Ptr (C2HSImp.Ptr ())) -> (IO C2HSImp.CDouble))))))
953
954 foreign import ccall safe "QuantLib/Currency.chs.h qlExchangeRateChain"
955 chainExchangeRate'_ :: ((C2HSImp.Ptr (CExchangeRate)) -> ((C2HSImp.Ptr (CExchangeRate)) -> ((C2HSImp.Ptr (C2HSImp.Ptr ())) -> (IO (C2HSImp.Ptr (CExchangeRate))))))
956
957 foreign import ccall safe "QuantLib/Currency.chs.h qlExchangeRateManagerAdd"
958 addExchangeRate'_ :: ((C2HSImp.Ptr (CExchangeRate)) -> (C2HSImp.CInt -> (C2HSImp.CInt -> ((C2HSImp.Ptr (C2HSImp.Ptr ())) -> (IO ())))))
959
960 foreign import ccall safe "QuantLib/Currency.chs.h qlExchangeRateManagerLookup"
961 lookupExchangeRate'_ :: ((C2HSImp.Ptr (CCurrency)) -> ((C2HSImp.Ptr (CCurrency)) -> (C2HSImp.CInt -> (C2HSImp.CInt -> ((C2HSImp.Ptr (C2HSImp.Ptr ())) -> (IO (C2HSImp.Ptr (CExchangeRate))))))))
962
963 foreign import ccall safe "QuantLib/Currency.chs.h qlExchangeRateManagerClear"
964 clearExchangeRates'_ :: (IO ())
965
966 foreign import ccall safe "QuantLib/Currency.chs.h qlMoneySettingsConversionType"
967 moneyConversionType'_ :: (IO C2HSImp.CInt)
968
969 foreign import ccall safe "QuantLib/Currency.chs.h qlMoneySettingsSetConversionType"
970 setMoneyConversionType'_ :: (C2HSImp.CInt -> (IO ()))
971
972 foreign import ccall safe "QuantLib/Currency.chs.h qlMoneySettingsBaseCurrency"
973 moneyBaseCurrency'_ :: ((C2HSImp.Ptr (C2HSImp.Ptr ())) -> (IO (C2HSImp.Ptr (CCurrency))))
974
975 foreign import ccall safe "QuantLib/Currency.chs.h qlMoneySettingsSetBaseCurrency"
976 setMoneyBaseCurrency'_ :: ((C2HSImp.Ptr (CCurrency)) -> (IO ()))
977
978 foreign import ccall safe "QuantLib/Currency.chs.h qlConvertToBaseCurrency"
979 convertToBaseCurrency'_ :: (C2HSImp.CDouble -> ((C2HSImp.Ptr (CCurrency)) -> ((C2HSImp.Ptr (C2HSImp.Ptr (CCurrency))) -> ((C2HSImp.Ptr (C2HSImp.Ptr ())) -> (IO C2HSImp.CDouble)))))