2021-02-05 09:29:38 +00:00
|
|
|
package ftx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-08 10:59:36 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2021-03-17 13:26:25 +00:00
|
|
|
"sort"
|
2022-02-22 06:15:47 +00:00
|
|
|
"strconv"
|
2021-03-25 08:57:54 +00:00
|
|
|
"strings"
|
2021-02-05 09:29:38 +00:00
|
|
|
"time"
|
|
|
|
|
2022-01-11 19:47:12 +00:00
|
|
|
"golang.org/x/time/rate"
|
|
|
|
|
2021-05-19 13:36:51 +00:00
|
|
|
"github.com/google/uuid"
|
2021-02-27 08:47:21 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2022-02-22 06:15:47 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/exchange/ftx/ftxapi"
|
2021-02-08 10:59:36 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
2021-02-05 09:29:38 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
2021-02-08 10:59:36 +00:00
|
|
|
const (
|
|
|
|
restEndpoint = "https://ftx.com"
|
|
|
|
defaultHTTPTimeout = 15 * time.Second
|
|
|
|
)
|
|
|
|
|
2021-02-27 09:01:20 +00:00
|
|
|
var logger = logrus.WithField("exchange", "ftx")
|
2021-05-26 12:53:51 +00:00
|
|
|
|
2021-12-10 15:08:26 +00:00
|
|
|
// POST https://ftx.com/api/orders 429, Success: false, err: Do not send more than 2 orders on this market per 200ms
|
|
|
|
var requestLimit = rate.NewLimiter(rate.Every(220*time.Millisecond), 2)
|
|
|
|
|
2021-05-26 12:53:51 +00:00
|
|
|
//go:generate go run generate_symbol_map.go
|
2021-02-27 08:47:21 +00:00
|
|
|
|
2021-02-05 09:29:38 +00:00
|
|
|
type Exchange struct {
|
2022-02-22 06:15:47 +00:00
|
|
|
client *ftxapi.RestClient
|
|
|
|
|
2021-03-15 11:01:23 +00:00
|
|
|
key, secret string
|
|
|
|
subAccount string
|
|
|
|
restEndpoint *url.URL
|
2021-02-05 09:29:38 +00:00
|
|
|
}
|
|
|
|
|
2021-12-13 23:19:21 +00:00
|
|
|
type MarketTicker struct {
|
|
|
|
Market types.Market
|
2022-02-03 04:55:25 +00:00
|
|
|
Price fixedpoint.Value
|
|
|
|
Ask fixedpoint.Value
|
|
|
|
Bid fixedpoint.Value
|
|
|
|
Last fixedpoint.Value
|
2021-12-13 23:19:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type MarketMap map[string]MarketTicker
|
|
|
|
|
2021-05-19 13:36:51 +00:00
|
|
|
// FTX does not have broker ID
|
|
|
|
const spotBrokerID = "BBGO"
|
|
|
|
|
|
|
|
func newSpotClientOrderID(originalID string) (clientOrderID string) {
|
|
|
|
prefix := "x-" + spotBrokerID
|
|
|
|
prefixLen := len(prefix)
|
|
|
|
|
|
|
|
if originalID != "" {
|
|
|
|
// try to keep the whole original client order ID if user specifies it.
|
|
|
|
if prefixLen+len(originalID) > 32 {
|
|
|
|
return originalID
|
|
|
|
}
|
|
|
|
|
|
|
|
clientOrderID = prefix + originalID
|
|
|
|
return clientOrderID
|
|
|
|
}
|
|
|
|
|
|
|
|
clientOrderID = uuid.New().String()
|
|
|
|
clientOrderID = prefix + clientOrderID
|
|
|
|
if len(clientOrderID) > 32 {
|
|
|
|
return clientOrderID[0:32]
|
|
|
|
}
|
|
|
|
|
|
|
|
return clientOrderID
|
|
|
|
}
|
|
|
|
|
2021-02-08 10:59:36 +00:00
|
|
|
func NewExchange(key, secret string, subAccount string) *Exchange {
|
|
|
|
u, err := url.Parse(restEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-05-26 12:53:51 +00:00
|
|
|
|
2022-02-22 06:15:47 +00:00
|
|
|
client := ftxapi.NewClient()
|
2021-02-08 10:59:36 +00:00
|
|
|
return &Exchange{
|
2022-02-22 06:15:47 +00:00
|
|
|
client: client,
|
2021-03-15 11:01:23 +00:00
|
|
|
restEndpoint: u,
|
|
|
|
key: key,
|
|
|
|
secret: secret,
|
|
|
|
subAccount: subAccount,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exchange) newRest() *restRequest {
|
|
|
|
r := newRestRequest(&http.Client{Timeout: defaultHTTPTimeout}, e.restEndpoint).Auth(e.key, e.secret)
|
|
|
|
if len(e.subAccount) > 0 {
|
|
|
|
r.SubAccount(e.subAccount)
|
2021-02-08 10:59:36 +00:00
|
|
|
}
|
2021-03-15 11:01:23 +00:00
|
|
|
return r
|
2021-02-05 09:29:38 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 10:59:36 +00:00
|
|
|
func (e *Exchange) Name() types.ExchangeName {
|
2021-02-08 14:33:12 +00:00
|
|
|
return types.ExchangeFTX
|
2021-02-05 09:29:38 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 10:59:36 +00:00
|
|
|
func (e *Exchange) PlatformFeeCurrency() string {
|
2021-03-18 00:49:33 +00:00
|
|
|
return toGlobalCurrency("FTT")
|
2021-02-05 09:29:38 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 10:59:36 +00:00
|
|
|
func (e *Exchange) NewStream() types.Stream {
|
2021-05-21 15:07:53 +00:00
|
|
|
return NewStream(e.key, e.secret, e.subAccount, e)
|
2021-02-05 09:29:38 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 10:59:36 +00:00
|
|
|
func (e *Exchange) QueryMarkets(ctx context.Context) (types.MarketMap, error) {
|
2021-12-13 23:19:21 +00:00
|
|
|
markets, err := e._queryMarkets(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
marketMap := types.MarketMap{}
|
|
|
|
for k, v := range markets {
|
|
|
|
marketMap[k] = v.Market
|
|
|
|
}
|
|
|
|
return marketMap, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exchange) _queryMarkets(ctx context.Context) (MarketMap, error) {
|
2021-03-21 02:52:41 +00:00
|
|
|
resp, err := e.newRest().Markets(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !resp.Success {
|
|
|
|
return nil, fmt.Errorf("ftx returns querying markets failure")
|
|
|
|
}
|
|
|
|
|
2021-12-13 23:19:21 +00:00
|
|
|
markets := MarketMap{}
|
2021-03-21 02:52:41 +00:00
|
|
|
for _, m := range resp.Result {
|
|
|
|
symbol := toGlobalSymbol(m.Name)
|
2021-05-17 10:32:29 +00:00
|
|
|
symbolMap[symbol] = m.Name
|
2021-03-21 02:52:41 +00:00
|
|
|
|
2021-12-13 23:19:21 +00:00
|
|
|
mkt2 := MarketTicker{
|
|
|
|
Market: types.Market{
|
|
|
|
Symbol: symbol,
|
|
|
|
LocalSymbol: m.Name,
|
|
|
|
// The max precision is length(DefaultPow). For example, currently fixedpoint.DefaultPow
|
|
|
|
// is 1e8, so the max precision will be 8.
|
2022-02-04 03:56:49 +00:00
|
|
|
PricePrecision: m.PriceIncrement.NumFractionalDigits(),
|
|
|
|
VolumePrecision: m.SizeIncrement.NumFractionalDigits(),
|
2021-12-13 23:19:21 +00:00
|
|
|
QuoteCurrency: toGlobalCurrency(m.QuoteCurrency),
|
|
|
|
BaseCurrency: toGlobalCurrency(m.BaseCurrency),
|
|
|
|
// FTX only limit your order by `MinProvideSize`, so I assign zero value to unsupported fields:
|
|
|
|
// MinNotional, MinAmount, MaxQuantity, MinPrice and MaxPrice.
|
2022-02-03 04:55:25 +00:00
|
|
|
MinNotional: fixedpoint.Zero,
|
|
|
|
MinAmount: fixedpoint.Zero,
|
2021-12-13 23:19:21 +00:00
|
|
|
MinQuantity: m.MinProvideSize,
|
2022-02-03 04:55:25 +00:00
|
|
|
MaxQuantity: fixedpoint.Zero,
|
2021-12-13 23:19:21 +00:00
|
|
|
StepSize: m.SizeIncrement,
|
2022-02-03 04:55:25 +00:00
|
|
|
MinPrice: fixedpoint.Zero,
|
|
|
|
MaxPrice: fixedpoint.Zero,
|
2021-12-13 23:19:21 +00:00
|
|
|
TickSize: m.PriceIncrement,
|
|
|
|
},
|
|
|
|
Price: m.Price,
|
|
|
|
Bid: m.Bid,
|
|
|
|
Ask: m.Ask,
|
|
|
|
Last: m.Last,
|
2021-03-21 02:52:41 +00:00
|
|
|
}
|
2021-12-13 23:19:21 +00:00
|
|
|
markets[symbol] = mkt2
|
2021-03-21 02:52:41 +00:00
|
|
|
}
|
|
|
|
return markets, nil
|
2021-02-05 09:29:38 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 10:59:36 +00:00
|
|
|
func (e *Exchange) QueryAccount(ctx context.Context) (*types.Account, error) {
|
2022-03-02 03:25:27 +00:00
|
|
|
|
|
|
|
req := e.client.NewGetAccountRequest()
|
|
|
|
ftxAccount, err := req.Do(ctx)
|
2021-03-18 00:33:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
a := &types.Account{
|
2022-03-02 03:25:27 +00:00
|
|
|
MakerCommission: ftxAccount.MakerFee,
|
|
|
|
TakerCommission: ftxAccount.TakerFee,
|
|
|
|
TotalAccountValue: ftxAccount.TotalAccountValue,
|
2021-03-18 00:33:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
balances, err := e.QueryAccountBalances(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
a.UpdateBalances(balances)
|
|
|
|
|
|
|
|
return a, nil
|
2021-02-05 09:29:38 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 10:59:36 +00:00
|
|
|
func (e *Exchange) QueryAccountBalances(ctx context.Context) (types.BalanceMap, error) {
|
2022-03-02 03:25:27 +00:00
|
|
|
balanceReq := e.client.NewGetBalancesRequest()
|
|
|
|
ftxBalances, err := balanceReq.Do(ctx)
|
2021-02-08 10:59:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-02 03:25:27 +00:00
|
|
|
|
2021-02-08 10:59:36 +00:00
|
|
|
var balances = make(types.BalanceMap)
|
2022-03-02 03:25:27 +00:00
|
|
|
for _, r := range ftxBalances {
|
2021-02-08 10:59:36 +00:00
|
|
|
balances[toGlobalCurrency(r.Coin)] = types.Balance{
|
|
|
|
Currency: toGlobalCurrency(r.Coin),
|
2022-02-03 04:55:25 +00:00
|
|
|
Available: r.Free,
|
|
|
|
Locked: r.Total.Sub(r.Free),
|
2021-02-08 10:59:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return balances, nil
|
|
|
|
}
|
|
|
|
|
2022-02-18 06:01:47 +00:00
|
|
|
// resolution field in api
|
|
|
|
// window length in seconds. options: 15, 60, 300, 900, 3600, 14400, 86400, or any multiple of 86400 up to 30*86400
|
2021-12-13 23:15:18 +00:00
|
|
|
var supportedIntervals = map[types.Interval]int{
|
|
|
|
types.Interval1m: 1,
|
|
|
|
types.Interval5m: 5,
|
|
|
|
types.Interval15m: 15,
|
|
|
|
types.Interval1h: 60,
|
|
|
|
types.Interval1d: 60 * 24,
|
|
|
|
types.Interval3d: 60 * 24 * 3,
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exchange) SupportedInterval() map[types.Interval]int {
|
|
|
|
return supportedIntervals
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exchange) IsSupportedInterval(interval types.Interval) bool {
|
|
|
|
return isIntervalSupportedInKLine(interval)
|
|
|
|
}
|
|
|
|
|
2021-02-08 10:59:36 +00:00
|
|
|
func (e *Exchange) QueryKLines(ctx context.Context, symbol string, interval types.Interval, options types.KLineQueryOptions) ([]types.KLine, error) {
|
2021-12-13 23:15:18 +00:00
|
|
|
var klines []types.KLine
|
2021-12-14 11:34:43 +00:00
|
|
|
var since, until, currentEnd time.Time
|
2021-12-13 23:15:18 +00:00
|
|
|
if options.StartTime != nil {
|
|
|
|
since = *options.StartTime
|
|
|
|
}
|
|
|
|
if options.EndTime != nil {
|
|
|
|
until = *options.EndTime
|
|
|
|
} else {
|
|
|
|
until = time.Now()
|
|
|
|
}
|
|
|
|
|
2021-12-14 11:34:43 +00:00
|
|
|
currentEnd = until
|
2021-12-13 23:15:18 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
|
2022-02-18 06:01:47 +00:00
|
|
|
// the fetch result is from newest to oldest
|
2021-12-14 11:34:43 +00:00
|
|
|
endTime := currentEnd.Add(interval.Duration())
|
2021-12-13 23:15:18 +00:00
|
|
|
options.EndTime = &endTime
|
2021-12-14 11:34:43 +00:00
|
|
|
lines, err := e._queryKLines(ctx, symbol, interval, types.KLineQueryOptions{
|
|
|
|
StartTime: &since,
|
|
|
|
EndTime: ¤tEnd,
|
|
|
|
})
|
2021-12-13 23:15:18 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(lines) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, line := range lines {
|
|
|
|
|
2021-12-14 11:34:43 +00:00
|
|
|
if line.StartTime.Unix() < currentEnd.Unix() {
|
2021-12-15 05:04:01 +00:00
|
|
|
currentEnd = line.StartTime.Time()
|
2021-12-13 23:15:18 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 11:34:43 +00:00
|
|
|
if line.StartTime.Unix() > since.Unix() {
|
2021-12-13 23:15:18 +00:00
|
|
|
klines = append(klines, line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 16:59:28 +00:00
|
|
|
if len(lines) == 1 && lines[0].StartTime.Unix() == currentEnd.Unix() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
outBound := currentEnd.Add(interval.Duration()*-1).Unix() <= since.Unix()
|
2021-12-14 11:34:43 +00:00
|
|
|
if since.IsZero() || currentEnd.Unix() == since.Unix() || outBound {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.Limit != 0 && options.Limit <= len(lines) {
|
2021-12-13 23:15:18 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Slice(klines, func(i, j int) bool { return klines[i].StartTime.Unix() < klines[j].StartTime.Unix() })
|
|
|
|
|
2021-12-14 11:34:43 +00:00
|
|
|
if options.Limit != 0 {
|
|
|
|
limitedItems := len(klines) - options.Limit
|
|
|
|
if limitedItems > 0 {
|
|
|
|
return klines[limitedItems:], nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-13 23:15:18 +00:00
|
|
|
return klines, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exchange) _queryKLines(ctx context.Context, symbol string, interval types.Interval, options types.KLineQueryOptions) ([]types.KLine, error) {
|
2021-03-31 10:09:13 +00:00
|
|
|
var since, until time.Time
|
|
|
|
if options.StartTime != nil {
|
|
|
|
since = *options.StartTime
|
|
|
|
}
|
|
|
|
if options.EndTime != nil {
|
|
|
|
until = *options.EndTime
|
|
|
|
} else {
|
|
|
|
until = time.Now()
|
|
|
|
}
|
|
|
|
if since.After(until) {
|
|
|
|
return nil, fmt.Errorf("invalid query klines time range, since: %+v, until: %+v", since, until)
|
|
|
|
}
|
|
|
|
if !isIntervalSupportedInKLine(interval) {
|
|
|
|
return nil, fmt.Errorf("interval %s is not supported", interval.String())
|
|
|
|
}
|
2021-12-13 23:15:18 +00:00
|
|
|
|
|
|
|
if err := requestLimit.Wait(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-14 11:34:43 +00:00
|
|
|
resp, err := e.newRest().HistoricalPrices(ctx, toLocalSymbol(symbol), interval, 0, since, until)
|
2021-03-31 10:09:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !resp.Success {
|
|
|
|
return nil, fmt.Errorf("ftx returns failure")
|
|
|
|
}
|
|
|
|
|
2021-12-13 23:15:18 +00:00
|
|
|
var klines []types.KLine
|
2021-03-31 10:09:13 +00:00
|
|
|
for _, r := range resp.Result {
|
|
|
|
globalKline, err := toGlobalKLine(symbol, interval, r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-13 23:15:18 +00:00
|
|
|
klines = append(klines, globalKline)
|
2021-03-31 10:09:13 +00:00
|
|
|
}
|
2021-12-13 23:15:18 +00:00
|
|
|
return klines, nil
|
2021-04-01 03:55:27 +00:00
|
|
|
}
|
|
|
|
|
2021-03-31 10:09:13 +00:00
|
|
|
func isIntervalSupportedInKLine(interval types.Interval) bool {
|
2021-12-13 23:15:18 +00:00
|
|
|
_, ok := supportedIntervals[interval]
|
2021-03-31 10:09:13 +00:00
|
|
|
return ok
|
2021-02-05 09:29:38 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 10:59:36 +00:00
|
|
|
func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *types.TradeQueryOptions) ([]types.Trade, error) {
|
2021-03-25 08:57:54 +00:00
|
|
|
var since, until time.Time
|
|
|
|
if options.StartTime != nil {
|
|
|
|
since = *options.StartTime
|
|
|
|
}
|
|
|
|
if options.EndTime != nil {
|
|
|
|
until = *options.EndTime
|
2021-03-29 14:07:21 +00:00
|
|
|
} else {
|
|
|
|
until = time.Now()
|
2021-03-25 08:57:54 +00:00
|
|
|
}
|
2021-03-29 14:07:21 +00:00
|
|
|
|
|
|
|
if since.After(until) {
|
|
|
|
return nil, fmt.Errorf("invalid query trades time range, since: %+v, until: %+v", since, until)
|
2021-03-25 08:57:54 +00:00
|
|
|
}
|
2021-03-29 14:07:21 +00:00
|
|
|
|
2021-03-25 08:57:54 +00:00
|
|
|
if options.Limit == 1 {
|
|
|
|
// FTX doesn't provide pagination api, so we have to split the since/until time range into small slices, and paginate ourselves.
|
|
|
|
// If the limit is 1, we always get the same data from FTX.
|
|
|
|
return nil, fmt.Errorf("limit can't be 1 which can't be used in pagination")
|
|
|
|
}
|
|
|
|
limit := options.Limit
|
|
|
|
if limit == 0 {
|
|
|
|
limit = 200
|
|
|
|
}
|
|
|
|
|
2021-12-23 05:15:27 +00:00
|
|
|
tradeIDs := make(map[uint64]struct{})
|
2021-03-25 08:57:54 +00:00
|
|
|
|
2021-05-25 15:14:49 +00:00
|
|
|
lastTradeID := options.LastTradeID
|
2021-03-25 08:57:54 +00:00
|
|
|
var trades []types.Trade
|
|
|
|
symbol = strings.ToUpper(symbol)
|
|
|
|
|
|
|
|
for since.Before(until) {
|
|
|
|
// DO not set limit to `1` since you will always get the same response.
|
2021-05-17 10:32:29 +00:00
|
|
|
resp, err := e.newRest().Fills(ctx, toLocalSymbol(symbol), since, until, limit, true)
|
2021-03-25 08:57:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !resp.Success {
|
|
|
|
return nil, fmt.Errorf("ftx returns failure")
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(resp.Result, func(i, j int) bool {
|
|
|
|
return resp.Result[i].TradeId < resp.Result[j].TradeId
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, r := range resp.Result {
|
2021-05-25 15:21:38 +00:00
|
|
|
// always update since to avoid infinite loop
|
|
|
|
since = r.Time.Time
|
|
|
|
|
2021-03-25 08:57:54 +00:00
|
|
|
if _, ok := tradeIDs[r.TradeId]; ok {
|
|
|
|
continue
|
|
|
|
}
|
2021-05-25 14:43:26 +00:00
|
|
|
|
|
|
|
if r.TradeId <= lastTradeID || r.Time.Before(since) || r.Time.After(until) || r.Market != toLocalSymbol(symbol) {
|
2021-03-25 08:57:54 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
tradeIDs[r.TradeId] = struct{}{}
|
|
|
|
lastTradeID = r.TradeId
|
|
|
|
|
|
|
|
t, err := toGlobalTrade(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
trades = append(trades, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
if int64(len(resp.Result)) < limit {
|
|
|
|
return trades, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return trades, nil
|
2021-02-05 09:29:38 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 10:59:36 +00:00
|
|
|
func (e *Exchange) QueryDepositHistory(ctx context.Context, asset string, since, until time.Time) (allDeposits []types.Deposit, err error) {
|
2021-03-29 14:07:21 +00:00
|
|
|
if until == (time.Time{}) {
|
|
|
|
until = time.Now()
|
|
|
|
}
|
|
|
|
if since.After(until) {
|
|
|
|
return nil, fmt.Errorf("invalid query deposit history time range, since: %+v, until: %+v", since, until)
|
2021-03-21 12:17:41 +00:00
|
|
|
}
|
2021-03-23 14:27:11 +00:00
|
|
|
asset = TrimUpperString(asset)
|
2021-03-21 12:17:41 +00:00
|
|
|
|
2021-03-23 14:27:11 +00:00
|
|
|
resp, err := e.newRest().DepositHistory(ctx, since, until, 0)
|
2021-03-21 12:17:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-25 08:57:54 +00:00
|
|
|
if !resp.Success {
|
|
|
|
return nil, fmt.Errorf("ftx returns failure")
|
|
|
|
}
|
2021-03-21 12:17:41 +00:00
|
|
|
sort.Slice(resp.Result, func(i, j int) bool {
|
2021-03-25 08:57:54 +00:00
|
|
|
return resp.Result[i].Time.Before(resp.Result[j].Time.Time)
|
2021-03-21 12:17:41 +00:00
|
|
|
})
|
|
|
|
for _, r := range resp.Result {
|
|
|
|
d, err := toGlobalDeposit(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if d.Asset == asset && !since.After(d.Time.Time()) && !until.Before(d.Time.Time()) {
|
|
|
|
allDeposits = append(allDeposits, d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2021-02-05 09:29:38 +00:00
|
|
|
}
|
|
|
|
|
2021-03-13 01:51:31 +00:00
|
|
|
func (e *Exchange) SubmitOrders(ctx context.Context, orders ...types.SubmitOrder) (types.OrderSlice, error) {
|
|
|
|
var createdOrders types.OrderSlice
|
|
|
|
// TODO: currently only support limit and market order
|
|
|
|
// TODO: support time in force
|
|
|
|
for _, so := range orders {
|
2021-12-10 15:08:26 +00:00
|
|
|
if err := requestLimit.Wait(ctx); err != nil {
|
|
|
|
logrus.WithError(err).Error("rate limit error")
|
|
|
|
}
|
2022-02-18 06:01:47 +00:00
|
|
|
|
|
|
|
orderType, err := toLocalOrderType(so.Type)
|
2022-01-11 19:47:12 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Error("type error")
|
|
|
|
}
|
2022-02-18 06:01:47 +00:00
|
|
|
|
2021-03-15 11:01:23 +00:00
|
|
|
or, err := e.newRest().PlaceOrder(ctx, PlaceOrderPayload{
|
2021-05-17 10:32:29 +00:00
|
|
|
Market: toLocalSymbol(TrimUpperString(so.Symbol)),
|
2021-03-13 01:51:31 +00:00
|
|
|
Side: TrimLowerString(string(so.Side)),
|
|
|
|
Price: so.Price,
|
2022-01-11 19:47:12 +00:00
|
|
|
Type: string(orderType),
|
2021-03-13 01:51:31 +00:00
|
|
|
Size: so.Quantity,
|
|
|
|
ReduceOnly: false,
|
2022-02-18 06:01:47 +00:00
|
|
|
IOC: so.TimeInForce == types.TimeInForceIOC,
|
|
|
|
PostOnly: so.Type == types.OrderTypeLimitMaker,
|
2021-05-19 13:36:51 +00:00
|
|
|
ClientID: newSpotClientOrderID(so.ClientOrderID),
|
2021-03-13 01:51:31 +00:00
|
|
|
})
|
2022-02-18 06:07:29 +00:00
|
|
|
|
2021-03-13 01:51:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return createdOrders, fmt.Errorf("failed to place order %+v: %w", so, err)
|
|
|
|
}
|
2022-02-18 06:07:29 +00:00
|
|
|
|
2021-03-13 01:51:31 +00:00
|
|
|
if !or.Success {
|
|
|
|
return createdOrders, fmt.Errorf("ftx returns placing order failure")
|
|
|
|
}
|
2022-02-18 06:07:29 +00:00
|
|
|
|
2021-03-13 01:51:31 +00:00
|
|
|
globalOrder, err := toGlobalOrder(or.Result)
|
|
|
|
if err != nil {
|
|
|
|
return createdOrders, fmt.Errorf("failed to convert response to global order")
|
|
|
|
}
|
2022-02-18 06:07:29 +00:00
|
|
|
|
2021-03-13 01:51:31 +00:00
|
|
|
createdOrders = append(createdOrders, globalOrder)
|
|
|
|
}
|
|
|
|
return createdOrders, nil
|
2021-02-05 09:29:38 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 06:15:47 +00:00
|
|
|
func (e *Exchange) QueryOrder(ctx context.Context, q types.OrderQuery) (*types.Order, error) {
|
|
|
|
orderID, err := strconv.ParseInt(q.OrderID, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_ = orderID
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-02-08 10:59:36 +00:00
|
|
|
func (e *Exchange) QueryOpenOrders(ctx context.Context, symbol string) (orders []types.Order, err error) {
|
2021-03-07 04:53:41 +00:00
|
|
|
// TODO: invoke open trigger orders
|
2021-05-17 10:32:29 +00:00
|
|
|
resp, err := e.newRest().OpenOrders(ctx, toLocalSymbol(symbol))
|
2021-03-07 04:47:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !resp.Success {
|
|
|
|
return nil, fmt.Errorf("ftx returns querying open orders failure")
|
|
|
|
}
|
|
|
|
for _, r := range resp.Result {
|
2021-03-13 01:51:31 +00:00
|
|
|
o, err := toGlobalOrder(r)
|
2021-03-07 04:47:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
orders = append(orders, o)
|
|
|
|
}
|
|
|
|
return orders, nil
|
2021-02-05 09:29:38 +00:00
|
|
|
}
|
|
|
|
|
2021-03-17 13:26:25 +00:00
|
|
|
// symbol, since and until are all optional. FTX can only query by order created time, not updated time.
|
|
|
|
// FTX doesn't support lastOrderID, so we will query by the time range first, and filter by the lastOrderID.
|
2021-02-08 10:59:36 +00:00
|
|
|
func (e *Exchange) QueryClosedOrders(ctx context.Context, symbol string, since, until time.Time, lastOrderID uint64) (orders []types.Order, err error) {
|
2021-03-29 14:07:21 +00:00
|
|
|
if until == (time.Time{}) {
|
|
|
|
until = time.Now()
|
|
|
|
}
|
|
|
|
if since.After(until) {
|
|
|
|
return nil, fmt.Errorf("invalid query closed orders time range, since: %+v, until: %+v", since, until)
|
2021-03-17 13:26:25 +00:00
|
|
|
}
|
2021-03-23 14:27:11 +00:00
|
|
|
|
|
|
|
symbol = TrimUpperString(symbol)
|
|
|
|
limit := int64(100)
|
2021-03-17 13:26:25 +00:00
|
|
|
hasMoreData := true
|
|
|
|
s := since
|
|
|
|
var lastOrder order
|
|
|
|
for hasMoreData {
|
2021-12-13 23:15:18 +00:00
|
|
|
|
|
|
|
if err := requestLimit.Wait(ctx); err != nil {
|
|
|
|
logrus.WithError(err).Error("rate limit error")
|
|
|
|
}
|
|
|
|
|
2021-05-17 10:32:29 +00:00
|
|
|
resp, err := e.newRest().OrdersHistory(ctx, toLocalSymbol(symbol), s, until, limit)
|
2021-03-17 13:26:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !resp.Success {
|
|
|
|
return nil, fmt.Errorf("ftx returns querying orders history failure")
|
|
|
|
}
|
|
|
|
|
|
|
|
sortByCreatedASC(resp.Result)
|
|
|
|
|
|
|
|
for _, r := range resp.Result {
|
|
|
|
// There may be more than one orders at the same time, so also have to check the ID
|
2021-03-25 08:57:54 +00:00
|
|
|
if r.CreatedAt.Before(lastOrder.CreatedAt.Time) || r.ID == lastOrder.ID || r.Status != "closed" || r.ID < int64(lastOrderID) {
|
2021-03-17 13:26:25 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
lastOrder = r
|
|
|
|
o, err := toGlobalOrder(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
orders = append(orders, o)
|
|
|
|
}
|
|
|
|
hasMoreData = resp.HasMoreData
|
|
|
|
// the start_time and end_time precision is second. There might be more than one orders within one second.
|
|
|
|
s = lastOrder.CreatedAt.Add(-1 * time.Second)
|
|
|
|
}
|
|
|
|
return orders, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sortByCreatedASC(orders []order) {
|
|
|
|
sort.Slice(orders, func(i, j int) bool {
|
2021-03-25 08:57:54 +00:00
|
|
|
return orders[i].CreatedAt.Before(orders[j].CreatedAt.Time)
|
2021-03-17 13:26:25 +00:00
|
|
|
})
|
2021-02-05 09:29:38 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 10:59:36 +00:00
|
|
|
func (e *Exchange) CancelOrders(ctx context.Context, orders ...types.Order) error {
|
2021-03-16 14:31:17 +00:00
|
|
|
for _, o := range orders {
|
|
|
|
rest := e.newRest()
|
2021-12-10 15:08:26 +00:00
|
|
|
if err := requestLimit.Wait(ctx); err != nil {
|
|
|
|
logrus.WithError(err).Error("rate limit error")
|
|
|
|
}
|
2021-03-16 14:31:17 +00:00
|
|
|
if len(o.ClientOrderID) > 0 {
|
|
|
|
if _, err := rest.CancelOrderByClientID(ctx, o.ClientOrderID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, err := rest.CancelOrderByOrderID(ctx, o.OrderID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2021-02-05 09:29:38 +00:00
|
|
|
}
|
2021-02-27 09:24:08 +00:00
|
|
|
|
|
|
|
func (e *Exchange) QueryTicker(ctx context.Context, symbol string) (*types.Ticker, error) {
|
2021-12-13 23:19:21 +00:00
|
|
|
ticketMap, err := e.QueryTickers(ctx, symbol)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ticker, ok := ticketMap[symbol]; ok {
|
|
|
|
return &ticker, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("ticker %s not found", symbol)
|
2021-02-27 09:24:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exchange) QueryTickers(ctx context.Context, symbol ...string) (map[string]types.Ticker, error) {
|
2021-12-13 23:19:21 +00:00
|
|
|
|
|
|
|
var tickers = make(map[string]types.Ticker)
|
|
|
|
|
|
|
|
markets, err := e._queryMarkets(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
m := make(map[string]struct{})
|
|
|
|
for _, s := range symbol {
|
|
|
|
m[toGlobalSymbol(s)] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
rest := e.newRest()
|
|
|
|
|
|
|
|
for k, v := range markets {
|
|
|
|
|
|
|
|
// if we provide symbol as condition then we only query the gieven symbol ,
|
|
|
|
// or we should query "ALL" symbol in the market.
|
|
|
|
if _, ok := m[toGlobalSymbol(k)]; len(symbol) != 0 && !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := requestLimit.Wait(ctx); err != nil {
|
|
|
|
logrus.WithError(err).Errorf("order rate limiter wait error")
|
|
|
|
}
|
|
|
|
|
2022-02-18 06:01:47 +00:00
|
|
|
// ctx context.Context, market string, interval types.Interval, limit int64, start, end time.Time
|
2021-12-13 23:19:21 +00:00
|
|
|
prices, err := rest.HistoricalPrices(ctx, v.Market.LocalSymbol, types.Interval1h, 1, time.Now().Add(time.Duration(-1)*time.Hour), time.Now())
|
|
|
|
if err != nil || !prices.Success || len(prices.Result) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
lastCandle := prices.Result[0]
|
|
|
|
tickers[toGlobalSymbol(k)] = types.Ticker{
|
|
|
|
Time: lastCandle.StartTime.Time,
|
|
|
|
Volume: lastCandle.Volume,
|
|
|
|
Last: v.Last,
|
|
|
|
Open: lastCandle.Open,
|
|
|
|
High: lastCandle.High,
|
|
|
|
Low: lastCandle.Low,
|
|
|
|
Buy: v.Bid,
|
|
|
|
Sell: v.Ask,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tickers, nil
|
2021-02-27 09:24:08 +00:00
|
|
|
}
|
2021-08-07 06:50:12 +00:00
|
|
|
|
|
|
|
func (e *Exchange) Transfer(ctx context.Context, coin string, size float64, destination string) (string, error) {
|
|
|
|
payload := TransferPayload{
|
|
|
|
Coin: coin,
|
|
|
|
Size: size,
|
|
|
|
Source: e.subAccount,
|
|
|
|
Destination: destination,
|
|
|
|
}
|
|
|
|
resp, err := e.newRest().Transfer(ctx, payload)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if !resp.Success {
|
|
|
|
return "", fmt.Errorf("ftx returns transfer failure")
|
|
|
|
}
|
|
|
|
return resp.Result.String(), nil
|
|
|
|
}
|