From 0690518dc7d796313cee5a2f2f3d7297355696d6 Mon Sep 17 00:00:00 2001 From: narumi Date: Mon, 13 Mar 2023 22:43:42 +0800 Subject: [PATCH] add option to rebalance on start --- config/rebalance.yaml | 1 + pkg/strategy/rebalance/strategy.go | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/config/rebalance.yaml b/config/rebalance.yaml index e9a01e94b..8a6f7f8bd 100644 --- a/config/rebalance.yaml +++ b/config/rebalance.yaml @@ -39,3 +39,4 @@ exchangeStrategies: maxAmount: 1_000 # max amount to buy or sell per order orderType: LIMIT_MAKER # LIMIT, LIMIT_MAKER or MARKET dryRun: false + onStart: false diff --git a/pkg/strategy/rebalance/strategy.go b/pkg/strategy/rebalance/strategy.go index 3a482080a..6df7c0aae 100644 --- a/pkg/strategy/rebalance/strategy.go +++ b/pkg/strategy/rebalance/strategy.go @@ -34,6 +34,7 @@ type Strategy struct { MaxAmount fixedpoint.Value `json:"maxAmount"` // max amount to buy or sell per order OrderType types.OrderType `json:"orderType"` DryRun bool `json:"dryRun"` + OnStart bool `json:"onStart"` // rebalance on start PositionMap PositionMap `persistence:"positionMap"` ProfitStatsMap ProfitStatsMap `persistence:"profitStatsMap"` @@ -114,6 +115,12 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo. s.activeOrderBook = bbgo.NewActiveOrderBook("") s.activeOrderBook.BindStream(s.session.UserDataStream) + session.UserDataStream.OnStart(func() { + if s.OnStart { + s.rebalance(ctx) + } + }) + s.session.MarketDataStream.OnKLineClosed(func(kline types.KLine) { s.rebalance(ctx) })