-
-
Notifications
You must be signed in to change notification settings - Fork 278
/
momentum.go
114 lines (92 loc) · 2.47 KB
/
momentum.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package factorzoo
import (
"fmt"
"time"
"github.com/c9s/bbgo/pkg/datatype/floats"
"github.com/c9s/bbgo/pkg/indicator"
"github.com/c9s/bbgo/pkg/types"
)
// gap jump momentum
// if the gap between current open price and previous close price gets larger
// meaning an opening price jump was happened, the larger momentum we get is our alpha, MOM
//go:generate callbackgen -type MOM
type MOM struct {
types.SeriesBase
types.IntervalWindow
// Values
Values floats.Slice
LastValue float64
opens *types.Queue
closes *types.Queue
EndTime time.Time
UpdateCallbacks []func(val float64)
}
func (inc *MOM) Index(i int) float64 {
if inc.Values == nil {
return 0
}
return inc.Values.Index(i)
}
func (inc *MOM) Last() float64 {
if inc.Values.Length() == 0 {
return 0
}
return inc.Values.Last()
}
func (inc *MOM) Length() int {
if inc.Values == nil {
return 0
}
return inc.Values.Length()
}
//var _ types.SeriesExtend = &MOM{}
func (inc *MOM) Update(open, close float64) {
if inc.SeriesBase.Series == nil {
inc.SeriesBase.Series = inc
inc.opens = types.NewQueue(inc.Window)
inc.closes = types.NewQueue(inc.Window + 1)
}
inc.opens.Update(open)
inc.closes.Update(close)
if inc.opens.Length() >= inc.Window && inc.closes.Length() >= inc.Window {
gap := inc.opens.Last()/inc.closes.Index(1) - 1
inc.Values.Push(gap)
}
}
func (inc *MOM) CalculateAndUpdate(allKLines []types.KLine) {
if len(inc.Values) == 0 {
for _, k := range allKLines {
inc.PushK(k)
}
inc.EmitUpdate(inc.Last())
} else {
k := allKLines[len(allKLines)-1]
inc.PushK(k)
inc.EmitUpdate(inc.Last())
}
}
func (inc *MOM) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
if inc.Interval != interval {
return
}
inc.CalculateAndUpdate(window)
}
func (inc *MOM) Bind(updater indicator.KLineWindowUpdater) {
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
}
func (inc *MOM) PushK(k types.KLine) {
if inc.EndTime != zeroTime && k.EndTime.Before(inc.EndTime) {
return
}
inc.Update(k.Open.Float64(), k.Close.Float64())
inc.EndTime = k.EndTime.Time()
inc.EmitUpdate(inc.Last())
}
func calculateMomentum(klines []types.KLine, window int, valA KLineValueMapper, valB KLineValueMapper) (float64, error) {
length := len(klines)
if length == 0 || length < window {
return 0.0, fmt.Errorf("insufficient elements for calculating VOL with window = %d", window)
}
momentum := (1 - valA(klines[length-1])/valB(klines[length-1])) * -1
return momentum, nil
}