-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmfi.go
96 lines (83 loc) · 2.8 KB
/
mfi.go
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
84
85
86
87
88
89
90
91
92
93
94
95
96
package tart
// The Money Flow Index (MFI) is an oscillator that uses
// both price and volume to measure buying and selling
// pressure. Created by Gene Quong and Avrum Soudack,
// MFI is also known as volume-weighted RSI. MFI starts
// with the typical price for each period. Money flow is
// positive when the typical price rises (buying pressure)
// and negative when the typical price declines (selling pressure).
// A ratio of positive and negative money flow is then plugged
// into an RSI formula to create an oscillator that moves
// between zero and one hundred. As a momentum oscillator
// tied to volume, MFI is best suited to identify reversals
// and price extremes with a variety of signals.
// https://school.stockcharts.com/doku.php?id=technical_indicators:money_flow_index_mfi
// https://www.investopedia.com/terms/m/mfi.asp
// https://www.fidelity.com/learning-center/trading-investing/technical-analysis/technical-indicator-guide/MFI
type Mfi struct {
n int64
positive *Sum
negative *Sum
prevTp float64
sz int64
}
func NewMfi(n int64) *Mfi {
return &Mfi{
n: n,
positive: NewSum(n),
negative: NewSum(n),
prevTp: 0,
sz: 0,
}
}
func (m *Mfi) Update(h, l, c, v float64) float64 {
m.sz++
tp := (h + l + c) / 3.0
prevTp := m.prevTp
m.prevTp = tp
if m.sz == 1 {
return 0
}
var pSum, nSum float64
if tp > prevTp {
pSum = m.positive.Update(tp * v)
nSum = m.negative.Update(0)
} else {
pSum = m.positive.Update(0)
nSum = m.negative.Update(tp * v)
}
if m.sz <= m.n {
return 0
}
sum := pSum + nSum
return pSum / sum * 100.0
}
func (m *Mfi) InitPeriod() int64 {
return m.n
}
func (m *Mfi) Valid() bool {
return m.sz > m.InitPeriod()
}
// The Money Flow Index (MFI) is an oscillator that uses
// both price and volume to measure buying and selling
// pressure. Created by Gene Quong and Avrum Soudack,
// MFI is also known as volume-weighted RSI. MFI starts
// with the typical price for each period. Money flow is
// positive when the typical price rises (buying pressure)
// and negative when the typical price declines (selling pressure).
// A ratio of positive and negative money flow is then plugged
// into an RSI formula to create an oscillator that moves
// between zero and one hundred. As a momentum oscillator
// tied to volume, MFI is best suited to identify reversals
// and price extremes with a variety of signals.
// https://school.stockcharts.com/doku.php?id=technical_indicators:money_flow_index_mfi
// https://www.investopedia.com/terms/m/mfi.asp
// https://www.fidelity.com/learning-center/trading-investing/technical-analysis/technical-indicator-guide/MFI
func MfiArr(h, l, c, v []float64, n int64) []float64 {
out := make([]float64, len(c))
m := NewMfi(n)
for i := 0; i < len(c); i++ {
out[i] = m.Update(h[i], l[i], c[i], v[i])
}
return out
}