Finance-Treasury 0.1 → 0.1.1
raw patch · 3 files changed
+72/−64 lines, 3 files
Files
- Finance-Treasury.cabal +1/−1
- Finance/Treasury.hs +66/−63
- tests/test.sh +5/−0
Finance-Treasury.cabal view
@@ -1,5 +1,5 @@ Name: Finance-Treasury-Version: 0.1+Version: 0.1.1 Description: Obtain Treasury yield curve data from Department of Treasury website Synopsis: Obtain Treasury yield curve data Category: Web
Finance/Treasury.hs view
@@ -1,67 +1,64 @@-{- | -Module : Finance.Treasury-Copyright : Copyright (c) 2008, Steve lihn <stevelihn@gmail.com>-License : BSD3-Maintainer : Steve lihn <stevelihn@gmail.com>-Stability : experimental-Tested with: GHC 6.6.1--Finance.Treasury is a module to obtain yield curve data from Department-of Treasury website.--Error reporting is somewhat of a mixed model in this module. More-improvement is desired in future releases.--Here is a small complete program illustrating the use of this module:--@-- module Main where- import Finance.Treasury- import qualified Data.Map as M- import Data.List (sort)- import Text.Printf- main = do- putStrLn "**************************************"- m <- getYieldCurveThisMonth - putStrLn "*** pretty print 10y rates for all dates"- prettyYieldCurve m (Just "10y")- --- d <- getLatestYieldCurve - putStrLn "*** pretty print the latest daily yield curve"- prettyYieldCurve (M.fromList [d]) Nothing- --- putStrLn "*** show some yield curves of past year"- h <- getYieldCurveHist- prettyYieldCurve (minmax h) (Just "10y")- mapM_ prt yrs - putStrLn "**************************************"- return ()- where minmax h = M.fromList [ M.findMin h, M.findMax h ]- yrs = reverse [ 1992..2007 ]- prt :: Int -> IO ()- prt yr = do putStrLn $ "*** show some yield curves of "++(show yr)- yc <- getYieldCurveYyyy yr- prettyYieldCurve (minmax yc) (Just "10y")- return ()---@---}-{- - License info:- The license is a simple BSD3-style license. --}+-- | +-- Module : Finance.Treasury+-- Copyright : Copyright (c) 2008, Steve lihn <stevelihn@gmail.com>+-- License : BSD3+-- Maintainer : Steve lihn <stevelihn@gmail.com>+-- Stability : experimental+-- Tested with: GHC 6.6.1+--+-- Finance.Treasury is a module to obtain yield curve data from Department+-- of Treasury website.+--+-- Error reporting is somewhat of a mixed model in this module. More+-- improvement is desired in future releases.+--+-- Here is a small complete program illustrating the use of this module:+--+-- > module Main where+-- > import Finance.Treasury+-- > import qualified Data.Map as M+-- > import Data.List (sort)+-- > import Text.Printf+-- > main = do+-- > putStrLn "**************************************"+-- > m <- getYieldCurveThisMonth +-- > putStrLn "*** pretty print 10y rates for all dates"+-- > prettyYieldCurve m (Just "10y")+-- > --+-- > d <- getLatestYieldCurve +-- > putStrLn "*** pretty print the latest daily yield curve"+-- > prettyYieldCurve (M.fromList [d]) Nothing+-- > --+-- > putStrLn "*** show some yield curves of past year"+-- > h <- getYieldCurveHist+-- > prettyYieldCurve (minmax h) (Just "10y")+-- > mapM_ prt yrs +-- > putStrLn "**************************************"+-- > return ()+-- > where minmax h = M.fromList [ M.findMin h, M.findMax h ]+-- > yrs = reverse [ 1992..2007 ]+-- > prt :: Int -> IO ()+-- > prt yr = do putStrLn $ "*** show some yield curves of "++(show yr)+-- > yc <- getYieldCurveYyyy yr+-- > prettyYieldCurve (minmax yc) (Just "10y")+-- > return ()+--+-- License info: The license is a simple BSD3-style license. +-- module Finance.Treasury ( + -- * Yield curve storage+ YieldCurveMap, DailyYieldCurve, DailyYieldCurveList,+ -- ** Yield curve maturity yieldCurveHash, + -- * Fetching yield curve data getLatestYieldCurve, getYieldCurveThisMonth, getYieldCurveHist, getYieldCurveYyyy,+ -- * Printing yield curve data prettyYieldCurve ) where @@ -95,10 +92,10 @@ else error "yieldYyyyURL: "++s1++" is not YYYYY" -{- yieldCurveHash translates maturity from XML names to abbreviations.- E.g. "BC_1MONTH" becomes "1m".- List of all maturities: 1m 3m 6m 1y 2y 3y 5y 7y 10y 20y 30y --}+-- | translates maturity from XML names to abbreviations.+-- E.g. BC_1MONTH becomes 1m.+-- List of all maturities: 1m 3m 6m 1y 2y 3y 5y 7y 10y 20y 30y.+-- However, 30y data may be lacking for some years when the bond was not in circulation. yieldCurveHash :: M.Map String String yieldCurveHash = M.fromList [ ( "BC_1MONTH", "1m" ),@@ -113,30 +110,36 @@ ( "BC_20YEAR", "20y" ), ( "BC_30YEAR", "30y" ) ] -+-- | a Map storing the assoc array of maturity to interest rate (in percent) type DailyYieldCurve = M.Map String Float+-- | a List storing the tuple of maturity and interest rate (in percent) type DailyYieldCurveList = [ (String, Float) ]+-- | a Map storing all the daily yield curves type YieldCurveMap = M.Map C.Day DailyYieldCurve +-- | fetch the latest daily yield curve from the monthly data. getLatestYieldCurve :: IO (C.Day, DailyYieldCurve) getLatestYieldCurve = do ycs <- getYieldCurveThisMonth return $ M.findMax ycs +-- | fetch the latest monthly data. -- There is no reason to fail, so it is an error if there is no XML returned getYieldCurveThisMonth :: IO YieldCurveMap getYieldCurveThisMonth = do jstr <- fetchXML yieldURL parseRawWrapper parseRawXML1 jstr +-- | fetch the latest yearly data (excluding current month). -- There is no reason to fail, so it is an error if there is no XML returned getYieldCurveHist :: IO YieldCurveMap getYieldCurveHist = do jstr <- fetchXML yieldHistURL parseRawWrapper parseRawXML2 jstr --- If YYYY is not in range, this call may fail--- Otherwise, there is no reason to fail+-- | fetch the historical yearly data (excluding current year).+-- If YYYY is not in range (since 1992), this call may fail.+-- Otherwise, there is no reason to fail. getYieldCurveYyyy :: Int -> IO YieldCurveMap getYieldCurveYyyy yr = do s <- fetchXML $ yieldYyyyURL yr@@ -178,7 +181,7 @@ justhead node (s:sx) = s justhead node [] = error $ "parse error at \n"++(XP.formatXmlTree node) --- a quick test function+-- | print yield curve data in a csv format for storage or testing. prettyYieldCurve :: YieldCurveMap -> Maybe String -> IO () prettyYieldCurve ycm mmat = do let ds = sort $ M.keys ycm
tests/test.sh view
@@ -18,9 +18,14 @@ cd tests/ ./treas }+_haddock ()+{+ haddock -h -o ~/www/haskell/treas-docs Finance/Treasury.hs+} [ -f "./treas" ] && rm ./treas cd ../+ _haddock Setup.lhs configure --user && \ Setup.lhs build && \ cd tests && \