-
-
Notifications
You must be signed in to change notification settings - Fork 283
/
datasource.go
78 lines (60 loc) · 1.66 KB
/
datasource.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
package glassnode
import (
"context"
"time"
"github.com/c9s/bbgo/pkg/datasource/glassnode/glassnodeapi"
)
type DataSource struct {
client *glassnodeapi.RestClient
}
func New(apiKey string) *DataSource {
client := glassnodeapi.NewRestClient()
client.Auth(apiKey)
return &DataSource{client: client}
}
func (d *DataSource) Query(ctx context.Context, category, metric, asset string, options QueryOptions) (glassnodeapi.DataSlice, error) {
req := glassnodeapi.Request{
Client: d.client,
Asset: asset,
Since: options.Since,
Until: options.Until,
Interval: options.Interval,
Category: category,
Metric: metric,
}
resp, err := req.Do(ctx)
if err != nil {
return nil, err
}
return glassnodeapi.DataSlice(resp), nil
}
// query last futures open interest
// https://docs.glassnode.com/api/derivatives#futures-open-interest
func (d *DataSource) QueryFuturesOpenInterest(ctx context.Context, currency string) (float64, error) {
until := time.Now()
since := until.Add(-24 * time.Hour)
options := QueryOptions{
Since: &since,
Until: &until,
}
resp, err := d.Query(ctx, "derivatives", "futures_open_interest_sum", currency, options)
if err != nil {
return 0, err
}
return resp.Last().Value, nil
}
// query last market cap in usd
// https://docs.glassnode.com/api/market#market-cap
func (d *DataSource) QueryMarketCapInUSD(ctx context.Context, currency string) (float64, error) {
until := time.Now()
since := until.Add(-24 * time.Hour)
options := QueryOptions{
Since: &since,
Until: &until,
}
resp, err := d.Query(ctx, "market", "marketcap_usd", currency, options)
if err != nil {
return 0, err
}
return resp.Last().Value, nil
}