mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-15 03:23:52 +00:00
59 lines
1.8 KiB
Go
59 lines
1.8 KiB
Go
|
package base
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
|
||
|
"github.com/c9s/bbgo/pkg/bbgo"
|
||
|
"github.com/c9s/bbgo/pkg/types"
|
||
|
)
|
||
|
|
||
|
// LongShortStrategy provides the core functionality that is required by a long/short strategy.
|
||
|
type LongShortStrategy struct {
|
||
|
Position *types.Position `json:"position,omitempty" persistence:"position"`
|
||
|
ProfitStats *types.ProfitStats `json:"profitStats,omitempty" persistence:"profit_stats"`
|
||
|
|
||
|
parent, ctx context.Context
|
||
|
cancel context.CancelFunc
|
||
|
|
||
|
Environ *bbgo.Environment
|
||
|
Session *bbgo.ExchangeSession
|
||
|
OrderExecutor *bbgo.GeneralOrderExecutor
|
||
|
}
|
||
|
|
||
|
func (s *LongShortStrategy) Setup(ctx context.Context, environ *bbgo.Environment, session *bbgo.ExchangeSession, market types.Market, strategyID, instanceID string) {
|
||
|
s.parent = ctx
|
||
|
s.ctx, s.cancel = context.WithCancel(ctx)
|
||
|
|
||
|
s.Environ = environ
|
||
|
s.Session = session
|
||
|
|
||
|
if s.ProfitStats == nil {
|
||
|
s.ProfitStats = types.NewProfitStats(market)
|
||
|
}
|
||
|
|
||
|
if s.Position == nil {
|
||
|
s.Position = types.NewPositionFromMarket(market)
|
||
|
}
|
||
|
|
||
|
// Always update the position fields
|
||
|
s.Position.Strategy = strategyID
|
||
|
s.Position.StrategyInstanceID = instanceID
|
||
|
|
||
|
// if anyone of the fee rate is defined, this assumes that both are defined.
|
||
|
// so that zero maker fee could be applied
|
||
|
if session.MakerFeeRate.Sign() > 0 || session.TakerFeeRate.Sign() > 0 {
|
||
|
s.Position.SetExchangeFeeRate(session.ExchangeName, types.ExchangeFee{
|
||
|
MakerFeeRate: session.MakerFeeRate,
|
||
|
TakerFeeRate: session.TakerFeeRate,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
s.OrderExecutor = bbgo.NewGeneralOrderExecutor(session, market.Symbol, strategyID, instanceID, s.Position)
|
||
|
s.OrderExecutor.BindEnvironment(environ)
|
||
|
s.OrderExecutor.BindProfitStats(s.ProfitStats)
|
||
|
s.OrderExecutor.Bind()
|
||
|
s.OrderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) {
|
||
|
bbgo.Sync(ctx, s)
|
||
|
})
|
||
|
}
|