bbgo_origin/pkg/strategy/emacross/strategy.go

102 lines
2.6 KiB
Go
Raw Permalink Normal View History

2023-11-05 23:44:34 +00:00
package emacross
import (
"context"
"fmt"
"sync"
log "github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/fixedpoint"
2024-04-17 07:27:46 +00:00
indicatorv2 "github.com/c9s/bbgo/pkg/indicator/v2"
2023-11-05 23:44:34 +00:00
"github.com/c9s/bbgo/pkg/strategy/common"
"github.com/c9s/bbgo/pkg/types"
2024-04-17 07:27:46 +00:00
"github.com/c9s/bbgo/pkg/util"
2023-11-05 23:44:34 +00:00
)
const ID = "emacross"
func init() {
bbgo.RegisterStrategy(ID, &Strategy{})
}
type Strategy struct {
*common.Strategy
Environment *bbgo.Environment
Market types.Market
Symbol string `json:"symbol"`
Interval types.Interval `json:"interval"`
SlowWindow int `json:"slowWindow"`
FastWindow int `json:"fastWindow"`
OpenBelow fixedpoint.Value `json:"openBelow"`
CloseAbove fixedpoint.Value `json:"closeAbove"`
lastKLine types.KLine
bbgo.OpenPositionOptions
}
func (s *Strategy) ID() string {
return ID
}
func (s *Strategy) InstanceID() string {
return fmt.Sprintf("%s:%s:%s:%d-%d", ID, s.Symbol, s.Interval, s.FastWindow, s.SlowWindow)
}
func (s *Strategy) Initialize() error {
if s.Strategy == nil {
s.Strategy = &common.Strategy{}
}
return nil
}
2023-11-05 23:44:34 +00:00
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
2023-11-07 03:17:08 +00:00
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
2023-11-05 23:44:34 +00:00
}
func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
s.Strategy.Initialize(ctx, s.Environment, session, s.Market, ID, s.InstanceID())
session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, types.Interval5m, func(k types.KLine) {
s.lastKLine = k
}))
2023-11-07 03:17:08 +00:00
fastEMA := session.Indicators(s.Symbol).EWMA(types.IntervalWindow{Interval: s.Interval, Window: s.FastWindow})
slowEMA := session.Indicators(s.Symbol).EWMA(types.IntervalWindow{Interval: s.Interval, Window: s.SlowWindow})
2023-11-05 23:44:34 +00:00
cross := indicatorv2.Cross(fastEMA, slowEMA)
cross.OnUpdate(func(v float64) {
switch indicatorv2.CrossType(v) {
case indicatorv2.CrossOver:
if err := s.Strategy.OrderExecutor.GracefulCancel(ctx); err != nil {
log.WithError(err).Errorf("unable to cancel order")
}
opts := s.OpenPositionOptions
opts.Long = true
if price, ok := session.LastPrice(s.Symbol); ok {
opts.Price = price
}
opts.Tags = []string{"emaCrossOver"}
_, err := s.Strategy.OrderExecutor.OpenPosition(ctx, opts)
2024-04-17 07:27:46 +00:00
util.LogErr(err, "unable to open position")
2023-11-05 23:44:34 +00:00
case indicatorv2.CrossUnder:
err := s.Strategy.OrderExecutor.ClosePosition(ctx, fixedpoint.One)
2024-04-17 07:27:46 +00:00
util.LogErr(err, "unable to submit close position order")
2023-11-05 23:44:34 +00:00
}
})
bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done()
})
return nil
}