2022-09-05 08:14:08 +00:00
|
|
|
package audacitymaker
|
2022-08-30 01:16:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
|
2022-10-03 08:01:08 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2022-08-30 01:16:53 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/bbgo"
|
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
2022-09-05 08:22:47 +00:00
|
|
|
const ID = "audacitymaker"
|
2022-08-30 01:16:53 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
types.IntervalWindow
|
|
|
|
|
|
|
|
// persistence fields
|
|
|
|
Position *types.Position `persistence:"position"`
|
|
|
|
ProfitStats *types.ProfitStats `persistence:"profit_stats"`
|
|
|
|
TradeStats *types.TradeStats `persistence:"trade_stats"`
|
|
|
|
|
|
|
|
activeOrders *bbgo.ActiveOrderBook
|
|
|
|
|
2022-09-07 07:46:41 +00:00
|
|
|
OrderFlow *PerTrade `json:"orderFlow"`
|
2022-08-30 01:16:53 +00:00
|
|
|
|
|
|
|
ExitMethods bbgo.ExitMethodSet `json:"exits"`
|
|
|
|
|
|
|
|
session *bbgo.ExchangeSession
|
|
|
|
orderExecutor *bbgo.GeneralOrderExecutor
|
|
|
|
|
|
|
|
// StrategyController
|
|
|
|
bbgo.StrategyController
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
|
|
|
|
session.Subscribe(types.BookChannel, s.Symbol, types.SubscribeOptions{})
|
|
|
|
session.Subscribe(types.MarketTradeChannel, s.Symbol, types.SubscribeOptions{})
|
2022-09-07 07:46:41 +00:00
|
|
|
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.OrderFlow.Interval})
|
2022-08-30 01:16:53 +00:00
|
|
|
|
|
|
|
if !bbgo.IsBackTesting {
|
|
|
|
session.Subscribe(types.MarketTradeChannel, s.Symbol, types.SubscribeOptions{})
|
|
|
|
}
|
|
|
|
|
|
|
|
s.ExitMethods.SetAndSubscribe(session, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) ID() string {
|
|
|
|
return ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) InstanceID() string {
|
|
|
|
return fmt.Sprintf("%s:%s", ID, s.Symbol)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.ProfitStats == nil {
|
|
|
|
s.ProfitStats = types.NewProfitStats(s.Market)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.TradeStats == nil {
|
|
|
|
s.TradeStats = types.NewTradeStats(s.Symbol)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2022-10-03 08:01:08 +00:00
|
|
|
// _ = s.ClosePosition(ctx, fixedpoint.One)
|
2022-08-30 01:16:53 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
s.orderExecutor.BindTradeStats(s.TradeStats)
|
|
|
|
s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) {
|
|
|
|
bbgo.Sync(s)
|
|
|
|
})
|
|
|
|
s.orderExecutor.Bind()
|
|
|
|
s.activeOrders = bbgo.NewActiveOrderBook(s.Symbol)
|
|
|
|
|
|
|
|
for _, method := range s.ExitMethods {
|
|
|
|
method.Bind(session, s.orderExecutor)
|
|
|
|
}
|
|
|
|
|
2022-09-07 07:46:41 +00:00
|
|
|
if s.OrderFlow != nil {
|
|
|
|
s.OrderFlow.Bind(session, s.orderExecutor)
|
2022-08-30 01:16:53 +00:00
|
|
|
}
|
|
|
|
|
2022-10-03 08:01:08 +00:00
|
|
|
bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) {
|
2022-08-30 01:16:53 +00:00
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
_, _ = fmt.Fprintln(os.Stderr, s.TradeStats.String())
|
|
|
|
_ = s.orderExecutor.GracefulCancel(ctx)
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|