-
Notifications
You must be signed in to change notification settings - Fork 0
/
candlesticks.go
101 lines (90 loc) · 2.3 KB
/
candlesticks.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
package shingo
import (
"fmt"
"sync"
)
type CandlestickMeta interface {
Total() int
Interval() Interval
ItemAtIndex(idx int) *Candlestick
}
type CandlestickIndicators interface {
AppendEMA(arg IndicatorInputArg) error
AppendSMA(arg IndicatorInputArg) error
AppendMACD(arg IndicatorInputArg) error
AppendIchimokuCloud(arg IndicatorInputArg) error
}
type Candlesticks struct {
CandlestickMeta
CandlestickIndicators
mux sync.Mutex
interval Interval
items []*Candlestick
maxCandles int
}
func (cs *Candlesticks) AppendCandlestick(c *Candlestick) error {
cs.items = append(cs.items, c)
if len(cs.items) > cs.maxCandles {
cs.items = cs.items[1:]
}
return nil
}
func NewCandlesticks(i Interval, maxCandles int) (*Candlesticks, error) {
return &Candlesticks{
maxCandles: maxCandles,
interval: i,
items: make([]*Candlestick, 0),
}, nil
}
// Total returns total candlesticks
func (cs *Candlesticks) Total() int {
return len(cs.items)
}
// ItemAtIndex returns the item at specific index
func (cs *Candlesticks) ItemAtIndex(idx int) *Candlestick {
if idx < 0 {
return nil
}
if len(cs.items) > idx {
return cs.items[idx]
}
return nil
}
// Interval returns currently set interval for the series of candlesticks
func (cs *Candlesticks) Interval() Interval {
return cs.interval
}
// GenerateIndicator generates requested signals on that series of candlesticks
func (cs *Candlesticks) GenerateIndicator(i IndicatorType, arg IndicatorInputArg) error {
switch i {
case IndicatorTypeSMA:
return cs.AppendSMA(arg)
case IndicatorTypeEMA:
return cs.AppendEMA(arg)
case IndicatorTypeMACD:
return cs.AppendMACD(arg)
case IndicatorTypeIchimokuCloud:
return cs.AppendIchimokuCloud(arg)
case IndicatorTypeATR:
return cs.AppendATR(arg)
case IndicatorTypeSuperTrend:
return cs.AppendSuperTrend(arg)
case IndicatorTypeHeikinAshi:
return cs.AppendHeikinAshi(arg)
case IndicatorTypeStdDev:
return cs.AppendStdDev(arg)
case IndicatorTypeHighest:
return cs.AppendHighest(arg)
case IndicatorTypeLowest:
return cs.AppendLowest(arg)
}
return fmt.Errorf("Error unsupported indicator type %+v", i)
}
// GetLastItem returns the candlestick that was most recently added
func (cs *Candlesticks) GetLastItem() *Candlestick {
t := cs.Total()
if t == 0 {
return nil
}
return cs.items[t-1]
}