mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
get marketcap values from coinmarketcap
This commit is contained in:
parent
53059824a6
commit
9b5f204cbe
|
@ -13,6 +13,11 @@ exchangeStrategies:
|
||||||
targetCurrencies:
|
targetCurrencies:
|
||||||
- BTC
|
- BTC
|
||||||
- ETH
|
- ETH
|
||||||
|
# - BNB
|
||||||
|
# - ADA
|
||||||
|
# - SOL
|
||||||
|
# - DOT
|
||||||
|
# - DOGE
|
||||||
- MATIC
|
- MATIC
|
||||||
threshold: 2%
|
threshold: 2%
|
||||||
# max amount to buy or sell per order
|
# max amount to buy or sell per order
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
package coinmarketcap
|
package coinmarketcap
|
||||||
|
|
||||||
import v1 "github.com/c9s/bbgo/pkg/datasource/coinmarketcap/v1"
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
v1 "github.com/c9s/bbgo/pkg/datasource/coinmarketcap/v1"
|
||||||
|
)
|
||||||
|
|
||||||
type DataSource struct {
|
type DataSource struct {
|
||||||
client *v1.RestClient
|
client *v1.RestClient
|
||||||
|
@ -11,3 +15,22 @@ func New(apiKey string) *DataSource {
|
||||||
client.Auth(apiKey)
|
client.Auth(apiKey)
|
||||||
return &DataSource{client: client}
|
return &DataSource{client: client}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *DataSource) QueryMarketCapInUSD(ctx context.Context, limit int) (map[string]float64, error) {
|
||||||
|
req := v1.ListingsLatestRequest{
|
||||||
|
Client: d.client,
|
||||||
|
Limit: &limit,
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := req.Do(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
marketcaps := make(map[string]float64)
|
||||||
|
for _, data := range resp {
|
||||||
|
marketcaps[data.Symbol] = data.Quote["USD"].MarketCap
|
||||||
|
}
|
||||||
|
|
||||||
|
return marketcaps, nil
|
||||||
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/c9s/bbgo/pkg/bbgo"
|
"github.com/c9s/bbgo/pkg/bbgo"
|
||||||
"github.com/c9s/bbgo/pkg/datasource/glassnode"
|
"github.com/c9s/bbgo/pkg/datasource/coinmarketcap"
|
||||||
"github.com/c9s/bbgo/pkg/datatype/floats"
|
"github.com/c9s/bbgo/pkg/datatype/floats"
|
||||||
"github.com/c9s/bbgo/pkg/fixedpoint"
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
||||||
"github.com/c9s/bbgo/pkg/types"
|
"github.com/c9s/bbgo/pkg/types"
|
||||||
|
@ -24,7 +24,7 @@ func init() {
|
||||||
|
|
||||||
type Strategy struct {
|
type Strategy struct {
|
||||||
Notifiability *bbgo.Notifiability
|
Notifiability *bbgo.Notifiability
|
||||||
glassnode *glassnode.DataSource
|
Datasource *coinmarketcap.DataSource
|
||||||
|
|
||||||
Interval types.Interval `json:"interval"`
|
Interval types.Interval `json:"interval"`
|
||||||
BaseCurrency string `json:"baseCurrency"`
|
BaseCurrency string `json:"baseCurrency"`
|
||||||
|
@ -40,8 +40,8 @@ type Strategy struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Strategy) Initialize() error {
|
func (s *Strategy) Initialize() error {
|
||||||
apiKey := os.Getenv("GLASSNODE_API_KEY")
|
apiKey := os.Getenv("COINMARKETCAP_API_KEY")
|
||||||
s.glassnode = glassnode.New(apiKey)
|
s.Datasource = coinmarketcap.New(apiKey)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -177,14 +177,15 @@ func (s *Strategy) generateSubmitOrders(ctx context.Context, session *bbgo.Excha
|
||||||
func (s *Strategy) getTargetWeights(ctx context.Context) types.ValueMap {
|
func (s *Strategy) getTargetWeights(ctx context.Context) types.ValueMap {
|
||||||
m := floats.Map{}
|
m := floats.Map{}
|
||||||
|
|
||||||
// get market cap values
|
// get marketcap from coinmarketcap
|
||||||
|
// set higher query limit to avoid target currency not in the list
|
||||||
|
marketcaps, err := s.Datasource.QueryMarketCapInUSD(ctx, 100)
|
||||||
|
if err != nil {
|
||||||
|
log.WithError(err).Error("failed to query market cap")
|
||||||
|
}
|
||||||
|
|
||||||
for _, currency := range s.TargetCurrencies {
|
for _, currency := range s.TargetCurrencies {
|
||||||
marketCap, err := s.glassnode.QueryMarketCapInUSD(ctx, currency)
|
m[currency] = marketcaps[currency]
|
||||||
if err != nil {
|
|
||||||
log.WithError(err).Error("failed to query market cap")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
m[currency] = marketCap
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// normalize
|
// normalize
|
||||||
|
|
Loading…
Reference in New Issue
Block a user