-
Notifications
You must be signed in to change notification settings - Fork 228
/
gdaxfeeder.go
274 lines (259 loc) · 7.88 KB
/
gdaxfeeder.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package main
import (
"context"
"encoding/json"
"fmt"
goio "io"
"math"
"net/http"
"sort"
"time"
gdax "github.com/preichenberger/go-gdax"
"github.com/alpacahq/marketstore/v4/executor"
"github.com/alpacahq/marketstore/v4/planner"
"github.com/alpacahq/marketstore/v4/plugins/bgworker"
"github.com/alpacahq/marketstore/v4/utils"
"github.com/alpacahq/marketstore/v4/utils/io"
"github.com/alpacahq/marketstore/v4/utils/log"
)
type byTime []gdax.HistoricRate
func (a byTime) Len() int { return len(a) }
func (a byTime) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byTime) Less(i, j int) bool { return a[i].Time.Before(a[j].Time) }
// FetcherConfig is the configuration for GdaxFetcher you can define in
// marketstore's config file through bgworker extension.
type FetcherConfig struct {
// list of currency symbols, defults to ["BTC", "ETH", "LTC", "BCH"]
Symbols []string `json:"symbols"`
// time string when to start first time, in "YYYY-MM-DD HH:MM" format
// if it is restarting, the start is the last written data timestamp
// otherwise, it starts from an hour ago by default
QueryStart string `json:"query_start"`
// such as 5Min, 1D. defaults to 1Min
BaseTimeframe string `json:"base_timeframe"`
}
// GdaxFetcher is the main worker instance. It implements bgworker.Run().
type GdaxFetcher struct {
config map[string]interface{}
symbols []string
queryStart time.Time
baseTimeframe *utils.Timeframe
}
func recast(config map[string]interface{}) (*FetcherConfig, error) {
data, _ := json.Marshal(config)
ret := FetcherConfig{}
err := json.Unmarshal(data, &ret)
return &ret, err
}
type gdaxProduct struct {
ID string `json:"id"`
}
func getSymbols() ([]string, error) {
req, err := http.NewRequestWithContext(context.Background(),
"GET", "https://api.pro.coinbase.com/products", http.NoBody)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer func(Body goio.ReadCloser) {
if err2 := Body.Close(); err2 != nil {
log.Error("failed to close body reader for gdax", err2.Error())
}
}(resp.Body)
var products []gdaxProduct
err = json.NewDecoder(resp.Body).Decode(&products)
if err != nil {
return nil, err
}
symbols := make([]string, len(products))
for i, symbol := range products {
symbols[i] = symbol.ID
}
return symbols, nil
}
// NewBgWorker returns the new instance of GdaxFetcher. See FetcherConfig
// for the details of available configurations.
func NewBgWorker(conf map[string]interface{}) (bgworker.BgWorker, error) {
symbols, err := getSymbols()
if err != nil {
return nil, err
}
config, err := recast(conf)
if err != nil {
return nil, fmt.Errorf("failed to cast config: %w", err)
}
if len(config.Symbols) > 0 {
symbols = config.Symbols
}
var queryStart time.Time
if config.QueryStart != "" {
trials := []string{
"2006-01-02 03:04:05",
"2006-01-02T03:04:05",
"2006-01-02 03:04",
"2006-01-02T03:04",
"2006-01-02",
}
for _, layout := range trials {
qs, err := time.Parse(layout, config.QueryStart)
if err == nil {
queryStart = qs.In(utils.InstanceConfig.Timezone)
break
}
}
}
timeframeStr := "1Min"
if config.BaseTimeframe != "" {
timeframeStr = config.BaseTimeframe
}
return &GdaxFetcher{
config: conf,
symbols: symbols,
queryStart: queryStart,
baseTimeframe: utils.NewTimeframe(timeframeStr),
}, nil
}
func findLastTimestamp(tbk *io.TimeBucketKey) time.Time {
cDir := executor.ThisInstance.CatalogDir
query := planner.NewQuery(cDir)
query.AddTargetKey(tbk)
start := time.Unix(0, 0).In(utils.InstanceConfig.Timezone)
end := time.Unix(math.MaxInt64, 0).In(utils.InstanceConfig.Timezone)
query.SetRange(start, end)
query.SetRowLimit(io.LAST, 1)
parsed, err := query.Parse()
if err != nil {
return time.Time{}
}
reader, err := executor.NewReader(parsed)
if err != nil {
log.Error(fmt.Sprintf("create query reader for tbk=%s", tbk))
return time.Time{}
}
csm, err := reader.Read()
if err != nil {
log.Error(fmt.Sprintf("failed to read a query for %s", tbk))
return time.Time{}
}
cs := csm[*tbk]
if cs == nil || cs.Len() == 0 {
return time.Time{}
}
ts, err := cs.GetTime()
if err != nil {
log.Error(fmt.Sprintf("failed to get time from a query for %s", tbk))
return time.Time{}
}
return ts[0]
}
// Run () runs forever to get public historical rate for each configured symbol,
// and writes in marketstore data format. In case any error including rate limit
// is returned from GDAX, it waits for a minute.
func (gd *GdaxFetcher) Run() {
symbols := gd.symbols
client := gdax.NewClient("", "", "")
timeStart := time.Time{}
for _, symbol := range symbols {
symbolDir := fmt.Sprintf("gdax_%s", symbol)
tbk := io.NewTimeBucketKey(symbolDir + "/" + gd.baseTimeframe.String + "/OHLCV")
lastTimestamp := findLastTimestamp(tbk)
log.Info("lastTimestamp for %s = %v", symbolDir, lastTimestamp)
if timeStart.IsZero() || (!lastTimestamp.IsZero() && lastTimestamp.Before(timeStart)) {
timeStart = lastTimestamp
}
}
if timeStart.IsZero() {
if !gd.queryStart.IsZero() {
timeStart = gd.queryStart
} else {
timeStart = time.Now().UTC().Add(-time.Hour)
}
}
for {
const getHistoricRatesChunksize = 300
timeEnd := timeStart.Add(gd.baseTimeframe.Duration * getHistoricRatesChunksize)
lastTime := timeStart
for _, symbol := range symbols {
params := gdax.GetHistoricRatesParams{
Start: timeStart,
End: timeEnd,
Granularity: int(gd.baseTimeframe.Duration.Seconds()),
}
log.Info("Requesting %s %v - %v", symbol, timeStart, timeEnd)
rates, err := client.GetHistoricRates(symbol, params)
if err != nil {
log.Info("Response error: %v", err)
// including rate limit case
time.Sleep(time.Second)
continue
}
if len(rates) == 0 {
log.Info("len(rates) == 0")
continue
}
epoch := make([]int64, 0)
open := make([]float64, 0)
high := make([]float64, 0)
low := make([]float64, 0)
clos := make([]float64, 0)
volume := make([]float64, 0)
sort.Sort(byTime(rates))
for _, rate := range rates {
if rate.Time.After(lastTime) {
lastTime = rate.Time
}
epoch = append(epoch, rate.Time.Unix())
open = append(open, rate.Open)
high = append(high, rate.High)
low = append(low, rate.Low)
clos = append(clos, rate.Close)
volume = append(volume, rate.Volume)
}
cs := io.NewColumnSeries()
cs.AddColumn("Epoch", epoch)
cs.AddColumn("Open", open)
cs.AddColumn("High", high)
cs.AddColumn("Low", low)
cs.AddColumn("Close", clos)
cs.AddColumn("Volume", volume)
log.Info("%s: %d rates between %v - %v", symbol, len(rates),
rates[0].Time, rates[(len(rates))-1].Time)
symbolDir := fmt.Sprintf("gdax_%s", symbol)
csm := io.NewColumnSeriesMap()
tbk := io.NewTimeBucketKey(symbolDir + "/" + gd.baseTimeframe.String + "/OHLCV")
csm.AddColumnSeries(*tbk, cs)
err = executor.WriteCSM(csm, false)
if err != nil {
log.Error("[gdaxfeeder] failed to write csm", err.Error())
}
}
// next fetch start point
timeStart = lastTime.Add(gd.baseTimeframe.Duration)
// for the next bar to complete, add it once more
nextExpected := timeStart.Add(gd.baseTimeframe.Duration)
now := time.Now()
toSleep := nextExpected.Sub(now)
log.Info("next expected(%v) - now(%v) = %v", nextExpected, now, toSleep)
if toSleep > 0 {
log.Debug("Sleep for %v\n", toSleep)
time.Sleep(toSleep)
} else if time.Since(lastTime) < time.Hour {
// let's not go too fast if the catch up is less than an hour
time.Sleep(time.Second)
}
}
}
func main() {
client := gdax.NewClient("", "", "")
params := gdax.GetHistoricRatesParams{
Start: time.Date(2017, 12, 1, 0, 0, 0, 0, time.UTC),
End: time.Date(2017, 12, 1, 1, 0, 0, 0, time.UTC),
Granularity: 60,
}
res, err := client.GetHistoricRates("BTC-USD", params)
// nolint:forbidigo // CLI output needs fmt.Println
fmt.Println(res, err)
}