-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathobv.go
77 lines (66 loc) · 2.07 KB
/
obv.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
package tart
// On Balance Volume (OBV) measures buying and selling
// pressure as a cumulative indicator, adding volume on
// up days and subtracting it on down days. OBV was
// developed by Joe Granville and introduced in his 1963
// book Granville's New Key to Stock Market Profits.
// It was one of the first indicators to measure positive
// and negative volume flow. Chartists can look for
// divergences between OBV and price to predict price
// movements or use OBV to confirm price trends.
// https://school.stockcharts.com/doku.php?id=technical_indicators:on_balance_volume_obv
// https://www.investopedia.com/terms/o/onbalancevolume.asp
// https://www.fidelity.com/learning-center/trading-investing/technical-analysis/technical-indicator-guide/obv
type Obv struct {
prev float64
obv float64
sz int64
}
func NewObv() *Obv {
return &Obv{
prev: 0,
obv: 0,
sz: 0,
}
}
func (o *Obv) Update(c, v float64) float64 {
o.sz++
prev := o.prev
o.prev = c
if o.sz == 1 {
o.obv = v
return o.obv
}
if c > prev {
o.obv += v
} else if c < prev {
o.obv -= v
}
return o.obv
}
func (o *Obv) InitPeriod() int64 {
return 0
}
func (o *Obv) Valid() bool {
return true
}
// On Balance Volume (OBV) measures buying and selling
// pressure as a cumulative indicator, adding volume on
// up days and subtracting it on down days. OBV was
// developed by Joe Granville and introduced in his 1963
// book Granville's New Key to Stock Market Profits.
// It was one of the first indicators to measure positive
// and negative volume flow. Chartists can look for
// divergences between OBV and price to predict price
// movements or use OBV to confirm price trends.
// https://school.stockcharts.com/doku.php?id=technical_indicators:on_balance_volume_obv
// https://www.investopedia.com/terms/o/onbalancevolume.asp
// https://www.fidelity.com/learning-center/trading-investing/technical-analysis/technical-indicator-guide/obv
func ObvArr(c, v []float64) []float64 {
out := make([]float64, len(c))
o := NewObv()
for i := 0; i < len(c); i++ {
out[i] = o.Update(c[i], v[i])
}
return out
}