Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions config/bfxfunding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
persistence:
redis:
host: 127.0.0.1
port: 6379
db: 0

sessions:
bitfinex:
exchange: bitfinex
envVarPrefix: BITFINEX

exchangeStrategies:

- on: bitfinex
bfxfunding:
currency: fUSD

# interval is how long do you want to update your order price and quantity
interval: 1m
1 change: 1 addition & 0 deletions pkg/cmd/strategy/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
_ "github.com/c9s/bbgo/pkg/strategy/audacitymaker"
_ "github.com/c9s/bbgo/pkg/strategy/autoborrow"
_ "github.com/c9s/bbgo/pkg/strategy/autobuy"
_ "github.com/c9s/bbgo/pkg/strategy/bfxfunding"
_ "github.com/c9s/bbgo/pkg/strategy/bollgrid"
_ "github.com/c9s/bbgo/pkg/strategy/bollmaker"
_ "github.com/c9s/bbgo/pkg/strategy/convert"
Expand Down
14 changes: 10 additions & 4 deletions pkg/exchange/bitfinex/bfxapi/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,10 @@ func parseBalanceUpdateEvent(payload json.RawMessage) (interface{}, error) {
return &event, nil
}

type FundingOfferSnapshotEvent struct {
Offers []FundingOfferUpdateEvent
}

// FundingOfferUpdateEvent represents a Bitfinex funding offer update event.
type FundingOfferUpdateEvent struct {
OfferID int64 // [0] OFFER_ID
Expand Down Expand Up @@ -1095,24 +1099,26 @@ func parseFundingOfferUpdate(arrJson json.RawMessage) (*FundingOfferUpdateEvent,
}

// parseFundingOfferSnapshot parses a funding offer snapshot array into []FundingOfferUpdateEvent.
func parseFundingOfferSnapshot(arrJson json.RawMessage) ([]FundingOfferUpdateEvent, error) {
func parseFundingOfferSnapshot(arrJson json.RawMessage) (*FundingOfferSnapshotEvent, error) {
var offerArrays []json.RawMessage
if err := json.Unmarshal(arrJson, &offerArrays); err != nil {
return nil, fmt.Errorf("failed to unmarshal funding offer snapshot array: %w", err)
}

events := make([]FundingOfferUpdateEvent, 0, len(offerArrays))
snapshot := &FundingOfferSnapshotEvent{}

snapshot.Offers = make([]FundingOfferUpdateEvent, 0, len(offerArrays))
for _, jsonArr := range offerArrays {
event, err := parseFundingOfferUpdate(jsonArr)
if err != nil {
log.WithError(err).Warnf("failed to parse funding offer fields: %s", jsonArr)
continue
} else if event != nil {
events = append(events, *event)
snapshot.Offers = append(snapshot.Offers, *event)
}
}

return events, nil
return snapshot, nil
}

// FundingInfo represents the inner funding info array.
Expand Down
7 changes: 4 additions & 3 deletions pkg/exchange/bitfinex/bfxapi/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,10 +545,11 @@ func TestParser_ParsePrivateMessages(t *testing.T) {
msg, err := p.Parse([]byte(body))
assert.NoError(t, err)
if assert.NotNil(t, msg) {
events, ok := msg.([]FundingOfferUpdateEvent)
event, ok := msg.(*FundingOfferSnapshotEvent)
offers := event.Offers
assert.True(t, ok, "expected []FundingOfferUpdateEvent type")
assert.Len(t, events, 1)
fo := events[0]
assert.Len(t, offers, 1)
fo := offers[0]
assert.Equal(t, int64(41237920), fo.OfferID)
assert.Equal(t, "fETH", fo.Symbol)
assert.Equal(t, int64(1573912039000), fo.MtsCreated.Time().UnixMilli())
Expand Down
8 changes: 6 additions & 2 deletions pkg/exchange/bitfinex/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,8 @@ func (e *Exchange) QueryOrderTrades(ctx context.Context, q types.OrderQuery) ([]
func (e *Exchange) QueryTrades(
ctx context.Context, symbol string, options *types.TradeQueryOptions,
) ([]types.Trade, error) {

req := e.client.NewGetTradeHistoryBySymbolRequest().Symbol(symbol)
req := e.client.NewGetTradeHistoryBySymbolRequest().
Symbol(toLocalSymbol(symbol))

if options != nil {
if options.StartTime != nil {
Expand Down Expand Up @@ -541,6 +541,10 @@ func (e *Exchange) QueryDepth(
return convertDepth(response, symbol), 0, nil
}

func (e *Exchange) GetApiClient() *bfxapi.Client {
return e.client
}

func MapSlice[T, M any](input []T, f func(T) M) []M {
result := make([]M, len(input))
for i, v := range input {
Expand Down
12 changes: 10 additions & 2 deletions pkg/exchange/bitfinex/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ type Stream struct {
bookUpdateEventCallbacks []func(e *bfxapi.BookUpdateEvent)
bookSnapshotEventCallbacks []func(e *bfxapi.BookSnapshotEvent)

fundingBookEventCallbacks []func(e *bfxapi.FundingBookUpdateEvent)
fundingBookSnapshotEventCallbacks []func(e *bfxapi.FundingBookSnapshotEvent)
fundingBookEventCallbacks []func(e *bfxapi.FundingBookUpdateEvent)
fundingBookSnapshotEventCallbacks []func(e *bfxapi.FundingBookSnapshotEvent)
fundingOfferSnapshotEventCallbacks []func(e *bfxapi.FundingOfferSnapshotEvent)
fundingOfferUpdateEventCallbacks []func(e *bfxapi.FundingOfferUpdateEvent)

walletSnapshotEventCallbacks []func(e *bfxapi.WalletSnapshotEvent)
walletUpdateEventCallbacks []func(e *bfxapi.Wallet)
Expand Down Expand Up @@ -342,6 +344,12 @@ func (s *Stream) dispatchEvent(e interface{}) {
case *bfxapi.FundingBookUpdateEvent:
s.EmitFundingBookEvent(evt)

case *bfxapi.FundingOfferSnapshotEvent:
s.EmitFundingOfferSnapshotEvent(evt)

case *bfxapi.FundingOfferUpdateEvent:
s.EmitFundingOfferUpdateEvent(evt)

default:
s.logger.Warnf("unhandled %T event: %+v", evt, evt)
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/exchange/bitfinex/stream_callbacks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

123 changes: 123 additions & 0 deletions pkg/strategy/bfxfunding/strategy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package bfxfunding

import (
"context"
"fmt"
"sync"

"github.com/sirupsen/logrus"

"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/exchange/bitfinex"
"github.com/c9s/bbgo/pkg/exchange/bitfinex/bfxapi"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)

const ID = "bfxfunding"

func init() {
bbgo.RegisterStrategy(ID, &Strategy{})
}

type Strategy struct {
Currency string `json:"currency"`
Amount fixedpoint.Value `json:"amount"`
Period int `json:"period"`

MinRate fixedpoint.Value `json:"minRate"`

exchange *bitfinex.Exchange
stream *bitfinex.Stream

client *bfxapi.Client

logger logrus.FieldLogger
}

func (s *Strategy) ID() string {
return ID
}

func (s *Strategy) InstanceID() string {
return fmt.Sprintf("%s-%s", ID, s.Currency)
}

func (s *Strategy) Defaults() error {
s.Currency = "fUSD"
s.MinRate = fixedpoint.NewFromFloat(0.01 * 0.01)
return nil
}

func (s *Strategy) Validate() error {
if s.Currency == "" {
return fmt.Errorf("currency is required")
}

if s.Amount.IsZero() {
return fmt.Errorf("amount must be greater than 0")
}

if s.Period <= 0 {
return fmt.Errorf("period must be greater than 0")
}

if s.MinRate.IsZero() {
return fmt.Errorf("minRate must be greater than 0")
}

return nil
}

func (s *Strategy) Initialize() error {
s.logger = logrus.WithFields(logrus.Fields{"strategy": s.InstanceID(), "currency": s.Currency})
return nil
}

func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
// session.Subscribe(types.KLineChannel, s.Currency, types.SubscribeOptions{Interval: s.Interval})
}

func (s *Strategy) handleFundingOfferSnapshot(e *bfxapi.FundingOfferSnapshotEvent) {
s.logger.Infof("funding offer snapshot event: %+v", e)
}

func (s *Strategy) handleFundingOfferUpdate(e *bfxapi.FundingOfferUpdateEvent) {
s.logger.Infof("funding offer update event: %+v", e)
}

func (s *Strategy) bookFunding(rate fixedpoint.Value) {
req := s.client.Funding().NewSubmitFundingOfferRequest()
req.Symbol(s.Currency).
Amount(s.Amount.String()).
OfferType(bfxapi.FundingOfferTypeLimit).
Rate(rate.String()).
Period(s.Period).Notify(true).
Hidden(false).
AutoRenew(true)
}

func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
if session.ExchangeName != types.ExchangeBitfinex {
return fmt.Errorf("bfxfunding strategy only works with bitfinex exchange")
}

ex, ok := session.Exchange.(*bitfinex.Exchange)
if !ok {
return fmt.Errorf("exchange is not bitfinex exchange")
}

s.exchange = ex
s.client = s.exchange.GetApiClient()
s.stream = session.UserDataStream.(*bitfinex.Stream)

s.stream.OnFundingOfferSnapshotEvent(s.handleFundingOfferSnapshot)
s.stream.OnFundingOfferUpdateEvent(s.handleFundingOfferUpdate)

bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done()
bbgo.Sync(ctx, s)
})

return nil
}
Loading