2020-10-12 14:46:06 +00:00
|
|
|
package buyandhold
|
|
|
|
|
|
|
|
import (
|
2020-10-13 06:50:59 +00:00
|
|
|
"context"
|
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/bbgo"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Strategy struct {
|
|
|
|
symbol string
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(symbol string) *Strategy {
|
|
|
|
return &Strategy{
|
|
|
|
symbol: symbol,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 06:50:59 +00:00
|
|
|
func (s *Strategy) Run(ctx context.Context, trader types.Trader, session *bbgo.ExchangeSession) error {
|
|
|
|
session.Subscribe(types.KLineChannel, s.symbol, types.SubscribeOptions{ Interval: "1h" })
|
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
session.Stream.OnKLineClosed(func(kline types.KLine) {
|
2020-10-13 06:50:59 +00:00
|
|
|
changePercentage := kline.GetChange() / kline.Open
|
|
|
|
|
|
|
|
// buy when price drops -10%
|
|
|
|
if changePercentage < -0.1 {
|
|
|
|
trader.SubmitOrder(ctx, &types.SubmitOrder{
|
|
|
|
Symbol: kline.Symbol,
|
|
|
|
Side: types.SideTypeBuy,
|
|
|
|
Type: types.OrderTypeMarket,
|
|
|
|
Quantity: 1.0,
|
|
|
|
})
|
|
|
|
}
|
2020-10-12 14:46:06 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|