2020-10-12 14:46:06 +00:00
|
|
|
package buyandhold
|
|
|
|
|
|
|
|
import (
|
2020-10-13 06:50:59 +00:00
|
|
|
"context"
|
2020-10-13 08:17:07 +00:00
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"math"
|
2020-10-13 06:50:59 +00:00
|
|
|
|
2020-10-15 15:38:00 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/bbgo"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
2020-10-13 08:17:07 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/util"
|
2020-10-12 14:46:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Strategy struct {
|
2020-10-13 08:17:07 +00:00
|
|
|
Symbol string `json:"symbol"`
|
|
|
|
Interval string `json:"interval"`
|
|
|
|
BaseQuantity float64 `json:"baseQuantity"`
|
|
|
|
MaxAssetQuantity float64 `json:"maxAssetQuantity"`
|
|
|
|
MinDropPercentage float64 `json:"minDropPercentage"`
|
2020-10-12 14:46:06 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 08:17:07 +00:00
|
|
|
func LoadFile(filepath string) (*Strategy, error) {
|
|
|
|
o, err := ioutil.ReadFile(filepath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var strategy Strategy
|
|
|
|
err = json.Unmarshal(o, &strategy)
|
|
|
|
return &strategy, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(symbol string, interval string, baseQuantity float64) *Strategy {
|
2020-10-12 14:46:06 +00:00
|
|
|
return &Strategy{
|
2020-10-13 08:17:07 +00:00
|
|
|
Symbol: symbol,
|
|
|
|
Interval: interval,
|
|
|
|
BaseQuantity: baseQuantity,
|
|
|
|
MinDropPercentage: -0.08,
|
2020-10-12 14:46:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 08:17:07 +00:00
|
|
|
func (s *Strategy) SetMinDropPercentage(p float64) *Strategy {
|
|
|
|
s.MinDropPercentage = p
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) SetMaxAssetQuantity(q float64) *Strategy {
|
|
|
|
s.MaxAssetQuantity = q
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2020-10-13 10:08:02 +00:00
|
|
|
func (s *Strategy) Run(ctx context.Context, orderExecutor types.OrderExecutor, session *bbgo.ExchangeSession) error {
|
2020-10-13 08:17:07 +00:00
|
|
|
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
|
2020-10-13 06:50:59 +00:00
|
|
|
|
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
|
|
|
|
|
2020-10-13 08:17:07 +00:00
|
|
|
// buy when price drops -8%
|
|
|
|
if changePercentage < s.MinDropPercentage {
|
|
|
|
market, ok := session.Markets[s.Symbol]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
baseBalance, ok := session.Account.Balance(market.BaseCurrency)
|
|
|
|
if ok {
|
|
|
|
// we hold too many
|
|
|
|
if util.NotZero(s.MaxAssetQuantity) && baseBalance.Available > s.MaxAssetQuantity {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 15:38:00 +00:00
|
|
|
err := orderExecutor.SubmitOrder(ctx, types.SubmitOrder{
|
2020-10-13 08:17:07 +00:00
|
|
|
Symbol: kline.Symbol,
|
|
|
|
Side: types.SideTypeBuy,
|
|
|
|
Type: types.OrderTypeMarket,
|
|
|
|
Quantity: s.BaseQuantity * math.Abs(changePercentage),
|
2020-10-13 06:50:59 +00:00
|
|
|
})
|
2020-10-15 15:38:00 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("submit order error")
|
|
|
|
}
|
2020-10-13 06:50:59 +00:00
|
|
|
}
|
2020-10-12 14:46:06 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|