-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbol.go
179 lines (152 loc) · 4.77 KB
/
symbol.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package coinfactory
import (
"sync"
"time"
"github.com/VividCortex/ewma"
"github.com/sinisterminister/coinfactory/pkg/binance"
log "github.com/sirupsen/logrus"
)
type Symbol struct {
binance.SymbolData
ticker binance.SymbolTickerData
trixKlineCache []binance.Kline
trixKlineCacheInterval string
klineStreamStopChannel chan bool
tickerStreamStopChan <-chan bool
tickerMutex *sync.RWMutex
}
func newSymbol(data binance.SymbolData, stopChan <-chan bool) *Symbol {
symbol := &Symbol{
SymbolData: data,
tickerMutex: &sync.RWMutex{},
tickerStreamStopChan: stopChan,
}
go symbol.tickerUpdater()
return symbol
}
func (s *Symbol) GetTickerStream(stopChan <-chan bool) <-chan binance.SymbolTickerData {
return getTickerStreamService().GetTickerStream(stopChan, s.Symbol)
}
func (s *Symbol) GetKLineStream(stopChan <-chan bool, interval string) <-chan binance.KlineStreamData {
return getKlineStreamService().GetKlineStream(stopChan, s.Symbol, interval)
}
func (s *Symbol) GetKLines(interval string, start time.Time, end time.Time, limit int) ([]binance.Kline, error) {
req := binance.KlineRequest{
Symbol: s.Symbol,
Interval: interval,
}
if !start.IsZero() {
req.StartTime = start.UnixNano() / int64(time.Millisecond)
}
if !end.IsZero() {
req.EndTime = end.UnixNano() / int64(time.Millisecond)
}
if limit != 0 {
req.Limit = limit
}
return binance.GetKlines(req)
}
func (s *Symbol) updateTrixKlineCache(interval string, periods float64) (err error) {
if s.trixKlineCache == nil || s.trixKlineCacheInterval != interval {
s.trixKlineCache = make([]binance.Kline, 0)
s.trixKlineCacheInterval = interval
if s.klineStreamStopChannel != nil {
close(s.klineStreamStopChannel)
}
s.klineStreamStopChannel = make(chan bool)
// Get the klines to work with
s.trixKlineCache, err = s.GetKLines(interval, time.Time{}, time.Time{}, int(periods*2*3+2))
if err != nil {
return err
}
// Start a stream to get updates
go func() {
stream := s.GetKLineStream(s.klineStreamStopChannel, interval)
for {
select {
case data := <-stream:
if data.Closed {
// Build a Kline
kline := binance.Kline{
OpenTime: time.Unix(0, data.OpenTime*1000000),
CloseTime: time.Unix(0, data.CloseTime*1000000),
OpenPrice: data.OpenPrice,
ClosePrice: data.ClosePrice,
LowPrice: data.LowPrice,
HighPrice: data.HighPrice,
BaseVolume: data.BaseVolume,
QuoteVolume: data.QuoteVolume,
TradeCount: data.TradeCount,
TakerAssetVolume: data.TakerAssetVolume,
TakerQuoteVolume: data.TakerQuoteVolume,
}
s.trixKlineCache = append(s.trixKlineCache[1:], kline)
log.WithField("kline", kline).Debug("Updated Kline cache")
}
case <-s.klineStreamStopChannel:
return
}
}
}()
}
return err
}
func (s *Symbol) GetCurrentTrixIndicator(interval string, periods float64) (ma float64, oscillator float64, err error) {
singleSmoothedValues := []float64{}
doubleSmoothedValues := []float64{}
tripleSmoothedValues := []float64{}
singleSmoothed := ewma.NewMovingAverage(periods)
doubleSmoothed := ewma.NewMovingAverage(periods)
tripleSmoothed := ewma.NewMovingAverage(periods)
// Update the kline cache
s.updateTrixKlineCache(interval, periods)
// Calculate the single smoothed moving average values
for _, kline := range s.trixKlineCache {
price, _ := kline.ClosePrice.Float64()
singleSmoothed.Add(price)
if singleSmoothed.Value() != 0.0 {
singleSmoothedValues = append(singleSmoothedValues, singleSmoothed.Value())
}
}
// Calculate the double smoothed moving average values
for _, s := range singleSmoothedValues {
doubleSmoothed.Add(s)
if doubleSmoothed.Value() != 0.0 {
doubleSmoothedValues = append(doubleSmoothedValues, doubleSmoothed.Value())
}
}
// Calculate the triple smoothed moving average values
for _, s := range doubleSmoothedValues {
tripleSmoothed.Add(s)
if tripleSmoothed.Value() != 0.0 {
tripleSmoothedValues = append(tripleSmoothedValues, tripleSmoothed.Value())
}
}
ma = tripleSmoothed.Value()
originalValue := tripleSmoothedValues[len(tripleSmoothedValues)-2]
oscillator = (ma - originalValue) / originalValue
return ma, oscillator, err
}
func (s *Symbol) GetTicker() binance.SymbolTickerData {
s.tickerMutex.RLock()
defer s.tickerMutex.RUnlock()
return s.ticker
}
func (s *Symbol) tickerUpdater() {
tickerStream := getTickerStreamService().GetTickerStream(s.tickerStreamStopChan, s.Symbol)
for {
select {
case <-s.tickerStreamStopChan:
return
default:
}
select {
case <-s.tickerStreamStopChan:
return
case data := <-tickerStream:
s.tickerMutex.Lock()
s.ticker = data
s.tickerMutex.Unlock()
}
}
}