mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 06:53:52 +00:00
glassnode: add QueryOptions
This commit is contained in:
parent
99d6c0550d
commit
cbb3effc42
|
@ -18,28 +18,18 @@ func New(apiKey string) *DataSource {
|
||||||
return &DataSource{client: client}
|
return &DataSource{client: client}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DataSource) Query(ctx context.Context, category, metric, asset, interval string, since, until *time.Time) (glassnodeapi.DataSlice, error) {
|
func (d *DataSource) Query(ctx context.Context, category, metric, asset string, options QueryOptions) (glassnodeapi.DataSlice, error) {
|
||||||
req := glassnodeapi.Request{
|
req := glassnodeapi.Request{
|
||||||
Client: d.client,
|
Client: d.client,
|
||||||
Asset: asset,
|
Asset: asset,
|
||||||
Format: glassnodeapi.FormatJSON,
|
Since: options.Since,
|
||||||
|
Until: options.Until,
|
||||||
|
Interval: options.Interval,
|
||||||
|
|
||||||
Category: category,
|
Category: category,
|
||||||
Metric: metric,
|
Metric: metric,
|
||||||
}
|
}
|
||||||
|
|
||||||
if since != nil {
|
|
||||||
req.Since = since
|
|
||||||
}
|
|
||||||
|
|
||||||
if until != nil {
|
|
||||||
req.Until = until
|
|
||||||
}
|
|
||||||
|
|
||||||
if interval != "" {
|
|
||||||
req.SetInterval(glassnodeapi.Interval(interval))
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, err := req.Do(ctx)
|
resp, err := req.Do(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -52,9 +42,13 @@ func (d *DataSource) Query(ctx context.Context, category, metric, asset, interva
|
||||||
// https://docs.glassnode.com/api/derivatives#futures-open-interest
|
// https://docs.glassnode.com/api/derivatives#futures-open-interest
|
||||||
func (d *DataSource) QueryFuturesOpenInterest(ctx context.Context, currency string) (float64, error) {
|
func (d *DataSource) QueryFuturesOpenInterest(ctx context.Context, currency string) (float64, error) {
|
||||||
until := time.Now()
|
until := time.Now()
|
||||||
since := until.Add(-25 * time.Hour)
|
since := until.Add(-24 * time.Hour)
|
||||||
|
|
||||||
resp, err := d.Query(ctx, "derivatives", "futures_open_interest_sum", currency, "24h", &since, &until)
|
options := QueryOptions{
|
||||||
|
Since: &since,
|
||||||
|
Until: &until,
|
||||||
|
}
|
||||||
|
resp, err := d.Query(ctx, "derivatives", "futures_open_interest_sum", currency, options)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
|
@ -67,9 +61,14 @@ func (d *DataSource) QueryFuturesOpenInterest(ctx context.Context, currency stri
|
||||||
// https://docs.glassnode.com/api/market#market-cap
|
// https://docs.glassnode.com/api/market#market-cap
|
||||||
func (d *DataSource) QueryMarketCapInUSD(ctx context.Context, currency string) (float64, error) {
|
func (d *DataSource) QueryMarketCapInUSD(ctx context.Context, currency string) (float64, error) {
|
||||||
until := time.Now()
|
until := time.Now()
|
||||||
since := until.Add(-25 * time.Hour)
|
since := until.Add(-24 * time.Hour)
|
||||||
|
|
||||||
resp, err := d.Query(ctx, "market", "marketcap_usd", currency, "24h", &since, &until)
|
options := QueryOptions{
|
||||||
|
Since: &since,
|
||||||
|
Until: &until,
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := d.Query(ctx, "market", "marketcap_usd", currency, options)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
|
|
|
@ -13,9 +13,10 @@ type Request struct {
|
||||||
Asset string `param:"a,required,query"`
|
Asset string `param:"a,required,query"`
|
||||||
Since *time.Time `param:"s,query,seconds"`
|
Since *time.Time `param:"s,query,seconds"`
|
||||||
Until *time.Time `param:"u,query,seconds"`
|
Until *time.Time `param:"u,query,seconds"`
|
||||||
Interval Interval `param:"i,query"`
|
Interval *Interval `param:"i,query"`
|
||||||
Format Format `param:"f,query"`
|
Format *Format `param:"f,query" default:"JSON"`
|
||||||
TimestampFormat string `param:"timestamp_format,query"`
|
Currency *string `param:"c,query"`
|
||||||
|
TimestampFormat *string `param:"timestamp_format,query"`
|
||||||
|
|
||||||
Category string `param:"category,slug"`
|
Category string `param:"category,slug"`
|
||||||
Metric string `param:"metric,slug"`
|
Metric string `param:"metric,slug"`
|
||||||
|
|
|
@ -29,17 +29,22 @@ func (r *Request) SetUntil(Until time.Time) *Request {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Request) SetInterval(Interval Interval) *Request {
|
func (r *Request) SetInterval(Interval Interval) *Request {
|
||||||
r.Interval = Interval
|
r.Interval = &Interval
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Request) SetFormat(Format Format) *Request {
|
func (r *Request) SetFormat(Format Format) *Request {
|
||||||
r.Format = Format
|
r.Format = &Format
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Request) SetCurrency(Currency string) *Request {
|
||||||
|
r.Currency = &Currency
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Request) SetTimestampFormat(TimestampFormat string) *Request {
|
func (r *Request) SetTimestampFormat(TimestampFormat string) *Request {
|
||||||
r.TimestampFormat = TimestampFormat
|
r.TimestampFormat = &TimestampFormat
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,7 +91,8 @@ func (r *Request) GetQueryParameters() (url.Values, error) {
|
||||||
} else {
|
} else {
|
||||||
}
|
}
|
||||||
// check Interval field -> json key i
|
// check Interval field -> json key i
|
||||||
Interval := r.Interval
|
if r.Interval != nil {
|
||||||
|
Interval := *r.Interval
|
||||||
|
|
||||||
// TEMPLATE check-valid-values
|
// TEMPLATE check-valid-values
|
||||||
switch Interval {
|
switch Interval {
|
||||||
|
@ -101,8 +107,11 @@ func (r *Request) GetQueryParameters() (url.Values, error) {
|
||||||
|
|
||||||
// assign parameter of Interval
|
// assign parameter of Interval
|
||||||
params["i"] = Interval
|
params["i"] = Interval
|
||||||
|
} else {
|
||||||
|
}
|
||||||
// check Format field -> json key f
|
// check Format field -> json key f
|
||||||
Format := r.Format
|
if r.Format != nil {
|
||||||
|
Format := *r.Format
|
||||||
|
|
||||||
// TEMPLATE check-valid-values
|
// TEMPLATE check-valid-values
|
||||||
switch Format {
|
switch Format {
|
||||||
|
@ -117,11 +126,28 @@ func (r *Request) GetQueryParameters() (url.Values, error) {
|
||||||
|
|
||||||
// assign parameter of Format
|
// assign parameter of Format
|
||||||
params["f"] = Format
|
params["f"] = Format
|
||||||
|
} else {
|
||||||
|
Format := "JSON"
|
||||||
|
|
||||||
|
// assign parameter of Format
|
||||||
|
params["f"] = Format
|
||||||
|
}
|
||||||
|
// check Currency field -> json key c
|
||||||
|
if r.Currency != nil {
|
||||||
|
Currency := *r.Currency
|
||||||
|
|
||||||
|
// assign parameter of Currency
|
||||||
|
params["c"] = Currency
|
||||||
|
} else {
|
||||||
|
}
|
||||||
// check TimestampFormat field -> json key timestamp_format
|
// check TimestampFormat field -> json key timestamp_format
|
||||||
TimestampFormat := r.TimestampFormat
|
if r.TimestampFormat != nil {
|
||||||
|
TimestampFormat := *r.TimestampFormat
|
||||||
|
|
||||||
// assign parameter of TimestampFormat
|
// assign parameter of TimestampFormat
|
||||||
params["timestamp_format"] = TimestampFormat
|
params["timestamp_format"] = TimestampFormat
|
||||||
|
} else {
|
||||||
|
}
|
||||||
|
|
||||||
query := url.Values{}
|
query := url.Values{}
|
||||||
for _k, _v := range params {
|
for _k, _v := range params {
|
||||||
|
|
14
pkg/datasource/glassnode/types.go
Normal file
14
pkg/datasource/glassnode/types.go
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
package glassnode
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/c9s/bbgo/pkg/datasource/glassnode/glassnodeapi"
|
||||||
|
)
|
||||||
|
|
||||||
|
type QueryOptions struct {
|
||||||
|
Since *time.Time
|
||||||
|
Until *time.Time
|
||||||
|
Interval *glassnodeapi.Interval
|
||||||
|
Currency *string
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user