Skip to content

Commit

Permalink
improve buyandhold strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Dec 14, 2020
1 parent 7469ba3 commit cabd8f8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
3 changes: 2 additions & 1 deletion config/bbgo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,6 @@ exchangeStrategies:
buyandhold:
symbol: "BTCUSDT"
interval: "1h"
minDropPercentage: -0.01
baseQuantity: 0.01
# minDropPercentage: 0.01
minDropChange: 100.0
48 changes: 35 additions & 13 deletions pkg/strategy/buyandhold/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,50 @@ package buyandhold

import (
"context"
"fmt"
"math"

log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"

"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)

var log = logrus.WithField("strategy", "buyandhold")

func init() {
bbgo.RegisterStrategy("buyandhold", &Strategy{})
}

type Strategy struct {
Symbol string `json:"symbol"`
Interval string `json:"interval"`
BaseQuantity float64 `json:"baseQuantity"`
MinDropPercentage float64 `json:"minDropPercentage"`
Symbol string `json:"symbol"`

Interval string `json:"interval"`
BaseQuantity float64 `json:"baseQuantity"`
MinDropPercentage fixedpoint.Value `json:"minDropPercentage"`
MinDropChange fixedpoint.Value `json:"minDropChange"`
}

func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
}

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) {
// skip k-lines from other symbols
if kline.Symbol != s.Symbol {
return
}

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) {
Expand All @@ -42,20 +54,30 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
return
}

changePercentage := kline.GetChange() / kline.Open
change := kline.GetChange()

if changePercentage > s.MinDropPercentage {
// skip positive change
if change > 0 {
return
}

// buy when price drops -8%
market, ok := session.Market(s.Symbol)
if !ok {
log.Warnf("market %s is not defined", s.Symbol)
changeP := change / kline.Open

if s.MinDropPercentage != 0 {
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
}

quantity := s.BaseQuantity * (1.0 + math.Abs(changePercentage))
quantity := s.BaseQuantity * (1.0 + math.Abs(changeP))
_, err := orderExecutor.SubmitOrders(ctx, types.SubmitOrder{
Symbol: kline.Symbol,
Market: market,
Expand Down

0 comments on commit cabd8f8

Please sign in to comment.