-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime_series.go
More file actions
84 lines (78 loc) · 1.94 KB
/
time_series.go
File metadata and controls
84 lines (78 loc) · 1.94 KB
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
package talib
import (
"sync"
"time"
"github.com/shopspring/decimal"
)
type TimeSeries struct {
data []Bar
period uint64
capacity uint64
threshold uint64
indicators sync.Map
mu sync.RWMutex
}
func NewSeries(period time.Duration) *TimeSeries {
return NewLimitSeries(period, 500, 625)
}
func NewLimitSeries(period time.Duration, capacity, threshold uint64) *TimeSeries {
if threshold < capacity {
panic("threshold must be greater than mcapacity")
}
return &TimeSeries{
capacity: capacity,
threshold: threshold,
data: make([]Bar, 0, capacity),
period: uint64(period.Seconds()),
}
}
func (series *TimeSeries) LoadOrStore(id string, supplier func() Indicator) Indicator {
if value, ok := series.indicators.Load(id); ok {
return value.(Indicator)
}
indicator := supplier()
series.indicators.Store(id, indicator)
return indicator
}
func (series *TimeSeries) Add(bar Bar) {
series.add(bar)
}
func (series *TimeSeries) add(bar Bar) {
if uint64(bar.Period().Seconds()) != series.period {
return
}
series.mu.Lock()
defer series.mu.Unlock()
series.data = append(series.data, bar)
}
func (series *TimeSeries) Size() uint64 {
series.mu.RLock()
defer series.mu.RUnlock()
return uint64(len(series.data))
}
func (series *TimeSeries) Offset(i uint64) Bar {
series.mu.RLock()
defer series.mu.RUnlock()
cursor := series.Cursor(i)
return series.data[cursor]
}
func (series *TimeSeries) Cursor(i uint64) uint64 {
series.mu.RLock()
defer series.mu.RUnlock()
return series.Size() - i - 1
}
func (series *TimeSeries) NewIndicator(id string, method func(Bar) decimal.Decimal) Indicator {
return series.LoadOrStore(id, func() Indicator {
indicator := NewCacheFrom(series, func(i Indicator, offset uint64) decimal.Decimal {
if i.OutOfBounds(offset) {
return decimal.Zero
}
bar := i.BarSeries().Offset(offset)
if bar == nil {
return decimal.Zero
}
return method(bar)
})
return indicator
})
}