Skip to content

Commit

Permalink
fix buyandhold strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Dec 14, 2020
1 parent 81c7d3c commit 6d15c62
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions pkg/strategy/buyandhold/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,41 @@ func init() {
type Strategy struct {
Symbol string `json:"symbol"`

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

MovingAverageWindow int `json:"movingAverageWindow"`
}

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: string(s.Interval)})
}

func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
if s.Interval == "" {
s.Interval = types.Interval5m
}

if s.MovingAverageWindow == 0 {
s.MovingAverageWindow = 99
}

// buy when price drops -8%
market, ok := session.Market(s.Symbol)
if !ok {
return fmt.Errorf("market %s is not defined", s.Symbol)
}

standardIndicatorSet, ok := session.StandardIndicatorSet(s.Symbol)
if !ok {
return fmt.Errorf("standardIndicatorSet is nil, symbol %s", s.Symbol)
}

var iw = types.IntervalWindow{Interval: s.Interval, Window: s.MovingAverageWindow}
var ema = standardIndicatorSet.EWMA(iw)

session.Stream.OnKLine(func(kline types.KLine) {
// skip k-lines from other symbols
if kline.Symbol != s.Symbol {
Expand All @@ -61,6 +79,11 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
return
}

if kline.Close > ema.Last() {
log.Warnf("kline close price %f is above EMA %s %f", kline.Close, ema.IntervalWindow, ema.Last())
return
}

changeP := change / kline.Open

if s.MinDropPercentage != 0 {
Expand Down

0 comments on commit 6d15c62

Please sign in to comment.