improve buyandhold strategy

This commit is contained in:
c9s 2020-12-14 14:21:02 +08:00
parent 7469ba3829
commit cabd8f8dcb
2 changed files with 37 additions and 14 deletions

View File

@ -65,5 +65,6 @@ exchangeStrategies:
buyandhold: buyandhold:
symbol: "BTCUSDT" symbol: "BTCUSDT"
interval: "1h" interval: "1h"
minDropPercentage: -0.01
baseQuantity: 0.01 baseQuantity: 0.01
# minDropPercentage: 0.01
minDropChange: 100.0

View File

@ -2,23 +2,29 @@ package buyandhold
import ( import (
"context" "context"
"fmt"
"math" "math"
log "github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
) )
var log = logrus.WithField("strategy", "buyandhold")
func init() { func init() {
bbgo.RegisterStrategy("buyandhold", &Strategy{}) bbgo.RegisterStrategy("buyandhold", &Strategy{})
} }
type Strategy struct { type Strategy struct {
Symbol string `json:"symbol"` Symbol string `json:"symbol"`
Interval string `json:"interval"`
BaseQuantity float64 `json:"baseQuantity"` Interval string `json:"interval"`
MinDropPercentage float64 `json:"minDropPercentage"` BaseQuantity float64 `json:"baseQuantity"`
MinDropPercentage fixedpoint.Value `json:"minDropPercentage"`
MinDropChange fixedpoint.Value `json:"minDropChange"`
} }
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) { func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
@ -26,6 +32,12 @@ func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
} }
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error { func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
// buy when price drops -8%
market, ok := session.Market(s.Symbol)
if !ok {
return fmt.Errorf("market %s is not defined", s.Symbol)
}
session.Stream.OnKLine(func(kline types.KLine) { session.Stream.OnKLine(func(kline types.KLine) {
// skip k-lines from other symbols // skip k-lines from other symbols
if kline.Symbol != s.Symbol { if kline.Symbol != s.Symbol {
@ -33,7 +45,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
} }
changePercentage := kline.GetChange() / kline.Open changePercentage := kline.GetChange() / kline.Open
log.Infof("change %f <=> %f", changePercentage, s.MinDropPercentage) log.Infof("change %f <=> %f", changePercentage, s.MinDropPercentage.Float64())
}) })
session.Stream.OnKLineClosed(func(kline types.KLine) { session.Stream.OnKLineClosed(func(kline types.KLine) {
@ -42,20 +54,30 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
return return
} }
changePercentage := kline.GetChange() / kline.Open change := kline.GetChange()
if changePercentage > s.MinDropPercentage { // skip positive change
if change > 0 {
return return
} }
// buy when price drops -8% changeP := change / kline.Open
market, ok := session.Market(s.Symbol)
if !ok { if s.MinDropPercentage != 0 {
log.Warnf("market %s is not defined", s.Symbol) if math.Abs(changeP) < math.Abs(s.MinDropPercentage.Float64()) {
return
}
} else if s.MinDropChange != 0 {
if math.Abs(change) < math.Abs(s.MinDropChange.Float64()) {
return
}
} else {
// not configured, we shall skip
log.Warnf("parameters are not configured, skipping action...")
return return
} }
quantity := s.BaseQuantity * (1.0 + math.Abs(changePercentage)) quantity := s.BaseQuantity * (1.0 + math.Abs(changeP))
_, err := orderExecutor.SubmitOrders(ctx, types.SubmitOrder{ _, err := orderExecutor.SubmitOrders(ctx, types.SubmitOrder{
Symbol: kline.Symbol, Symbol: kline.Symbol,
Market: market, Market: market,