initial
This commit is contained in:
77
trade/binance_klines.go
Normal file
77
trade/binance_klines.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package trade
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
"git.apinb.com/quant/strategy/types"
|
||||
)
|
||||
|
||||
const (
|
||||
MaxKlinesFetch = 500
|
||||
)
|
||||
|
||||
func (bn *BinanceClient) FetchKlines(symbol, interval string, limit int, debug bool) ([]*types.KLine, []float64, error) {
|
||||
if limit > MaxKlinesFetch {
|
||||
limit = MaxKlinesFetch
|
||||
}
|
||||
ctx := context.Background()
|
||||
klines, err := bn.Futures.NewKlinesService().Symbol(symbol).Interval(interval).Limit(limit).Do(ctx)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if debug {
|
||||
fmt.Println("FetchKlines:", symbol, interval, limit)
|
||||
}
|
||||
k := make([]*types.KLine, 0)
|
||||
closes := make([]float64, len(klines))
|
||||
for i, val := range klines {
|
||||
|
||||
c := utils.String2Float64(val.Close)
|
||||
closes[i] = c
|
||||
k = append(k, &types.KLine{
|
||||
Timestamp: val.OpenTime,
|
||||
Open: utils.String2Float64(val.Open),
|
||||
High: utils.String2Float64(val.High),
|
||||
Low: utils.String2Float64(val.Low),
|
||||
Close: c,
|
||||
Volume: utils.String2Float64(val.QuoteAssetVolume),
|
||||
})
|
||||
|
||||
if debug {
|
||||
fmt.Println(time.Unix(val.OpenTime/1000, 0).Format(time.DateTime), val.Open, val.High, val.Low, val.Close, val.Volume)
|
||||
}
|
||||
}
|
||||
if len(k) == 0 {
|
||||
return nil, nil, errors.New("no klines")
|
||||
}
|
||||
|
||||
return k, closes, nil
|
||||
}
|
||||
|
||||
func (bn *BinanceClient) FetchSymbolsPrice() (map[string]float64, error) {
|
||||
ctx := context.Background()
|
||||
prices, err := bn.Futures.NewListPricesService().Do(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
priceMap := make(map[string]float64)
|
||||
for _, p := range prices {
|
||||
priceMap[p.Symbol] = utils.String2Float64(p.Price)
|
||||
}
|
||||
|
||||
return priceMap, nil
|
||||
}
|
||||
|
||||
func (bn *BinanceClient) BookTicker(symbol string) (bidPrice, askPrice string, err error) {
|
||||
res, err := bn.Futures.NewListBookTickersService().Symbol(symbol).Do(context.Background())
|
||||
if len(res) == 0 || err != nil {
|
||||
return "", "", errors.New("BookTicker: No Data")
|
||||
}
|
||||
|
||||
return res[0].BidPrice, res[0].AskPrice, nil
|
||||
}
|
||||
Reference in New Issue
Block a user