-- Reads SP 500 stock data in the format provided by: -- http://biz.swcp.com/stocks/ -- and creates time series for each stock. -- There is one line per stock per day. The fields are: -- Date Ticker Open High Low Close Volume -- They are separated by commas, but use a text editor to -- replace commas by spaces before using this program. -- The stocks are sorted by Ticker, and within a stock -- by Date. This program uses this fact in processing. -- by Scot Drysdale on 10/14/07 module CovertStocks where import qualified Data.Map as M main = do contents <- readFile "spmohstSP.txt" let lineList = lines contents let wordsList = map words lineList let pairsList = map (extractDataPair 5) wordsList -- This call creates a map with stocks as keys. Note that -- because foldr associates right the order ends up by date. let stockMap = foldr (insertWithPair (++)) M.empty pairsList writeFile "stockData.txt" (show (M.toList stockMap)) -- Creates a list of ticker name and another quantity (column -- selected as described above). extractDataPair :: Int -> [String] -> (String, [Float]) extractDataPair pos ln = (ln !! 1, [read (ln !! pos)]) -- Allows input of a (k,v) pair instead of k v. insertWithPair f (k, v) = M.insertWith f k v