bbgo_origin/pkg/datasource/glassnode/datasource.go

60 lines
1.3 KiB
Go
Raw Normal View History

2022-04-25 08:44:57 +00:00
package glassnode
import (
"context"
"time"
2022-04-26 10:51:06 +00:00
"github.com/c9s/bbgo/pkg/datasource/glassnode/glassnodeapi"
2022-04-25 08:44:57 +00:00
)
2022-04-26 10:51:06 +00:00
type DataSource struct {
2022-04-25 08:44:57 +00:00
client *glassnodeapi.RestClient
}
2022-04-26 10:51:06 +00:00
func New(apiKey string) *DataSource {
2022-04-25 08:44:57 +00:00
client := glassnodeapi.NewRestClient()
client.Auth(apiKey)
2022-04-26 10:51:06 +00:00
return &DataSource{client: client}
2022-04-25 08:44:57 +00:00
}
2022-04-25 17:25:42 +00:00
// query last futures open interest
// https://docs.glassnode.com/api/derivatives#futures-open-interest
2022-04-26 10:51:06 +00:00
func (d *DataSource) QueryFuturesOpenInterest(ctx context.Context, currency string) (float64, error) {
2022-04-25 17:25:42 +00:00
req := glassnodeapi.DerivativesRequest{
2022-04-26 10:51:06 +00:00
Client: d.client,
2022-04-25 17:25:42 +00:00
Asset: currency,
// 25 hours ago
Since: time.Now().Add(-25 * time.Hour).Unix(),
Interval: glassnodeapi.Interval24h,
Metric: "futures_open_interest_sum",
}
resp, err := req.Do(ctx)
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
2022-04-26 10:51:06 +00:00
func (d *DataSource) QueryMarketCapInUSD(ctx context.Context, currency string) (float64, error) {
2022-04-25 08:44:57 +00:00
req := glassnodeapi.MarketRequest{
2022-04-26 10:51:06 +00:00
Client: d.client,
2022-04-25 08:44:57 +00:00
Asset: currency,
// 25 hours ago
Since: time.Now().Add(-25 * time.Hour).Unix(),
Interval: glassnodeapi.Interval24h,
Metric: "marketcap_usd",
}
resp, err := req.Do(ctx)
if err != nil {
return 0, err
}
return resp.Last().Value, nil
}