stockpairthing.hs (2891B)
1 {-# OPTIONS_GHC -Wall #-} 2 3 import Data.Function 4 import Data.List 5 6 main :: IO () 7 main = print $ minimumBy (compare `on` sumDiffs) portfolios 8 9 -- Data ------------------------------------------------------------------------ 10 11 -- | A list of stocks we're interested in. 12 stocks :: [String] 13 stocks = [ "HUSK", "NALC", "SUNC", "EXXN" 14 , "AAPL", "MSFT", "CISC", "ORAC" 15 , "BHP", "GOLD", "JPM", "BOA" 16 , "GM", "FORD", "VOLK", "KIA" 17 , "CHAR", "MERRY", "KENM", "TOPS" 18 ] 19 20 -- | A list of sectors to match. 21 sectors :: [String] 22 sectors = concatMap (replicate 4) 23 [ "Energy", "Technology", "Finance", "Auto", "STJOHNS"] 24 25 -- | Market betas for each. 26 betas :: [Float] 27 betas = [ 0.56, -0.76, 0.81, 0.34 28 , 0.79, 0.88, 0.98, 0.65 29 , 0.38, -0.89, 0.91, 0.32 30 , -0.05, -0.23, 0.16, 0.65 31 , 0.53, 0.28, -0.06, 0.72 32 ] 33 34 -- | All combinations of unique stock pairs such that the first stock is from 35 -- the Energy sector. 36 combos :: [((String, String), String, Float)] 37 combos = let paired = zip3 stocks sectors betas 38 in [ ((a, d), e, abs (c - f)) | (a, b, c) <- paired 39 , (d, e, f) <- paired 40 , b /= e, b == "Energy" ] 41 42 -- | All possible portfolios. 43 portfolios :: [Portfolio] 44 portfolios = [ (huskPair, nalcPair, suncPair, exxnPair) 45 | huskPair <- pickStock "HUSK" combos 46 , nalcPair <- pickStock "NALC" combos 47 , suncPair <- pickStock "SUNC" combos 48 , exxnPair <- pickStock "EXXN" combos 49 , second huskPair /= second nalcPair 50 , second huskPair /= second suncPair 51 , second huskPair /= second exxnPair 52 , second nalcPair /= second suncPair 53 , second nalcPair /= second exxnPair 54 , second suncPair /= second exxnPair 55 ] 56 57 -- Utilities ------------------------------------------------------------------- 58 59 -- | The sum of beta differences for a portfolio. 60 sumDiffs :: Portfolio -> Float 61 sumDiffs (h, n, s, e) = third h + third n + third s + third e 62 63 -- | Get the first element of a triple. 64 first :: (t, t1, t2) -> t 65 first (a, _, _) = a 66 67 -- | Get the second element of a triple. 68 second :: (t, t1, t2) -> t1 69 second (_, b, _) = b 70 71 -- | Get the third element of a triple. 72 third :: (t, t1, t2) -> t2 73 third (_, _, c) = c 74 75 -- | Retrieve combinations of stock pairs including the given stock. 76 pickStock :: Eq a => a -> [((a, b), t, t1)] -> [((a, b), t, t1)] 77 pickStock s = filter (\x -> (fst . first) x == s) 78 79 -- | Just an abbreviation for the Portfolio type. 80 type Portfolio = (((String, String), String, Float) 81 ,((String, String), String, Float) 82 ,((String, String), String, Float) 83 ,((String, String), String, Float)) 84