From 50d7d235a4e1f0401ed14cba170d74112aa27773 Mon Sep 17 00:00:00 2001 From: c9s Date: Fri, 3 Jun 2022 02:15:23 +0800 Subject: [PATCH] bollmaker: pull out functions --- pkg/strategy/bollmaker/strategy.go | 51 ++++++++---------------------- pkg/strategy/bollmaker/trend.go | 28 ++++++++++++++++ 2 files changed, 41 insertions(+), 38 deletions(-) create mode 100644 pkg/strategy/bollmaker/trend.go diff --git a/pkg/strategy/bollmaker/strategy.go b/pkg/strategy/bollmaker/strategy.go index 461ce65bb..993c0b87c 100644 --- a/pkg/strategy/bollmaker/strategy.go +++ b/pkg/strategy/bollmaker/strategy.go @@ -477,7 +477,7 @@ func (s *Strategy) placeOrders(ctx context.Context, orderExecutor bbgo.OrderExec } for i := range submitOrders { - submitOrders[i] = s.adjustOrderQuantity(submitOrders[i]) + submitOrders[i] = adjustOrderQuantity(submitOrders[i], s.Market) } createdOrders, err := orderExecutor.SubmitOrders(ctx, submitOrders...) @@ -496,43 +496,6 @@ func (s *Strategy) hasShortSet() bool { return s.Short != nil && *s.Short } -type PriceTrend string - -const ( - NeutralTrend PriceTrend = "neutral" - UpTrend PriceTrend = "upTrend" - DownTrend PriceTrend = "downTrend" - UnknownTrend PriceTrend = "unknown" -) - -func 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 { - if submitOrder.Quantity.Mul(submitOrder.Price).Compare(s.Market.MinNotional) < 0 { - submitOrder.Quantity = bbgo.AdjustFloatQuantityByMinAmount(submitOrder.Quantity, submitOrder.Price, s.Market.MinNotional.Mul(notionModifier)) - } - - if submitOrder.Quantity.Compare(s.Market.MinQuantity) < 0 { - submitOrder.Quantity = fixedpoint.Max(submitOrder.Quantity, s.Market.MinQuantity) - } - - return submitOrder -} - func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error { // StrategyController s.Status = types.StrategyStatusRunning @@ -774,3 +737,15 @@ func calculateBandPercentage(up, down, sma, midPrice float64) float64 { func inBetween(x, a, b float64) bool { return a < x && x < b } + +func adjustOrderQuantity(submitOrder types.SubmitOrder, market types.Market) types.SubmitOrder { + if submitOrder.Quantity.Mul(submitOrder.Price).Compare(market.MinNotional) < 0 { + submitOrder.Quantity = bbgo.AdjustFloatQuantityByMinAmount(submitOrder.Quantity, submitOrder.Price, market.MinNotional.Mul(notionModifier)) + } + + if submitOrder.Quantity.Compare(market.MinQuantity) < 0 { + submitOrder.Quantity = fixedpoint.Max(submitOrder.Quantity, market.MinQuantity) + } + + return submitOrder +} diff --git a/pkg/strategy/bollmaker/trend.go b/pkg/strategy/bollmaker/trend.go new file mode 100644 index 000000000..654ac4cce --- /dev/null +++ b/pkg/strategy/bollmaker/trend.go @@ -0,0 +1,28 @@ +package bollmaker + +import "github.com/c9s/bbgo/pkg/indicator" + +type PriceTrend string + +const ( + NeutralTrend PriceTrend = "neutral" + UpTrend PriceTrend = "upTrend" + DownTrend PriceTrend = "downTrend" + UnknownTrend PriceTrend = "unknown" +) + +func 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 +}