-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrend.fs
83 lines (68 loc) · 2.46 KB
/
Trend.fs
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
module GeldMachine.Indicator.Trend
open GeldMachine.Data
type Trend =
| Bearish
| Sideways
| Bullish
type Strength =
| Ambivalent
| Suspect
| Confirmed
type SwingpointTest =
| SWPTestLow
| SWPTestHigh
let private transition = function
| Bearish, SWPTestLow -> Bearish
| Bearish, SWPTestHigh -> Sideways
| Sideways, SWPTestLow -> Bearish
| Sideways, SWPTestHigh -> Bullish
| Bullish, SWPTestLow -> Sideways
| Bullish, SWPTestHigh -> Bullish
let strengthTest (p:OHLC) (swp:OHLC) =
if p.Volume > swp.Volume then
Confirmed
else
Suspect
let smallerThan a b = a < b
let greaterThan a b = a > b
let rec swingpointTest cmp spOp test p (sps:list<OHLC>) (trend,strength) =
match sps with
| h :: t ->
if cmp p.Close (spOp h) then
let newTrend = transition (trend, test)
let newStrength = strengthTest p h
swingpointTest cmp spOp test p t (newTrend,newStrength)
else
(trend,strength), sps
| _ -> (trend,strength), sps
let swingpointLowTest = swingpointTest smallerThan low SWPTestLow
let swingpointHighTest = swingpointTest greaterThan high SWPTestHigh
let updateInUseSWPs (p:OHLC) (all:list<OHLC>) inUse =
match all with
| (h::t) ->
if System.DateTime.Equals(h.Date, p.Date) then
t, (h::inUse)
else
all, inUse
| _ -> all, inUse
//TODO: Add ambivalent sideways transition: no swingpoint tests resulting in (2 lower SPH & higher SPL) or (2 higher SPH & lower SPL) -> See aapl chart
let getTrends (data:list<OHLC>) (spls:list<OHLC>) (sphs:list<OHLC>) =
let mutable allSPLs = List.sortBy (fun e -> e.Date) spls
let mutable allSPHs = List.sortBy (fun e -> e.Date) sphs
let mutable useSPLs = []
let mutable useSPHs = []
let mutable trend = Sideways, Ambivalent
let mutable trendChanges = [(List.head data, trend)]
for p in data do
let (trend' , useSPLs') = swingpointLowTest p useSPLs trend
let (trend'', useSPHs') = swingpointHighTest p useSPHs trend'
if trend <> trend'' then
trendChanges <- (p,trend'') :: trendChanges
let allSPLs', useSPLs'' = updateInUseSWPs p allSPLs useSPLs'
let allSPHs', useSPHs'' = updateInUseSWPs p allSPHs useSPHs'
trend <- trend''
allSPLs <- allSPLs'
allSPHs <- allSPHs'
useSPLs <- useSPLs''
useSPHs <- useSPHs''
trendChanges