From 6ef54bf2fbc62f21c0d3a5ee69747b180604f77d Mon Sep 17 00:00:00 2001 From: c9s Date: Tue, 21 Jun 2022 15:57:26 +0800 Subject: [PATCH 01/21] call bbgo.Sync to sync persistence --- pkg/bbgo/persistence.go | 10 +++++++--- pkg/strategy/bollmaker/strategy.go | 6 ++---- pkg/strategy/pivotshort/strategy.go | 3 +++ pkg/strategy/supertrend/strategy.go | 7 +++---- pkg/strategy/support/strategy.go | 2 +- 5 files changed, 16 insertions(+), 12 deletions(-) diff --git a/pkg/bbgo/persistence.go b/pkg/bbgo/persistence.go index 2b1be9f83..b435c8f07 100644 --- a/pkg/bbgo/persistence.go +++ b/pkg/bbgo/persistence.go @@ -91,14 +91,18 @@ func (p *Persistence) Sync(obj interface{}) error { } // Sync syncs the object properties into the persistence layer -func Sync(obj interface{}) error { +func Sync(obj interface{}) { id := callID(obj) if len(id) == 0 { - return nil + log.Warnf("InstanceID() is not provided, can not sync persistence") + return } ps := PersistenceServiceFacade.Get() - return storePersistenceFields(obj, id, ps) + err := storePersistenceFields(obj, id, ps) + if err != nil { + log.WithError(err).Errorf("persistence sync failed") + } } func loadPersistenceFields(obj interface{}, id string, persistence service.PersistenceService) error { diff --git a/pkg/strategy/bollmaker/strategy.go b/pkg/strategy/bollmaker/strategy.go index 9dec53ca9..a48eba33d 100644 --- a/pkg/strategy/bollmaker/strategy.go +++ b/pkg/strategy/bollmaker/strategy.go @@ -494,7 +494,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.OnSuspend(func() { s.Status = types.StrategyStatusStopped _ = s.orderExecutor.GracefulCancel(ctx) - _ = s.Persistence.Sync(s) + bbgo.Sync(s) }) s.OnEmergencyStop(func() { @@ -578,9 +578,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se // TODO: migrate persistance to singleton s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) { - if err := s.Persistence.Sync(s); err != nil { - log.WithError(err).Errorf("can not sync state to persistence") - } + bbgo.Sync(s) }) s.SmartStops.RunStopControllers(ctx, session, s.orderExecutor.TradeCollector()) diff --git a/pkg/strategy/pivotshort/strategy.go b/pkg/strategy/pivotshort/strategy.go index c9b28d631..7f4250ab8 100644 --- a/pkg/strategy/pivotshort/strategy.go +++ b/pkg/strategy/pivotshort/strategy.go @@ -231,6 +231,9 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.orderExecutor.BindEnvironment(s.Environment) s.orderExecutor.BindProfitStats(s.ProfitStats) s.orderExecutor.BindTradeStats(s.TradeStats) + s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) { + bbgo.Sync(s) + }) s.orderExecutor.Bind() store, _ := session.MarketDataStore(s.Symbol) diff --git a/pkg/strategy/supertrend/strategy.go b/pkg/strategy/supertrend/strategy.go index 5d70edfa9..e91313be3 100644 --- a/pkg/strategy/supertrend/strategy.go +++ b/pkg/strategy/supertrend/strategy.go @@ -3,9 +3,10 @@ package supertrend import ( "context" "fmt" - "github.com/c9s/bbgo/pkg/util" "sync" + "github.com/c9s/bbgo/pkg/util" + "github.com/pkg/errors" "github.com/sirupsen/logrus" @@ -256,9 +257,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se // Sync position to redis on trade s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) { - if err := s.Persistence.Sync(s); err != nil { - log.WithError(err).Errorf("can not sync state to persistence") - } + bbgo.Sync(s) }) s.stopC = make(chan struct{}) diff --git a/pkg/strategy/support/strategy.go b/pkg/strategy/support/strategy.go index e79eca793..ec489530a 100644 --- a/pkg/strategy/support/strategy.go +++ b/pkg/strategy/support/strategy.go @@ -352,7 +352,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.OnSuspend(func() { // Cancel all order _ = s.orderExecutor.GracefulCancel(ctx) - _ = s.Persistence.Sync(s) + bbgo.Sync(s) }) s.OnEmergencyStop(func() { From 3112b406349fd5149b2c547390d45d78f2dc71cc Mon Sep 17 00:00:00 2001 From: c9s Date: Tue, 21 Jun 2022 15:57:42 +0800 Subject: [PATCH 02/21] support: remove unused const --- pkg/strategy/support/strategy.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/pkg/strategy/support/strategy.go b/pkg/strategy/support/strategy.go index ec489530a..4a02e443a 100644 --- a/pkg/strategy/support/strategy.go +++ b/pkg/strategy/support/strategy.go @@ -15,8 +15,6 @@ import ( const ID = "support" -const stateKey = "state-v1" - var log = logrus.WithField("strategy", ID) var zeroiw = types.IntervalWindow{} From 46691d5ae14897afcc0313d643b4f0da05970634 Mon Sep 17 00:00:00 2001 From: c9s Date: Tue, 21 Jun 2022 16:10:23 +0800 Subject: [PATCH 03/21] strategy/xbalance: update xbalance persistence usage --- pkg/strategy/xbalance/strategy.go | 64 +++++++------------------------ 1 file changed, 14 insertions(+), 50 deletions(-) diff --git a/pkg/strategy/xbalance/strategy.go b/pkg/strategy/xbalance/strategy.go index 4ac0e851b..60f0ac6ae 100644 --- a/pkg/strategy/xbalance/strategy.go +++ b/pkg/strategy/xbalance/strategy.go @@ -13,7 +13,6 @@ import ( "github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/fixedpoint" - "github.com/c9s/bbgo/pkg/service" "github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/util" ) @@ -137,7 +136,6 @@ func (a *Address) UnmarshalJSON(body []byte) error { type Strategy struct { *bbgo.Graceful - *bbgo.Persistence Interval types.Duration `json:"interval"` @@ -158,7 +156,7 @@ type Strategy struct { Verbose bool `json:"verbose"` - state *State + State *State `persistence:"state"` } func (s *Strategy) ID() string { @@ -235,23 +233,23 @@ func (s *Strategy) checkBalance(ctx context.Context, sessions map[string]*bbgo.E requiredAmount = requiredAmount.Add(toAddress.ForeignFee) } - if s.state != nil { + if s.State != nil { if s.MaxDailyNumberOfTransfer > 0 { - if s.state.DailyNumberOfTransfers >= s.MaxDailyNumberOfTransfer { + if s.State.DailyNumberOfTransfers >= s.MaxDailyNumberOfTransfer { bbgo.Notify("⚠️ Exceeded %s max daily number of transfers %d (current %d), skipping transfer...", s.Asset, s.MaxDailyNumberOfTransfer, - s.state.DailyNumberOfTransfers) + s.State.DailyNumberOfTransfers) return } } if s.MaxDailyAmountOfTransfer.Sign() > 0 { - if s.state.DailyAmountOfTransfers.Compare(s.MaxDailyAmountOfTransfer) >= 0 { + if s.State.DailyAmountOfTransfers.Compare(s.MaxDailyAmountOfTransfer) >= 0 { bbgo.Notify("⚠️ Exceeded %s max daily amount of transfers %v (current %v), skipping transfer...", s.Asset, s.MaxDailyAmountOfTransfer, - s.state.DailyAmountOfTransfers) + s.State.DailyAmountOfTransfers) return } } @@ -275,14 +273,14 @@ func (s *Strategy) checkBalance(ctx context.Context, sessions map[string]*bbgo.E bbgo.Notify("%s withdrawal request sent", s.Asset) - if s.state != nil { - if s.state.IsOver24Hours() { - s.state.Reset() + if s.State != nil { + if s.State.IsOver24Hours() { + s.State.Reset() } - s.state.DailyNumberOfTransfers += 1 - s.state.DailyAmountOfTransfers = s.state.DailyAmountOfTransfers.Add(requiredAmount) - s.SaveState() + s.State.DailyNumberOfTransfers += 1 + s.State.DailyAmountOfTransfers = s.State.DailyAmountOfTransfers.Add(requiredAmount) + bbgo.Sync(s) } } @@ -327,15 +325,6 @@ func (s *Strategy) findLowBalanceLevelSession(sessions map[string]*bbgo.Exchange return nil, balance, nil } -func (s *Strategy) SaveState() { - if err := s.Persistence.Save(s.state, ID, s.Asset, stateKey); err != nil { - log.WithError(err).Errorf("can not save state: %+v", s.state) - } else { - log.Infof("%s %s state is saved: %+v", ID, s.Asset, s.state) - bbgo.Notify("%s %s state is saved", ID, s.Asset, s.state) - } -} - func (s *Strategy) newDefaultState() *State { return &State{ Asset: s.Asset, @@ -344,42 +333,17 @@ func (s *Strategy) newDefaultState() *State { } } -func (s *Strategy) LoadState() error { - var state State - if err := s.Persistence.Load(&state, ID, s.Asset, stateKey); err != nil { - if err != service.ErrPersistenceNotExists { - return err - } - - s.state = s.newDefaultState() - s.state.Reset() - } else { - // we loaded it successfully - s.state = &state - - // update Asset name for legacy caches - s.state.Asset = s.Asset - - log.Infof("%s %s state is restored: %+v", ID, s.Asset, s.state) - bbgo.Notify("%s %s state is restored", ID, s.Asset, s.state) - } - - return nil -} - func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, sessions map[string]*bbgo.ExchangeSession) error { if s.Interval == 0 { return errors.New("interval can not be zero") } - if err := s.LoadState(); err != nil { - return err + if s.State == nil { + s.State = s.newDefaultState() } s.Graceful.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) { defer wg.Done() - - s.SaveState() }) if s.CheckOnStart { From 2cd44b194a0e1bbab7bdd2ac9c20fc9d39a8fb45 Mon Sep 17 00:00:00 2001 From: c9s Date: Tue, 21 Jun 2022 16:14:01 +0800 Subject: [PATCH 04/21] pivotshort: remove persistence from pivotshort --- pkg/strategy/pivotshort/strategy.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/strategy/pivotshort/strategy.go b/pkg/strategy/pivotshort/strategy.go index 7f4250ab8..63db02a82 100644 --- a/pkg/strategy/pivotshort/strategy.go +++ b/pkg/strategy/pivotshort/strategy.go @@ -78,7 +78,6 @@ type Exit struct { type Strategy struct { *bbgo.Graceful - *bbgo.Persistence Environment *bbgo.Environment Symbol string `json:"symbol"` From 3e5d252c109a5545f3e04dd9bc1d5792a464bc3d Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 22 Jun 2022 13:05:31 +0800 Subject: [PATCH 05/21] rsmaker: clean up and remove unused code Signed-off-by: c9s --- pkg/strategy/pivotshort/strategy.go | 1 - pkg/strategy/rsmaker/strategy.go | 496 ++++++---------------------- 2 files changed, 92 insertions(+), 405 deletions(-) diff --git a/pkg/strategy/pivotshort/strategy.go b/pkg/strategy/pivotshort/strategy.go index 63db02a82..10a4d1b8f 100644 --- a/pkg/strategy/pivotshort/strategy.go +++ b/pkg/strategy/pivotshort/strategy.go @@ -204,7 +204,6 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.ProfitStats = types.NewProfitStats(s.Market) } - // trade stats if s.TradeStats == nil { s.TradeStats = &types.TradeStats{} } diff --git a/pkg/strategy/rsmaker/strategy.go b/pkg/strategy/rsmaker/strategy.go index fa7461df4..ed50d0189 100644 --- a/pkg/strategy/rsmaker/strategy.go +++ b/pkg/strategy/rsmaker/strategy.go @@ -4,19 +4,18 @@ import ( "context" "fmt" "math" - "time" "github.com/c9s/bbgo/pkg/indicator" "github.com/pkg/errors" "github.com/sirupsen/logrus" - "github.com/c9s/bbgo/pkg/bbgo" - "github.com/c9s/bbgo/pkg/fixedpoint" - "github.com/c9s/bbgo/pkg/service" - "github.com/c9s/bbgo/pkg/types" "github.com/muesli/clusters" "github.com/muesli/kmeans" + + "github.com/c9s/bbgo/pkg/bbgo" + "github.com/c9s/bbgo/pkg/fixedpoint" + "github.com/c9s/bbgo/pkg/types" ) // TODO: @@ -140,21 +139,20 @@ type Strategy struct { ShadowProtection bool `json:"shadowProtection"` ShadowProtectionRatio fixedpoint.Value `json:"shadowProtectionRatio"` + Position *types.Position `persistence:"position"` + ProfitStats *types.ProfitStats `persistence:"profit_stats"` + TradeStats *types.TradeStats `persistence:"trade_stats"` + bbgo.SmartStops - session *bbgo.ExchangeSession - book *types.StreamOrderBook + session *bbgo.ExchangeSession + orderExecutor *bbgo.GeneralOrderExecutor + book *types.StreamOrderBook state *State - activeMakerOrders *bbgo.ActiveOrderBook - orderStore *bbgo.OrderStore - tradeCollector *bbgo.TradeCollector - groupID uint32 - stopC chan struct{} - // defaultBoll is the BOLLINGER indicator we used for predicting the price. defaultBoll *indicator.BOLL @@ -178,23 +176,23 @@ func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) { Interval: s.Interval, }) - //session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{ + // session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{ // Interval: types.Interval12h.String(), - //}) + // }) - //if s.DefaultBollinger != nil && s.DefaultBollinger.Interval != "" { + // if s.DefaultBollinger != nil && s.DefaultBollinger.Interval != "" { // session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{ // Interval: string(s.DefaultBollinger.Interval), // }) - //} + // } // - //if s.NeutralBollinger != nil && s.NeutralBollinger.Interval != "" { + // if s.NeutralBollinger != nil && s.NeutralBollinger.Interval != "" { // session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{ // Interval: string(s.NeutralBollinger.Interval), // }) - //} + // } - //s.SmartStops.Subscribe(session) + // s.SmartStops.Subscribe(session) } func (s *Strategy) Validate() error { @@ -206,13 +204,13 @@ func (s *Strategy) Validate() error { } func (s *Strategy) CurrentPosition() *types.Position { - return s.state.Position + return s.Position } func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Value) error { - base := s.state.Position.GetBase() + base := s.Position.GetBase() if base.IsZero() { - return fmt.Errorf("no opened %s position", s.state.Position.Symbol) + return fmt.Errorf("no opened %s position", s.Position.Symbol) } // make it negative @@ -236,15 +234,10 @@ func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Valu s.Notify("Submitting %s %s order to close position by %v", s.Symbol, side.String(), percentage, submitOrder) - createdOrders, err := s.session.Exchange.SubmitOrders(ctx, submitOrder) + _, err := s.orderExecutor.SubmitOrders(ctx, submitOrder) if err != nil { log.WithError(err).Errorf("can not place position close order") } - - s.orderStore.Add(createdOrders...) - s.activeMakerOrders.Add(createdOrders...) - s.tradeCollector.Process() - return err } @@ -257,23 +250,11 @@ func (s *Strategy) GetStatus() types.StrategyStatus { func (s *Strategy) Suspend(ctx context.Context) error { s.status = types.StrategyStatusStopped - // Cancel all order - if err := s.activeMakerOrders.GracefulCancel(ctx, s.session.Exchange); err != nil { + if err := s.orderExecutor.GracefulCancel(ctx); err != nil { log.WithError(err).Errorf("graceful cancel order error") - s.Notify("graceful cancel order error") - } else { - s.Notify("All orders cancelled.") - } - - s.tradeCollector.Process() - - // Save state - if err := s.SaveState(); err != nil { - log.WithError(err).Errorf("can not save state: %+v", s.state) - } else { - log.Infof("%s position is saved.", s.Symbol) } + bbgo.Sync(s) return nil } @@ -283,7 +264,7 @@ func (s *Strategy) Resume(ctx context.Context) error { return nil } -//func (s *Strategy) EmergencyStop(ctx context.Context) error { +// func (s *Strategy) EmergencyStop(ctx context.Context) error { // // Close 100% position // percentage, _ := fixedpoint.NewFromString("100%") // err := s.ClosePosition(ctx, percentage) @@ -292,7 +273,7 @@ func (s *Strategy) Resume(ctx context.Context) error { // _ = s.Suspend(ctx) // // return err -//} +// } func (s *Strategy) SaveState() error { if err := s.Persistence.Save(s.state, ID, s.Symbol, stateKey); err != nil { @@ -303,37 +284,6 @@ func (s *Strategy) SaveState() error { return nil } -func (s *Strategy) LoadState() error { - var state State - - // load position - if err := s.Persistence.Load(&state, ID, s.Symbol, stateKey); err != nil { - if err != service.ErrPersistenceNotExists { - return err - } - - s.state = &State{} - } else { - s.state = &state - log.Infof("state is restored: %+v", s.state) - } - - // if position is nil, we need to allocate a new position for calculation - if s.state.Position == nil { - s.state.Position = types.NewPositionFromMarket(s.Market) - } - - // init profit states - s.state.ProfitStats.Symbol = s.Market.Symbol - s.state.ProfitStats.BaseCurrency = s.Market.BaseCurrency - s.state.ProfitStats.QuoteCurrency = s.Market.QuoteCurrency - if s.state.ProfitStats.AccumulatedSince == 0 { - s.state.ProfitStats.AccumulatedSince = time.Now().Unix() - } - - return nil -} - func (s *Strategy) getCurrentAllowedExposurePosition(bandPercentage float64) (fixedpoint.Value, error) { if s.DynamicExposurePositionScale != nil { v, err := s.DynamicExposurePositionScale.Scale(bandPercentage) @@ -346,16 +296,7 @@ func (s *Strategy) getCurrentAllowedExposurePosition(bandPercentage float64) (fi return s.MaxExposurePosition, nil } -func (s *Strategy) placeOrders(ctx context.Context, orderExecutor bbgo.OrderExecutor, midPrice fixedpoint.Value, klines []*types.KLine) { - //bidSpread := s.Spread - //if s.BidSpread.Sign() > 0 { - // bidSpread = s.BidSpread - //} - // - //askSpread := s.Spread - //if s.AskSpread.Sign() > 0 { - // askSpread = s.AskSpread - //} +func (s *Strategy) placeOrders(ctx context.Context, midPrice fixedpoint.Value, klines []*types.KLine) { // preprocessing max := 0. min := 100000. @@ -373,21 +314,21 @@ func (s *Strategy) placeOrders(ctx context.Context, orderExecutor bbgo.OrderExec } mv = mv / 50 - //logrus.Info(max, min) + // logrus.Info(max, min) // set up a random two-dimensional data set (float64 values between 0.0 and 1.0) var d clusters.Observations for x := 0; x < 50; x++ { - //if klines[x].High.Float64() < max || klines[x].Low.Float64() > min { + // if klines[x].High.Float64() < max || klines[x].Low.Float64() > min { if klines[x].Volume.Float64() > mv*0.3 { d = append(d, clusters.Coordinates{ klines[x].High.Float64(), klines[x].Low.Float64(), - //klines[x].Open.Float64(), - //klines[x].Close.Float64(), - //klines[x].Volume.Float64(), + // klines[x].Open.Float64(), + // klines[x].Close.Float64(), + // klines[x].Volume.Float64(), }) } - //} + // } } log.Info(len(d)) @@ -396,16 +337,16 @@ func (s *Strategy) placeOrders(ctx context.Context, orderExecutor bbgo.OrderExec km := kmeans.New() clusters, err := km.Partition(d, 3) - //for _, c := range clusters { - //fmt.Printf("Centered at x: %.2f y: %.2f\n", c.Center[0], c.Center[1]) - //fmt.Printf("Matching data points: %+v\n\n", c.Observations) - //} + // for _, c := range clusters { + // fmt.Printf("Centered at x: %.2f y: %.2f\n", c.Center[0], c.Center[1]) + // fmt.Printf("Matching data points: %+v\n\n", c.Observations) + // } // clustered virtual kline_1's mid price - //vk1mp := fixedpoint.NewFromFloat((clusters[0].Center[0] + clusters[0].Center[1]) / 2.) + // vk1mp := fixedpoint.NewFromFloat((clusters[0].Center[0] + clusters[0].Center[1]) / 2.) // clustered virtual kline_2's mid price - //vk2mp := fixedpoint.NewFromFloat((clusters[1].Center[0] + clusters[1].Center[1]) / 2.) + // vk2mp := fixedpoint.NewFromFloat((clusters[1].Center[0] + clusters[1].Center[1]) / 2.) // clustered virtual kline_3's mid price - //vk3mp := fixedpoint.NewFromFloat((clusters[2].Center[0] + clusters[2].Center[1]) / 2.) + // vk3mp := fixedpoint.NewFromFloat((clusters[2].Center[0] + clusters[2].Center[1]) / 2.) // clustered virtual kline_1's high price vk1hp := fixedpoint.NewFromFloat(clusters[0].Center[0]) @@ -421,50 +362,43 @@ func (s *Strategy) placeOrders(ctx context.Context, orderExecutor bbgo.OrderExec // clustered virtual kline_3's low price vk3lp := fixedpoint.NewFromFloat(clusters[2].Center[1]) - askPrice := fixedpoint.NewFromFloat(math.Max(math.Max(vk1hp.Float64(), vk2hp.Float64()), vk3hp.Float64())) //fixedpoint.NewFromFloat(math.Max(math.Max(vk1mp.Float64(), vk2mp.Float64()), vk3mp.Float64())) - bidPrice := fixedpoint.NewFromFloat(math.Min(math.Min(vk1lp.Float64(), vk2lp.Float64()), vk3lp.Float64())) //fixedpoint.NewFromFloat(math.Min(math.Min(vk1mp.Float64(), vk2mp.Float64()), vk3mp.Float64())) + askPrice := fixedpoint.NewFromFloat(math.Max(math.Max(vk1hp.Float64(), vk2hp.Float64()), vk3hp.Float64())) // fixedpoint.NewFromFloat(math.Max(math.Max(vk1mp.Float64(), vk2mp.Float64()), vk3mp.Float64())) + bidPrice := fixedpoint.NewFromFloat(math.Min(math.Min(vk1lp.Float64(), vk2lp.Float64()), vk3lp.Float64())) // fixedpoint.NewFromFloat(math.Min(math.Min(vk1mp.Float64(), vk2mp.Float64()), vk3mp.Float64())) - //if vk1mp.Compare(vk2mp) > 0 { + // if vk1mp.Compare(vk2mp) > 0 { // askPrice = vk1mp //.Mul(fixedpoint.NewFromFloat(1.001)) // bidPrice = vk2mp //.Mul(fixedpoint.NewFromFloat(0.999)) - //} else if vk1mp.Compare(vk2mp) < 0 { + // } else if vk1mp.Compare(vk2mp) < 0 { // askPrice = vk2mp //.Mul(fixedpoint.NewFromFloat(1.001)) // bidPrice = vk1mp //.Mul(fixedpoint.NewFromFloat(0.999)) - //} - //midPrice.Mul(fixedpoint.One.Add(askSpread)) - //midPrice.Mul(fixedpoint.One.Sub(bidSpread)) - base := s.state.Position.GetBase() - //balances := s.session.GetAccount().Balances() + // } + // midPrice.Mul(fixedpoint.One.Add(askSpread)) + // midPrice.Mul(fixedpoint.One.Sub(bidSpread)) + base := s.Position.GetBase() + // balances := s.session.GetAccount().Balances() - //log.Infof("mid price:%v spread: %s ask:%v bid: %v position: %s", - // midPrice, - // s.Spread.Percentage(), - // askPrice, - // bidPrice, - // s.state.Position, - //) canSell := true canBuy := true - //predMidPrice := (askPrice + bidPrice) / 2. + // predMidPrice := (askPrice + bidPrice) / 2. - //if midPrice.Float64() > predMidPrice.Float64() { + // if midPrice.Float64() > predMidPrice.Float64() { // bidPrice = predMidPrice.Mul(fixedpoint.NewFromFloat(0.999)) - //} + // } // - //if midPrice.Float64() < predMidPrice.Float64() { + // if midPrice.Float64() < predMidPrice.Float64() { // askPrice = predMidPrice.Mul(fixedpoint.NewFromFloat(1.001)) - //} + // } // - //if midPrice.Float64() > askPrice.Float64() { + // if midPrice.Float64() > askPrice.Float64() { // canBuy = false // askPrice = midPrice.Mul(fixedpoint.NewFromFloat(1.001)) - //} + // } // - //if midPrice.Float64() < bidPrice.Float64() { + // if midPrice.Float64() < bidPrice.Float64() { // canSell = false // bidPrice = midPrice.Mul(fixedpoint.NewFromFloat(0.999)) - //} + // } sellQuantity := s.QuantityOrAmount.CalculateQuantity(askPrice) buyQuantity := s.QuantityOrAmount.CalculateQuantity(bidPrice) @@ -491,8 +425,8 @@ func (s *Strategy) placeOrders(ctx context.Context, orderExecutor bbgo.OrderExec var submitBuyOrders []types.SubmitOrder var submitSellOrders []types.SubmitOrder - //baseBalance, hasBaseBalance := balances[s.Market.BaseCurrency] - //quoteBalance, hasQuoteBalance := balances[s.Market.QuoteCurrency] + // baseBalance, hasBaseBalance := balances[s.Market.BaseCurrency] + // quoteBalance, hasQuoteBalance := balances[s.Market.QuoteCurrency] downBand := s.defaultBoll.LastDownBand() upBand := s.defaultBoll.LastUpBand() @@ -522,106 +456,13 @@ func (s *Strategy) placeOrders(ctx context.Context, orderExecutor bbgo.OrderExec } } - //if s.ShadowProtection && kline != nil { - // switch kline.Direction() { - // case types.DirectionDown: - // shadowHeight := kline.GetLowerShadowHeight() - // shadowRatio := kline.GetLowerShadowRatio() - // if shadowHeight.IsZero() && shadowRatio.Compare(s.ShadowProtectionRatio) < 0 { - // log.Infof("%s shadow protection enabled, lower shadow ratio %v < %v", s.Symbol, shadowRatio, s.ShadowProtectionRatio) - // canBuy = false - // } - // case types.DirectionUp: - // shadowHeight := kline.GetUpperShadowHeight() - // shadowRatio := kline.GetUpperShadowRatio() - // if shadowHeight.IsZero() || shadowRatio.Compare(s.ShadowProtectionRatio) < 0 { - // log.Infof("%s shadow protection enabled, upper shadow ratio %v < %v", s.Symbol, shadowRatio, s.ShadowProtectionRatio) - // canSell = false - // } - // } - //} - - // Apply quantity skew - // CASE #1: - // WHEN: price is in the neutral bollginer band (window 1) == neutral - // THEN: we don't apply skew - // CASE #2: - // WHEN: price is in the upper band (window 2 > price > window 1) == upTrend - // THEN: we apply upTrend skew - // CASE #3: - // WHEN: price is in the lower band (window 2 < price < window 1) == downTrend - // THEN: we apply downTrend skew - // CASE #4: - // WHEN: price breaks the lower band (price < window 2) == strongDownTrend - // THEN: we apply strongDownTrend skew - // CASE #5: - // WHEN: price breaks the upper band (price > window 2) == strongUpTrend - // THEN: we apply strongUpTrend skew - //if s.TradeInBand { - // if !inBetween(midPrice.Float64(), s.neutralBoll.LastDownBand(), s.neutralBoll.LastUpBand()) { - // log.Infof("tradeInBand is set, skip placing orders when the price is outside of the band") - // return - // } - //} - - //revmacd := s.detectPriceTrend(s.neutralBoll, midPrice.Float64()) - //switch revmacd { - //case NeutralTrend: - // // do nothing - // - //case UpTrend: - // skew := s.UptrendSkew - // buyOrder.Quantity = fixedpoint.Max(s.Market.MinQuantity, sellOrder.Quantity.Mul(skew)) - // - //case DownTrend: - // skew := s.DowntrendSkew - // ratio := fixedpoint.One.Div(skew) - // sellOrder.Quantity = fixedpoint.Max(s.Market.MinQuantity, buyOrder.Quantity.Mul(ratio)) - // - //} - - //if !hasQuoteBalance || buyOrder.Quantity.Mul(buyOrder.Price).Compare(quoteBalance.Available) > 0 { - // canBuy = false - //} - // - //if !hasBaseBalance || sellOrder.Quantity.Compare(baseBalance.Available) > 0 { - // canSell = false - //} - - //if midPrice.Compare(s.state.Position.AverageCost.Mul(fixedpoint.One.Add(s.MinProfitSpread))) < 0 { - // canSell = false - //} - - //if s.Long != nil && *s.Long && base.Sub(sellOrder.Quantity).Sign() < 0 { - // canSell = false - //} - // - //if s.BuyBelowNeutralSMA && midPrice.Float64() > s.neutralBoll.LastSMA() { - // canBuy = false - //} - if canSell { submitSellOrders = append(submitSellOrders, sellOrder) - //sellOrder = s.adjustOrderPrice(sellOrder, false) - //submitSellOrders = append(submitSellOrders, sellOrder) - //sellOrder = s.adjustOrderPrice(sellOrder, false) - //submitSellOrders = append(submitSellOrders, sellOrder) } if canBuy { submitBuyOrders = append(submitBuyOrders, buyOrder) - //buyOrder = s.adjustOrderPrice(buyOrder, true) - //submitBuyOrders = append(submitBuyOrders, buyOrder) - //buyOrder = s.adjustOrderPrice(buyOrder, true) - //submitBuyOrders = append(submitBuyOrders, buyOrder) } - // condition for lower the average cost - /* - if midPrice < s.state.Position.AverageCost.MulFloat64(1.0-s.MinProfitSpread.Float64()) && canBuy { - submitOrders = append(submitOrders, buyOrder) - } - */ - for i := range submitBuyOrders { submitBuyOrders[i] = s.adjustOrderQuantity(submitBuyOrders[i]) } @@ -630,44 +471,12 @@ func (s *Strategy) placeOrders(ctx context.Context, orderExecutor bbgo.OrderExec submitSellOrders[i] = s.adjustOrderQuantity(submitSellOrders[i]) } - createdBuyOrders, err := orderExecutor.SubmitOrders(ctx, submitBuyOrders...) - if err != nil { - log.WithError(err).Errorf("can not place ping pong orders") + if _, err := s.orderExecutor.SubmitOrders(ctx, submitBuyOrders...); err != nil { + log.WithError(err).Errorf("can not place orders") } - s.orderStore.Add(createdBuyOrders...) - s.activeMakerOrders.Add(createdBuyOrders...) - - createdSellOrders, err := orderExecutor.SubmitOrders(ctx, submitSellOrders...) - if err != nil { - log.WithError(err).Errorf("can not place ping pong orders") + if _, err := s.orderExecutor.SubmitOrders(ctx, submitSellOrders...); err != nil { + log.WithError(err).Errorf("can not place orders") } - s.orderStore.Add(createdSellOrders...) - s.activeMakerOrders.Add(createdSellOrders...) -} - -type PriceTrend string - -const ( - NeutralTrend PriceTrend = "neutral" - UpTrend PriceTrend = "upTrend" - DownTrend PriceTrend = "downTrend" - UnknownTrend PriceTrend = "unknown" -) - -func (s *Strategy) detectPriceTrend(inc *indicator.BOLL, price float64) PriceTrend { - if inBetween(price, inc.LastDownBand(), inc.LastUpBand()) { - return NeutralTrend - } - - if price < inc.LastDownBand() { - return DownTrend - } - - if price > inc.LastUpBand() { - return UpTrend - } - - return UnknownTrend } func (s *Strategy) adjustOrderQuantity(submitOrder types.SubmitOrder) types.SubmitOrder { @@ -682,187 +491,66 @@ func (s *Strategy) adjustOrderQuantity(submitOrder types.SubmitOrder) types.Subm return submitOrder } -func (s *Strategy) adjustOrderPrice(submitOrder types.SubmitOrder, side bool) types.SubmitOrder { +func (s *Strategy) Run(ctx context.Context, session *bbgo.ExchangeSession) error { + instanceID := fmt.Sprintf("%s-%s", ID, s.Symbol) - if side { - submitOrder.Price = submitOrder.Price.Mul(fixedpoint.NewFromFloat(0.995)) - } else { - submitOrder.Price = submitOrder.Price.Mul(fixedpoint.NewFromFloat(1.005)) - } - - return submitOrder -} - -func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error { - // StrategyController s.status = types.StrategyStatusRunning - //if s.DisableShort { - // s.Long = &[]bool{true}[0] - //} - // - //if s.MinProfitSpread.IsZero() { - // s.MinProfitSpread = fixedpoint.NewFromFloat(0.001) - //} - // - //if s.UptrendSkew.IsZero() { - // s.UptrendSkew = fixedpoint.NewFromFloat(1.0 / 1.2) - //} - // - //if s.DowntrendSkew.IsZero() { - // s.DowntrendSkew = fixedpoint.NewFromFloat(1.2) - //} - // - //if s.ShadowProtectionRatio.IsZero() { - // s.ShadowProtectionRatio = fixedpoint.NewFromFloat(0.01) - //} + if s.Position == nil { + s.Position = types.NewPositionFromMarket(s.Market) + } + + if s.ProfitStats == nil { + s.ProfitStats = types.NewProfitStats(s.Market) + } + + if s.TradeStats == nil { + s.TradeStats = &types.TradeStats{} + } // initial required information s.session = session + s.orderExecutor = bbgo.NewGeneralOrderExecutor(session, s.Symbol, ID, instanceID, s.Position) + s.orderExecutor.BindEnvironment(s.Environment) + s.orderExecutor.BindProfitStats(s.ProfitStats) + s.orderExecutor.BindTradeStats(s.TradeStats) + s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) { + bbgo.Sync(s) + }) + s.orderExecutor.Bind() + s.neutralBoll = s.StandardIndicatorSet.BOLL(s.NeutralBollinger.IntervalWindow, s.NeutralBollinger.BandWidth) s.defaultBoll = s.StandardIndicatorSet.BOLL(s.DefaultBollinger.IntervalWindow, s.DefaultBollinger.BandWidth) - // calculate group id for orders - instanceID := fmt.Sprintf("%s-%s", ID, s.Symbol) - //s.groupID = max.GenerateGroupID(instanceID) - log.Infof("using group id %d from fnv(%s)", s.groupID, instanceID) - - // restore state - if err := s.LoadState(); err != nil { - return err - } - - s.state.Position.Strategy = ID - s.state.Position.StrategyInstanceID = instanceID - - //s.stopC = make(chan struct{}) - - s.activeMakerOrders = bbgo.NewActiveOrderBook(s.Symbol) - s.activeMakerOrders.BindStream(session.UserDataStream) - - s.orderStore = bbgo.NewOrderStore(s.Symbol) - s.orderStore.BindStream(session.UserDataStream) - - s.tradeCollector = bbgo.NewTradeCollector(s.Symbol, s.state.Position, s.orderStore) - - //s.tradeCollector.OnTrade(func(trade types.Trade, profit, netProfit fixedpoint.Value) { - // // StrategyController - // if s.status != types.StrategyStatusRunning { - // return - // } - // - // s.Notifiability.Notify(trade) - // s.state.ProfitStats.AddTrade(trade) - // - // if profit.Compare(fixedpoint.Zero) == 0 { - // s.Environment.RecordPosition(s.state.Position, trade, nil) - // } else { - // log.Infof("%s generated profit: %v", s.Symbol, profit) - // p := s.state.Position.NewProfit(trade, profit, netProfit) - // p.Strategy = ID - // p.StrategyInstanceID = instanceID - // s.Notify(&p) - // - // s.state.ProfitStats.AddProfit(p) - // s.Notify(&s.state.ProfitStats) - // - // s.Environment.RecordPosition(s.state.Position, trade, &p) - // } - //}) - // - //s.tradeCollector.OnPositionUpdate(func(position *types.Position) { - // log.Infof("position changed: %s", s.state.Position) - // s.Notify(s.state.Position) - //}) - - s.tradeCollector.BindStream(session.UserDataStream) - - //s.SmartStops.RunStopControllers(ctx, session, s.tradeCollector) - - //session.UserDataStream.OnStart(func() { - //if s.UseTickerPrice { - // ticker, err := s.session.Exchange.QueryTicker(ctx, s.Symbol) - // if err != nil { - // return - // } - // - // midPrice := ticker.Buy.Add(ticker.Sell).Div(two) - // s.placeOrders(ctx, orderExecutor, midPrice, nil) - //} else { - // if price, ok := session.LastPrice(s.Symbol); ok { - // s.placeOrders(ctx, orderExecutor, price, nil) - // } - //} - //}) + // s.SmartStops.RunStopControllers(ctx, session, s.tradeCollector) var klines []*types.KLine - session.MarketDataStream.OnKLineClosed(func(kline types.KLine) { // StrategyController if s.status != types.StrategyStatusRunning { return } - //if kline.Symbol != s.Symbol || kline.Interval != s.Interval { + // if kline.Symbol != s.Symbol || kline.Interval != s.Interval { // return - //} + // } if kline.Interval == s.Interval { klines = append(klines, &kline) } + if len(klines) > 50 { - //if s.UseTickerPrice { - // ticker, err := s.session.Exchange.QueryTicker(ctx, s.Symbol) - // if err != nil { - // return - // } - // - // midPrice := ticker.Buy.Add(ticker.Sell).Div(two) - // log.Infof("using ticker price: bid %v / ask %v, mid price %v", ticker.Buy, ticker.Sell, midPrice) - // s.placeOrders(ctx, orderExecutor, midPrice, klines[len(klines)-100:]) - // s.tradeCollector.Process() - //} - //else { if kline.Interval == s.Interval { - - //if s.state.Position.AverageCost.Div(kline.Close).Float64() < 0.999 { - // s.ClosePosition(ctx, fixedpoint.One) - // s.tradeCollector.Process() - //} - - if err := s.activeMakerOrders.GracefulCancel(ctx, s.session.Exchange); err != nil { + if err := s.orderExecutor.GracefulCancel(ctx); err != nil { log.WithError(err).Errorf("graceful cancel order error") } - // check if there is a canceled order had partially filled. - s.tradeCollector.Process() - - s.placeOrders(ctx, orderExecutor, kline.Close, klines[len(klines)-50:]) - s.tradeCollector.Process() + s.placeOrders(ctx, kline.Close, klines[len(klines)-50:]) } - //} } }) - // s.book = types.NewStreamBook(s.Symbol) - // s.book.BindStreamForBackground(session.MarketDataStream) - - //s.Graceful.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) { - // //defer wg.Done() - // //close(s.stopC) - // - // if err := s.activeMakerOrders.GracefulCancel(ctx, s.session.Exchange); err != nil { - // log.WithError(err).Errorf("graceful cancel order error") - // } - // - // s.tradeCollector.Process() - // - // if err := s.SaveState(); err != nil { - // log.WithError(err).Errorf("can not save state: %+v", s.state) - // } - //}) - return nil } From 16eeeb852c34060b31dd17414723ddb3d28661ef Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 22 Jun 2022 13:31:25 +0800 Subject: [PATCH 06/21] rsmaker: drop the legacy persistence state --- pkg/strategy/rsmaker/strategy.go | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/pkg/strategy/rsmaker/strategy.go b/pkg/strategy/rsmaker/strategy.go index ed50d0189..b3db11c0b 100644 --- a/pkg/strategy/rsmaker/strategy.go +++ b/pkg/strategy/rsmaker/strategy.go @@ -264,26 +264,6 @@ func (s *Strategy) Resume(ctx context.Context) error { return nil } -// func (s *Strategy) EmergencyStop(ctx context.Context) error { -// // Close 100% position -// percentage, _ := fixedpoint.NewFromString("100%") -// err := s.ClosePosition(ctx, percentage) -// -// // Suspend strategy -// _ = s.Suspend(ctx) -// -// return err -// } - -func (s *Strategy) SaveState() error { - if err := s.Persistence.Save(s.state, ID, s.Symbol, stateKey); err != nil { - return err - } - - log.Infof("state is saved => %+v", s.state) - return nil -} - func (s *Strategy) getCurrentAllowedExposurePosition(bandPercentage float64) (fixedpoint.Value, error) { if s.DynamicExposurePositionScale != nil { v, err := s.DynamicExposurePositionScale.Scale(bandPercentage) From b75da154a8b29253235579948c7445bd9d1b8916 Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 22 Jun 2022 13:31:48 +0800 Subject: [PATCH 07/21] rsmaker: remove legacy state struct --- pkg/strategy/rsmaker/strategy.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/pkg/strategy/rsmaker/strategy.go b/pkg/strategy/rsmaker/strategy.go index b3db11c0b..3233fa3d5 100644 --- a/pkg/strategy/rsmaker/strategy.go +++ b/pkg/strategy/rsmaker/strategy.go @@ -36,11 +36,6 @@ func init() { bbgo.RegisterStrategy(ID, &Strategy{}) } -type State struct { - Position *types.Position `json:"position,omitempty"` - ProfitStats types.ProfitStats `json:"profitStats,omitempty"` -} - type BollingerSetting struct { types.IntervalWindow BandWidth float64 `json:"bandWidth"` @@ -149,8 +144,6 @@ type Strategy struct { orderExecutor *bbgo.GeneralOrderExecutor book *types.StreamOrderBook - state *State - groupID uint32 // defaultBoll is the BOLLINGER indicator we used for predicting the price. From 5fe0f5a29958809462c47b7ed76482807480c813 Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 22 Jun 2022 13:33:15 +0800 Subject: [PATCH 08/21] pull out bollinger settings --- pkg/strategy/rsmaker/strategy.go | 9 ++------- pkg/types/bollinger.go | 8 ++++++++ 2 files changed, 10 insertions(+), 7 deletions(-) create mode 100644 pkg/types/bollinger.go diff --git a/pkg/strategy/rsmaker/strategy.go b/pkg/strategy/rsmaker/strategy.go index 3233fa3d5..0bce3fca5 100644 --- a/pkg/strategy/rsmaker/strategy.go +++ b/pkg/strategy/rsmaker/strategy.go @@ -36,11 +36,6 @@ func init() { bbgo.RegisterStrategy(ID, &Strategy{}) } -type BollingerSetting struct { - types.IntervalWindow - BandWidth float64 `json:"bandWidth"` -} - type Strategy struct { *bbgo.Graceful *bbgo.Notifiability @@ -108,11 +103,11 @@ type Strategy struct { // NeutralBollinger is the smaller range of the bollinger band // If price is in this band, it usually means the price is oscillating. // If price goes out of this band, we tend to not place sell orders or buy orders - NeutralBollinger *BollingerSetting `json:"neutralBollinger"` + NeutralBollinger *types.BollingerSetting `json:"neutralBollinger"` // DefaultBollinger is the wide range of the bollinger band // for controlling your exposure position - DefaultBollinger *BollingerSetting `json:"defaultBollinger"` + DefaultBollinger *types.BollingerSetting `json:"defaultBollinger"` // DowntrendSkew is the order quantity skew for normal downtrend band. // The price is still in the default bollinger band. diff --git a/pkg/types/bollinger.go b/pkg/types/bollinger.go new file mode 100644 index 000000000..9e7f4b82c --- /dev/null +++ b/pkg/types/bollinger.go @@ -0,0 +1,8 @@ +package types + +// BollingerSetting contains the bollinger indicator setting propers +// Interval, Window and BandWidth +type BollingerSetting struct { + IntervalWindow + BandWidth float64 `json:"bandWidth"` +} From a5cb8355d433a15624580ae14ee2ba36127bfaa7 Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 22 Jun 2022 13:35:27 +0800 Subject: [PATCH 09/21] dca: rewrite dca with the new order executor --- pkg/strategy/dca/strategy.go | 69 ++++++++---------------------------- 1 file changed, 14 insertions(+), 55 deletions(-) diff --git a/pkg/strategy/dca/strategy.go b/pkg/strategy/dca/strategy.go index 932eb570e..615d76159 100644 --- a/pkg/strategy/dca/strategy.go +++ b/pkg/strategy/dca/strategy.go @@ -71,12 +71,13 @@ type Strategy struct { BudgetQuota fixedpoint.Value `persistence:"budget_quota"` BudgetPeriodStartTime time.Time `persistence:"budget_period_start_time"` + session *bbgo.ExchangeSession + orderExecutor *bbgo.GeneralOrderExecutor + activeMakerOrders *bbgo.ActiveOrderBook orderStore *bbgo.OrderStore tradeCollector *bbgo.TradeCollector - session *bbgo.ExchangeSession - bbgo.StrategyController } @@ -141,20 +142,10 @@ func (s *Strategy) InstanceID() string { return fmt.Sprintf("%s:%s", ID, s.Symbol) } -// check if position can be close or not -func canClosePosition(position *types.Position, signal fixedpoint.Value, price fixedpoint.Value) bool { - return !signal.IsZero() && position.IsShort() && !position.IsDust(price) -} - func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error { - // initial required information - s.session = session - - s.activeMakerOrders = bbgo.NewActiveOrderBook(s.Symbol) - s.activeMakerOrders.BindStream(session.UserDataStream) - - s.orderStore = bbgo.NewOrderStore(s.Symbol) - s.orderStore.BindStream(session.UserDataStream) + if s.BudgetQuota.IsZero() { + s.BudgetQuota = s.Budget + } if s.Position == nil { s.Position = types.NewPositionFromMarket(s.Market) @@ -165,50 +156,18 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se } instanceID := s.InstanceID() - - if s.BudgetQuota.IsZero() { - s.BudgetQuota = s.Budget - } + s.session = session + s.orderExecutor = bbgo.NewGeneralOrderExecutor(session, s.Symbol, ID, instanceID, s.Position) + s.orderExecutor.BindEnvironment(s.Environment) + s.orderExecutor.BindProfitStats(s.ProfitStats) + s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) { + bbgo.Sync(s) + }) + s.orderExecutor.Bind() numOfInvestmentPerPeriod := fixedpoint.NewFromFloat(float64(s.BudgetPeriod.Duration()) / float64(s.InvestmentInterval.Duration())) s.budgetPerInvestment = s.Budget.Div(numOfInvestmentPerPeriod) - // Always update the position fields - s.Position.Strategy = ID - s.Position.StrategyInstanceID = instanceID - - s.tradeCollector = bbgo.NewTradeCollector(s.Symbol, s.Position, s.orderStore) - s.tradeCollector.OnTrade(func(trade types.Trade, profit, netProfit fixedpoint.Value) { - bbgo.Notify(trade) - s.ProfitStats.AddTrade(trade) - - if profit.Compare(fixedpoint.Zero) == 0 { - s.Environment.RecordPosition(s.Position, trade, nil) - } else { - log.Infof("%s generated profit: %v", s.Symbol, profit) - p := s.Position.NewProfit(trade, profit, netProfit) - p.Strategy = ID - p.StrategyInstanceID = instanceID - bbgo.Notify(&p) - - s.ProfitStats.AddProfit(p) - bbgo.Notify(&s.ProfitStats) - - s.Environment.RecordPosition(s.Position, trade, &p) - } - }) - - s.tradeCollector.OnTrade(func(trade types.Trade, profit fixedpoint.Value, netProfit fixedpoint.Value) { - s.BudgetQuota = s.BudgetQuota.Sub(trade.QuoteQuantity) - }) - - s.tradeCollector.OnPositionUpdate(func(position *types.Position) { - log.Infof("position changed: %s", s.Position) - bbgo.Notify(s.Position) - }) - - s.tradeCollector.BindStream(session.UserDataStream) - session.UserDataStream.OnStart(func() {}) session.MarketDataStream.OnKLine(func(kline types.KLine) {}) session.MarketDataStream.OnKLineClosed(func(kline types.KLine) { From 929ffc3e5e54a43426a2d3b5e641523d0a5b71fb Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 22 Jun 2022 13:37:32 +0800 Subject: [PATCH 10/21] dca: clean up --- pkg/strategy/dca/strategy.go | 31 ++++++------------------------- 1 file changed, 6 insertions(+), 25 deletions(-) diff --git a/pkg/strategy/dca/strategy.go b/pkg/strategy/dca/strategy.go index 615d76159..fd4e371bb 100644 --- a/pkg/strategy/dca/strategy.go +++ b/pkg/strategy/dca/strategy.go @@ -48,7 +48,6 @@ func (b BudgetPeriod) Duration() time.Duration { // Strategy is the Dollar-Cost-Average strategy type Strategy struct { *bbgo.Graceful - *bbgo.Persistence Environment *bbgo.Environment Symbol string `json:"symbol"` @@ -74,10 +73,6 @@ type Strategy struct { session *bbgo.ExchangeSession orderExecutor *bbgo.GeneralOrderExecutor - activeMakerOrders *bbgo.ActiveOrderBook - orderStore *bbgo.OrderStore - tradeCollector *bbgo.TradeCollector - bbgo.StrategyController } @@ -89,17 +84,6 @@ func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) { session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.InvestmentInterval}) } -func (s *Strategy) submitOrders(ctx context.Context, orderExecutor bbgo.OrderExecutor, submitOrders ...types.SubmitOrder) { - createdOrders, err := orderExecutor.SubmitOrders(ctx, submitOrders...) - if err != nil { - log.WithError(err).Errorf("can not place orders") - } - - s.orderStore.Add(createdOrders...) - s.activeMakerOrders.Add(createdOrders...) - s.tradeCollector.Process() -} - func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Value) error { base := s.Position.GetBase() if base.IsZero() { @@ -125,16 +109,10 @@ func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Valu Market: s.Market, } - // s.Notify("Submitting %s %s order to close position by %v", s.Symbol, side.String(), percentage, submitOrder) - - createdOrders, err := s.session.Exchange.SubmitOrders(ctx, submitOrder) + _, err := s.orderExecutor.SubmitOrders(ctx, submitOrder) if err != nil { log.WithError(err).Errorf("can not place position close order") } - - s.orderStore.Add(createdOrders...) - s.activeMakerOrders.Add(createdOrders...) - s.tradeCollector.Process() return err } @@ -142,7 +120,7 @@ func (s *Strategy) InstanceID() string { return fmt.Sprintf("%s:%s", ID, s.Symbol) } -func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error { +func (s *Strategy) Run(ctx context.Context, session *bbgo.ExchangeSession) error { if s.BudgetQuota.IsZero() { s.BudgetQuota = s.Budget } @@ -193,13 +171,16 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se price := kline.Close quantity := s.budgetPerInvestment.Div(price) - s.submitOrders(ctx, orderExecutor, types.SubmitOrder{ + _, err := s.orderExecutor.SubmitOrders(ctx, types.SubmitOrder{ Symbol: s.Symbol, Side: types.SideTypeBuy, Type: types.OrderTypeMarket, Quantity: quantity, Market: s.Market, }) + if err != nil { + log.WithError(err).Errorf("submit order failed") + } }) return nil From b3160815ffbb04602b2497f959d39cf0c65c49df Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 22 Jun 2022 13:43:54 +0800 Subject: [PATCH 11/21] dca: use order executor to close position --- pkg/strategy/dca/strategy.go | 30 +----------------------------- 1 file changed, 1 insertion(+), 29 deletions(-) diff --git a/pkg/strategy/dca/strategy.go b/pkg/strategy/dca/strategy.go index fd4e371bb..e8dd92774 100644 --- a/pkg/strategy/dca/strategy.go +++ b/pkg/strategy/dca/strategy.go @@ -85,35 +85,7 @@ func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) { } func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Value) error { - base := s.Position.GetBase() - if base.IsZero() { - return fmt.Errorf("no opened %s position", s.Position.Symbol) - } - - // make it negative - quantity := base.Mul(percentage).Abs() - side := types.SideTypeBuy - if base.Sign() > 0 { - side = types.SideTypeSell - } - - if quantity.Compare(s.Market.MinQuantity) < 0 { - return fmt.Errorf("order quantity %v is too small, less than %v", quantity, s.Market.MinQuantity) - } - - submitOrder := types.SubmitOrder{ - Symbol: s.Symbol, - Side: side, - Type: types.OrderTypeMarket, - Quantity: quantity, - Market: s.Market, - } - - _, err := s.orderExecutor.SubmitOrders(ctx, submitOrder) - if err != nil { - log.WithError(err).Errorf("can not place position close order") - } - return err + return s.orderExecutor.ClosePosition(ctx, percentage) } func (s *Strategy) InstanceID() string { From dbc6d4fb44a636fb4aa87fd9df54f4a3a7188447 Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 22 Jun 2022 13:44:46 +0800 Subject: [PATCH 12/21] bollmaker: refactor ClosePosition method --- pkg/strategy/bollmaker/strategy.go | 29 +---------------------------- 1 file changed, 1 insertion(+), 28 deletions(-) diff --git a/pkg/strategy/bollmaker/strategy.go b/pkg/strategy/bollmaker/strategy.go index a48eba33d..25a7859c3 100644 --- a/pkg/strategy/bollmaker/strategy.go +++ b/pkg/strategy/bollmaker/strategy.go @@ -215,34 +215,7 @@ func (s *Strategy) CurrentPosition() *types.Position { } func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Value) error { - base := s.Position.GetBase() - if base.IsZero() { - return fmt.Errorf("no opened %s position", s.Position.Symbol) - } - - // make it negative - quantity := base.Mul(percentage).Abs() - side := types.SideTypeBuy - if base.Sign() > 0 { - side = types.SideTypeSell - } - - if quantity.Compare(s.Market.MinQuantity) < 0 { - return fmt.Errorf("order quantity %v is too small, less than %v", quantity, s.Market.MinQuantity) - } - - submitOrder := types.SubmitOrder{ - Symbol: s.Symbol, - Side: side, - Type: types.OrderTypeMarket, - Quantity: quantity, - Market: s.Market, - } - - bbgo.Notify("Submitting %s %s order to close position by %v", s.Symbol, side.String(), percentage, submitOrder) - - _, err := s.orderExecutor.SubmitOrders(ctx, submitOrder) - return err + return s.orderExecutor.ClosePosition(ctx, percentage) } // Deprecated: LoadState method is migrated to the persistence struct tag. From 09d0a9bbc7ed50ea404bb59d076906d7f8850ec5 Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 22 Jun 2022 13:45:48 +0800 Subject: [PATCH 13/21] pivotshort: clean up ClosePosition method --- pkg/strategy/pivotshort/strategy.go | 29 +++++------------------------ 1 file changed, 5 insertions(+), 24 deletions(-) diff --git a/pkg/strategy/pivotshort/strategy.go b/pkg/strategy/pivotshort/strategy.go index 10a4d1b8f..38ce71244 100644 --- a/pkg/strategy/pivotshort/strategy.go +++ b/pkg/strategy/pivotshort/strategy.go @@ -162,35 +162,16 @@ func (s *Strategy) placeMarketSell(ctx context.Context, quantity fixedpoint.Valu }) } +func (s *Strategy) InstanceID() string { + return fmt.Sprintf("%s:%s", ID, s.Symbol) +} + func (s *Strategy) CurrentPosition() *types.Position { return s.Position } func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Value) error { - // Cancel active orders - _ = s.orderExecutor.GracefulCancel(ctx) - - submitOrder := s.Position.NewMarketCloseOrder(percentage) // types.SubmitOrder{ - if submitOrder == nil { - return nil - } - - if s.session.Margin { - submitOrder.MarginSideEffect = s.Exit.MarginSideEffect - } - - bbgo.Notify("Closing %s position by %f", s.Symbol, percentage.Float64()) - log.Infof("Closing %s position by %f", s.Symbol, percentage.Float64()) - _, err := s.orderExecutor.SubmitOrders(ctx, *submitOrder) - if err != nil { - bbgo.Notify("close %s position error", s.Symbol) - log.WithError(err).Errorf("close %s position error", s.Symbol) - } - return err -} - -func (s *Strategy) InstanceID() string { - return fmt.Sprintf("%s:%s", ID, s.Symbol) + return s.orderExecutor.ClosePosition(ctx, percentage) } func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error { From bae685d63d1abcbd47eb35cc95f459f7da4f409c Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 22 Jun 2022 13:51:36 +0800 Subject: [PATCH 14/21] rsmaker: refactor ClosePosition method --- pkg/strategy/rsmaker/strategy.go | 50 +------------------------------- 1 file changed, 1 insertion(+), 49 deletions(-) diff --git a/pkg/strategy/rsmaker/strategy.go b/pkg/strategy/rsmaker/strategy.go index 0bce3fca5..1e8209e31 100644 --- a/pkg/strategy/rsmaker/strategy.go +++ b/pkg/strategy/rsmaker/strategy.go @@ -163,23 +163,6 @@ func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) { session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{ Interval: s.Interval, }) - - // session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{ - // Interval: types.Interval12h.String(), - // }) - - // if s.DefaultBollinger != nil && s.DefaultBollinger.Interval != "" { - // session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{ - // Interval: string(s.DefaultBollinger.Interval), - // }) - // } - // - // if s.NeutralBollinger != nil && s.NeutralBollinger.Interval != "" { - // session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{ - // Interval: string(s.NeutralBollinger.Interval), - // }) - // } - // s.SmartStops.Subscribe(session) } @@ -196,41 +179,10 @@ func (s *Strategy) CurrentPosition() *types.Position { } func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Value) error { - base := s.Position.GetBase() - if base.IsZero() { - return fmt.Errorf("no opened %s position", s.Position.Symbol) - } - - // make it negative - quantity := base.Mul(percentage).Abs() - side := types.SideTypeBuy - if base.Sign() > 0 { - side = types.SideTypeSell - } - - if quantity.Compare(s.Market.MinQuantity) < 0 { - return fmt.Errorf("order quantity %v is too small, less than %v", quantity, s.Market.MinQuantity) - } - - submitOrder := types.SubmitOrder{ - Symbol: s.Symbol, - Side: side, - Type: types.OrderTypeMarket, - Quantity: quantity, - Market: s.Market, - } - - s.Notify("Submitting %s %s order to close position by %v", s.Symbol, side.String(), percentage, submitOrder) - - _, err := s.orderExecutor.SubmitOrders(ctx, submitOrder) - if err != nil { - log.WithError(err).Errorf("can not place position close order") - } - return err + return s.orderExecutor.ClosePosition(ctx, percentage) } // StrategyController - func (s *Strategy) GetStatus() types.StrategyStatus { return s.status } From 51a2f14af7dd3d0544cc28909575890f2c86591a Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 22 Jun 2022 13:52:18 +0800 Subject: [PATCH 15/21] rsmaker: remove unused vars --- pkg/strategy/rsmaker/strategy.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/pkg/strategy/rsmaker/strategy.go b/pkg/strategy/rsmaker/strategy.go index 1e8209e31..86c5745fb 100644 --- a/pkg/strategy/rsmaker/strategy.go +++ b/pkg/strategy/rsmaker/strategy.go @@ -18,17 +18,9 @@ import ( "github.com/c9s/bbgo/pkg/types" ) -// TODO: -// 1) add option for placing orders only when in neutral band -// 2) add option for only placing buy orders when price is below the SMA line - const ID = "rsmaker" -const stateKey = "state-v1" - -var defaultFeeRate = fixedpoint.NewFromFloat(0.001) var notionModifier = fixedpoint.NewFromFloat(1.1) -var two = fixedpoint.NewFromInt(2) var log = logrus.WithField("strategy", ID) From 5d72ffaa0f61261db67df06658fc6985bc181a22 Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 22 Jun 2022 13:52:40 +0800 Subject: [PATCH 16/21] rsmaker: remove embedded bbgo.Persistence --- pkg/strategy/rsmaker/strategy.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/strategy/rsmaker/strategy.go b/pkg/strategy/rsmaker/strategy.go index 86c5745fb..b4ad560b3 100644 --- a/pkg/strategy/rsmaker/strategy.go +++ b/pkg/strategy/rsmaker/strategy.go @@ -31,7 +31,6 @@ func init() { type Strategy struct { *bbgo.Graceful *bbgo.Notifiability - *bbgo.Persistence Environment *bbgo.Environment StandardIndicatorSet *bbgo.StandardIndicatorSet From 027f1f01cf9f51d5eb587d2a743b899d4ead10ba Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 22 Jun 2022 15:19:22 +0800 Subject: [PATCH 17/21] improve callID fallback for persistence --- pkg/bbgo/persistence_test.go | 21 +++++++++++++++++++-- pkg/bbgo/reflect.go | 12 +++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/pkg/bbgo/persistence_test.go b/pkg/bbgo/persistence_test.go index c58cd9eda..ebc5314f0 100644 --- a/pkg/bbgo/persistence_test.go +++ b/pkg/bbgo/persistence_test.go @@ -12,6 +12,15 @@ import ( "github.com/c9s/bbgo/pkg/types" ) +type TestStructWithoutInstanceID struct { + Symbol string +} + +func (s *TestStructWithoutInstanceID) ID() string { + return "test-struct-no-instance-id" +} + + type TestStruct struct { *Environment *Graceful @@ -48,8 +57,16 @@ func preparePersistentServices() []service.PersistenceService { } func Test_callID(t *testing.T) { - id := callID(&TestStruct{}) - assert.NotEmpty(t, id) + t.Run("default", func(t *testing.T) { + id := callID(&TestStruct{}) + assert.NotEmpty(t, id) + assert.Equal(t, "test-struct", id) + }) + + t.Run("fallback", func(t *testing.T) { + id := callID(&TestStructWithoutInstanceID{Symbol: "BTCUSDT"}) + assert.Equal(t, "test-struct-no-instance-id:BTCUSDT", id) + }) } func Test_loadPersistenceFields(t *testing.T) { diff --git a/pkg/bbgo/reflect.go b/pkg/bbgo/reflect.go index 4f72e29ed..c9cb5a33b 100644 --- a/pkg/bbgo/reflect.go +++ b/pkg/bbgo/reflect.go @@ -18,7 +18,17 @@ func callID(obj interface{}) string { ret := m.Call(nil) return ret[0].String() } - return "" + + if symbol, ok := isSymbolBasedStrategy(sv); ok { + m := sv.MethodByName("ID") + ret := m.Call(nil) + return ret[0].String() + ":" + symbol + } + + // fallback to just ID + m := sv.MethodByName("ID") + ret := m.Call(nil) + return ret[0].String() + ":" } func isSymbolBasedStrategy(rs reflect.Value) (string, bool) { From 60d2ac161688dc4df1e377dc81cdb97b9f4631a9 Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 22 Jun 2022 15:37:02 +0800 Subject: [PATCH 18/21] ewoDgtrd: clean up embedded struct --- pkg/strategy/ewoDgtrd/strategy.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/pkg/strategy/ewoDgtrd/strategy.go b/pkg/strategy/ewoDgtrd/strategy.go index 48f5dc3d0..2f36a4544 100644 --- a/pkg/strategy/ewoDgtrd/strategy.go +++ b/pkg/strategy/ewoDgtrd/strategy.go @@ -51,8 +51,6 @@ type Strategy struct { KLineEndTime types.Time *bbgo.Environment - *bbgo.Notifiability - *bbgo.Persistence *bbgo.Graceful bbgo.StrategyController From fa26d5260fc1368680c34c95ed262d9db6a863ea Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 22 Jun 2022 16:18:50 +0800 Subject: [PATCH 19/21] bollmaker: use bbgo.IsBackTesting --- pkg/strategy/bollmaker/strategy.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/strategy/bollmaker/strategy.go b/pkg/strategy/bollmaker/strategy.go index 25a7859c3..eaa4da009 100644 --- a/pkg/strategy/bollmaker/strategy.go +++ b/pkg/strategy/bollmaker/strategy.go @@ -556,7 +556,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.SmartStops.RunStopControllers(ctx, session, s.orderExecutor.TradeCollector()) - if s.Environment.IsBackTesting() { + if bbgo.IsBackTesting { log.Warn("turning of useTickerPrice option in the back-testing environment...") s.UseTickerPrice = false } From c26d0d7824af1705b5cc5ffd762646cabde5100a Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 22 Jun 2022 16:20:59 +0800 Subject: [PATCH 20/21] bollmaker: clean up commment --- pkg/strategy/bollmaker/strategy.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/strategy/bollmaker/strategy.go b/pkg/strategy/bollmaker/strategy.go index eaa4da009..a355f2ea8 100644 --- a/pkg/strategy/bollmaker/strategy.go +++ b/pkg/strategy/bollmaker/strategy.go @@ -549,7 +549,6 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.stopC = make(chan struct{}) - // TODO: migrate persistance to singleton s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) { bbgo.Sync(s) }) From 3150480db84fe71e8edad25f7ea840f0298dd27e Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 22 Jun 2022 16:30:29 +0800 Subject: [PATCH 21/21] bollmaker: remove stopC --- pkg/strategy/bollmaker/strategy.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/pkg/strategy/bollmaker/strategy.go b/pkg/strategy/bollmaker/strategy.go index a355f2ea8..93b135b9d 100644 --- a/pkg/strategy/bollmaker/strategy.go +++ b/pkg/strategy/bollmaker/strategy.go @@ -158,8 +158,6 @@ type Strategy struct { groupID uint32 - stopC chan struct{} - // defaultBoll is the BOLLINGER indicator we used for predicting the price. defaultBoll *indicator.BOLL @@ -547,8 +545,6 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.orderExecutor.BindProfitStats(s.ProfitStats) s.orderExecutor.Bind() - s.stopC = make(chan struct{}) - s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) { bbgo.Sync(s) }) @@ -622,7 +618,6 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.Graceful.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) { defer wg.Done() - close(s.stopC) _ = s.orderExecutor.GracefulCancel(ctx) })