mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
add cron schedule to xnav
This commit is contained in:
parent
f4941bef74
commit
5592d93c13
|
@ -30,6 +30,7 @@ crossExchangeStrategies:
|
||||||
|
|
||||||
- xnav:
|
- xnav:
|
||||||
interval: 1h
|
interval: 1h
|
||||||
|
# schedule: "0 * * * *" # every hour
|
||||||
reportOnStart: true
|
reportOnStart: true
|
||||||
ignoreDusts: true
|
ignoreDusts: true
|
||||||
|
|
||||||
|
|
|
@ -2,19 +2,19 @@ package xnav
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/c9s/bbgo/pkg/fixedpoint"
|
|
||||||
"github.com/c9s/bbgo/pkg/util/templateutil"
|
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
"github.com/slack-go/slack"
|
|
||||||
|
|
||||||
"github.com/c9s/bbgo/pkg/bbgo"
|
"github.com/c9s/bbgo/pkg/bbgo"
|
||||||
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
||||||
"github.com/c9s/bbgo/pkg/types"
|
"github.com/c9s/bbgo/pkg/types"
|
||||||
"github.com/c9s/bbgo/pkg/util"
|
"github.com/c9s/bbgo/pkg/util"
|
||||||
|
"github.com/c9s/bbgo/pkg/util/templateutil"
|
||||||
|
|
||||||
|
"github.com/robfig/cron/v3"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"github.com/slack-go/slack"
|
||||||
)
|
)
|
||||||
|
|
||||||
const ID = "xnav"
|
const ID = "xnav"
|
||||||
|
@ -59,16 +59,30 @@ type Strategy struct {
|
||||||
*bbgo.Environment
|
*bbgo.Environment
|
||||||
|
|
||||||
Interval types.Interval `json:"interval"`
|
Interval types.Interval `json:"interval"`
|
||||||
|
Schedule string `json:"schedule"`
|
||||||
ReportOnStart bool `json:"reportOnStart"`
|
ReportOnStart bool `json:"reportOnStart"`
|
||||||
IgnoreDusts bool `json:"ignoreDusts"`
|
IgnoreDusts bool `json:"ignoreDusts"`
|
||||||
|
|
||||||
State *State `persistence:"state"`
|
State *State `persistence:"state"`
|
||||||
|
|
||||||
|
cron *cron.Cron
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Strategy) ID() string {
|
func (s *Strategy) ID() string {
|
||||||
return ID
|
return ID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Strategy) Initialize() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Strategy) Validate() error {
|
||||||
|
if s.Interval == "" && s.Schedule == "" {
|
||||||
|
return fmt.Errorf("interval or schedule is required")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
var Ten = fixedpoint.NewFromInt(10)
|
var Ten = fixedpoint.NewFromInt(10)
|
||||||
|
|
||||||
func (s *Strategy) CrossSubscribe(sessions map[string]*bbgo.ExchangeSession) {}
|
func (s *Strategy) CrossSubscribe(sessions map[string]*bbgo.ExchangeSession) {}
|
||||||
|
@ -138,10 +152,6 @@ func (s *Strategy) recordNetAssetValue(ctx context.Context, sessions map[string]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, sessions map[string]*bbgo.ExchangeSession) error {
|
func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, sessions map[string]*bbgo.ExchangeSession) error {
|
||||||
if s.Interval == "" {
|
|
||||||
return errors.New("interval can not be empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
if s.State == nil {
|
if s.State == nil {
|
||||||
s.State = &State{}
|
s.State = &State{}
|
||||||
s.State.Reset()
|
s.State.Reset()
|
||||||
|
@ -161,25 +171,32 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se
|
||||||
log.Warnf("xnav does not support backtesting")
|
log.Warnf("xnav does not support backtesting")
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: if interval is supported, we can use kline as the ticker
|
if s.Interval != "" {
|
||||||
if _, ok := types.SupportedIntervals[s.Interval]; ok {
|
go func() {
|
||||||
|
ticker := time.NewTicker(util.MillisecondsJitter(s.Interval.Duration(), 1000))
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
}
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
|
||||||
go func() {
|
case <-ticker.C:
|
||||||
ticker := time.NewTicker(util.MillisecondsJitter(s.Interval.Duration(), 1000))
|
s.recordNetAssetValue(ctx, sessions)
|
||||||
defer ticker.Stop()
|
}
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
|
|
||||||
case <-ticker.C:
|
|
||||||
s.recordNetAssetValue(ctx, sessions)
|
|
||||||
}
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
} else if s.Schedule != "" {
|
||||||
|
s.cron = cron.New()
|
||||||
|
_, err := s.cron.AddFunc(s.Schedule, func() {
|
||||||
|
s.recordNetAssetValue(ctx, sessions)
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
}()
|
s.cron.Start()
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user