A BUY/SELL/HOLD Monoidal Strategy

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
module Analytics.Trading.Advice.SMA where

import Control.Monad
import Data.Time

import Analytics.Trading.Charts.SMA    -- http://lpaste.net/3427480809555099648
import Analytics.Trading.Data.Row      -- http://lpaste.net/109658
import Analytics.Trading.Data.Advice   -- http://lpaste.net/7565330848883408896
import Control.Comonad                 -- http://lpaste.net/107661
import Data.Monetary.USD               -- http://lpaste.net/109653

-- A solution to the advisor problem posted at
-- http://lpaste.net/5255378926062010368

-- incorporating 'zombie'-advice into analyst's buy-sell-hold strategy

-- data SMAStrategy = StarvingZ | HappyZ
   -- deriving (Eq, Ord, Show)

advice :: [SMArow] -> [Advice]

-- we're going to pursue a simple strategy here: when SMA-15 is above
-- SMA-50 we buy, when it's below we (short-(?)) sell.

-- very ... 'jittery,' I know

advice = -- strat rows = rows =>> analyst strat -- comonads to the rescue!

-- old way, pre-comonadic: 

   foldr (\(SMA day close sma15 sma50) accum -> 
      (Infallible day close (if sma15 > sma50 then Buy else Sell)):accum) []
   -- not much in-between 

{-- So, from 

*Main> liftM (advice StarvingZ . smaAnalysis) (readRows "screens/buys/c.csv")

we get:

[Infallible {dayo = 2015-04-07, cls = $51.52, rec = Buy},
 Infallible {dayo = 2015-04-06, cls = $51.61, rec = Buy},
 Infallible {dayo = 2015-04-02, cls = $51.85, rec = Buy},
 Infallible {dayo = 2015-04-01, cls = $51.61, rec = Buy},
 Infallible {dayo = 2015-03-31, cls = $51.52, rec = Buy}, ...]

for every single day!

Standard disclaimer: I'm not an investment guru, so if you lose your shirt,
that's your problem, not mine, as these are educational exercises for your
edification and supreme enlightenment. Your financial well-being is your (sole)
responsibility, and blah-di-blah and more legal mumbo jumbo that nobody reads
anyway, except lawyers on their days off to poke fun at the gaping loopholes
in all these boilerplate disclaimers, anyway, so there it is. --}
32:7: Warning: Redundant bracket
Found:
(Infallible day close (if sma15 > sma50 then Buy else Sell)) :
accum
Why not:
Infallible day close (if sma15 > sma50 then Buy else Sell) : accum