From a stock-screen to a Haskell data type

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
module Analytics.Trading.Data.Row where

import Control.Monad
import Data.Time

import Control.Scan.CSV                          -- http://lpaste.net/109651
import Data.Monetary.USD                         -- http://lpaste.net/109653

-- represents a Row of data from a (stock) security screen, and gives an
-- easy way to read it in from a file as a Read instance.

data Row = Row { date :: Day, open :: USD, high :: USD, low :: USD,
                 close :: USD, volume :: Integer, adjClose :: USD }
     deriving Show

instance Read Row where
     readsPrec _ line = let (date:open:high:low:close:vol:adj:_) = csv line
                        in  [(Row (read date) (mknMoney open) (mknMoney high)
                                  (mknMoney low) (mknMoney close) (read vol)
                                  (mknMoney adj), "")]

readRows :: FilePath -> IO [Row]
readRows = liftM (ans . tail . lines) . readFile 
    where ans = foldr (\ row accum -> read row : accum) []

type Symbol = String

instance Raw Symbol where rep = id