{-# LINE 1 "./QuantLib/Time/Schedule.chs" #-}
{-# OPTIONS_GHC -Wno-orphans #-}
module QuantLib.Time.Schedule
(
DayCounter
, Schedule
, DayCounterConstructor(..)
, DateGenerationRule(..)
, TimeUnit(..)
, Frequency(..)
, dayCounter
, schedule
, fromDates
, days
, yearFraction
, until
, fromFrequency
, toFrequency
, parse
, add
, divide
, lessThan
, normalize
, dates
) where
import qualified Foreign.C.String as C2HSImp
import qualified Foreign.C.Types as C2HSImp
import qualified Foreign.ForeignPtr as C2HSImp
import qualified Foreign.Marshal.Utils as C2HSImp
import qualified Foreign.Ptr as C2HSImp
import Prelude hiding(until)
import QuantLib.Time.Date
import QuantLib.Internal
import QuantLib.Internal.Type
import QuantLib.Internal.Common
import QuantLib.Internal.CalendarEnum
import QuantLib.Time.Calendar (calendar)
import System.IO.Unsafe(unsafePerformIO)
data DateGenerationRule = Backward
| Forward
| Zero
| ThirdWednesday
| ThirdWednesdayInclusive
| Twentieth
| TwentiethIMM
| OldCDS
| CDS
| CDS2015
deriving (Enum,Show,Eq,Read)
{-# LINE 54 "./QuantLib/Time/Schedule.chs" #-}
data Frequency = NoFrequency
| Once
| Annual
| Semiannual
| EveryFourthMonth
| Quarterly
| Bimonthly
| Monthly
| EveryFourthWeek
| Biweekly
| Weekly
| Daily
| OtherFrequency
deriving (Show,Eq,Read,Bounded)
instance Enum Frequency where
succ NoFrequency = Once
succ Once = Annual
succ Annual = Semiannual
succ Semiannual = EveryFourthMonth
succ EveryFourthMonth = Quarterly
succ Quarterly = Bimonthly
succ Bimonthly = Monthly
succ Monthly = EveryFourthWeek
succ EveryFourthWeek = Biweekly
succ Biweekly = Weekly
succ Weekly = Daily
succ Daily = OtherFrequency
succ OtherFrequency = error "Frequency.succ: OtherFrequency has no successor"
pred Once = NoFrequency
pred Annual = Once
pred Semiannual = Annual
pred EveryFourthMonth = Semiannual
pred Quarterly = EveryFourthMonth
pred Bimonthly = Quarterly
pred Monthly = Bimonthly
pred EveryFourthWeek = Monthly
pred Biweekly = EveryFourthWeek
pred Weekly = Biweekly
pred Daily = Weekly
pred OtherFrequency = Daily
pred NoFrequency = error "Frequency.pred: NoFrequency has no predecessor"
enumFromTo :: Frequency -> Frequency -> [Frequency]
enumFromTo Frequency
from Frequency
to = Frequency -> [Frequency]
forall {t}. Enum t => t -> [t]
go Frequency
from
where
end :: Int
end = Frequency -> Int
forall a. Enum a => a -> Int
fromEnum Frequency
to
go :: t -> [t]
go t
v = case Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (t -> Int
forall a. Enum a => a -> Int
fromEnum t
v) Int
end of
Ordering
LT -> t
v t -> [t] -> [t]
forall a. a -> [a] -> [a]
: t -> [t]
go (t -> t
forall a. Enum a => a -> a
succ t
v)
Ordering
EQ -> [t
v]
Ordering
GT -> []
enumFrom from = enumFromTo from OtherFrequency
fromEnum NoFrequency = (-1)
fromEnum Once = 0
fromEnum Annual = 1
fromEnum Semiannual = 2
fromEnum EveryFourthMonth = 3
fromEnum Quarterly = 4
fromEnum Bimonthly = 6
fromEnum Monthly = 12
fromEnum EveryFourthWeek = 13
fromEnum Biweekly = 26
fromEnum Weekly = 52
fromEnum Daily = 365
fromEnum OtherFrequency = 999
toEnum (-1) = NoFrequency
toEnum 0 = Once
toEnum 1 = Annual
toEnum 2 = Semiannual
toEnum 3 = EveryFourthMonth
toEnum 4 = Quarterly
toEnum 6 = Bimonthly
toEnum 12 = Monthly
toEnum 13 = EveryFourthWeek
toEnum 26 = Biweekly
toEnum 52 = Weekly
toEnum 365 = Daily
toEnum 999 = OtherFrequency
toEnum unmatched = error ("Frequency.toEnum: Cannot match " ++ show unmatched)
{-# LINE 55 "./QuantLib/Time/Schedule.chs" #-}
{-# LINE 57 "./QuantLib/Time/Schedule.chs" #-}
{-# LINE 58 "./QuantLib/Time/Schedule.chs" #-}
{-# LINE 59 "./QuantLib/Time/Schedule.chs" #-}
qlDayCounter :: (Int) -> (Int) -> IO ((DayCounter))
qlDayCounter a1 a2 =
let {a1' = fromIntegral a1} in
let {a2' = fromIntegral a2} in
preErrorCheck $ \a3' ->
qlDayCounter'_ a1' a2' a3' >>= \res ->
peekDayCounter res >>= \res' ->
errorCheck a3'>>
return (res')
{-# LINE 62 "./QuantLib/Time/Schedule.chs" #-}
qlDayCounterBusiness252 :: (Calendar) -> IO ((DayCounter))
qlDayCounterBusiness252 a1 =
withCalendar a1 $ \a1' ->
preErrorCheck $ \a2' ->
qlDayCounterBusiness252'_ a1' a2' >>= \res ->
peekDayCounter res >>= \res' ->
errorCheck a2'>>
return (res')
{-# LINE 65 "./QuantLib/Time/Schedule.chs" #-}
actualActualBond' :: (Schedule) -> IO ((DayCounter))
actualActualBond' a1 =
withSchedule a1 $ \a1' ->
preErrorCheck $ \a2' ->
actualActualBond''_ a1' a2' >>= \res ->
peekDayCounter res >>= \res' ->
errorCheck a2'>>
return (res')
{-# LINE 68 "./QuantLib/Time/Schedule.chs" #-}
actualActualISMA' :: (Schedule) -> IO ((DayCounter))
actualActualISMA' a1 =
withSchedule a1 $ \a1' ->
preErrorCheck $ \a2' ->
actualActualISMA''_ a1' a2' >>= \res ->
peekDayCounter res >>= \res' ->
errorCheck a2'>>
return (res')
{-# LINE 71 "./QuantLib/Time/Schedule.chs" #-}
dayCounter :: DayCounterConstructor -> IO DayCounter
dayCounter (Business252 x) = qlDayCounterBusiness252 x
dayCounter (ActualActualBond' sched) = actualActualBond' sched
dayCounter (ActualActualISMA' sched) = actualActualISMA' sched
dayCounter x = uncurry qlDayCounter $ mapDayCounter x
days :: (DayCounter) -> (Day) -> (Day) -> IO ((Int))
days a1 a2 a3 =
withDayCounter a1 $ \a1' ->
withDay a2 $ \a2' ->
withDay a3 $ \a3' ->
days'_ a1' a2' a3' >>= \res ->
let {res' = fromIntegral res} in
return (res')
{-# LINE 80 "./QuantLib/Time/Schedule.chs" #-}
yearFraction :: (DayCounter) -> (Day) -> (Day) -> (Maybe Day)
-> (Maybe Day)
-> IO ((Double))
yearFraction a1 a2 a3 a4 a5 =
withDayCounter a1 $ \a1' ->
withDay a2 $ \a2' ->
withDay a3 $ \a3' ->
withMaybeDay a4 $ \a4' ->
withMaybeDay a5 $ \a5' ->
preErrorCheck $ \a6' ->
yearFraction'_ a1' a2' a3' a4' a5' a6' >>= \res ->
let {res' = realToFrac res} in
errorCheck a6'>>
return (res')
{-# LINE 86 "./QuantLib/Time/Schedule.chs" #-}
schedule :: (Maybe Day)
-> (Day)
-> ((Word,TimeUnit))
-> (Calendar)
-> (BusinessDayConvention)
-> (BusinessDayConvention)
-> (DateGenerationRule)
-> (Bool)
-> (Maybe Day)
-> (Maybe Day)
-> IO ((Schedule))
schedule a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 =
withMaybeDay a1 $ \a1' ->
withDay a2 $ \a2' ->
let {(a3'1, a3'2) = fromEnumQuantity a3} in
withCalendar a4 $ \a4' ->
let {a5' = fromEnumC a5} in
let {a6' = fromEnumC a6} in
let {a7' = (fromIntegral . fromEnum) a7} in
let {a8' = C2HSImp.fromBool a8} in
withMaybeDay a9 $ \a9' ->
withMaybeDay a10 $ \a10' ->
preErrorCheck $ \a11' ->
schedule'_ a1' a2' a3'1 a3'2 a4' a5' a6' a7' a8' a9' a10' a11' >>= \res ->
peekSchedule res >>= \res' ->
errorCheck a11'>>
return (res')
{-# LINE 99 "./QuantLib/Time/Schedule.chs" #-}
fromDates :: ([Day]) -> (Calendar)
-> (BusinessDayConvention)
-> (Maybe BusinessDayConvention)
-> (Maybe (Word, TimeUnit))
-> (Maybe DateGenerationRule)
-> (Maybe Bool)
-> IO ((Schedule))
fromDates a1 a2 a3 a4 a5 a6 a7 =
withDayArray a1 $ \(a1'1, a1'2) ->
withCalendar a2 $ \a2' ->
let {a3' = fromEnumC a3} in
let {a4' = fromMaybeEnum a4} in
let {(a5'1, a5'2) = fromMaybeEnumQuantity a5} in
let {a6' = fromMaybeEnum a6} in
let {a7' = fromMaybeBool a7} in
preErrorCheck $ \a8' ->
fromDates'_ a1'1 a1'2 a2' a3' a4' a5'1 a5'2 a6' a7' a8' >>= \res ->
peekSchedule res >>= \res' ->
errorCheck a8'>>
return (res')
{-# LINE 109 "./QuantLib/Time/Schedule.chs" #-}
until :: (Schedule) -> (Day) -> IO ((Schedule))
until a1 a2 =
withSchedule a1 $ \a1' ->
withDay a2 $ \a2' ->
preErrorCheck $ \a3' ->
until'_ a1' a2' a3' >>= \res ->
peekSchedule res >>= \res' ->
errorCheck a3'>>
return (res')
{-# LINE 114 "./QuantLib/Time/Schedule.chs" #-}
dates :: (Schedule) -> IO (([Day]))
dates a1 =
withSchedule a1 $ \a1' ->
preArray $ \(a2'1, a2'2) ->
preErrorCheck $ \a3' ->
dates'_ a1' a2'1 a2'2 a3' >>
peekDayArray a2'1 a2'2>>= \a2'' ->
errorCheck a3'>>
return (a2'')
{-# LINE 117 "./QuantLib/Time/Schedule.chs" #-}
fromFrequency :: (Frequency) -> IO ((Word), (TimeUnit))
fromFrequency a1 =
let {a1' = (fromIntegral . fromEnum) a1} in
preEnum $ \a2' ->
preErrorCheck $ \a3' ->
fromFrequency'_ a1' a2' a3' >>= \res ->
let {res' = fromIntegral res} in
peekEnum a2'>>= \a2'' ->
errorCheck a3'>>
return (res', a2'')
{-# LINE 120 "./QuantLib/Time/Schedule.chs" #-}
toFrequency :: (Word,TimeUnit) -> IO ((Frequency))
toFrequency a1 =
let {(a1'1, a1'2) = fromEnumQuantity a1} in
preErrorCheck $ \a2' ->
toFrequency'_ a1'1 a1'2 a2' >>= \res ->
let {res' = (toEnum . fromIntegral) res} in
errorCheck a2'>>
return (res')
{-# LINE 123 "./QuantLib/Time/Schedule.chs" #-}
parse :: (String) -> IO ((Int), (TimeUnit))
parse a1 =
C2HSImp.withCString a1 $ \a1' ->
preEnum $ \a2' ->
preErrorCheck $ \a3' ->
parse'_ a1' a2' a3' >>= \res ->
let {res' = fromIntegral res} in
peekEnum a2'>>= \a2'' ->
errorCheck a3'>>
return (res', a2'')
{-# LINE 126 "./QuantLib/Time/Schedule.chs" #-}
addPeriods :: (Int,TimeUnit) -> (Int,TimeUnit) -> IO ((Int), (TimeUnit))
addPeriods a1 a2 =
let {(a1'1, a1'2) = fromEnumQuantity a1} in
let {(a2'1, a2'2) = fromEnumQuantity a2} in
preEnum $ \a3' ->
preErrorCheck $ \a4' ->
addPeriods'_ a1'1 a1'2 a2'1 a2'2 a3' a4' >>= \res ->
let {res' = fromIntegral res} in
peekEnum a3'>>= \a3'' ->
errorCheck a4'>>
return (res', a3'')
{-# LINE 128 "./QuantLib/Time/Schedule.chs" #-}
add :: (Int, TimeUnit) -> (Int, TimeUnit) -> IO (Int, TimeUnit)
add = addPeriods
divide :: (Int,TimeUnit) -> (Int) -> IO ((Int), (TimeUnit))
divide a1 a2 =
let {(a1'1, a1'2) = fromEnumQuantity a1} in
let {a2' = fromIntegral a2} in
preEnum $ \a3' ->
preErrorCheck $ \a4' ->
divide'_ a1'1 a1'2 a2' a3' a4' >>= \res ->
let {res' = fromIntegral res} in
peekEnum a3'>>= \a3'' ->
errorCheck a4'>>
return (res', a3'')
{-# LINE 135 "./QuantLib/Time/Schedule.chs" #-}
lessThan :: (Int,TimeUnit) -> (Int,TimeUnit) -> IO ((Bool))
lessThan a1 a2 =
let {(a1'1, a1'2) = fromEnumQuantity a1} in
let {(a2'1, a2'2) = fromEnumQuantity a2} in
preErrorCheck $ \a3' ->
lessThan'_ a1'1 a1'2 a2'1 a2'2 a3' >>= \res ->
let {res' = C2HSImp.toBool res} in
errorCheck a3'>>
return (res')
{-# LINE 138 "./QuantLib/Time/Schedule.chs" #-}
normalize :: (Int,TimeUnit) -> IO ((Int), (TimeUnit))
normalize a1 =
let {(a1'1, a1'2) = fromEnumQuantity a1} in
preEnum $ \a2' ->
preErrorCheck $ \a3' ->
normalize'_ a1'1 a1'2 a2' a3' >>= \res ->
let {res' = fromIntegral res} in
peekEnum a2'>>= \a2'' ->
errorCheck a3'>>
return (res', a2'')
{-# LINE 141 "./QuantLib/Time/Schedule.chs" #-}
unsafeCalendar :: CalendarConstructor -> Calendar
unsafeCalendar = unsafePerformIO . calendar
{-# NOINLINE unsafeCalendar #-}
instance Read DayCounterConstructor where
readsPrec :: Int -> ReadS DayCounterConstructor
readsPrec Int
d String
r = Int -> ReadS DayCounterConstructor
readDayCounterConstructorPlain Int
d String
r
[(DayCounterConstructor, String)]
-> [(DayCounterConstructor, String)]
-> [(DayCounterConstructor, String)]
forall a. [a] -> [a] -> [a]
++ Bool -> ReadS DayCounterConstructor -> ReadS DayCounterConstructor
forall a. Bool -> ReadS a -> ReadS a
readParen (Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
10) (\String
r' ->
[ (Calendar -> DayCounterConstructor
Business252 Calendar
c, String
s1)
| (String
"Business252", String
s0) <- ReadS String
lex String
r'
, (CalendarConstructor
p, String
s1) <- Int -> ReadS CalendarConstructor
forall a. Read a => Int -> ReadS a
readsPrec Int
11 String
s0, let c :: Calendar
c = CalendarConstructor -> Calendar
unsafeCalendar CalendarConstructor
p
]) String
r
foreign import ccall safe "QuantLib/Time/Schedule.chs.h qlDayCounter"
qlDayCounter'_ :: (C2HSImp.CInt -> (C2HSImp.CInt -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO (C2HSImp.Ptr (CDayCounter))))))
foreign import ccall safe "QuantLib/Time/Schedule.chs.h qlDayCounterBusiness252"
qlDayCounterBusiness252'_ :: ((C2HSImp.Ptr (CCalendar)) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO (C2HSImp.Ptr (CDayCounter)))))
foreign import ccall safe "QuantLib/Time/Schedule.chs.h qlDayCounterActualActualBond"
actualActualBond''_ :: ((C2HSImp.Ptr (CSchedule)) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO (C2HSImp.Ptr (CDayCounter)))))
foreign import ccall safe "QuantLib/Time/Schedule.chs.h qlDayCounterActualActualISMA"
actualActualISMA''_ :: ((C2HSImp.Ptr (CSchedule)) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO (C2HSImp.Ptr (CDayCounter)))))
foreign import ccall safe "QuantLib/Time/Schedule.chs.h qlDayCounterDayCount"
days'_ :: ((C2HSImp.Ptr (CDayCounter)) -> (C2HSImp.CInt -> (C2HSImp.CInt -> (IO C2HSImp.CInt))))
foreign import ccall safe "QuantLib/Time/Schedule.chs.h qlDayCounterYearFraction"
yearFraction'_ :: ((C2HSImp.Ptr (CDayCounter)) -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CDouble)))))))
foreign import ccall safe "QuantLib/Time/Schedule.chs.h qlSchedule"
schedule'_ :: (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> ((C2HSImp.Ptr (CCalendar)) -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO (C2HSImp.Ptr (CSchedule)))))))))))))))
foreign import ccall safe "QuantLib/Time/Schedule.chs.h qlSchedule1"
fromDates'_ :: (C2HSImp.CUInt -> ((C2HSImp.Ptr C2HSImp.CInt) -> ((C2HSImp.Ptr (CCalendar)) -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO (C2HSImp.Ptr (CSchedule)))))))))))))
foreign import ccall safe "QuantLib/Time/Schedule.chs.h qlScheduleUntil"
until'_ :: ((C2HSImp.Ptr (CSchedule)) -> (C2HSImp.CInt -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO (C2HSImp.Ptr (CSchedule))))))
foreign import ccall safe "QuantLib/Time/Schedule.chs.h qlScheduleDates"
dates'_ :: ((C2HSImp.Ptr (CSchedule)) -> ((C2HSImp.Ptr C2HSImp.CUInt) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CInt)) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO ())))))
foreign import ccall safe "QuantLib/Time/Schedule.chs.h qlPeriodFromFrequency1"
fromFrequency'_ :: (C2HSImp.CInt -> ((C2HSImp.Ptr C2HSImp.CInt) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CInt))))
foreign import ccall safe "QuantLib/Time/Schedule.chs.h qlPeriodToFrequency1"
toFrequency'_ :: (C2HSImp.CInt -> (C2HSImp.CInt -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CInt))))
foreign import ccall safe "QuantLib/Time/Schedule.chs.h qlPeriodParserParse1"
parse'_ :: ((C2HSImp.Ptr C2HSImp.CChar) -> ((C2HSImp.Ptr C2HSImp.CInt) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CInt))))
foreign import ccall safe "QuantLib/Time/Schedule.chs.h qlPeriodAdd1"
addPeriods'_ :: (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> ((C2HSImp.Ptr C2HSImp.CInt) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CInt)))))))
foreign import ccall safe "QuantLib/Time/Schedule.chs.h qlPeriodDivide1"
divide'_ :: (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> ((C2HSImp.Ptr C2HSImp.CInt) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CInt))))))
foreign import ccall safe "QuantLib/Time/Schedule.chs.h qlPeriodsLT1"
lessThan'_ :: (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CInt))))))
foreign import ccall safe "QuantLib/Time/Schedule.chs.h qlPeriodNormalize1"
normalize'_ :: (C2HSImp.CInt -> (C2HSImp.CInt -> ((C2HSImp.Ptr C2HSImp.CInt) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CInt)))))