-
Notifications
You must be signed in to change notification settings - Fork 142
/
timeseries.go
47 lines (37 loc) · 1.12 KB
/
timeseries.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
package techan
import (
"fmt"
)
// TimeSeries represents an array of candles
type TimeSeries struct {
Candles []*Candle
}
// NewTimeSeries returns a new, empty, TimeSeries
func NewTimeSeries() (t *TimeSeries) {
t = new(TimeSeries)
t.Candles = make([]*Candle, 0)
return t
}
// AddCandle adds the given candle to this TimeSeries if it is not nil and after the last candle in this timeseries.
// If the candle is added, AddCandle will return true, otherwise it will return false.
func (ts *TimeSeries) AddCandle(candle *Candle) bool {
if candle == nil {
panic(fmt.Errorf("error adding Candle: candle cannot be nil"))
}
if ts.LastCandle() == nil || candle.Period.Since(ts.LastCandle().Period) >= 0 {
ts.Candles = append(ts.Candles, candle)
return true
}
return false
}
// LastCandle will return the lastCandle in this series, or nil if this series is empty
func (ts *TimeSeries) LastCandle() *Candle {
if len(ts.Candles) > 0 {
return ts.Candles[len(ts.Candles)-1]
}
return nil
}
// LastIndex will return the index of the last candle in this series
func (ts *TimeSeries) LastIndex() int {
return len(ts.Candles) - 1
}