bbgo_origin/pkg/strategy/rsicross/strategy.go

103 lines
2.6 KiB
Go
Raw Permalink Normal View History

2023-07-09 13:23:42 +00:00
package rsicross
import (
"context"
"fmt"
"sync"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/fixedpoint"
indicatorv2 "github.com/c9s/bbgo/pkg/indicator/v2"
2023-07-09 13:23:42 +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-07-09 13:23:42 +00:00
)
const ID = "rsicross"
func init() {
bbgo.RegisterStrategy(ID, &Strategy{})
}
type Strategy struct {
*common.Strategy
2023-07-09 13:23:42 +00:00
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"`
2023-07-09 13:23:42 +00:00
bbgo.OpenPositionOptions
}
func (s *Strategy) Initialize() error {
2024-01-28 06:29:54 +00:00
if s.Strategy == nil {
s.Strategy = &common.Strategy{}
}
return nil
}
2023-07-09 13:23:42 +00:00
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) 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 {
s.Strategy.Initialize(ctx, s.Environment, session, s.Market, ID, s.InstanceID())
fastRsi := session.Indicators(s.Symbol).RSI(types.IntervalWindow{Interval: s.Interval, Window: s.FastWindow})
slowRsi := session.Indicators(s.Symbol).RSI(types.IntervalWindow{Interval: s.Interval, Window: s.SlowWindow})
2023-07-10 08:54:22 +00:00
rsiCross := indicatorv2.Cross(fastRsi, slowRsi)
2023-07-09 13:23:42 +00:00
rsiCross.OnUpdate(func(v float64) {
2023-07-10 08:54:22 +00:00
switch indicatorv2.CrossType(v) {
case indicatorv2.CrossOver:
if s.OpenBelow.Sign() > 0 && fastRsi.Last(0) > s.OpenBelow.Float64() {
return
}
2023-07-09 13:23:42 +00:00
opts := s.OpenPositionOptions
opts.Long = true
if price, ok := session.LastPrice(s.Symbol); ok {
opts.Price = price
}
// opts.Price = closePrice
opts.Tags = []string{"rsiCrossOver"}
if _, err := s.OrderExecutor.OpenPosition(ctx, opts); err != nil {
2024-04-17 07:27:46 +00:00
util.LogErr(err, "unable to open position")
2023-07-09 13:23:42 +00:00
}
2023-07-10 08:54:22 +00:00
case indicatorv2.CrossUnder:
if s.CloseAbove.Sign() > 0 && fastRsi.Last(0) < s.CloseAbove.Float64() {
return
}
2023-07-09 13:23:42 +00:00
if err := s.OrderExecutor.ClosePosition(ctx, fixedpoint.One); err != nil {
2024-04-17 07:27:46 +00:00
util.LogErr(err, "failed to close position")
2023-07-09 13:23:42 +00:00
}
}
})
bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done()
2024-05-14 10:54:24 +00:00
bbgo.Sync(ctx, s)
2023-07-09 13:23:42 +00:00
})
return nil
}