bollmaker: pull out functions

This commit is contained in:
c9s 2022-06-03 02:15:23 +08:00
parent 1a85299204
commit 50d7d235a4
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 41 additions and 38 deletions

View File

@ -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
}

View File

@ -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
}