forked from rodrigo-brito/ninjabot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
series.go
60 lines (49 loc) · 1.59 KB
/
series.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
package model
import (
"strconv"
"strings"
"golang.org/x/exp/constraints"
)
// Series is a time series of values
type Series[T constraints.Ordered] []T
// Values returns the values of the series
func (s Series[T]) Values() []T {
return s
}
// Lenght returns the number of values in the series
func (s Series[T]) Lenght() int {
return len(s)
}
// Last returns the last value of the series given a past index position
func (s Series[T]) Last(position int) T {
return s[len(s)-1-position]
}
// LastValues returns the last values of the series given a size
func (s Series[T]) LastValues(size int) []T {
if l := len(s); l > size {
return s[l-size:]
}
return s
}
// Crossover returns true if the last value of the series is greater than the last value of the reference series
func (s Series[T]) Crossover(ref Series[T]) bool {
return s.Last(0) > ref.Last(0) && s.Last(1) <= ref.Last(1)
}
// Crossunder returns true if the last value of the series is less than the last value of the reference series
func (s Series[T]) Crossunder(ref Series[T]) bool {
return s.Last(0) <= ref.Last(0) && s.Last(1) > ref.Last(1)
}
// Cross returns true if the last value of the series is greater than the last value of the
// reference series or less than the last value of the reference series
func (s Series[T]) Cross(ref Series[T]) bool {
return s.Crossover(ref) || s.Crossunder(ref)
}
// NumDecPlaces returns the number of decimal places of a float64
func NumDecPlaces(v float64) int64 {
s := strconv.FormatFloat(v, 'f', -1, 64)
i := strings.IndexByte(s, '.')
if i > -1 {
return int64(len(s) - i - 1)
}
return 0
}