Skip to content

Commit

Permalink
autoborrow: add price solver
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Dec 3, 2024
1 parent ee22f9a commit 1b6b730
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
10 changes: 7 additions & 3 deletions pkg/pricesolver/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,13 @@ func (m *SimplePriceSolver) UpdateFromTrade(trade types.Trade) {
}

func (m *SimplePriceSolver) BindStream(stream types.Stream) {
stream.OnKLineClosed(func(k types.KLine) {
m.Update(k.Symbol, k.Close)
})
if stream.GetPublicOnly() {
stream.OnKLineClosed(func(k types.KLine) {
m.Update(k.Symbol, k.Close)
})
} else {
stream.OnTradeUpdate(m.UpdateFromTrade)
}
}

func (m *SimplePriceSolver) UpdateFromTickers(ctx context.Context, ex types.Exchange, symbols ...string) error {
Expand Down
8 changes: 8 additions & 0 deletions pkg/strategy/autoborrow/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/exchange/binance"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/pricesolver"
"github.com/c9s/bbgo/pkg/slack/slackalert"
"github.com/c9s/bbgo/pkg/types"
)
Expand Down Expand Up @@ -71,6 +72,8 @@ type Strategy struct {
ExchangeSession *bbgo.ExchangeSession

marginBorrowRepay types.MarginBorrowRepayService

priceSolver *pricesolver.SimplePriceSolver
}

func (s *Strategy) ID() string {
Expand Down Expand Up @@ -580,6 +583,11 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
go s.marginAlertWorker(ctx, alertInterval)
}

markets := session.Markets()
s.priceSolver = pricesolver.NewSimplePriceResolver(markets)
s.priceSolver.BindStream(session.MarketDataStream)
s.priceSolver.BindStream(session.UserDataStream)

go s.run(ctx, s.Interval.Duration())
return nil
}
Expand Down

0 comments on commit 1b6b730

Please sign in to comment.