bbgo_origin/pkg/strategy/pivotshort/strategy.go

177 lines
4.3 KiB
Go
Raw Normal View History

package pivotshort
import (
"context"
"fmt"
"os"
2022-06-09 16:49:32 +00:00
"sync"
2022-06-09 03:30:24 +00:00
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/dynamic"
"github.com/c9s/bbgo/pkg/fixedpoint"
2022-05-13 10:05:25 +00:00
"github.com/c9s/bbgo/pkg/indicator"
"github.com/c9s/bbgo/pkg/types"
)
const ID = "pivotshort"
var one = fixedpoint.One
var zero = fixedpoint.Zero
var log = logrus.WithField("strategy", ID)
func init() {
bbgo.RegisterStrategy(ID, &Strategy{})
}
type IntervalWindowSetting struct {
types.IntervalWindow
}
type Strategy struct {
Environment *bbgo.Environment
Symbol string `json:"symbol"`
Market types.Market
// pivot interval and window
types.IntervalWindow
2022-05-12 11:27:57 +00:00
// persistence fields
Position *types.Position `persistence:"position"`
ProfitStats *types.ProfitStats `persistence:"profit_stats"`
TradeStats *types.TradeStats `persistence:"trade_stats"`
// BreakLow is one of the entry method
BreakLow *BreakLow `json:"breakLow"`
2022-06-10 16:26:44 +00:00
// ResistanceShort is one of the entry method
ResistanceShort *ResistanceShort `json:"resistanceShort"`
2022-06-10 16:26:44 +00:00
ExitMethods bbgo.ExitMethodSet `json:"exits"`
session *bbgo.ExchangeSession
orderExecutor *bbgo.GeneralOrderExecutor
2022-05-12 11:27:57 +00:00
// StrategyController
bbgo.StrategyController
}
func (s *Strategy) ID() string {
return ID
}
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
2022-05-19 01:48:36 +00:00
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: types.Interval1m})
2022-06-10 16:26:44 +00:00
if s.ResistanceShort != nil && s.ResistanceShort.Enabled {
dynamic.InheritStructValues(s.ResistanceShort, s)
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.ResistanceShort.Interval})
2022-06-10 16:26:44 +00:00
}
if s.BreakLow != nil {
dynamic.InheritStructValues(s.BreakLow, s)
}
if !bbgo.IsBackTesting {
session.Subscribe(types.MarketTradeChannel, s.Symbol, types.SubscribeOptions{})
}
s.ExitMethods.SetAndSubscribe(session, s)
}
func (s *Strategy) InstanceID() string {
return fmt.Sprintf("%s:%s", ID, s.Symbol)
}
2022-06-20 03:39:18 +00:00
func (s *Strategy) CurrentPosition() *types.Position {
return s.Position
}
func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Value) error {
return s.orderExecutor.ClosePosition(ctx, percentage)
2022-05-12 11:27:57 +00:00
}
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
var instanceID = s.InstanceID()
if s.Position == nil {
s.Position = types.NewPositionFromMarket(s.Market)
}
2022-06-04 17:48:56 +00:00
if s.ProfitStats == nil {
s.ProfitStats = types.NewProfitStats(s.Market)
}
2022-06-09 16:49:32 +00:00
if s.TradeStats == nil {
s.TradeStats = &types.TradeStats{}
2022-06-09 16:49:32 +00:00
}
2022-06-20 03:39:18 +00:00
// StrategyController
s.Status = types.StrategyStatusRunning
s.OnSuspend(func() {
// Cancel active orders
_ = s.orderExecutor.GracefulCancel(ctx)
})
s.OnEmergencyStop(func() {
// Cancel active orders
_ = s.orderExecutor.GracefulCancel(ctx)
// Close 100% position
_ = s.ClosePosition(ctx, fixedpoint.One)
})
// initial required information
s.session = session
s.orderExecutor = bbgo.NewGeneralOrderExecutor(session, s.Symbol, ID, instanceID, s.Position)
s.orderExecutor.BindEnvironment(s.Environment)
s.orderExecutor.BindProfitStats(s.ProfitStats)
2022-06-18 08:32:53 +00:00
s.orderExecutor.BindTradeStats(s.TradeStats)
2022-06-21 07:57:26 +00:00
s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) {
bbgo.Sync(s)
})
2022-06-19 05:01:22 +00:00
s.orderExecutor.Bind()
for _, method := range s.ExitMethods {
method.Bind(session, s.orderExecutor)
}
if s.ResistanceShort != nil && s.ResistanceShort.Enabled {
s.ResistanceShort.Bind(session, s.orderExecutor)
}
if s.BreakLow != nil {
s.BreakLow.Bind(session, s.orderExecutor)
2022-06-27 10:17:57 +00:00
}
bbgo.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) {
2022-07-01 08:29:03 +00:00
defer wg.Done()
_, _ = fmt.Fprintln(os.Stderr, s.TradeStats.String())
2022-07-01 08:29:03 +00:00
_ = s.orderExecutor.GracefulCancel(ctx)
2022-06-09 16:49:32 +00:00
})
return nil
}
2022-06-09 03:30:24 +00:00
func preloadPivot(pivot *indicator.Pivot, store *bbgo.MarketDataStore) *types.KLine {
klines, ok := store.KLinesOfInterval(pivot.Interval)
if !ok {
return nil
}
last := (*klines)[len(*klines)-1]
log.Debugf("updating pivot indicator: %d klines", len(*klines))
for i := pivot.Window; i < len(*klines); i++ {
pivot.Update((*klines)[0 : i+1])
}
log.Debugf("found %v previous lows: %v", pivot.IntervalWindow, pivot.Lows)
log.Debugf("found %v previous highs: %v", pivot.IntervalWindow, pivot.Highs)
return &last
}