mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 08:45:16 +00:00
supertrend: clean up and update
This commit is contained in:
parent
72f18c1057
commit
581e4be218
|
@ -6,8 +6,6 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/c9s/bbgo/pkg/util"
|
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
@ -19,12 +17,9 @@ import (
|
||||||
|
|
||||||
const ID = "supertrend"
|
const ID = "supertrend"
|
||||||
|
|
||||||
const stateKey = "state-v1"
|
|
||||||
|
|
||||||
var log = logrus.WithField("strategy", ID)
|
var log = logrus.WithField("strategy", ID)
|
||||||
|
|
||||||
// TODO: limit order for ATR TP
|
// TODO: limit order for ATR TP
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
// Register the pointer of the strategy struct,
|
// Register the pointer of the strategy struct,
|
||||||
// so that bbgo knows what struct to be used to unmarshal the configs (YAML or JSON)
|
// so that bbgo knows what struct to be used to unmarshal the configs (YAML or JSON)
|
||||||
|
@ -33,10 +28,7 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Strategy struct {
|
type Strategy struct {
|
||||||
*bbgo.Persistence
|
|
||||||
|
|
||||||
Environment *bbgo.Environment
|
Environment *bbgo.Environment
|
||||||
session *bbgo.ExchangeSession
|
|
||||||
Market types.Market
|
Market types.Market
|
||||||
|
|
||||||
// persistence fields
|
// persistence fields
|
||||||
|
@ -44,14 +36,6 @@ type Strategy struct {
|
||||||
ProfitStats *types.ProfitStats `persistence:"profit_stats"`
|
ProfitStats *types.ProfitStats `persistence:"profit_stats"`
|
||||||
TradeStats *types.TradeStats `persistence:"trade_stats"`
|
TradeStats *types.TradeStats `persistence:"trade_stats"`
|
||||||
|
|
||||||
// Order and trade
|
|
||||||
orderExecutor *bbgo.GeneralOrderExecutor
|
|
||||||
|
|
||||||
// groupID is the group ID used for the strategy instance for canceling orders
|
|
||||||
groupID uint32
|
|
||||||
|
|
||||||
stopC chan struct{}
|
|
||||||
|
|
||||||
// Symbol is the market symbol you want to trade
|
// Symbol is the market symbol you want to trade
|
||||||
Symbol string `json:"symbol"`
|
Symbol string `json:"symbol"`
|
||||||
|
|
||||||
|
@ -90,12 +74,14 @@ type Strategy struct {
|
||||||
// StopByReversedLinGre TP/SL by reversed linear regression signal
|
// StopByReversedLinGre TP/SL by reversed linear regression signal
|
||||||
StopByReversedLinGre bool `json:"stopByReversedLinGre"`
|
StopByReversedLinGre bool `json:"stopByReversedLinGre"`
|
||||||
|
|
||||||
|
// ExitMethods Exit methods
|
||||||
|
ExitMethods bbgo.ExitMethodSet `json:"exits"`
|
||||||
|
|
||||||
|
session *bbgo.ExchangeSession
|
||||||
|
orderExecutor *bbgo.GeneralOrderExecutor
|
||||||
currentTakeProfitPrice fixedpoint.Value
|
currentTakeProfitPrice fixedpoint.Value
|
||||||
currentStopLossPrice fixedpoint.Value
|
currentStopLossPrice fixedpoint.Value
|
||||||
|
|
||||||
// ExitMethods Exit methods
|
|
||||||
ExitMethods []bbgo.ExitMethod `json:"exits"`
|
|
||||||
|
|
||||||
// StrategyController
|
// StrategyController
|
||||||
bbgo.StrategyController
|
bbgo.StrategyController
|
||||||
}
|
}
|
||||||
|
@ -256,7 +242,6 @@ func (s *Strategy) generateOrderForm(side types.SideType, quantity fixedpoint.Va
|
||||||
Type: types.OrderTypeMarket,
|
Type: types.OrderTypeMarket,
|
||||||
Quantity: quantity,
|
Quantity: quantity,
|
||||||
MarginSideEffect: marginOrderSideEffect,
|
MarginSideEffect: marginOrderSideEffect,
|
||||||
GroupID: s.groupID,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return orderForm
|
return orderForm
|
||||||
|
@ -279,9 +264,11 @@ func (s *Strategy) calculateQuantity(currentPrice fixedpoint.Value) fixedpoint.V
|
||||||
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
|
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
|
||||||
s.session = session
|
s.session = session
|
||||||
|
|
||||||
|
s.currentStopLossPrice = fixedpoint.Zero
|
||||||
|
s.currentTakeProfitPrice = fixedpoint.Zero
|
||||||
|
|
||||||
// calculate group id for orders
|
// calculate group id for orders
|
||||||
instanceID := s.InstanceID()
|
instanceID := s.InstanceID()
|
||||||
s.groupID = util.FNV32(instanceID)
|
|
||||||
|
|
||||||
// If position is nil, we need to allocate a new position for calculation
|
// If position is nil, we need to allocate a new position for calculation
|
||||||
if s.Position == nil {
|
if s.Position == nil {
|
||||||
|
@ -321,16 +308,12 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
||||||
bbgo.Sync(s)
|
bbgo.Sync(s)
|
||||||
})
|
})
|
||||||
|
|
||||||
s.stopC = make(chan struct{})
|
|
||||||
|
|
||||||
// StrategyController
|
// StrategyController
|
||||||
s.Status = types.StrategyStatusRunning
|
s.Status = types.StrategyStatusRunning
|
||||||
|
|
||||||
s.OnSuspend(func() {
|
s.OnSuspend(func() {
|
||||||
_ = s.orderExecutor.GracefulCancel(ctx)
|
_ = s.orderExecutor.GracefulCancel(ctx)
|
||||||
_ = s.Persistence.Sync(s)
|
bbgo.Sync(s)
|
||||||
})
|
})
|
||||||
|
|
||||||
s.OnEmergencyStop(func() {
|
s.OnEmergencyStop(func() {
|
||||||
_ = s.orderExecutor.GracefulCancel(ctx)
|
_ = s.orderExecutor.GracefulCancel(ctx)
|
||||||
// Close 100% position
|
// Close 100% position
|
||||||
|
@ -345,20 +328,12 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
||||||
method.Bind(session, s.orderExecutor)
|
method.Bind(session, s.orderExecutor)
|
||||||
}
|
}
|
||||||
|
|
||||||
s.currentStopLossPrice = fixedpoint.Zero
|
session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, s.Interval, func(kline types.KLine) {
|
||||||
s.currentTakeProfitPrice = fixedpoint.Zero
|
|
||||||
|
|
||||||
session.MarketDataStream.OnKLineClosed(func(kline types.KLine) {
|
|
||||||
// StrategyController
|
// StrategyController
|
||||||
if s.Status != types.StrategyStatusRunning {
|
if s.Status != types.StrategyStatusRunning {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// skip k-lines from other symbols or other intervals
|
|
||||||
if kline.Symbol != s.Symbol || kline.Interval != s.Interval {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
closePrice := kline.GetClose()
|
closePrice := kline.GetClose()
|
||||||
openPrice := kline.GetOpen()
|
openPrice := kline.GetOpen()
|
||||||
closePrice64 := closePrice.Float64()
|
closePrice64 := closePrice.Float64()
|
||||||
|
@ -426,12 +401,11 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
||||||
bbgo.Notify("can not place %s open position order", s.Symbol)
|
bbgo.Notify("can not place %s open position order", s.Symbol)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
}))
|
||||||
|
|
||||||
// Graceful shutdown
|
// Graceful shutdown
|
||||||
bbgo.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) {
|
bbgo.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
close(s.stopC)
|
|
||||||
|
|
||||||
_ = s.orderExecutor.GracefulCancel(ctx)
|
_ = s.orderExecutor.GracefulCancel(ctx)
|
||||||
_, _ = fmt.Fprintln(os.Stderr, s.TradeStats.String())
|
_, _ = fmt.Fprintln(os.Stderr, s.TradeStats.String())
|
||||||
|
|
Loading…
Reference in New Issue
Block a user